OpenASIP  2.0
MapTools.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 MapTools.icc
26  * @author Pekka Jääskeläinen (pekka.jaaskelainen-no.spam-tut.fi) 2004
27  *
28  * Tools for handling STL Pair Associative Containers (usually map).
29  *
30  * Inline and template definitions.
31  *
32  */
33 
34 /**
35  *
36  * Returns true if given container contains the given value.
37  *
38  * @param aMap The map to look in.
39  * @param aValue Value to compare to.
40  */
41 template <typename ContainerType, typename ValueType>
42 bool
43 MapTools::containsValue(const ContainerType& aMap, const ValueType& aValue) {
44 
45  for (typename ContainerType::const_iterator i = aMap.begin();
46  i != aMap.end(); i++) {
47  if ((*i).second == aValue)
48  return true;
49  }
50  return false;
51 }
52 
53 /**
54  *
55  * Returns the (first) key found connected to given value.
56  *
57  * @return The key.
58  * @param aMap The map to look in.
59  * @param aValue Value to compare to.
60  * @exception KeyNotFound if key wasn't found in the map.
61  */
62 template <typename KeyType, typename ContainerType, typename ValueType>
63 KeyType
64 MapTools::keyForValue(const ContainerType& aMap, const ValueType& aValue) {
65  for (typename ContainerType::const_iterator i = aMap.begin();
66  i != aMap.end(); i++) {
67  if ((*i).second == aValue)
68  return (*i).first;
69  }
70 
71  throw KeyNotFound(
72  __FILE__, __LINE__, "MapTools::keyForValue()", "Key was not found.");
73 }
74 
75 /**
76  *
77  * Returns true if given map contains the given key.
78  *
79  * @param aMap The map to look in.
80  * @param aKey Value to compare to.
81  */
82 template <typename ContainerType, typename KeyType>
83 bool
84 MapTools::containsKey(const ContainerType& aMap, const KeyType& aKey) {
85  return (aMap.find(aKey) != aMap.end());
86 }
87 
88 /**
89  * Returns value requested by key.
90  *
91  * Does not create a new cell in case key was not found. Useful for methods
92  * that want to access a map in a const method.
93  *
94  * @param aMap The map to look in.
95  * @param aKey Key of to use for finding value.
96  * @return Value of requested key.
97  */
98 template <typename ValueType, typename MapType, typename KeyType>
99 ValueType
100 MapTools::valueForKey(const MapType& aMap, const KeyType& aKey) {
101  typename MapType::const_iterator valueToFind = aMap.find(aKey);
102 
103  if (valueToFind == aMap.end()) {
104  throw KeyNotFound(
105  __FILE__, __LINE__,
106  "MapTools::valueForKey()", "Key was not found.");
107  }
108 
109  return (*valueToFind).second;
110 }
111 
112 /**
113  * Returns value requested by key.
114  *
115  * Does not create a new cell in case key was not found. Useful for methods
116  * that want to access a map in a const method.
117  *
118  * @param aMap The map to look in.
119  * @param aKey Key of to use for finding value.
120  * @return Value of requested key. empty (default constructor) if not found.
121  */
122 template <typename ValueType, typename MapType, typename KeyType>
123 const ValueType
124 MapTools::valueForKeyNoThrow(const MapType& aMap, const KeyType& aKey) {
125  typename MapType::const_iterator valueToFind = aMap.find(aKey);
126 
127  if (valueToFind == aMap.end()) {
128  return ValueType();
129  }
130 
131  return (*valueToFind).second;
132 }
133 
134 /**
135  *
136  * Deletes all values in a map and clears it.
137  *
138  * Calls delete for all values in map and clears it.
139  *
140  * @param aMap The map to delete all values from.
141  */
142 template <typename ContainerType>
143 void
144 MapTools::deleteAllValues(ContainerType& aMap) {
145 
146  typename ContainerType::iterator next;
147  for (typename ContainerType::iterator i = aMap.begin();
148  i != aMap.end(); ) {
149 
150  // This trick is necessary because this same container can be
151  // modified (element erased) in the destructor of the deleted
152  // object. It would render the iterator faulty. Note that this
153  // works only for associative containers, with vectors the
154  // next iterator would get corrupted too.
155  next = i;
156  next++;
157  delete (*i).second;
158  i = next;
159  }
160 
161  aMap.clear();
162 }
163 
164 /**
165  * Deletes all keys in a map and clears it.
166  *
167  * @param aMap Map in which keys are deleted.
168  */
169 template<typename MapType>
170 void
171 MapTools::deleteAllKeys(MapType& aMap) {
172 
173  typename MapType::iterator next;
174  for (typename MapType::iterator i = aMap.begin(); i != aMap.end(); ) {
175  next = i;
176  next++;
177  delete (*i).first;
178  i = next;
179  }
180 
181  aMap.clear();
182 }
183 
184 /**
185  * Deletes and erases item from a map by a given key
186  *
187  * @param aMap Map to delete the item from
188  * @param aKey Key to the item to be deleted
189  */
190 template <typename MapType, typename KeyType>
191 void
192 MapTools::deleteByKey(MapType& aMap, const KeyType& aKey) {
193  delete aMap[aKey];
194  aMap.erase(aKey);
195 }
196 
197 /**
198  * Removes all the items that has the given value from the map.
199  *
200  * @param aMap The map to remove the items from.
201  * @param aValue The value to be removed.
202  * @return True, if at least one item was removed, otherwise false.
203  */
204 template <typename MapType, typename ValueType>
205 bool
206 MapTools::removeItemsByValue(MapType& aMap, const ValueType& aValue) {
207 
208  bool erased = false;
209 
210  for (typename MapType::iterator iter = aMap.begin();
211  iter != aMap.end();) {
212 
213  typename MapType::iterator next = iter;
214  next++;
215 
216  if ((*iter).second == aValue) {
217  aMap.erase(iter);
218  erased = true;
219  }
220 
221  iter = next;
222  }
223 
224  return erased;
225 }
226 
227 
228 /**
229  * Returns a set of keys that exists in the given map.
230  *
231  * @param aMap The map to look the keys for.
232  * @return A set of keys.
233  */
234 template <typename KeyType, typename MapType>
235 std::set<KeyType>
236 MapTools::keys(const MapType& aMap) {
237 
238  std::set<KeyType> keySet;
239 
240  for (typename MapType::const_iterator iter = aMap.begin();
241  iter != aMap.end(); iter++) {
242  keySet.insert((*iter).first);
243  }
244 
245  return keySet;
246 }
247