2 Copyright (c) 2002-2009 Tampere University.
4 This file is part of TTA-Based Codesign Environment (TCE).
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:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
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.
26 * @author Pekka Jääskeläinen (pekka.jaaskelainen-no.spam-tut.fi) 2004
28 * Tools for handling STL Pair Associative Containers (usually map).
30 * Inline and template definitions.
36 * Returns true if given container contains the given value.
38 * @param aMap The map to look in.
39 * @param aValue Value to compare to.
41 template <typename ContainerType, typename ValueType>
43 MapTools::containsValue(const ContainerType& aMap, const ValueType& aValue) {
45 for (typename ContainerType::const_iterator i = aMap.begin();
46 i != aMap.end(); i++) {
47 if ((*i).second == aValue)
55 * Returns the (first) key found connected to given value.
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.
62 template <typename KeyType, typename ContainerType, typename ValueType>
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)
72 __FILE__, __LINE__, "MapTools::keyForValue()", "Key was not found.");
77 * Returns true if given map contains the given key.
79 * @param aMap The map to look in.
80 * @param aKey Value to compare to.
82 template <typename ContainerType, typename KeyType>
84 MapTools::containsKey(const ContainerType& aMap, const KeyType& aKey) {
85 return (aMap.find(aKey) != aMap.end());
89 * Returns value requested by key.
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.
94 * @param aMap The map to look in.
95 * @param aKey Key of to use for finding value.
96 * @return Value of requested key.
98 template <typename ValueType, typename MapType, typename KeyType>
100 MapTools::valueForKey(const MapType& aMap, const KeyType& aKey) {
101 typename MapType::const_iterator valueToFind = aMap.find(aKey);
103 if (valueToFind == aMap.end()) {
106 "MapTools::valueForKey()", "Key was not found.");
109 return (*valueToFind).second;
113 * Returns value requested by key.
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.
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.
122 template <typename ValueType, typename MapType, typename KeyType>
124 MapTools::valueForKeyNoThrow(const MapType& aMap, const KeyType& aKey) {
125 typename MapType::const_iterator valueToFind = aMap.find(aKey);
127 if (valueToFind == aMap.end()) {
131 return (*valueToFind).second;
136 * Deletes all values in a map and clears it.
138 * Calls delete for all values in map and clears it.
140 * @param aMap The map to delete all values from.
142 template <typename ContainerType>
144 MapTools::deleteAllValues(ContainerType& aMap) {
146 typename ContainerType::iterator next;
147 for (typename ContainerType::iterator i = aMap.begin();
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.
165 * Deletes all keys in a map and clears it.
167 * @param aMap Map in which keys are deleted.
169 template<typename MapType>
171 MapTools::deleteAllKeys(MapType& aMap) {
173 typename MapType::iterator next;
174 for (typename MapType::iterator i = aMap.begin(); i != aMap.end(); ) {
185 * Deletes and erases item from a map by a given key
187 * @param aMap Map to delete the item from
188 * @param aKey Key to the item to be deleted
190 template <typename MapType, typename KeyType>
192 MapTools::deleteByKey(MapType& aMap, const KeyType& aKey) {
198 * Removes all the items that has the given value from the map.
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.
204 template <typename MapType, typename ValueType>
206 MapTools::removeItemsByValue(MapType& aMap, const ValueType& aValue) {
210 for (typename MapType::iterator iter = aMap.begin();
211 iter != aMap.end();) {
213 typename MapType::iterator next = iter;
216 if ((*iter).second == aValue) {
229 * Returns a set of keys that exists in the given map.
231 * @param aMap The map to look the keys for.
232 * @return A set of keys.
234 template <typename KeyType, typename MapType>
236 MapTools::keys(const MapType& aMap) {
238 std::set<KeyType> keySet;
240 for (typename MapType::const_iterator iter = aMap.begin();
241 iter != aMap.end(); iter++) {
242 keySet.insert((*iter).first);