5 * Created by Victor Grishchenko on 4/1/09.
6 * Copyright 2009 Delft University of Technology. All rights reserved.
13 // make it work piece by piece
15 const uint8_t bins::SPLIT[16] =
16 {0, 3, 12, 15, 48, 51, 60, 63, 192, 195, 204, 207, 240, 243, 252, 255};
17 const uint8_t bins::JOIN[16] =
18 {0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15};
19 const int bins::NOJOIN = 0x10000;
22 void bins::extend () {
23 uint16_t nblocks = blocks_allocated ? (blocks_allocated<<1) : 1;
24 size_t had_bytes = blocks_allocated<<6;
25 size_t need_bytes = nblocks<<6;
26 cells = (uint32_t*) realloc(cells,need_bytes);
27 memset(((char*)cells)+had_bytes,0,need_bytes-had_bytes);
28 for(int b=blocks_allocated; b<nblocks; b++)
29 cells[(b<<4)|0xf] = 0x55555555; // cells are free
30 blocks_allocated = nblocks;
33 bins::bins() : height(4), blocks_allocated(0), cells(NULL),
34 ap(0), cells_allocated(0) {
36 assert(!alloc_cell());
39 bins::bins (const bins& b) : height(b.height), ap(b.ap),
40 blocks_allocated(b.blocks_allocated), cells_allocated(b.cells_allocated) {
41 size_t memsz = blocks_allocated*16*32;
42 cells = (uint32_t*) malloc(memsz);
43 memcpy(cells,b.cells,memsz);
46 void bins::dump (const char* note) {
48 for(int i=0; i<(blocks_allocated<<5); i++) {
50 printf("|%x ",halves[i]);
52 printf(">%i ",halves[i]);
54 printf("%x ",halves[i]);
61 uint32_t bins::split16to32(uint16_t halfval) {
63 for(int i=0; i<4; i++) {
65 nval |= (SPLIT[halfval&0xf])<<24;
72 int bins::join32to16(uint32_t cval) {
73 union { uint32_t i; uint8_t a[4]; } uvar;
74 uvar.i = cval & (cval>>1) & 0x55555555;
75 if ( (uvar.i|(uvar.i<<1)) != cval )
77 uvar.i = (uvar.i&0x05050505) | ((uvar.i&0x50505050U)>>3);
79 for(int i=3; i>=0; i--) {
81 res |= JOIN[uvar.a[i]];
87 void bins::split (uint32_t half) {
90 uint32_t cell = alloc_cell(), left=cell<<1, right=left+1;
91 mark(half); //cells[(half>>1)|0xf] |= 1<<(half&0x1f);
92 uint16_t halfval = halves[half];
93 uint32_t nval = split16to32(halfval);
94 halves[left] = nval&0xffff;
95 halves[right] = nval>>16;
100 bool bins::join (uint32_t half) {
101 uint32_t cellno = halves[half];
102 int left = cellno<<1, right=left+1;
103 if (deep(left) || deep(right))
105 int res = join32to16(cells[cellno]);
108 halves[half] = (uint16_t)res;
111 //cells[(half>>1)|0xf] &= ~(1<<(half&0x1f));
112 //(*childdeepcell) &= 0xffff>>1; // clean the full bit
116 void bins::free_cell (uint16_t cell) {
118 int left = cell<<1, right=left+1;
126 /** Get a free cell. */
127 uint16_t bins::alloc_cell () {
130 for(; ap<(blocks_allocated<<4); ap++) {
133 if (!cells[ap] && deep(ap<<1)) {
147 bin64_t iterator::next (bool need_solid) {
149 while (pos.is_right())
152 //if (need_solid ? !solid() : deep())
155 while (need_solid ? !solid() : deep())
161 iterator::iterator(bins* host_, bin64_t start, bool split) {
164 for(int i=0; i<64; i++)
166 pos = bin64_t(host->height,0);
167 while (!start.within(pos))
169 while (pos!=start && (deep() || split))
174 iterator::~iterator () {
175 while (half>1 && !deep())
177 // PROBLEM: may hang in the air if two iters
178 // exist simultaneously
179 // FIX: iterators are not exposed (protected)
183 void iterator::to (bool right) {
186 history[pos.layer()] = half; // FIXME
188 half = (host->halves[half]<<1) + right;
189 //host->dump("/\\ ");
193 void bins::extend_range () {
196 uint16_t newroot = alloc_cell();
197 int left = newroot<<1, right = left+1;
198 cells[newroot] = cells[0];
211 void iterator::parent () {
213 host->extend_range();
214 history[pos.layer()+1] = 0;
217 half = history[pos.layer()];
223 bin64_t bins::find (const bin64_t range, const uint8_t layer, fill_t seek) {
224 iterator i(this,range,true);
225 fill_t stop = seek==EMPTY ? FILLED : EMPTY;
227 while ( i.bin().layer()>layer && (i.deep() || *i!=stop) )
229 if (i.bin().layer()==layer && !i.deep() && *i==seek)
231 while (i.bin().is_right() && i.bin()!=range)
238 return bin64_t::NONE;
242 uint16_t bins::get (bin64_t bin) {
243 iterator i(this,bin,true);
244 //while ( i.pos!=bin &&
245 // (i.deep() || (*i!=BIN_FULL && *i!=BIN_EMPTY)) )
247 //printf("at %i ",i.half);
249 return *i; // deep cell is never 0xffff or 0x0000
253 void bins::clear () {
254 set(bin64_t(height,0),EMPTY);
258 uint64_t bins::mass () {
259 iterator i(this,bin64_t(0,0),false);
264 if (*i==bins::FILLED)
265 ret += i.pos.width();
272 void bins::set (bin64_t bin, fill_t val) {
273 assert(val==FILLED || val==EMPTY);
274 iterator i(this,bin,false);
275 while (i.bin()!=bin && (i.deep() || *i!=val))
277 if (!i.deep() && *i==val)
284 } while (i.bin().within(bin));
289 uint64_t* bins::get_stripes (int& count) {
291 uint64_t *stripes = (uint64_t*) malloc(32*8);
293 uint16_t cur = bins::EMPTY;
294 stripes[count++] = 0;
295 iterator i(this,0,false);
301 if (cur!=*i) { // new stripe
303 stripes[count++] = i.bin().base_offset();
306 stripes = (uint64_t*) realloc(stripes,size*8);
315 stripes[count++] = i.bin().base_offset();
321 void bins::remove (bins& b) {
322 uint8_t start_lr = b.height>height ? b.height : height;
323 bin64_t top(start_lr,0);
324 iterator zis(this,top), zat(&b,top);
326 while (zis.deep() || zat.deep()) {
327 zis.left(); zat.left();
332 while (zis.pos.is_right()) {
333 zis.parent(); zat.parent();
335 zis.sibling(); zat.sibling();
340 bin64_t bins::cover(bin64_t val) {
341 iterator i(this,val,false);
342 while (i.pos!=val && !i.solid())
344 //if (!i.half && !halves[0])
345 // return bin64_t::ALL;
350 bin64_t bins::find_filtered
351 (bins& filter, bin64_t range, const uint8_t layer, fill_t seek)
353 if (range==bin64_t::ALL)
354 range = bin64_t ( height>filter.height ? height : filter.height, 0 );
355 iterator i(this,range,true), j(&filter,range,true);
356 fill_t stop = seek==EMPTY ? FILLED : EMPTY;
358 while ( i.bin().layer()>layer && (i.deep() || *i!=stop || j.deep() || *j!=FILLED) )
359 i.left(), j.left(); // TODO may optimize a lot here
360 if (i.bin().layer()==layer && !i.deep() && *i==seek && *j==EMPTY)
362 while (i.bin().is_right() && i.bin()!=range)
363 i.parent(), j.parent();
366 i.parent(), j.parent();
367 i.right(), j.right();
369 return bin64_t::NONE;
372 // FIXME unite with remove(); do bitwise()
373 void bins::copy_range (bins& origin, bin64_t range) {
374 if (range==bin64_t::ALL)
375 range = bin64_t ( height>origin.height ? height : origin.height, 0 );
376 iterator zis(this,range,true), zat(&origin,range,true);
377 while (zis.pos.within(range)) {
378 while (zis.deep() || zat.deep()) {
379 zis.left(); zat.left();
384 while (zis.pos.is_right()) {
385 zis.parent(); zat.parent();
387 zis.sibling(); zat.sibling();
393 heap_ = (bin64_t*) malloc(size_*sizeof(bin64_t));
397 bool bincomp (const bin64_t& a, const bin64_t& b) {
398 register uint64_t ab = a.base_offset(), bb = b.base_offset();
400 return a.tail_bit() < b.tail_bit();
405 bool bincomp_rev (const bin64_t& a, const bin64_t& b) {
406 register uint64_t ab = a.base_offset(), bb = b.base_offset();
408 return a.tail_bit() > b.tail_bit();
413 bin64_t binheap::pop() {
415 return bin64_t::NONE;
416 bin64_t ret = heap_[0];
417 std::pop_heap(heap_, heap_+filled_--,bincomp);
418 while (filled_ && heap_[0].within(ret))
419 std::pop_heap(heap_, heap_+filled_--,bincomp);
423 void binheap::extend() {
424 std::sort(heap_,heap_+filled_,bincomp_rev);
426 for(int i=1; i<filled_; i++)
427 if (!heap_[i].within(heap_[solid]))
428 heap_[++solid] = heap_[i];
430 if (2*filled_>size_) {
432 heap_ = (bin64_t*) realloc(heap_,size_*sizeof(bin64_t));
436 void binheap::push(bin64_t val) {
439 heap_[filled_++] = val;
440 std::push_heap(heap_, heap_+filled_,bincomp);
443 binheap::~binheap() {