Analytics Template Library
 All Classes Namespaces Functions Variables Pages
small_set.hpp
1 #include <algorithm>
2 #include <cassert>
3 
4 #include <iostream>
5 #include <set>
6 
7 
8 
9 namespace util{
10 
11 enum class NoneType {
12  None
13 };
14 const NoneType None = None;
15 
16 template <typename Object, size_t N>
17 class small_vector {
18 public:
19 
20  explicit small_vector(size_t initSize = 0)
21  : theSize(initSize), theCapacity(N) {
22  assert(initSize <= N);
23  }
24 
25  small_vector(const small_vector & rhs) : objects(NULL) {
26  operator=(rhs);
27  }
28 
29  ~small_vector() {
30 
31  }
32 
33  bool empty() const {
34  return size() == 0;
35  }
36 
37  int size() const {
38  return theSize;
39  }
40 
41  int capacity() const {
42  return theCapacity;
43  }
44 
45  Object & operator[](int index) {
46 #ifndef NO_CHECK
47  assert(index < theSize);
48 #endif
49  return objects[ index ];
50  }
51 
52  const Object & operator[](int index) const {
53 #ifndef NO_CHECK
54  assert(index < theSize);
55 #endif
56  return objects[ index ];
57  }
58 
59  const small_vector & operator=(const small_vector & rhs) {
60  if (this != &rhs) {
61  std::memset(&objects, 0, N);
62  theSize = rhs.size();
63  theCapacity = rhs.theCapacity;
64 
65  for (int k = 0; k < size(); k++)
66  objects[ k ] = rhs.objects[ k ];
67  }
68  return *this;
69  }
70 
71 
72 
73  // Stacky stuff
74 
75  void push_back(const Object & x) {
76  assert(theSize < (N));
77  objects[ theSize++ ] = x;
78  }
79 
80  void pop_back() {
81  assert(theSize > 0);
82  theSize--;
83  }
84 
85  const Object & back() const {
86  // if (empty())
87  // throw UnderflowException();
88  return objects[ theSize - 1 ];
89  }
90 
91  // Iterator stuff: not bounds checked
92  typedef Object * iterator;
93  typedef const Object * const_iterator;
94 
95  iterator begin() {
96  return &objects[ 0 ];
97  }
98 
99  const_iterator begin() const {
100  return &objects[ 0 ];
101  }
102 
103  iterator end() {
104  return &objects[ size() ];
105  }
106 
107  const_iterator end() const {
108  return &objects[ size() ];
109  }
110 
111  void clear() {
112  std::memset(&objects, 0, N);
113  this->theSize = 0;
114  }
115 
116 private:
117  int theSize;
118  int theCapacity;
119  Object objects[N];
120 };
121 
122 
123 
124 template <typename T, unsigned N = 5, typename C = std::less<T> >
125 class small_set {
126 
127  small_vector<T, N> data;
128 
129 public:
130 
131  typedef typename small_vector<T, N>::const_iterator const_iterator;
132  typedef typename small_vector<T, N>::iterator iterator;
133 
134  typedef size_t size_type;
135 
136  small_set() {
137  }
138 
139  bool empty() const {
140  return data.empty();
141  }
142 
143  const_iterator begin() const {
144  return data.begin();
145  }
146 
147  iterator begin() {
148  return data.begin();
149  }
150 
151  const_iterator end() const {
152  return data.end();
153  }
154 
155  iterator end() {
156  return data.end();
157  }
158 
159  size_type size() const {
160 
161  return data.size();
162  }
163 
164  size_type contains(const T &V) const {
165 
166  return vfind(V) == data.end() ? 0 : 1;
167  }
168 
169  std::pair<NoneType, bool> insert(const T &V) {
170 
171  const_iterator I = vfind(V);
172  if (I != data.end()) // Don't reinsert if it already exists.
173  return std::make_pair(None, false);
174  if (data.size() < N) {
175  data.push_back(V);
176  return std::make_pair(None, true);
177  }
178 
179 
180 
181  return std::make_pair(None, true);
182  }
183 
184  template <typename IterT>
185  void insert(IterT I, IterT E) {
186 
187  for (; I != E; ++I)
188  insert(*I);
189  }
190 
191  bool erase(const T &V) {
192 
193  for (iterator I = data.begin(), E = data.end(); I != E; ++I)
194  if (*I == V) {
195  data.erase(I);
196 
197  return true;
198  }
199  return false;
200  }
201 
202  void clear() {
203 
204  data.clear();
205  }
206 private:
207 
208  const_iterator vfind(const T &V) const {
209  for (const_iterator I = data.begin(), E = data.end(); I != E; ++I)
210  if (*I == V)
211  return I;
212 
213  return data.end();
214  }
215 };
216 }
Definition: Combinations.hpp:17
Definition: small_set.hpp:125
Definition: small_set.hpp:17