2727namespace cql {
2828 // this template class stores up to 128 values no more
2929 template <typename value_t >
30- class indexed_storage {
30+ class small_indexed_storage {
3131 private:
3232 template <typename _value_t >
33- struct entry_t {
33+ class entry_t {
3434 struct {
3535 struct {
3636 // -1 - there is no next free index
@@ -41,37 +41,87 @@ namespace cql {
4141 _value_t value;
4242 } e;
4343
44- _value_t & value () { return e.value ; }
45- int8_t next_free_index () const { return e.next_free .index ; }
44+ public:
45+ _value_t &
46+ value ()
47+ {
48+ return e.value ;
49+ }
50+
51+ const _value_t &
52+ value () const
53+ {
54+ return e.value ;
55+ }
56+
57+ int8_t
58+ next_free_index () const
59+ {
60+ return e.next_free .index ;
61+ }
62+
4663 // not counting this (so 0 means there are no free blocks behind this one)
47- int8_t next_free_cnt () const { return e.next_free .count ; }
64+ int8_t
65+ next_free_cnt () const
66+ {
67+ return e.next_free .count ;
68+ }
4869
4970 // count does not include this (so 0 means there are no free blocks behind this one)
5071 // you won't need to set cnt in any case except initial allocation
51- void set_next_free (int32_t index, int32_t cnt = 0 ) { e.next_free .index = index; e.next_free .count = cnt;}
52- void set_value (const _value_t & val) { e.value = val; }
53- bool is_allocated () { return e.next_free .index == -2 ; }
54- void set_allocated () { e.next_free .index = -2 ; }
72+ void
73+ set_next_free (int32_t index,
74+ int32_t cnt = 0 )
75+ {
76+ e.next_free .index = index;
77+ e.next_free .count = cnt;
78+ }
79+
80+ void
81+ set_value (const _value_t & val)
82+ {
83+ e.value = val;
84+ }
85+
86+ bool
87+ is_allocated ()
88+ {
89+ return e.next_free .index == -2 ;
90+ }
91+
92+ void
93+ set_allocated ()
94+ {
95+ e.next_free .index = -2 ;
96+ }
5597 };
5698
5799 typedef entry_t <value_t > array_entry_t ;
58100 array_entry_t * array;
59101 int32_t next_free_index;
60102 public:
61- explicit indexed_storage (uint32_t size) : next_free_index(0 ) {
62- if (size > 1u + static_cast <uint32_t >(INT32_MAX)) throw std::bad_alloc ();
103+ explicit
104+ small_indexed_storage (uint16_t size) :
105+ next_free_index (0 )
106+ {
63107 array = new array_entry_t [size];
64108 array[0 ].set_next_free (-1 , size-1 );
65109 }
66110
67- ~indexed_storage () { delete [] array; }
111+ ~small_indexed_storage ()
112+ {
113+ delete [] array;
114+ }
68115
69- int32_t allocate_index () {
116+ int32_t
117+ allocate ()
118+ {
70119 int32_t result;
71120 if ( (result = next_free_index) >= 0 ) {
72121 if (array[next_free_index].next_free_cnt () > 0 ) {
73122 array[++next_free_index].set_next_free (array[result].next_free_index (), array[result].next_free_cnt ()-1 );
74- } else {
123+ }
124+ else {
75125 next_free_index = array[next_free_index].next_free_index ();
76126 }
77127 // mark it allocated
@@ -81,23 +131,35 @@ namespace cql {
81131 return result;
82132 }
83133
84- // do not even thing about passing negative value here
85- // there are no checks... none.
86- void deallocate_index (int32_t index) {
87- // we are good.
134+ void
135+ release (int32_t index)
136+ {
88137 array[index].set_next_free (next_free_index);
89138 next_free_index = index;
90139 }
91140
92- bool has (int32_t index) const {
141+ bool
142+ has (int32_t index) const
143+ {
93144 return array[index].is_allocated ();
94145 }
95146
96- value_t & get (int32_t index) const {
147+ value_t &
148+ get (int32_t index)
149+ {
150+ return array[index].value ();
151+ }
152+
153+ const value_t &
154+ get (int32_t index) const
155+ {
97156 return array[index].value ();
98157 }
99158
100- void put (int32_t index, const value_t & val) {
159+ void
160+ put (int32_t index,
161+ const value_t & val)
162+ {
101163 array[index].set_value (val);
102164 }
103165
0 commit comments