00001
00002
00010 #ifndef CXXUTILS_PACKEDARRAY_H
00011 #define CXXUTILS_PACKEDARRAY_H
00012
00013
00014 #include <vector>
00015 #include <cstddef>
00016
00017
00018 namespace CxxUtils {
00019
00020
00036 class PackedArray
00037 {
00038 private:
00039
00040 typedef std::vector<unsigned int> basetype;
00041
00042 public:
00043
00044 typedef size_t size_type;
00045 typedef unsigned int value_type;
00046 typedef basetype::allocator_type allocator_type;
00047
00052 class proxy
00053 {
00054 public:
00056 proxy (PackedArray& arr, size_type n) : m_arr (arr), m_n (n) {}
00057
00059 operator value_type() const { return m_arr.get (m_n); }
00060
00062 proxy& operator= (value_type v) { m_arr.set (m_n, v); return *this; }
00063
00064 private:
00066 PackedArray& m_arr;
00067
00069 size_type m_n;
00070 };
00071
00072
00080 PackedArray (int bitsize,
00081 const allocator_type& allocator = allocator_type());
00082
00092 PackedArray (int bitsize,
00093 size_type n,
00094 value_type val = 0,
00095 const allocator_type& allocator = allocator_type());
00096
00102 void assign (size_type n, value_type u);
00103
00107 allocator_type get_allocator() const;
00108
00112 size_type size() const;
00113
00117 size_type max_size() const;
00118
00123 size_type capacity() const;
00124
00130 void resize (size_type sz, value_type c = 0);
00131
00135 bool empty() const;
00136
00142 void reserve (size_type n);
00143
00148 value_type get(size_type n) const;
00149
00155 void set(size_type n, value_type val);
00156
00165 value_type operator[](size_type n) const;
00166
00175 proxy operator[](size_type n);
00176
00185 value_type at(size_type n) const;
00186
00195 proxy at(size_type n);
00196
00204 value_type front() const;
00205
00213 value_type back() const;
00214
00222 proxy front();
00223
00231 proxy back();
00232
00237 void push_back (value_type x);
00238
00242 void pop_back();
00243
00248 void swap (PackedArray& other);
00249
00253 void clear();
00254
00261 void set_bitsize (int bitsize);
00262
00266 int bitsize () const;
00267
00268
00269 private:
00271 int m_bitsize;
00272
00274 size_type m_size;
00275
00277 value_type m_mask;
00278
00280 basetype m_vec;
00281
00284 size_t nbase (size_type n) const;
00285
00287 size_t tondx (size_type n) const;
00288
00290 int tooff (size_type n) const;
00291
00293 value_type doget (size_type ndx, int off) const;
00294
00296 void doset (size_type ndx, int off, value_type v);
00297
00299 void range_check (size_type n) const;
00300 };
00301
00302
00303 }
00304
00305
00306 #endif // not CXXUTILS_PACKEDARRAY_H