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.
25 * @file AssocTools.icc
27 * Tools for handling STL Associative Containers (usually set).
29 * Inline and template definitions.
31 * @author Pekka Jääskeläinen 2007 (pekka.jaaskelainen-no.spam-tut.fi)
38 * Deletes all items in a map and clears it.
40 * Calls delete for all items in container and clears it.
42 * @param aMap The map to delete all items from.
44 template <typename ContainerType>
46 AssocTools::deleteAllItems(ContainerType& aMap) {
48 typename ContainerType::iterator next;
49 for (typename ContainerType::iterator i = aMap.begin();
52 // This trick is necessary because this same container can be
53 // modified (element erased) in the destructor of the deleted
54 // object. It would render the iterator faulty. Note that this
55 // works only for associative containers, with vectors the
56 // next iterator would get corrupted too.
67 * Delete all values in a map and clears it.
69 * @param aMap The map to delete the values from.
71 template <typename ContainerType>
73 AssocTools::deleteAllValues(ContainerType& aMap) {
75 for (typename ContainerType::iterator i = aMap.begin(); i != aMap.end();
83 * Checks if an element is found by the given key from the given container.
85 * @param aContainer The container to look in.
86 * @param aKey The key to look for.
87 * @return True if the key is found, otherwise false.
89 template <typename ContainerType, typename KeyType>
91 AssocTools::containsKey(
92 const ContainerType& aContainer,
93 const KeyType& aKey) {
95 return (aContainer.find(aKey) != aContainer.end());
102 * Constructs set difference of the given containers.
104 * The elements in the difference are selected by using < operator
105 * of the elements in the container. If the first given container contains
106 * an element that the second does not, it is added to the difference.
108 * @param firstContainer The first container.
109 * @param secondContainer The second container.
110 * @param difference Result set. Updated by adding all elements in the first
111 * container that are not in the second container. Note the items
112 * are added to an existing (potentially non-empty) container.
114 template <typename ContainerType>
116 AssocTools::difference(
117 const ContainerType& firstContainer,
118 const ContainerType& secondContainer,
119 ContainerType& difference) {
121 std::insert_iterator<ContainerType>
122 differenceIter(difference, difference.end());
123 std::set_difference(firstContainer.begin(), firstContainer.end(),
124 secondContainer.begin(), secondContainer.end(),
129 * Finds all pair-wise combinations of elements in two containers.
131 * @return A set of pairs containing all combinations of elements.
133 template <typename ContainerType1, typename ContainerType2>
135 typename ContainerType1::value_type,
136 typename ContainerType2::value_type> >
138 ContainerType1& firstContainer,
139 ContainerType2& secondContainer) {
141 typedef std::set<std::pair<
142 typename ContainerType1::value_type,
143 typename ContainerType2::value_type> > CombinationSet;
144 CombinationSet combinations;
145 for (typename ContainerType1::const_iterator i1 = firstContainer.begin();
146 i1 != firstContainer.end(); ++i1) {
147 for (typename ContainerType2::const_iterator i2 =
148 secondContainer.begin(); i2 != secondContainer.end(); ++i2) {
149 combinations.insert(std::make_pair(*i1, *i2));
156 * Finds all pair-wise combinations of elements in two containers.
158 * @return A set of pairs containing all combinations of elements.
160 template <typename Comparator,typename ContainerType1, typename ContainerType2>
162 typename ContainerType1::value_type,
163 typename ContainerType2::value_type>, Comparator >
165 ContainerType1& firstContainer,
166 ContainerType2& secondContainer) {
168 typedef std::set<std::pair<
169 typename ContainerType1::value_type,
170 typename ContainerType2::value_type>, Comparator > CombinationSet;
171 CombinationSet combinations;
172 for (typename ContainerType1::const_iterator i1 = firstContainer.begin();
173 i1 != firstContainer.end(); ++i1) {
174 for (typename ContainerType2::const_iterator i2 =
175 secondContainer.begin(); i2 != secondContainer.end(); ++i2) {
176 combinations.insert(std::make_pair(*i1, *i2));
184 * Appends all data from the first container to the second.
186 template <typename ContainerType>
187 void AssocTools::append(const ContainerType& src, ContainerType &dest) {
188 for (typename ContainerType::const_iterator i = src.begin();
189 i != src.end(); i++) {