KaliVeda
Toolkit for HIC analysis
KVRefVec.h
1 #pragma once
2 
21 #include <vector>
22 
23 template<typename T>
24 class KVRefVec {
25  std::vector<T*> objects;
26 public:
27  KVRefVec(size_t N) : objects(N) {}
28  const T& operator[](size_t i) const
29  {
30  return *objects[i];
31  }
32  void set(size_t i, T* t)
33  {
34  objects[i] = t;
35  }
36  size_t size() const
37  {
38  return objects.size();
39  }
40 
41  struct iterator {
42  typedef std::forward_iterator_tag iterator_category;
43  typedef T value_type;
44  typedef std::ptrdiff_t difference_type;
45  typedef T* pointer;
46  typedef T& reference;
47 
48  typename std::vector<T*>::iterator vec_it;
49 
50  iterator() {}
51  iterator(const iterator& i) : vec_it{i.vec_it} {}
53  {
54  vec_it = refvec->objects.begin();
55  }
56  T& operator*() const
57  {
58  return *(*vec_it);
59  }
60  bool operator!=(const iterator& it) const
61  {
62  return vec_it != it.vec_it;
63  }
64  bool operator==(const iterator& it) const
65  {
66  return vec_it == it.vec_it;
67  }
69  {
70  ++vec_it;
71  return *this;
72  }
74  {
75  iterator tmp(*this);
76  operator++();
77  return tmp;
78  }
80  {
81  if (this != &rhs) {
82  vec_it = rhs.vec_it;
83  }
84  return *this;
85  }
87  {
88  iterator end_it;
89  end_it.vec_it = refvec->objects.end();
90  return end_it;
91  }
92  };
93 
95  {
96  return iterator(this);
97  }
99  {
100  return iterator::make_end_iterator(this);
101  }
102 };
#define N
Vector of references to objects.
Definition: KVRefVec.h:24
KVRefVec(size_t N)
Definition: KVRefVec.h:27
size_t size() const
Definition: KVRefVec.h:36
std::vector< T * > objects
Definition: KVRefVec.h:25
void set(size_t i, T *t)
Definition: KVRefVec.h:32
const T & operator[](size_t i) const
Definition: KVRefVec.h:28
iterator end()
Definition: KVRefVec.h:98
iterator begin()
Definition: KVRefVec.h:94
double T(double x)
iterator(KVRefVec< T > *refvec)
Definition: KVRefVec.h:52
iterator & operator=(const iterator &rhs)
Definition: KVRefVec.h:79
std::forward_iterator_tag iterator_category
Definition: KVRefVec.h:42
static iterator make_end_iterator(KVRefVec< T > *refvec)
Definition: KVRefVec.h:86
bool operator==(const iterator &it) const
Definition: KVRefVec.h:64
const iterator & operator++()
Definition: KVRefVec.h:68
T & operator*() const
Definition: KVRefVec.h:56
iterator(const iterator &i)
Definition: KVRefVec.h:51
iterator operator++(int)
Definition: KVRefVec.h:73
std::vector< T * >::iterator vec_it
Definition: KVRefVec.h:48
std::ptrdiff_t difference_type
Definition: KVRefVec.h:44
bool operator!=(const iterator &it) const
Definition: KVRefVec.h:60