5 * Created by Victor Grishchenko on 4/1/09.
6 * Copyright 2009 Delft University of Technology. All rights reserved.
12 // make it work piece by piece
14 const uint8_t bins::SPLIT[16] =
15 {0, 3, 12, 15, 48, 51, 60, 63, 192, 195, 204, 207, 240, 243, 252, 255};
16 const uint8_t bins::JOIN[16] =
17 {0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15};
18 const int bins::NOJOIN = 0x10000;
21 void bins::extend () {
22 uint16_t nblocks = blocks_allocated ? (blocks_allocated<<1) : 1;
23 size_t had_bytes = blocks_allocated<<6;
24 size_t need_bytes = nblocks<<6;
25 cells = (uint32_t*) realloc(cells,need_bytes);
26 memset(((char*)cells)+had_bytes,0,need_bytes-had_bytes);
27 for(int b=blocks_allocated; b<nblocks; b++)
28 cells[(b<<4)|0xf] = 0x55555555; // cells are free
29 blocks_allocated = nblocks;
32 bins::bins() : height(4), blocks_allocated(0), cells(NULL),
33 ap(0), cells_allocated(0) {
35 assert(!alloc_cell());
38 bins::bins (const bins& b) : height(b.height), ap(b.ap),
39 blocks_allocated(b.blocks_allocated), cells_allocated(b.cells_allocated) {
40 size_t memsz = blocks_allocated*16*32;
41 cells = (uint32_t*) malloc(memsz);
42 memcpy(cells,b.cells,memsz);
45 void bins::dump (const char* note) {
47 for(int i=0; i<(blocks_allocated<<5); i++) {
49 printf("|%x ",halves[i]);
51 printf(">%i ",halves[i]);
53 printf("%x ",halves[i]);
60 uint32_t bins::split16to32(uint16_t halfval) {
62 for(int i=0; i<4; i++) {
64 nval |= (SPLIT[halfval&0xf])<<24;
71 int bins::join32to16(uint32_t cval) {
72 union { uint32_t i; uint8_t a[4]; } uvar;
73 uvar.i = cval & (cval>>1) & 0x55555555;
74 if ( (uvar.i|(uvar.i<<1)) != cval )
76 uvar.i = (uvar.i&0x05050505) | ((uvar.i&0x50505050U)>>3);
78 for(int i=3; i>=0; i--) {
80 res |= JOIN[uvar.a[i]];
86 void bins::split (uint32_t half) {
89 uint32_t cell = alloc_cell(), left=cell<<1, right=left+1;
90 mark(half); //cells[(half>>1)|0xf] |= 1<<(half&0x1f);
91 uint16_t halfval = halves[half];
92 uint32_t nval = split16to32(halfval);
93 halves[left] = nval&0xffff;
94 halves[right] = nval>>16;
99 bool bins::join (uint32_t half) {
100 uint32_t cellno = halves[half];
101 int left = cellno<<1, right=left+1;
102 if (deep(left) || deep(right))
104 int res = join32to16(cells[cellno]);
107 halves[half] = (uint16_t)res;
110 //cells[(half>>1)|0xf] &= ~(1<<(half&0x1f));
111 //(*childdeepcell) &= 0xffff>>1; // clean the full bit
115 void bins::free_cell (uint16_t cell) {
117 int left = cell<<1, right=left+1;
125 /** Get a free cell. */
126 uint16_t bins::alloc_cell () {
129 for(; ap<(blocks_allocated<<4); ap++) {
132 if (!cells[ap] && deep(ap<<1)) {
146 bin64_t iterator::next (bool need_solid) {
148 while (pos.is_right())
151 //if (need_solid ? !solid() : deep())
154 while (need_solid ? !solid() : deep())
160 iterator::iterator(bins* host_, bin64_t start, bool split) {
163 for(int i=0; i<64; i++)
165 pos = bin64_t(host->height,0);
166 while (!start.within(pos))
168 while (pos!=start && (deep() || split))
173 iterator::~iterator () {
174 while (half>1 && !deep())
176 // PROBLEM: may hang in the air if two iters
177 // exist simultaneously
178 // FIX: iterators are not exposed (protected)
182 void iterator::to (bool right) {
185 history[pos.layer()] = half; // FIXME
187 half = (host->halves[half]<<1) + right;
188 //host->dump("/\\ ");
192 void bins::extend_range () {
195 uint16_t newroot = alloc_cell();
196 int left = newroot<<1, right = left+1;
197 cells[newroot] = cells[0];
210 void iterator::parent () {
212 host->extend_range();
213 history[pos.layer()+1] = 0;
216 half = history[pos.layer()];
222 bin64_t bins::find (const bin64_t range, const uint8_t layer, fill_t seek) {
223 iterator i(this,range,true);
224 fill_t stop = seek==EMPTY ? FILLED : EMPTY;
226 while ( i.bin().layer()>layer && (i.deep() || *i!=stop) )
228 if (i.bin().layer()==layer && !i.deep() && *i==seek)
230 while (i.bin().is_right() && i.bin()!=range)
237 return bin64_t::NONE;
241 uint16_t bins::get (bin64_t bin) {
242 iterator i(this,bin,true);
243 //while ( i.pos!=bin &&
244 // (i.deep() || (*i!=BIN_FULL && *i!=BIN_EMPTY)) )
246 //printf("at %i ",i.half);
248 return *i; // deep cell is never 0xffff or 0x0000
252 void bins::set (bin64_t bin, fill_t val) {
253 assert(val==FILLED || val==EMPTY);
254 iterator i(this,bin,false);
255 while (i.bin()!=bin && (i.deep() || *i!=val))
257 if (!i.deep() && *i==val)
264 } while (i.bin().within(bin));
269 uint64_t* bins::get_stripes (int& count) {
271 uint64_t *stripes = (uint64_t*) malloc(32*8);
273 uint16_t cur = bins::EMPTY;
274 stripes[count++] = 0;
275 iterator i(this,0,false);
281 if (cur!=*i) { // new stripe
283 stripes[count++] = i.bin().base_offset();
286 stripes = (uint64_t*) realloc(stripes,size*8);
295 stripes[count++] = i.bin().base_offset();
301 void bins::remove (bins& b) {
302 uint8_t start_lr = b.height>height ? b.height : height;
303 bin64_t top(start_lr,0);
304 iterator zis(this,top), zat(&b,top);
306 while (zis.deep() || zat.deep()) {
307 zis.left(); zat.left();
312 while (zis.pos.is_right()) {
313 zis.parent(); zat.parent();
315 zis.sibling(); zat.sibling();
320 bin64_t bins::cover(bin64_t val) {
321 iterator i(this,val,false);
322 while (i.pos!=val && !i.solid())
324 //if (!i.half && !halves[0])
325 // return bin64_t::ALL;
330 bin64_t bins::find_filtered
331 (bins& filter, bin64_t range, const uint8_t layer, fill_t seek)
333 if (range==bin64_t::ALL)
334 range = bin64_t ( height>filter.height ? height : filter.height, 0 );
335 iterator i(this,range,true), j(&filter,range,true);
336 fill_t stop = seek==EMPTY ? FILLED : EMPTY;
338 while ( i.bin().layer()>layer && (i.deep() || *i!=stop || j.deep() || *j!=FILLED) )
339 i.left(), j.left(); // TODO may optimize a lot here
340 if (i.bin().layer()==layer && !i.deep() && *i==seek && *j==EMPTY)
342 while (i.bin().is_right() && i.bin()!=range)
343 i.parent(), j.parent();
346 i.parent(), j.parent();
347 i.right(), j.right();
349 return bin64_t::NONE;
352 // FIXME unite with remove(); do bitwise()
353 void bins::copy_range (bins& origin, bin64_t range) {
354 if (range==bin64_t::ALL)
355 range = bin64_t ( height>origin.height ? height : origin.height, 0 );
356 iterator zis(this,range,true), zat(&origin,range,true);
357 while (zis.pos.within(range)) {
358 while (zis.deep() || zat.deep()) {
359 zis.left(); zat.left();
364 while (zis.pos.is_right()) {
365 zis.parent(); zat.parent();
367 zis.sibling(); zat.sibling();
373 heap_ = (bin64_t*) malloc(size_*sizeof(bin64_t));
377 bool bincomp (const bin64_t& a, const bin64_t& b) {
378 register uint64_t ab = a.base_offset(), bb = b.base_offset();
380 return a.tail_bit() < b.tail_bit();
385 bool bincomp_rev (const bin64_t& a, const bin64_t& b) {
386 register uint64_t ab = a.base_offset(), bb = b.base_offset();
388 return a.tail_bit() > b.tail_bit();
393 bin64_t binheap::pop() {
395 return bin64_t::NONE;
396 bin64_t ret = heap_[0];
397 std::pop_heap(heap_, heap_+filled_--,bincomp);
398 while (filled_ && heap_[0].within(ret))
399 std::pop_heap(heap_, heap_+filled_--,bincomp);
403 void binheap::extend() {
404 std::sort(heap_,heap_+filled_,bincomp_rev);
406 for(int i=1; i<filled_; i++)
407 if (!heap_[i].within(heap_[solid]))
408 heap_[++solid] = heap_[i];
410 if (2*filled_>size_) {
412 heap_ = (bin64_t*) realloc(heap_,size_*sizeof(bin64_t));
416 void binheap::push(bin64_t val) {
419 heap_[filled_++] = val;
420 std::push_heap(heap_, heap_+filled_,bincomp);
423 binheap::~binheap() {