OpenASIP 2.2
Loading...
Searching...
No Matches
FilterSearch.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 FilterSearch.cc
26 *
27 * Implementation of FilterSearch 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 "FilterSearch.hh"
37#include "ExactMatch.hh"
38#include "SubSet.hh"
39#include "SuperSet.hh"
40#include "Interpolation.hh"
41#include "Application.hh"
42
43
44/**
45 * Default constructor.
46 */
49
50/**
51 * Destructor.
52 */
54
55 for (CacheTable::iterator i = entryCache_.begin();
56 i != entryCache_.end(); i++) {
57
58 assert(*i != NULL);
59 delete *i;
60 *i = NULL;
61 }
62 for (MatcherTable::iterator i = matcherStorage_.begin();
63 i != matcherStorage_.end(); i++) {
64
65 assert(*i != NULL);
66 delete *i;
67 *i = NULL;
68 }
69}
70
71/**
72 * Returns a copy of this search strategy.
73 *
74 * Client is responsible of deallocating the memory reserved for the
75 * returned object.
76 *
77 * @return Copy of this search strategy.
78 */
81
82 CacheTable newEntryCache;
83 for (CacheTable::const_iterator i = entryCache_.begin();
84 i != entryCache_.end(); i++) {
85
86 newEntryCache.push_back((*i)->copy());
87 }
88 FilterSearch* newSearch = new FilterSearch();
89 newSearch->entryCache_ = newEntryCache;
90 return newSearch;
91}
92
93/**
94 * Searches entries that match with certain search key on a specific
95 * type of match.
96 *
97 * Search results are valid until the strategy is deleted, after which
98 * the use of the results of the queries might lead into unexpected
99 * behaviour.
100 *
101 * Client must not deallocate the memory reserved for the results.
102 *
103 * @param searchKey Search key.
104 * @param components Entries from which to find.
105 * @param match Type of match. The fields are searched in the order
106 * they exist in this table.
107 * @return Entries matching search key and type of match.
108 */
111 const CostDBEntryKey& searchKey,
112 CostDBTypes::EntryTable components,
113 const CostDBTypes::MatchTypeTable& match) {
114
115 if (components.size() == 0) {
116 return components;
117 }
118
119 // check the cache
120 CostDBTypes::EntryTable cache_entries = findFromCache(searchKey, match);
121 if (!cache_entries.empty()) {
122 return cache_entries;
123 }
124
125 MatcherTable matcher = createMatchers(match);
126
127 // filter quickly poor ones out
128 for (MatcherTable::const_iterator i = matcher.begin();
129 i != matcher.end(); i++) {
130
131 (*i)->quickFilter(searchKey, components);
132 }
133
134 // choose correct entries from acceptable ones
135 for (MatcherTable::const_iterator i = matcher.begin();
136 i != matcher.end(); i++) {
137
138 (*i)->filter(searchKey, components);
139 }
140
141 // insert found entries into cache
142 entryCache_.push_back(new Cache(match, searchKey.copy(), components));
143
144 return components;
145}
146
147/**
148 * Finds entries matching search key and type of match from the cache.
149 *
150 * @param searchKey Search key.
151 * @param match Type of match.
152 * @return Entries matching search key and type of match.
153 */
156 const CostDBEntryKey& searchKey,
157 const CostDBTypes::MatchTypeTable& match) {
158
159 CostDBTypes::EntryTable cacheEntries;
160 for (CacheTable::iterator i = entryCache_.begin();
161 i != entryCache_.end(); i++) {
162
163 if ((*i)->isEqual(match, &searchKey)) {
164 cacheEntries = (*i)->entries();
165 break;
166 }
167 }
168 return cacheEntries;
169}
170
171/**
172 * Creates sub components of this search strategy.
173 *
174 * @param match Type of match.
175 * @return Sub components of this search strategy.
176 * @exception TypeMismatch Requested type is unknown.
177 */
180 MatcherTable matcher;
181 for (CostDBTypes::MatchTypeTable::const_iterator i = match.begin();
182 i != match.end(); i++) {
183
184 Matcher* new_matcher = NULL;
185 if ((*i)->matchingType() == CostDBTypes::MATCH_EXACT) {
186 new_matcher = new ExactMatch((*i)->fieldType());
187 } else if ((*i)->matchingType() == CostDBTypes::MATCH_SUBSET) {
188 new_matcher = new SubSet((*i)->fieldType());
189 } else if ((*i)->matchingType() == CostDBTypes::MATCH_SUPERSET) {
190 new_matcher = new SuperSet((*i)->fieldType());
191 } else if ((*i)->matchingType() == CostDBTypes::MATCH_INTERPOLATION) {
192 new_matcher = new Interpolation((*i)->fieldType());
193 } else if ((*i)->matchingType() == CostDBTypes::MATCH_ALL) {
194
195 } else {
196 throw TypeMismatch(__FILE__, __LINE__,
197 "FilterSearch::createMatchers");
198 }
199
200 assert(new_matcher != NULL);
201 matcherStorage_.push_back(new_matcher);
202 matcher.push_back(new_matcher);
203 }
204 return matcher;
205}
206
207//////////////////////////////////////////////////
208// FilterSearch::Cache
209//////////////////////////////////////////////////
210
211/**
212 * Constructor.
213 *
214 * @param matchingType Type of match.
215 * @param key Search key.
216 * @param entry Database entries.
217 */
219 CostDBTypes::MatchTypeTable matchingType,
220 CostDBEntryKey* key,
222 searchKey_(key), entries_(entry) {
223
224 for (CostDBTypes::MatchTypeTable::iterator i = matchingType.begin();
225 i != matchingType.end(); i++) {
226 matchType_.push_back(new MatchType(*(*i)));
227 }
228}
229
230/**
231 * Destructor.
232 *
233 * Deallocates memory reserved for search key and type of match. Not
234 * responsible of deleting entries.
235 */
237
238 assert(searchKey_ != NULL);
239 delete searchKey_;
240 searchKey_ = NULL;
241
242 for (CostDBTypes::MatchTypeTable::iterator i = matchType_.begin();
243 i != matchType_.end(); i++) {
244
245 assert(*i != NULL);
246 delete *i;
247 *i = NULL;
248 }
249}
250
251/**
252 * Returns a copy of this cache.
253 *
254 * Client is responsible of deallocating the memory reserved for the
255 * returned object.
256 *
257 * @return Copy of this cache.
258 */
261
262 CostDBEntryKey* newSearchKey = searchKey_->copy();
263 CostDBTypes::MatchTypeTable newMatchType;
264
265 for (CostDBTypes::MatchTypeTable::const_iterator i = matchType_.begin();
266 i != matchType_.end(); i++) {
267 newMatchType.push_back(
268 new MatchType((*i)->fieldType(), (*i)->matchingType()));
269 }
270
271 CostDBTypes::EntryTable newEntries;
272 for (CostDBTypes::EntryTable::const_iterator i = entries_.begin();
273 i != entries_.end(); i++) {
274 newEntries.push_back(*i);
275 }
276
277 return new FilterSearch::Cache(newMatchType, newSearchKey, newEntries);
278}
279
280/**
281 * Tests if cache matches to the type of match and search key.
282 *
283 * @param matchingType Type of match.
284 * @param key Search key.
285 * @return true If cache matches to the type of match and search key,
286 * false otherwise.
287 */
288bool
290 CostDBTypes::MatchTypeTable matchingType,
291 const CostDBEntryKey* key) const {
292
293 // warning: there could be different keys that yield the same result
294 if (!searchKey_->isEqual(*key)) {
295 return false;
296 }
297
298 // warning: a search may succeed if it's cached and fail if not
299 // since the order of match types accepted is insignificant
300 for (CostDBTypes::MatchTypeTable::const_iterator i = matchingType.begin();
301 i != matchingType.end(); i++) {
302
303 bool isThere = false;
304 for (CostDBTypes::MatchTypeTable::const_iterator j =
305 matchType_.begin(); j != matchType_.end(); j++) {
306
307 if ((*i)->isEqual(*(*j))) {
308 isThere = true;
309 break;
310 }
311 }
312 if (!isThere) {
313 return false;
314 }
315 }
316 return true;
317}
318
319/**
320 * Returns entries.
321 *
322 * @return Cached entries.
323 */
326 return entries_;
327}
#define assert(condition)
CostDBEntryKey * copy() const
std::vector< MatchType * > MatchTypeTable
Table of types of match.
std::vector< CostDBEntry * > EntryTable
Table of database entries.
CostDBTypes::MatchTypeTable matchType_
Type of match used for these results.
CostDBTypes::EntryTable entries() const
Cache * copy() const
bool isEqual(CostDBTypes::MatchTypeTable matchingType, const CostDBEntryKey *key) const
Cache(CostDBTypes::MatchTypeTable matchingType, CostDBEntryKey *key, CostDBTypes::EntryTable &entry)
MatcherTable matcherStorage_
Storage for all matchers. They cannot be deleted before search strategy itself is deleted....
CacheTable entryCache_
Results of the previous queries.
CostDBTypes::EntryTable findFromCache(const CostDBEntryKey &searchKey, const CostDBTypes::MatchTypeTable &match)
CostDBTypes::EntryTable search(const CostDBEntryKey &searchKey, CostDBTypes::EntryTable components, const CostDBTypes::MatchTypeTable &match)
std::vector< Matcher * > MatcherTable
Table of matcher types.
SearchStrategy * copy() const
std::vector< Cache * > CacheTable
Table of cache entries.
MatcherTable createMatchers(const CostDBTypes::MatchTypeTable &match)
virtual ~FilterSearch()