Changeset 361:ab0899df30d2 in lemon-0.x for src/work/klao/iter_map.h
- Timestamp:
- 04/21/04 17:46:40 (21 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@489
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/klao/iter_map.h
r347 r361 17 17 /// \todo Decide whether we need all the range checkings!!! 18 18 19 template<typename KeyIntMap, uint8_t N >19 template<typename KeyIntMap, uint8_t N, typename Val = uint8_t> 20 20 class IterableMap { 21 21 public: 22 22 23 23 typedef typename KeyIntMap::KeyType KeyType; 24 typedef uint8_tValueType;24 typedef Val ValueType; 25 25 26 26 typedef typename std::vector<KeyType>::const_iterator iterator; … … 30 30 std::vector<KeyType> data; 31 31 size_t bounds[N]; 32 Val def_val; 32 33 33 uint8_t find(size_t a) const { 34 uint8_t n=0; 35 for(; n<N && bounds[n]<=a; ++n); 36 return n; 34 Val find(size_t a) const { 35 for(uint8_t n=0; n<N; ++n) { 36 if(bounds[n] > a) 37 return n; 38 } 39 return def_val; 37 40 } 38 41 … … 67 70 public: 68 71 69 IterableMap(KeyIntMap &_base ) : base(_base) {72 IterableMap(KeyIntMap &_base, Val d = N+1) : base(_base), def_val(d) { 70 73 memset(bounds, 0, sizeof(bounds)); 71 74 // for(int i=0; i<N; ++i) { bounds[i]=0; } 72 75 } 73 76 74 uint8_toperator[](const KeyType& k) const {77 Val operator[](const KeyType& k) const { 75 78 return find(base[k]); 76 79 } 77 80 78 void set(const KeyType& k, uint8_t n) { 81 void set(const KeyType& k, Val n) { 82 // FIXME: n < N ??? 79 83 size_t a = base[k]; 80 if(a < bounds[N-1] ) {84 if(a < bounds[N-1] && n < N) { 81 85 base.set(k, move(a, find(a), n)); 82 86 } 83 87 } 84 88 85 void insert(const KeyType& k, uint8_tn) {86 if(n <N) {89 void insert(const KeyType& k, Val n) { 90 if(n < N) { 87 91 data.push_back(k); 88 92 base.set(k, move(bounds[N-1]++, N-1, n)); … … 90 94 } 91 95 92 iterator begin( uint8_tn) const {96 iterator begin(Val n) const { 93 97 if(n < N) 94 98 return data.begin() + (n ? bounds[n-1] : 0); … … 97 101 } 98 102 99 iterator end( uint8_tn) const {103 iterator end(Val n) const { 100 104 if(n < N) 101 105 return data.begin() + bounds[n]; … … 104 108 } 105 109 106 size_t size( uint8_tn) const {110 size_t size(Val n) const { 107 111 if(n < N) 108 112 return bounds[n] - (n ? bounds[n-1] : 0); … … 118 122 }; 119 123 124 125 126 127 template<typename KeyIntMap> 128 class IterableBoolMap : public IterableMap<KeyIntMap, 2, bool> { 129 typedef IterableMap<KeyIntMap, 2, bool> Parent; 130 131 public: 132 IterableBoolMap(KeyIntMap &_base, bool d = false) : Parent(_base, d) {} 133 }; 134 120 135 } 121 136 #endif
Note: See TracChangeset
for help on using the changeset viewer.