00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _OASYS_SPARSE_BITMAP_H_
00018 #define _OASYS_SPARSE_BITMAP_H_
00019
00020 #include <vector>
00021 #include "config.h"
00022
00023 #include "../debug/DebugUtils.h"
00024 #include "../debug/Formatter.h"
00025 #include "../serialize/Serialize.h"
00026 #include "../serialize/SerializableVector.h"
00027 #include "../util/StringAppender.h"
00028
00029 namespace oasys {
00030
00043 template <typename _inttype_t>
00044 class SparseBitmap : public Formatter,
00045 public SerializableObject {
00046 public:
00050 SparseBitmap();
00051
00055 SparseBitmap(const Builder&);
00056
00060 void set(_inttype_t start, _inttype_t len = 1);
00061
00065 void clear(_inttype_t start, _inttype_t len = 1);
00066
00070 bool is_set(_inttype_t start, _inttype_t len = 1);
00071
00075 bool operator[](_inttype_t val)
00076 {
00077 return is_set(val, 1);
00078 }
00079
00083 _inttype_t num_set();
00084
00088 bool empty() { return bitmap_.empty(); }
00089
00093 size_t num_entries() { return bitmap_.size(); }
00094
00098 void clear() { bitmap_.clear(); }
00099
00104 _inttype_t num_contiguous();
00105
00109 int format(char* bp, size_t len) const;
00110
00114 void serialize(SerializeAction* a);
00115
00116 protected:
00117 class Range : public SerializableObject {
00118 public:
00120 Range(_inttype_t start, _inttype_t end)
00121 : start_(start), end_(end) {}
00122
00124 Range(const Builder&)
00125 : start_(0), end_(0) {}
00126
00128 void serialize(SerializeAction* a);
00129
00130 _inttype_t start_;
00131 _inttype_t end_;
00132 };
00133
00134 typedef SerializableVector<Range> RangeVector;
00135 RangeVector bitmap_;
00136
00137 void validate();
00138
00139 public:
00150 class iterator {
00151 public:
00155 iterator();
00156
00160 _inttype_t operator*();
00161
00165 iterator& operator++();
00166
00170 iterator operator++(int);
00171
00175 iterator operator+(unsigned int diff);
00176
00180 iterator& operator+=(unsigned int diff);
00181
00185 iterator& operator--();
00186
00190 iterator operator--(int);
00191
00195 iterator operator-(unsigned int diff);
00196
00200 iterator& operator-=(unsigned int diff);
00201
00205 bool operator==(const iterator& other);
00206
00210 bool operator!=(const iterator& other);
00211
00217 iterator& skip_contiguous();
00218
00219 private:
00220 friend class SparseBitmap<_inttype_t>;
00221
00223 iterator(typename RangeVector::iterator iter,
00224 _inttype_t offset);
00225
00227 typename RangeVector::iterator iter_;
00228
00230 _inttype_t offset_;
00231 };
00232
00236 iterator begin();
00237
00241 iterator end();
00242
00246 _inttype_t first() { return *begin(); }
00247
00251 _inttype_t last() { return *--end(); }
00252 };
00253
00254 #include "SparseBitmap.tcc"
00255
00256 }
00257
00258 #endif