OpenASIP  2.0
SetTools.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 SetTools.icc
26  *
27  * Tools for handling STL Sets.
28  *
29  * Inline and template definitions.
30  *
31  * @author Heikki Kultala 2008 (hkultala-no.spam-cs.tut.fi)
32  */
33 
34 #include <algorithm>
35 
36 /**
37  * Creates an intersection of the given containers.
38  *
39  * The elements in the intersection are selected by using < and > operators
40  * of the elements in the container. Thus, if both containers contains an
41  * equal element, it is added to the intersection.
42  *
43  * @param firstContainer The first container.
44  * @param secondContainer The second container.
45  * @param intersection Intersection of the container is added to this
46  * container.
47  */
48 template <typename ValueType>
49 void
50 SetTools::intersection(
51  const std::set<ValueType>& firstContainer,
52  const std::set<ValueType>& secondContainer,
53  std::set<ValueType>& intersection) {
54 
55  std::insert_iterator<std::set<ValueType> >
56  intersectIter(intersection, intersection.end());
57  std::set_intersection(firstContainer.begin(), firstContainer.end(),
58  secondContainer.begin(), secondContainer.end(),
59  intersectIter);
60 }
61 
62 /**
63  * Creates an intersection of the given containers.
64  *
65  * Same as intersection(3), but this returns the intersection instead of
66  * appending existing set via reference.
67  *
68  * @param firstContainer The first container.
69  * @param secondContainer The second container.
70  * @returns The intersection.
71  */
72 template <typename ValueType>
73 std::set<ValueType>
74 SetTools::intersection(
75  const std::set<ValueType>& firstContainer,
76  const std::set<ValueType>& secondContainer) {
77 
78  std::set<ValueType> result;
79  std::set_intersection(firstContainer.begin(), firstContainer.end(),
80  secondContainer.begin(), secondContainer.end(),
81  std::inserter(result, result.end()));
82  return result;
83 }
84 
85 
86 /**
87  * Creates an intersection of the given containers.
88  *
89  * The elements in the intersection are selected by using comparator operator
90  * of the elements in the container. Thus, if both containers contains an
91  * equal element, it is added to the intersection.
92  *
93  * @param firstContainer The first container.
94  * @param secondContainer The second container.
95  * @param intersection Intersection of the container is added to this
96  * container.
97  */
98 template <typename ValueType, typename Comparator>
99 void
100 SetTools::intersection(
101  const std::set<ValueType, Comparator>& firstContainer,
102  const std::set<ValueType, Comparator>& secondContainer,
103  std::set<ValueType, Comparator>& intersection) {
104 
105  std::insert_iterator<std::set<ValueType, Comparator> >
106  intersectIter(intersection, intersection.end());
107  std::set_intersection(firstContainer.begin(), firstContainer.end(),
108  secondContainer.begin(), secondContainer.end(),
109  intersectIter, Comparator());
110 }
111