1 | /*
|
---|
2 | * SortedVector.hpp
|
---|
3 | *
|
---|
4 | * Created on: Jul 3, 2012
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef SORTEDVECTOR_HPP_
|
---|
9 | #define SORTEDVECTOR_HPP_
|
---|
10 |
|
---|
11 |
|
---|
12 | // include config.h
|
---|
13 | #ifdef HAVE_CONFIG_H
|
---|
14 | #include <config.h>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #include <algorithm>
|
---|
18 | #include <boost/lambda/lambda.hpp>
|
---|
19 | #include <boost/shared_ptr.hpp>
|
---|
20 | #include <boost/bind.hpp>
|
---|
21 | #include <vector>
|
---|
22 |
|
---|
23 | /** Functor for transforming instance of type T to copies contained in shared_ptr.
|
---|
24 | *
|
---|
25 | */
|
---|
26 | template <typename T>
|
---|
27 | struct SharedPtrAllocator {
|
---|
28 | boost::shared_ptr<T> operator()(const T& a) {
|
---|
29 | return boost::shared_ptr<T>( new T(a) );
|
---|
30 | }
|
---|
31 | };
|
---|
32 |
|
---|
33 |
|
---|
34 | /** This class represents a general sorted container of instances, stored as shared_ptr.
|
---|
35 | *
|
---|
36 | * These instances are stored in order not by appearance memory due to shared_ptr but
|
---|
37 | * by the comparison operators of the underlying type.
|
---|
38 | *
|
---|
39 | * The sorted vector is truely a sorted vector. Hence, we must access often but change
|
---|
40 | * only seldomly. This is due to item#23 in [Meyers, "Effective STL"].
|
---|
41 | *
|
---|
42 | */
|
---|
43 | template <typename T>
|
---|
44 | struct SortedVector
|
---|
45 | {
|
---|
46 | typedef typename T::ptr T_ptr;
|
---|
47 | typedef std::vector<T_ptr> Container_t;
|
---|
48 | public:
|
---|
49 | /** Constructor from an unsorted vector of instances.
|
---|
50 | *
|
---|
51 | * @param _container unsorted vector of instances
|
---|
52 | */
|
---|
53 | SortedVector(const Container_t &_container) :
|
---|
54 | ContainerSorted(false),
|
---|
55 | container(_container)
|
---|
56 | {
|
---|
57 | sort();
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** Constructor from an unsorted vector of instances.
|
---|
61 | *
|
---|
62 | * @param _container unsorted vector of instances
|
---|
63 | */
|
---|
64 | SortedVector(const std::vector<T> &_container) :
|
---|
65 | ContainerSorted(false)
|
---|
66 | {
|
---|
67 | container.resize(_container.size());
|
---|
68 | SharedPtrAllocator<T> allocator;
|
---|
69 | std::transform(_container.begin(), _container.end(),
|
---|
70 | container.begin(), allocator);
|
---|
71 | sort();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /** Constructor from a single instance.
|
---|
75 | *
|
---|
76 | * A single instance is automatically sorted.
|
---|
77 | *
|
---|
78 | * @param _instance single instance
|
---|
79 | */
|
---|
80 | SortedVector(T_ptr &_instance) :
|
---|
81 | ContainerSorted(true)
|
---|
82 | {
|
---|
83 | container.push_back(_instance);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Default constructor.
|
---|
87 | *
|
---|
88 | * An empty vector is automatically sorted.
|
---|
89 | *
|
---|
90 | * @return
|
---|
91 | */
|
---|
92 | SortedVector() :
|
---|
93 | ContainerSorted(true)
|
---|
94 | {}
|
---|
95 |
|
---|
96 | /** Getter to sorted container.
|
---|
97 | *
|
---|
98 | * This getter is made const only through the virtue of making the member
|
---|
99 | * variables mutable. This is however the only sensible way as to the outside
|
---|
100 | * the container must be treatable as const despite "lazy" resorting on this
|
---|
101 | * read-only access in the background.
|
---|
102 | *
|
---|
103 | * @return const ref to internal sorted container
|
---|
104 | */
|
---|
105 | const Container_t &getContainer() const {
|
---|
106 | if (!ContainerSorted)
|
---|
107 | sort();
|
---|
108 | return container;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Insert a shared_ptr instance
|
---|
112 | *
|
---|
113 | * We lazily set the container to not sorted, it is sorted on next access.
|
---|
114 | *
|
---|
115 | * @param a instance to insert
|
---|
116 | */
|
---|
117 | void insert(T_ptr &a) {
|
---|
118 | // check if present via binary_search
|
---|
119 | if (!std::binary_search(container.begin(), container.end(), a, Comparator)) {
|
---|
120 | // only insert if not present
|
---|
121 | container.push_back(a);
|
---|
122 | ContainerSorted = false;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** Insert an instance not wrapped in shared_ptr.
|
---|
127 | *
|
---|
128 | * We lazily set the container to not sorted, it is sorted on next access.
|
---|
129 | *
|
---|
130 | * @param a instance to copy and insert
|
---|
131 | */
|
---|
132 | void insert(const T &a) {
|
---|
133 | T_ptr ptr(new IndexSet(a));
|
---|
134 | // check if present via binary_search
|
---|
135 | if (!std::binary_search(container.begin(), container.end(), ptr, Comparator)) {
|
---|
136 | // only insert if not present
|
---|
137 | container.push_back(ptr);
|
---|
138 | ContainerSorted = false;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** Internal static Comparator function, passing the check on to comparison
|
---|
143 | * operator of underlying type.
|
---|
144 | * @param a First instance
|
---|
145 | * @param b Second instance
|
---|
146 | * @return pointee of a < pointee of b
|
---|
147 | */
|
---|
148 | struct Comparator_t {
|
---|
149 | bool operator()(const T_ptr &a, const T_ptr &b) const {
|
---|
150 | return *a < *b;
|
---|
151 | }
|
---|
152 | } Comparator;
|
---|
153 |
|
---|
154 | private:
|
---|
155 | /** Internal sort function.
|
---|
156 | *
|
---|
157 | */
|
---|
158 | void sort() const {
|
---|
159 | std::sort(container.begin(), container.end(), Comparator);
|
---|
160 | ContainerSorted = true;
|
---|
161 | }
|
---|
162 |
|
---|
163 | protected:
|
---|
164 | //!> internal flag that container is currently sorted, mutable to let getContainer() be const member function
|
---|
165 | mutable bool ContainerSorted;
|
---|
166 |
|
---|
167 | private:
|
---|
168 | //!> internal container that is always sorted, mutable to let getContainer() be const member function
|
---|
169 | mutable Container_t container;
|
---|
170 | };
|
---|
171 |
|
---|
172 |
|
---|
173 |
|
---|
174 | #endif /* SORTEDVECTOR_HPP_ */
|
---|