OpenASIP 2.2
Loading...
Searching...
No Matches
Interpolation.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file Interpolation.cc
26 *
27 * Implementation of Interpolation class.
28 *
29 * @author Tommi Rantanen 2003 (tommi.rantanen-no.spam-tut.fi)
30 * @author Jari Mäntyneva 2005 (jari.mantyneva-no.spam-tut.fi)
31 * @note rating: red
32 */
33
34#include <vector>
35
36#include "Interpolation.hh"
37#include "Application.hh"
38
39using std::vector;
40
41/**
42 * Constructor.
43 *
44 * @param type Type of the field.
45 */
49
50/**
51 * Destructor.
52 *
53 * Deallocates memory reserved for entries created during interpolation.
54 */
56
57 for (CostDBTypes::EntryTable::iterator i = created_.begin();
58 i != created_.end(); i++) {
59
60 assert(*i != NULL);
61 delete *i;
62 *i = NULL;
63 }
64}
65
66/**
67 * Nothing to do since no entry can be removed in linear time.
68 */
69void
72
73/**
74 * Searches for database entries.
75 *
76 * If a value equal to the search key in the requested field is not
77 * found, interpolation of the smaller and greater field value is
78 * applied. Only the best matches are returned, i.e. equal match is
79 * chosen and no interpolation is done, or if two entries has only the
80 * field to which search is applied different, the closer one to the
81 * search key is chosen for interpolation.
82 *
83 * @param searchKey Search key.
84 * @param components Entries from which to find. Updated to contain
85 * entries that matched the search request.
86 * @exception TypeMismatch Interpolation was requested for field that cannot
87 * be interpolated.
88 * @exception KeyNotFound Some CostDBEntryStats have no value caused by
89 * missing cost data.
90 */
91void
93 const CostDBEntryKey& searchKey, CostDBTypes::EntryTable& components) {
94 vector<Pair> entries;
95 EntryKeyField searchField = searchKey.keyFieldOfType(*fieldType());
96 for (CostDBTypes::EntryTable::iterator i = components.begin();
97 i != components.end(); i++) {
98
99 EntryKeyField field = (*i)->keyFieldOfType(*fieldType());
100 bool newPair = true;
101 for (vector<Pair>::iterator p = entries.begin();
102 p != entries.end(); p++) {
103
104 if ((p->smaller != 0 &&
105 !onlyThisFieldDiffers(fieldType(), *(p->smaller), *(*i))) ||
106 (p->greater != 0 &&
107 !onlyThisFieldDiffers(fieldType(), *(p->greater), *(*i)))) {
108
109 continue;
110 }
111 if (field.isEqual(searchField)) {
112 p->smaller = *i;
113 p->greater = 0;
114 } else if (field.isSmaller(searchField)) {
115 if (p->smaller == 0 ||
116 (p->smaller != 0 &&
117 field.isGreater(
118 p->smaller->keyFieldOfType(*fieldType())))) {
119
120 p->smaller = *i;
121 }
122 } else {
123 if (!(field.isGreater(searchField))) {
124 throw TypeMismatch(__FILE__, __LINE__,
125 "Interpolation::filter");
126 }
127 if (p->greater == 0 ||
128 (p->greater != 0 &&
129 field.isSmaller(
130 p->greater->keyFieldOfType(*fieldType())))) {
131
132 p->greater = *i;
133 }
134 }
135 newPair = false;
136 break;
137 }
138 if (newPair) {
139 Pair pair;
140 if (field.isEqual(searchField)) {
141 pair.smaller = *i;
142 pair.greater = 0;
143 } else if (field.isSmaller(searchField)) {
144 pair.smaller = *i;
145 pair.greater = 0;
146 } else {
147 if (!(field.isGreater(searchField))) {
148 throw TypeMismatch(__FILE__, __LINE__,
149 "Interpolation::filter");
150 }
151 pair.greater = *i;
152 pair.smaller = 0;
153 }
154 entries.push_back(pair);
155 }
156 }
158 for (vector<Pair>::iterator p = entries.begin(); p != entries.end(); p++) {
159 if (p->smaller != 0 &&
160 p->smaller->keyFieldOfType(*fieldType()).isEqual(searchField)) {
161 filtered.push_back(p->smaller);
162 } else if (p->smaller != 0 && p->greater != 0) {
163 CostDBEntry* newEntry = new CostDBEntry(
164 *p->smaller, *p->greater, searchField);
165 filtered.push_back(newEntry);
166 created_.push_back(newEntry);
167 }
168 }
169 components = filtered;
170}
#define assert(condition)
EntryKeyField keyFieldOfType(const EntryKeyFieldProperty &fieldType) const
std::vector< CostDBEntry * > EntryTable
Table of database entries.
bool isGreater(const EntryKeyField &field) const
bool isEqual(const EntryKeyField &field) const
bool isSmaller(const EntryKeyField &field) const
Interpolation(const EntryKeyFieldProperty *type)
CostDBTypes::EntryTable created_
Entries created during interpolation.
virtual ~Interpolation()
void quickFilter(const CostDBEntryKey &, CostDBTypes::EntryTable &)
void filter(const CostDBEntryKey &searchKey, CostDBTypes::EntryTable &components)
const EntryKeyFieldProperty * fieldType() const
bool onlyThisFieldDiffers(const EntryKeyFieldProperty *type, const CostDBEntry &entry1, const CostDBEntry &entry2) const
Definition Matcher.cc:64
CostDBEntry * greater
An entry.
CostDBEntry * smaller
An entry.