OpenASIP  2.0
AssocTools.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 AssocTools.icc
26  *
27  * Tools for handling STL Associative Containers (usually set).
28  *
29  * Inline and template definitions.
30  *
31  * @author Pekka Jääskeläinen 2007 (pekka.jaaskelainen-no.spam-tut.fi)
32  */
33 
34 #include <algorithm>
35 
36 /**
37  *
38  * Deletes all items in a map and clears it.
39  *
40  * Calls delete for all items in container and clears it.
41  *
42  * @param aMap The map to delete all items from.
43  */
44 template <typename ContainerType>
45 void
46 AssocTools::deleteAllItems(ContainerType& aMap) {
47 
48  typename ContainerType::iterator next;
49  for (typename ContainerType::iterator i = aMap.begin();
50  i != aMap.end(); ) {
51 
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.
57  next = i;
58  next++;
59  delete (*i);
60  i = next;
61  }
62 
63  aMap.clear();
64 }
65 
66 /**
67  * Delete all values in a map and clears it.
68  *
69  * @param aMap The map to delete the values from.
70  */
71 template <typename ContainerType>
72 void
73 AssocTools::deleteAllValues(ContainerType& aMap) {
74 
75  for (typename ContainerType::iterator i = aMap.begin(); i != aMap.end();
76  i++) {
77  delete (*i).second;
78  }
79  aMap.clear();
80 }
81 
82 /**
83  * Checks if an element is found by the given key from the given container.
84  *
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.
88  */
89 template <typename ContainerType, typename KeyType>
90 bool
91 AssocTools::containsKey(
92  const ContainerType& aContainer,
93  const KeyType& aKey) {
94 
95  return (aContainer.find(aKey) != aContainer.end());
96 }
97 
98 
99 
100 
101 /**
102  * Constructs set difference of the given containers.
103  *
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.
107  *
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.
113  */
114 template <typename ContainerType>
115 void
116 AssocTools::difference(
117  const ContainerType& firstContainer,
118  const ContainerType& secondContainer,
119  ContainerType& difference) {
120 
121  std::insert_iterator<ContainerType>
122  differenceIter(difference, difference.end());
123  std::set_difference(firstContainer.begin(), firstContainer.end(),
124  secondContainer.begin(), secondContainer.end(),
125  differenceIter);
126 }
127 
128 /**
129  * Finds all pair-wise combinations of elements in two containers.
130  *
131  * @return A set of pairs containing all combinations of elements.
132  */
133 template <typename ContainerType1, typename ContainerType2>
134 std::set<std::pair<
135  typename ContainerType1::value_type,
136  typename ContainerType2::value_type> >
137 AssocTools::pairs(
138  ContainerType1& firstContainer,
139  ContainerType2& secondContainer) {
140 
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));
150  }
151  }
152  return combinations;
153 }
154 
155 /**
156  * Finds all pair-wise combinations of elements in two containers.
157  *
158  * @return A set of pairs containing all combinations of elements.
159  */
160 template <typename Comparator,typename ContainerType1, typename ContainerType2>
161 std::set<std::pair<
162  typename ContainerType1::value_type,
163  typename ContainerType2::value_type>, Comparator >
164 AssocTools::pairs(
165  ContainerType1& firstContainer,
166  ContainerType2& secondContainer) {
167 
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));
177  }
178  }
179  return combinations;
180 }
181 
182 
183 /**
184  * Appends all data from the first container to the second.
185  */
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++) {
190  dest.insert(*i);
191  }
192 }