QUDA  v0.7.0
A library for QCD on GPUs
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
loki.h
Go to the documentation of this file.
1 // The Loki Library
3 // Copyright (c) 2001 by Andrei Alexandrescu
4 // This code accompanies the book:
5 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
6 // Patterns Applied". Copyright (c) 2001. Addison-Wesley.
7 // Permission to use, copy, modify, distribute and sell this software for any
8 // purpose is hereby granted without fee, provided that the above copyright
9 // notice appear in all copies and that both that copyright notice and this
10 // permission notice appear in supporting documentation.
11 // The author or Addison-Wesley Longman make no representations about the
12 // suitability of this software for any purpose. It is provided "as is"
13 // without express or implied warranty.
15 
16 #ifndef ASSOCVECTOR_INC_
17 #define ASSOCVECTOR_INC_
18 
19 #include <algorithm>
20 #include <functional>
21 #include <vector>
22 #include <utility>
23 
24 namespace Loki
25 {
27 // class template AssocVectorCompare
28 // Used by AssocVector
30 
31  namespace Private
32  {
33  template <class Value, class C>
34  class AssocVectorCompare : public C
35  {
36  typedef std::pair<typename C::first_argument_type, Value>
37  Data;
38  typedef typename C::first_argument_type first_argument_type;
39 
40  public:
42  {}
43 
44  AssocVectorCompare(const C& src) : C(src)
45  {}
46 
47  bool operator()(const first_argument_type& lhs,
48  const first_argument_type& rhs) const
49  { return C::operator()(lhs, rhs); }
50 
51  bool operator()(const Data& lhs, const Data& rhs) const
52  { return operator()(lhs.first, rhs.first); }
53 
54  bool operator()(const Data& lhs,
55  const first_argument_type& rhs) const
56  { return operator()(lhs.first, rhs); }
57 
58  bool operator()(const first_argument_type& lhs,
59  const Data& rhs) const
60  { return operator()(lhs, rhs.first); }
61  };
62  }
63 
65 // class template AssocVector
66 // An associative vector built as a syntactic drop-in replacement for std::map
67 // BEWARE: AssocVector doesn't respect all map's guarantees, the most important
68 // being:
69 // * iterators are invalidated by insert and erase operations
70 // * the complexity of insert/erase is O(N) not O(log N)
71 // * value_type is std::pair<K, V> not std::pair<const K, V>
72 // * iterators are random
74 
75  template
76  <
77  class K,
78  class V,
79  class C = std::less<K>,
80  class A = std::allocator< std::pair<K, V> >
81  >
83  : private std::vector< std::pair<K, V>, A >
84  , private Private::AssocVectorCompare<V, C>
85  {
86  typedef std::vector<std::pair<K, V>, A> Base;
88 
89  public:
90  typedef K key_type;
91  typedef V mapped_type;
92  typedef typename Base::value_type value_type;
93 
94  typedef C key_compare;
95  typedef A allocator_type;
96  typedef typename A::reference reference;
97  typedef typename A::const_reference const_reference;
98  typedef typename Base::iterator iterator;
99  typedef typename Base::const_iterator const_iterator;
100  typedef typename Base::size_type size_type;
101  typedef typename Base::difference_type difference_type;
102  typedef typename A::pointer pointer;
103  typedef typename A::const_pointer const_pointer;
104  typedef typename Base::reverse_iterator reverse_iterator;
105  typedef typename Base::const_reverse_iterator const_reverse_iterator;
106 
108  : public std::binary_function<value_type, value_type, bool>
109  , private key_compare
110  {
111  friend class AssocVector;
112 
113  protected:
115  {}
116 
117  public:
118  bool operator()(const value_type& lhs, const value_type& rhs) const
119  { return key_compare::operator()(lhs.first, rhs.first); }
120  };
121 
122  // 23.3.1.1 construct/copy/destroy
123 
124  explicit AssocVector(const key_compare& comp = key_compare(),
125  const A& alloc = A())
126  : Base(alloc), MyCompare(comp)
127  {}
128 
129  template <class InputIterator>
130  AssocVector(InputIterator first, InputIterator last,
131  const key_compare& comp = key_compare(),
132  const A& alloc = A())
133  : Base(first, last, alloc), MyCompare(comp)
134  {
135  MyCompare& me = *this;
136  std::sort(begin(), end(), me);
137  }
138 
140  {
141  AssocVector(rhs).swap(*this);
142  return *this;
143  }
144 
145  // iterators:
146  // The following are here because MWCW gets 'using' wrong
147  iterator begin() { return Base::begin(); }
148  const_iterator begin() const { return Base::begin(); }
149  iterator end() { return Base::end(); }
150  const_iterator end() const { return Base::end(); }
151  reverse_iterator rbegin() { return Base::rbegin(); }
152  const_reverse_iterator rbegin() const { return Base::rbegin(); }
153  reverse_iterator rend() { return Base::rend(); }
154  const_reverse_iterator rend() const { return Base::rend(); }
155 
156  // capacity:
157  bool empty() const { return Base::empty(); }
158  size_type size() const { return Base::size(); }
159  size_type max_size() { return Base::max_size(); }
160 
161  // 23.3.1.2 element access:
163  { return insert(value_type(key, mapped_type())).first->second; }
164 
165  // modifiers:
166  std::pair<iterator, bool> insert(const value_type& val)
167  {
168  bool found(true);
169  iterator i(lower_bound(val.first));
170 
171  if (i == end() || this->operator()(val.first, i->first))
172  {
173  i = Base::insert(i, val);
174  found = false;
175  }
176  return std::make_pair(i, !found);
177  }
178 
180  {
181  if (pos != end() && this->operator()(*pos, val) &&
182  (pos == end() - 1 ||
183  !this->operator()(val, pos[1]) &&
184  this->operator()(pos[1], val)))
185  {
186  return Base::insert(pos, val);
187  }
188  return insert(val).first;
189  }
190 
191  template <class InputIterator>
192  void insert(InputIterator first, InputIterator last)
193  { for (; first != last; ++first) insert(*first); }
194 
195  void erase(iterator pos)
196  { Base::erase(pos); }
197 
199  {
200  iterator i(find(k));
201  if (i == end()) return 0;
202  erase(i);
203  return 1;
204  }
205 
206  void erase(iterator first, iterator last)
207  { Base::erase(first, last); }
208 
209  void swap(AssocVector& other)
210  {
211  using std::swap;
212  Base::swap(other);
213  MyCompare& me = *this;
214  MyCompare& rhs = other;
215  swap(me, rhs);
216  }
217 
218  void clear()
219  { Base::clear(); }
220 
221  // observers:
223  { return *this; }
224 
226  {
227  const key_compare& comp = *this;
228  return value_compare(comp);
229  }
230 
231  // 23.3.1.3 map operations:
233  {
234  iterator i(lower_bound(k));
235  if (i != end() && this->operator()(k, i->first))
236  {
237  i = end();
238  }
239  return i;
240  }
241 
242  const_iterator find(const key_type& k) const
243  {
245  if (i != end() && this->operator()(k, i->first))
246  {
247  i = end();
248  }
249  return i;
250  }
251 
252  size_type count(const key_type& k) const
253  { return find(k) != end(); }
254 
256  {
257  MyCompare& me = *this;
258  return std::lower_bound(begin(), end(), k, me);
259  }
260 
262  {
263  const MyCompare& me = *this;
264  return std::lower_bound(begin(), end(), k, me);
265  }
266 
268  {
269  MyCompare& me = *this;
270  return std::upper_bound(begin(), end(), k, me);
271  }
272 
274  {
275  const MyCompare& me = *this;
276  return std::upper_bound(begin(), end(), k, me);
277  }
278 
279  std::pair<iterator, iterator> equal_range(const key_type& k)
280  {
281  MyCompare& me = *this;
282  return std::equal_range(begin(), end(), k, me);
283  }
284 
285  std::pair<const_iterator, const_iterator> equal_range(
286  const key_type& k) const
287  {
288  const MyCompare& me = *this;
289  return std::equal_range(begin(), end(), k, me);
290  }
291 
292  friend bool operator==(const AssocVector& lhs, const AssocVector& rhs)
293  {
294  const Base& me = lhs;
295  return me == rhs;
296  }
297 
298  bool operator<(const AssocVector& rhs) const
299  {
300  const Base& me = *this;
301  const Base& yo = rhs;
302  return me < yo;
303  }
304 
305  friend bool operator!=(const AssocVector& lhs, const AssocVector& rhs)
306  { return !(lhs == rhs); }
307 
308  friend bool operator>(const AssocVector& lhs, const AssocVector& rhs)
309  { return rhs < lhs; }
310 
311  friend bool operator>=(const AssocVector& lhs, const AssocVector& rhs)
312  { return !(lhs < rhs); }
313 
314  friend bool operator<=(const AssocVector& lhs, const AssocVector& rhs)
315  { return !(rhs < lhs); }
316  };
317 
318  // specialized algorithms:
319  template <class K, class V, class C, class A>
321  { lhs.swap(rhs); }
322 
323 } // namespace Loki
324 
326 // Change log:
327 // May 20, 2001: change operator= - credit due to Cristoph Koegl
328 // June 11, 2001: remove paren in equal_range - credit due to Cristoph Koegl
329 // June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!!
330 // January 22, 2002: fixed operator= - credit due to Tom Hyer
331 // June 25, 2002: fixed template insert() - credit due to Robert Minsk
332 // June 27, 2002: fixed member swap() - credit due to David Brookman
333 // February 2, 2003: fixed dependent names - credit due to Rani Sharoni
335 
336 #endif // ASSOCVECTOR_INC_
A allocator_type
Definition: loki.h:95
A::reference reference
Definition: loki.h:96
int V
Definition: test_util.cpp:29
AssocVectorCompare(const C &src)
Definition: loki.h:44
A::const_pointer const_pointer
Definition: loki.h:103
std::pair< const_iterator, const_iterator > equal_range(const key_type &k) const
Definition: loki.h:285
bool operator()(const Data &lhs, const Data &rhs) const
Definition: loki.h:51
Base::const_iterator const_iterator
Definition: loki.h:99
void erase(iterator first, iterator last)
Definition: loki.h:206
std::pair< iterator, bool > insert(const value_type &val)
Definition: loki.h:166
friend bool operator>(const AssocVector &lhs, const AssocVector &rhs)
Definition: loki.h:308
bool operator()(const first_argument_type &lhs, const Data &rhs) const
Definition: loki.h:58
size_type erase(const key_type &k)
Definition: loki.h:198
bool empty() const
Definition: loki.h:157
iterator end()
Definition: loki.h:149
const_reverse_iterator rbegin() const
Definition: loki.h:152
const_reverse_iterator rend() const
Definition: loki.h:154
size_type count(const key_type &k) const
Definition: loki.h:252
Base::const_reverse_iterator const_reverse_iterator
Definition: loki.h:105
const_iterator end() const
Definition: loki.h:150
const_iterator find(const key_type &k) const
Definition: loki.h:242
Base::value_type value_type
Definition: loki.h:92
friend bool operator==(const AssocVector &lhs, const AssocVector &rhs)
Definition: loki.h:292
Base::iterator iterator
Definition: loki.h:98
bool operator()(const first_argument_type &lhs, const first_argument_type &rhs) const
Definition: loki.h:47
A::pointer pointer
Definition: loki.h:102
std::pair< iterator, iterator > equal_range(const key_type &k)
Definition: loki.h:279
iterator insert(iterator pos, const value_type &val)
Definition: loki.h:179
Base::difference_type difference_type
Definition: loki.h:101
A::const_reference const_reference
Definition: loki.h:97
void swap(AssocVector &other)
Definition: loki.h:209
Base::reverse_iterator reverse_iterator
Definition: loki.h:104
void swap(AssocVector< K, V, C, A > &lhs, AssocVector< K, V, C, A > &rhs)
Definition: loki.h:320
value_compare(key_compare pred)
Definition: loki.h:114
bool operator()(const Data &lhs, const first_argument_type &rhs) const
Definition: loki.h:54
reverse_iterator rend()
Definition: loki.h:153
AssocVector(const key_compare &comp=key_compare(), const A &alloc=A())
Definition: loki.h:124
friend bool operator>=(const AssocVector &lhs, const AssocVector &rhs)
Definition: loki.h:311
reverse_iterator rbegin()
Definition: loki.h:151
void clear()
Definition: loki.h:218
size_type size() const
Definition: loki.h:158
value_compare value_comp() const
Definition: loki.h:225
key_compare key_comp() const
Definition: loki.h:222
AssocVector(InputIterator first, InputIterator last, const key_compare &comp=key_compare(), const A &alloc=A())
Definition: loki.h:130
DEVICEHOST void swap(Real &a, Real &b)
Definition: svd_quda.h:153
bool operator()(const value_type &lhs, const value_type &rhs) const
Definition: loki.h:118
void insert(InputIterator first, InputIterator last)
Definition: loki.h:192
friend bool operator<=(const AssocVector &lhs, const AssocVector &rhs)
Definition: loki.h:314
void erase(iterator pos)
Definition: loki.h:195
iterator begin()
Definition: loki.h:147
const_iterator lower_bound(const key_type &k) const
Definition: loki.h:261
size_type max_size()
Definition: loki.h:159
iterator lower_bound(const key_type &k)
Definition: loki.h:255
iterator find(const key_type &k)
Definition: loki.h:232
iterator upper_bound(const key_type &k)
Definition: loki.h:267
const_iterator upper_bound(const key_type &k) const
Definition: loki.h:273
AssocVector & operator=(const AssocVector &rhs)
Definition: loki.h:139
const_iterator begin() const
Definition: loki.h:148
bool operator<(const AssocVector &rhs) const
Definition: loki.h:298
Base::size_type size_type
Definition: loki.h:100
void end()
mapped_type & operator[](const key_type &key)
Definition: loki.h:162
friend bool operator!=(const AssocVector &lhs, const AssocVector &rhs)
Definition: loki.h:305