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), twist_mask(0) {
36 assert(!alloc_cell());
39 void bins::twist (uint64_t mask) {
40 assert( (1<<height) > mask );
44 bins::bins (const bins& b) : height(b.height), ap(b.ap),
45 blocks_allocated(b.blocks_allocated), cells_allocated(b.cells_allocated) {
46 size_t memsz = blocks_allocated*16*32;
47 cells = (uint32_t*) malloc(memsz);
48 memcpy(cells,b.cells,memsz);
51 void bins::dump (const char* note) {
53 for(int i=0; i<(blocks_allocated<<5); i++) {
55 printf("|%x ",halves[i]);
57 printf(">%i ",halves[i]);
59 printf("%x ",halves[i]);
66 uint32_t bins::split16to32(uint16_t halfval) {
68 for(int i=0; i<4; i++) {
70 nval |= (SPLIT[halfval&0xf])<<24;
77 int bins::join32to16(uint32_t cval) {
78 union { uint32_t i; uint8_t a[4]; } uvar;
79 uvar.i = cval & (cval>>1) & 0x55555555;
80 if ( (uvar.i|(uvar.i<<1)) != cval )
82 uvar.i = (uvar.i&0x05050505) | ((uvar.i&0x50505050U)>>3);
84 for(int i=3; i>=0; i--) {
86 res |= JOIN[uvar.a[i]];
92 void bins::split (uint32_t half) {
95 uint32_t cell = alloc_cell(), left=cell<<1, right=left+1;
96 mark(half); //cells[(half>>1)|0xf] |= 1<<(half&0x1f);
97 uint16_t halfval = halves[half];
98 uint32_t nval = split16to32(halfval);
99 halves[left] = nval&0xffff;
100 halves[right] = nval>>16;
105 bool bins::join (uint32_t half) {
106 uint32_t cellno = halves[half];
107 int left = cellno<<1, right=left+1;
108 if (deep(left) || deep(right))
110 int res = join32to16(cells[cellno]);
113 halves[half] = (uint16_t)res;
116 //cells[(half>>1)|0xf] &= ~(1<<(half&0x1f));
117 //(*childdeepcell) &= 0xffff>>1; // clean the full bit
121 void bins::free_cell (uint16_t cell) {
123 int left = cell<<1, right=left+1;
131 /** Get a free cell. */
132 uint16_t bins::alloc_cell () {
135 for(; ap<(blocks_allocated<<4); ap++) {
138 if (!cells[ap] && deep(ap<<1)) {
152 bin64_t iterator::next (bool need_solid) {
154 while (pos.is_right())
157 //if (need_solid ? !solid() : deep())
160 while (need_solid ? !solid() : deep())
166 iterator::iterator(bins* host_, bin64_t start, bool split) {
169 for(int i=0; i<64; i++)
171 pos = bin64_t(host->height,0);
172 while (!start.within(pos))
174 while (pos!=start && (deep() || split))
179 iterator::~iterator () {
180 while (half>1 && !deep())
182 // PROBLEM: may hang in the air if two iters
183 // exist simultaneously
184 // FIX: iterators are not exposed (protected)
188 void iterator::to (bool right) {
191 history[pos.layer()] = half; // FIXME
193 if ( (host->twist_mask >> pos.layer()) & 1 )
194 right = !right; // twist it!
195 half = (host->halves[half]<<1) + right;
199 void bins::extend_range () {
202 uint16_t newroot = alloc_cell();
203 int left = newroot<<1, right = left+1;
204 cells[newroot] = cells[0];
217 void iterator::parent () {
219 host->extend_range();
220 history[pos.layer()+1] = 0;
223 half = history[pos.layer()];
229 bin64_t bins::find (const bin64_t range, const uint8_t layer, fill_t seek) {
230 iterator i(this,range,true);
231 fill_t stop = seek==EMPTY ? FILLED : EMPTY;
233 while ( i.bin().layer()>layer && (i.deep() || *i!=stop) )
235 if (i.bin().layer()==layer && !i.deep() && *i==seek)
237 while (i.bin().is_right() && i.bin()!=range)
244 return bin64_t::NONE;
248 uint16_t bins::get (bin64_t bin) {
249 iterator i(this,bin,true);
250 //while ( i.pos!=bin &&
251 // (i.deep() || (*i!=BIN_FULL && *i!=BIN_EMPTY)) )
253 //printf("at %i ",i.half);
255 return *i; // deep cell is never 0xffff or 0x0000
259 void bins::clear () {
260 set(bin64_t(height,0),EMPTY);
264 uint64_t bins::mass () {
265 iterator i(this,bin64_t(0,0),false);
270 if (*i==bins::FILLED)
271 ret += i.pos.width();
278 void bins::set (bin64_t bin, fill_t val) {
279 assert(val==FILLED || val==EMPTY);
280 iterator i(this,bin,false);
281 while (i.bin()!=bin && (i.deep() || *i!=val))
283 if (!i.deep() && *i==val)
290 } while (i.bin().within(bin));
295 uint64_t* bins::get_stripes (int& count) {
297 uint64_t *stripes = (uint64_t*) malloc(32*8);
299 uint16_t cur = bins::EMPTY;
300 stripes[count++] = 0;
301 iterator i(this,0,false);
307 if (cur!=*i) { // new stripe
309 stripes[count++] = i.bin().base_offset();
312 stripes = (uint64_t*) realloc(stripes,size*8);
321 stripes[count++] = i.bin().base_offset();
327 void bins::remove (bins& b) {
328 uint8_t start_lr = b.height>height ? b.height : height;
329 bin64_t top(start_lr,0);
330 iterator zis(this,top), zat(&b,top);
332 while (zis.deep() || zat.deep()) {
333 zis.left(); zat.left();
338 while (zis.pos.is_right()) {
339 zis.parent(); zat.parent();
341 zis.sibling(); zat.sibling();
346 bin64_t bins::cover(bin64_t val) {
347 iterator i(this,val,false);
348 while (i.pos!=val && !i.solid())
350 //if (!i.half && !halves[0])
351 // return bin64_t::ALL;
356 bin64_t bins::find_filtered
357 (bins& filter, bin64_t range, const uint8_t layer, fill_t seek)
359 if (range==bin64_t::ALL)
360 range = bin64_t ( height>filter.height ? height : filter.height, 0 );
361 iterator i(this,range,true), j(&filter,range,true);
362 fill_t stop = seek==EMPTY ? FILLED : EMPTY;
364 while ( i.bin().layer()>layer && (i.deep() || *i!=stop || j.deep() || *j!=FILLED) )
365 i.left(), j.left(); // TODO may optimize a lot here
366 if (i.bin().layer()==layer && !i.deep() && *i==seek && *j==EMPTY)
368 while (i.bin().is_right() && i.bin()!=range)
369 i.parent(), j.parent();
372 i.parent(), j.parent();
373 i.right(), j.right();
375 return bin64_t::NONE;
378 // FIXME unite with remove(); do bitwise()
379 void bins::copy_range (bins& origin, bin64_t range) {
380 if (range==bin64_t::ALL)
381 range = bin64_t ( height>origin.height ? height : origin.height, 0 );
382 iterator zis(this,range,true), zat(&origin,range,true);
383 while (zis.pos.within(range)) {
384 while (zis.deep() || zat.deep()) {
385 zis.left(); zat.left();
390 while (zis.pos.is_right()) {
391 zis.parent(); zat.parent();
393 zis.sibling(); zat.sibling();
399 heap_ = (bin64_t*) malloc(size_*sizeof(bin64_t));
403 bool bincomp (const bin64_t& a, const bin64_t& b) {
404 register uint64_t ab = a.base_offset(), bb = b.base_offset();
406 return a.tail_bit() < b.tail_bit();
411 bool bincomp_rev (const bin64_t& a, const bin64_t& b) {
412 register uint64_t ab = a.base_offset(), bb = b.base_offset();
414 return a.tail_bit() > b.tail_bit();
419 bin64_t binheap::pop() {
421 return bin64_t::NONE;
422 bin64_t ret = heap_[0];
423 std::pop_heap(heap_, heap_+filled_--,bincomp);
424 while (filled_ && heap_[0].within(ret))
425 std::pop_heap(heap_, heap_+filled_--,bincomp);
429 void binheap::extend() {
430 std::sort(heap_,heap_+filled_,bincomp_rev);
432 for(int i=1; i<filled_; i++)
433 if (!heap_[i].within(heap_[solid]))
434 heap_[++solid] = heap_[i];
436 if (2*filled_>size_) {
438 heap_ = (bin64_t*) realloc(heap_,size_*sizeof(bin64_t));
442 void binheap::push(bin64_t val) {
445 heap_[filled_++] = val;
446 std::push_heap(heap_, heap_+filled_,bincomp);
449 binheap::~binheap() {