OpenASIP  2.0
SelectSet.icc
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 SelectSet.icc
26  *
27  * Inline implementation of SelectSet class.
28  *
29  * @author Tommi Rantanen 2004 (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 "Application.hh"
35 
36 
37 /**
38  * Constructor.
39  *
40  * @param type Type of the field.&%
41  */
42 template <bool (EntryKeyField::*select)(const EntryKeyField&) const,
43  bool (EntryKeyField::*unSelect)(const EntryKeyField&) const>
44 SelectSet<select, unSelect>::SelectSet(const EntryKeyFieldProperty* type):
45  Matcher(type) {
46 }
47 
48 /**
49  * Destructor.
50  */
51 template <bool (EntryKeyField::*select)(const EntryKeyField&) const,
52  bool (EntryKeyField::*unSelect)(const EntryKeyField&) const>
53 SelectSet<select, unSelect>::~SelectSet() {
54 }
55 
56 /**
57  * Filters out entries which do not satisfy selection criteria.
58  *
59  * However, equal values are accepted.
60  *
61  * @param searchKey Search key.
62  * @param components Entries from which to find. Updated to contain
63  * entries that matched the search request.
64  */
65 template <bool (EntryKeyField::*select)(const EntryKeyField&) const,
66  bool (EntryKeyField::*unSelect)(const EntryKeyField&) const>
67 void
68 SelectSet<select, unSelect>::quickFilter(
69  const CostDBEntryKey& searchKey,
70  CostDBTypes::EntryTable& components) {
71 
72  CostDBTypes::EntryTable filtered;
73  EntryKeyField searchField = searchKey.keyFieldOfType(*fieldType());
74 
75  for (CostDBTypes::EntryTable::iterator i = components.begin();
76  i != components.end(); i++) {
77 
78  EntryKeyField field = (*i)->keyFieldOfType(*fieldType());
79  if (field.isEqual(searchField) || (field.*select)(searchField)) {
80  filtered.push_back(*i);
81  }
82  }
83  components = filtered;
84 }
85 
86 
87 /**
88  * Searches for database entries that satisfies selection criteria.
89  *
90  * However, equal values are accepted.
91  *
92  * Filtering does not require a search key since QuickFilter()
93  * guarantees that unacceptable entries do not exist.
94  *
95  * Only the best matches are returned, i.e. if two entries has only
96  * the field to which search is applied different, closer one to the
97  * search key is chosen.
98  *
99  * @param components Entries from which to find. Updated to contain
100  * entries that matched the search request.
101  */
102 template <bool (EntryKeyField::*select)(const EntryKeyField&) const,
103  bool (EntryKeyField::*unSelect)(const EntryKeyField&) const>
104 void
105 SelectSet<select, unSelect>::filter(
106  const CostDBEntryKey&,
107  CostDBTypes::EntryTable& components) {
108 
109  CostDBTypes::EntryTable filtered;
110 
111  for (CostDBTypes::EntryTable::iterator i1 = components.begin();
112  i1 != components.end(); i1++) {
113 
114  EntryKeyField field1 = (*i1)->keyFieldOfType(*fieldType());
115 
116  bool addEntry = true;
117 
118  for (CostDBTypes::EntryTable::iterator i2 = filtered.begin();
119  i2 != filtered.end(); i2++) {
120 
121  if (!onlyThisFieldDiffers(fieldType(), *(*i1), *(*i2))) {
122  continue;
123  }
124 
125  EntryKeyField field2 = (*i2)->keyFieldOfType(*fieldType());
126 
127  if ((field1.*unSelect)(field2)) {
128  filtered.erase(i2);
129  i2--;
130  } else if ((field1.*select)(field2)) {
131  addEntry = false;
132  } else if (field1.isEqual(field2)) {
133  assert(false);
134  }
135  }
136 
137  if (addEntry) {
138  filtered.push_back(*i1);
139  }
140  *i1 = 0;
141  }
142 
143  components = filtered;
144 }