OpenASIP  2.0
ContainerTools.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 ContainerTools.icc
26  * @author Pekka Jääskeläinen (pjaaskel-no.spam-cs.tut.fi) 2003
27  *
28  * Tools for handling STL containers.
29  *
30  * Inline and template definitions.
31  *
32  */
33 
34 #include <algorithm>
35 
36 /**
37  *
38  * Returns true if given container contains the given value.
39  *
40  * @param aContainer The container to look in.
41  * @param aValue Value to look for.
42  */
43 template <typename ContainerType, typename ElementType>
44 bool
45 ContainerTools::containsValue(
46  const ContainerType& aContainer, const ElementType& aValue) {
47 
48  for (typename ContainerType::const_iterator iter = aContainer.begin();
49  iter != aContainer.end(); iter++) {
50 
51  if (*iter == aValue) {
52  return true;
53  }
54  }
55 
56  return false;
57 }
58 
59 
60 /**
61  * Removes an element from a container, if its found.
62  *
63  * @param aContainer The container to look in.
64  * @param aKey The key to look for.
65  * @return True if the element was removed, otherwise false.
66  */
67 template <typename ContainerType, typename ElementType>
68 bool
69 ContainerTools::removeValueIfExists(
70  ContainerType& aContainer,
71  const ElementType& aKey) {
72 
73  for (typename ContainerType::iterator i = aContainer.begin();
74  i != aContainer.end(); i++) {
75  if (*i == aKey) {
76  aContainer.erase(i);
77  return true;
78  }
79  }
80 
81  return false;
82 }
83 
84 
85 /**
86  * Removes a pointer from a container if it is found and destructs the
87  * object that it points to.
88  *
89  * @param aContainer The container to look in.
90  * @param aKey The key to look for.
91  * @return True if the element was removed, otherwise false.
92  */
93 template <typename ContainerType, typename ElementType>
94 bool
95 ContainerTools::deleteValueIfExists(
96  ContainerType& aContainer,
97  const ElementType& aKey) {
98 
99  for (typename ContainerType::iterator i = aContainer.begin();
100  i != aContainer.end(); i++) {
101  if (*i == aKey) {
102  aContainer.erase(i);
103  delete aKey;
104  return true;
105  }
106  }
107 
108  return false;
109 }
110 
111 
112 /**
113  * Removes the values in the second container from the first container
114  * given.
115  *
116  * @param aContainer The container to remove values from.
117  * @param toRemove The container which contains the values to be removed.
118  */
119 template <typename ContainerType>
120 void
121 ContainerTools::removeValues(
122  ContainerType& aContainer,
123  const ContainerType& toRemove) {
124 
125  for (typename ContainerType::const_iterator iter = toRemove.begin();
126  iter != toRemove.end(); iter++) {
127  removeValueIfExists(aContainer, *iter);
128  }
129 }
130 
131 /**
132  * Remove element at index from vector by swapping to-be-removed element and
133  * last element first and then removing the last element making removing fast.
134  *
135  * Useful when removing element that is not the last element from vector where
136  * order of the elements does not matter
137  *
138  * @param aContainer The container to remove value from.
139  * @param index The index to element to be removed.
140  */
141 template <typename E, typename I>
142 void
143 ContainerTools::swapRemoveValue(std::vector<E>& aContainer, I index) {
144  size_t castedIndex = static_cast<size_t>(index);
145 
146  aContainer.at(castedIndex) = aContainer.back();
147  aContainer.pop_back();
148 }