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 ContainerTools.icc
26 * @author Pekka Jääskeläinen (pjaaskel-no.spam-cs.tut.fi) 2003
28 * Tools for handling STL containers.
30 * Inline and template definitions.
38 * Returns true if given container contains the given value.
40 * @param aContainer The container to look in.
41 * @param aValue Value to look for.
43 template <typename ContainerType, typename ElementType>
45 ContainerTools::containsValue(
46 const ContainerType& aContainer, const ElementType& aValue) {
48 for (typename ContainerType::const_iterator iter = aContainer.begin();
49 iter != aContainer.end(); iter++) {
51 if (*iter == aValue) {
61 * Removes an element from a container, if its found.
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.
67 template <typename ContainerType, typename ElementType>
69 ContainerTools::removeValueIfExists(
70 ContainerType& aContainer,
71 const ElementType& aKey) {
73 for (typename ContainerType::iterator i = aContainer.begin();
74 i != aContainer.end(); i++) {
86 * Removes a pointer from a container if it is found and destructs the
87 * object that it points to.
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.
93 template <typename ContainerType, typename ElementType>
95 ContainerTools::deleteValueIfExists(
96 ContainerType& aContainer,
97 const ElementType& aKey) {
99 for (typename ContainerType::iterator i = aContainer.begin();
100 i != aContainer.end(); i++) {
113 * Removes the values in the second container from the first container
116 * @param aContainer The container to remove values from.
117 * @param toRemove The container which contains the values to be removed.
119 template <typename ContainerType>
121 ContainerTools::removeValues(
122 ContainerType& aContainer,
123 const ContainerType& toRemove) {
125 for (typename ContainerType::const_iterator iter = toRemove.begin();
126 iter != toRemove.end(); iter++) {
127 removeValueIfExists(aContainer, *iter);
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.
135 * Useful when removing element that is not the last element from vector where
136 * order of the elements does not matter
138 * @param aContainer The container to remove value from.
139 * @param index The index to element to be removed.
141 template <typename E, typename I>
143 ContainerTools::swapRemoveValue(std::vector<E>& aContainer, I index) {
144 size_t castedIndex = static_cast<size_t>(index);
146 aContainer.at(castedIndex) = aContainer.back();
147 aContainer.pop_back();