ROSE 0.11.145.147
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. */
23template <typename _Tp>
25public:
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
33private:
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
39private:
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
59public:
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
87 // constructs a non-extendible vector of particular size
88 explicit SgSharedVector(pointer pool, size_type n)
89 : p_pool(pool), p_capacity(n), p_size(n) {}
90
91 // constructs an extendible vector of specified capacity and size
92 explicit SgSharedVector(pointer pool, size_type n, size_type nres)
93 : p_pool(pool), p_capacity(nres), p_size(0) {
94 resize(n);
95 }
96
97 // constructs a new vector pointing to same data as initializer vector
99 : p_pool(x.pool()), p_capacity(x.capacity()), p_size(x.size()) {
100 }
101
102 // new vector points to offset in initializer vector
103 SgSharedVector(const SgSharedVector &x, size_type offset) {
104 ASSERT_require(offset <= x.size());
105 p_pool = x.pool() + offset;
106 p_capacity = x.capacity() - offset;
107 p_size = x.size() - offset;
108 }
109
110 // new vector points to subset of initializer vector
111 SgSharedVector(const SgSharedVector &x, size_type offset, size_type size) {
112 assert(offset+size <= x.size());
113 p_pool = x.pool() + offset;
114 p_capacity = x.capacity() - offset;
115 p_size = size;
116 }
117
118 // pool memory is managed externally
119 ~SgSharedVector() {}
120
121 /**** Assignment ****/
122 SgSharedVector& operator=(const SgSharedVector &x) {
123 p_pool = x.pool();
124 p_capacity = x.capacity();
125 p_size = x.size();
126 return *this;
127 }
128
129 /**** Erasure ****/
130 void clear() {
131 p_pool = NULL;
132 p_size = 0;
133 p_capacity = 0;
134 }
135 void erase(iterator /*position*/) {
136 abort(); //FIXME
137 }
138 void erase(iterator /*start*/, iterator /*finish*/) {
139 abort(); //FIXME
140 }
141 void pop_back() {
142 assert(p_size>0);
143 --p_size;
144 }
145
146 /**** Insertion ****/
147 void insert(iterator /*position*/, const_reference /*x*/) {
148 abort(); //FIXME
149 }
150 void insert(iterator /*position*/, size_type /*n*/, const_reference /*x*/) {
151 abort(); //FIXME
152 }
153 void insert(iterator /*position*/, iterator /*start*/, iterator /*finish*/) {
154 abort(); //FIXME
155 }
156 void push_back(const_reference x) {
157 assert(p_size<p_capacity);
158 p_pool[p_size++] = x;
159 }
160
161 /**** Iterators ****/
162 iterator begin() { return iterator(p_pool); };
163 iterator end() { return iterator(p_pool+p_size); };
164
165 const_iterator begin() const { return const_iterator(p_pool); };
166 const_iterator end() const { return const_iterator(p_pool+p_size); };
167
168 /**** Element Reference ****/
169 reference operator[](size_type n) {
170 return at(n);
171 }
172 const_reference operator[](size_type n) const {
173 return at(n);
174 }
175 reference at(size_type n) {
176 // DQ (11/3/2011): Avoid warning about pointless comparison of unsigned integer with zero.
177 // assert(n>=0 && n<p_size);
178 assert(n < p_size);
179
180 return p_pool[n];
181 }
182 const_reference at(size_type n) const {
183 // DQ (11/3/2011): Avoid warning about pointless comparison of unsigned integer with zero.
184 assert(n < p_size);
185 return p_pool[n];
186 }
187 reference back() {
188 assert(p_size>0);
189 return p_pool[p_size-1];
190 }
191 const_reference back() const {
192 assert(p_size>0);
193 return p_pool[p_size-1];
194 }
195 reference front() {
196 assert(p_size>0);
197 return p_pool[0];
198 }
199 const_reference front() const {
200 assert(p_size>0);
201 return p_pool[0];
202 }
203 pointer pool() const {
204 return p_pool;
205 }
206
207 /**** Size ****/
208 size_type capacity() const {
209 return p_capacity;
210 }
211 bool empty() const {
212 return 0==p_size;
213 }
214 size_type max_size() const {
215 return (size_t)-1;
216 }
217 void reserve(size_type n) {
218 assert(n<=p_capacity); //not possible to increase capacity
219 }
220 void resize(size_type n) {
221 if (n<p_size) {
222 p_size = n;
223 } else {
224 assert(n<=p_capacity);
225 p_size = n;
226 }
227 }
228 size_type size() const {
229 return p_size;
230 }
231
232 /**** Not implemented ****/
233 // allocator_type get_allocator() const;
234 // void swap(SgSharedVector&)
235};
236
237template<class T>bool operator==(const SgSharedVector<T> &x, const SgSharedVector<T> &y);
238template<class T>bool operator!=(const SgSharedVector<T> &x, const SgSharedVector<T> &y);
239template<class T>void swap(SgSharedVector<T> &x, SgSharedVector<T> &y);
240
241#endif
242#endif