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