ROSE  0.9.9.109
SgSharedVector.h
1 #ifndef ROSE_SgSharedVector_H
2 #define ROSE_SgSharedVector_H
3 
4 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
5 #include <boost/serialization/access.hpp>
6 #include <boost/serialization/array.hpp>
7 #include <boost/serialization/base_object.hpp>
8 #include <boost/serialization/nvp.hpp>
9 #include <boost/serialization/split_member.hpp>
10 #endif
11 
12 /* An SgSharedVector is like an STL vector except for two things:
13  *
14  * 1. The underlying storage is managed external to this class
15  * 2. Copying (copy ctor, assignment) an SgSharedVector results in two vectors pointing to the same underlying storage.
16  *
17  * This class is meant to support memory mapped files (see mmap(2)) in such a way so as not to cause the entire file contents
18  * to be accessed when a vector is created or copied. If the mapped memory supplied to the constructor (the "pool") is
19  * read-only then modifying the vector will fail with a segmentation fault. */
20 template <typename _Tp>
22 public:
23  typedef _Tp value_type;
24  typedef _Tp* pointer;
25  typedef const _Tp* const_pointer;
26  typedef _Tp& reference;
27  typedef const _Tp& const_reference;
28  typedef size_t size_type;
29 
30 private:
31  pointer p_pool; // backing store managed externally to this class
32  size_type p_capacity; // number of bytes in pool
33  size_type p_size; // current size of vector (never greater than capacity)
34 
35 #ifdef ROSE_HAVE_BOOST_SERIALIZATION_LIB
36 private:
37  friend class boost::serialization::access;
38 
39  template<class S>
40  void save(S &s, const unsigned version) const {
41  s & BOOST_SERIALIZATION_NVP(p_size);
42  s & boost::serialization::make_nvp("p_pool", boost::serialization::make_array(p_pool, p_size));
43  }
44 
45  template<class S>
46  void load(S &s, const unsigned version) {
47  s & BOOST_SERIALIZATION_NVP(p_size);
48  p_capacity = p_size;
49  p_pool = new value_type[p_capacity];
50  s & boost::serialization::make_nvp("p_pool", boost::serialization::make_array(p_pool, p_size));
51  }
52 
53  BOOST_SERIALIZATION_SPLIT_MEMBER();
54 #endif
55 
56 public:
57  /**** Iterators ****/
58  class iterator {
59  public:
60  iterator operator++() { return (this->p)++; }
61  bool operator!=( const iterator & x ) const { return (this->p != x->p); }
62  value_type & operator*() const { return *(this->p); }
63  iterator(pointer x) : p(x) {}
64  private:
65  value_type *p;
66  };
67 
69  public:
70  const_iterator operator++() { return (this->p)++; }
71  bool operator!=( const const_iterator & x ) const { return (this->p != x.p); }
72  const value_type & operator*() const { return *(this->p); }
73  const_iterator(pointer x) : p(x) {}
74  private:
75  value_type *p;
76  };
77 
78  /**** Construct/Copy/Destroy ****/
79 
80  // constructs a vector having no data
81  explicit SgSharedVector()
82  : p_pool(0), p_capacity(0), p_size(0) {}
83  // constructs a non-extendible vector of particular size
84  explicit SgSharedVector(pointer pool, size_type n)
85  : p_pool(pool), p_capacity(n), p_size(n) {}
86  // constructs an extendible vector of specified capacity and size
87  explicit SgSharedVector(pointer pool, size_type n, size_type nres)
88  : p_pool(pool), p_capacity(nres), p_size(0) {
89  resize(n);
90  }
91  // constructs a new vector pointing to same data as initializer vector
93  : p_pool(x.pool()), p_capacity(x.capacity()), p_size(x.size()) {}
94  // new vector points to offset in initializer vector
95  SgSharedVector(const SgSharedVector &x, size_type offset) {
96 
97  // DQ (11/3/2011): Avoid warning about pointless comparison of unsigned integer with zero.
98  // assert(offset>=0 && offset<=x.size());
99  assert(offset <= x.size());
100 
101  p_pool = x.pool() + offset;
102  p_capacity = x.capacity() - offset;
103  p_size = x.size() - offset;
104  }
105  // new vector points to subset of initializer vector
106  SgSharedVector(const SgSharedVector &x, size_type offset, size_type size) {
107  assert(offset+size <= x.size());
108  p_pool = x.pool() + offset;
109  p_capacity = x.capacity() - offset;
110  p_size = size;
111  }
112  // pool memory is managed externally
113  ~SgSharedVector() {}
114 
115  /**** Assignment ****/
116  SgSharedVector& operator=(const SgSharedVector &x) {
117  p_pool = x.pool();
118  p_capacity = x.capacity();
119  p_size = x.size();
120  return *this;
121  }
122 
123  /**** Erasure ****/
124  void clear() {
125  p_pool = NULL;
126  p_size = 0;
127  p_capacity = 0;
128  }
129  void erase(iterator position) {
130  abort(); //FIXME
131  }
132  void erase(iterator start, iterator finish) {
133  abort(); //FIXME
134  }
135  void pop_back() {
136  assert(p_size>0);
137  --p_size;
138  }
139 
140  /**** Insertion ****/
141  void insert(iterator position, const_reference x) {
142  abort(); //FIXME
143  }
144  void insert(iterator position, size_type n, const_reference x) {
145  abort(); //FIXME
146  }
147  void insert(iterator position, iterator start, iterator finish) {
148  abort(); //FIXME
149  }
150  void push_back(const_reference x) {
151  assert(p_size<p_capacity);
152  p_pool[p_size++] = x;
153  }
154 
155  /**** Iterators ****/
156  iterator begin() { return iterator(p_pool); };
157  iterator end() { return iterator(p_pool+p_size); };
158 
159  const_iterator begin() const { return const_iterator(p_pool); };
160  const_iterator end() const { return const_iterator(p_pool+p_size); };
161 
162  /**** Element Reference ****/
163  reference operator[](size_type n) {
164  return at(n);
165  }
166  const_reference operator[](size_type n) const {
167  return at(n);
168  }
169  reference at(size_type n) {
170  // DQ (11/3/2011): Avoid warning about pointless comparison of unsigned integer with zero.
171  // assert(n>=0 && n<p_size);
172  assert(n < p_size);
173 
174  return p_pool[n];
175  }
176  const_reference at(size_type n) const {
177  // DQ (11/3/2011): Avoid warning about pointless comparison of unsigned integer with zero.
178  assert(n < p_size);
179  return p_pool[n];
180  }
181  reference back() {
182  assert(p_size>0);
183  return p_pool[p_size-1];
184  }
185  const_reference back() const {
186  assert(p_size>0);
187  return p_pool[p_size-1];
188  }
189  reference front() {
190  assert(p_size>0);
191  return p_pool[0];
192  }
193  const_reference front() const {
194  assert(p_size>0);
195  return p_pool[0];
196  }
197  pointer pool() const {
198  return p_pool;
199  }
200 
201  /**** Size ****/
202  size_type capacity() const {
203  return p_capacity;
204  }
205  bool empty() const {
206  return 0==p_size;
207  }
208  size_type max_size() const {
209  return (size_t)-1;
210  }
211  void reserve(size_type n) {
212  assert(n<=p_capacity); //not possible to increase capacity
213  }
214  void resize(size_type n) {
215  if (n<p_size) {
216  p_size = n;
217  } else {
218  assert(n<=p_capacity);
219  p_size = n;
220  }
221  }
222  size_type size() const {
223  return p_size;
224  }
225 
226  /**** Not implemented ****/
227  // allocator_type get_allocator() const;
228  // void swap(SgSharedVector&)
229 };
230 
231 template<class T>bool operator==(const SgSharedVector<T> &x, const SgSharedVector<T> &y);
232 template<class T>bool operator!=(const SgSharedVector<T> &x, const SgSharedVector<T> &y);
233 template<class T>void swap(SgSharedVector<T> &x, SgSharedVector<T> &y);
234 
235 #endif