more tests to coarse_bitmap
authorVictor Grishchenko <victor.grishchenko@gmail.com>
Fri, 21 May 2010 12:34:32 +0000 (14:34 +0200)
committerVictor Grishchenko <victor.grishchenko@gmail.com>
Fri, 21 May 2010 12:34:32 +0000 (14:34 +0200)
bins.cpp
bins.h
tests/binstest2.cpp

index aa81a70..5f41017 100644 (file)
--- a/bins.cpp
+++ b/bins.cpp
@@ -492,13 +492,13 @@ void    binmap_t::map16 (uint16_t* target, bin64_t range) {
 }
 
 
-void    binmap_t::to_coarse_bitmap (void* bits, bin64_t range, uint8_t height) {
-    uint16_t* bits16 = (uint16_t*) bits;
+void    binmap_t::to_coarse_bitmap (uint16_t* bits, bin64_t range, uint8_t height) {
+    assert(range.layer()-height>=4);
     int height16 = range.layer()-height-4;
-    int wordwidth = 1 << height16;
+    int wordwidth = height16 > 0 ? (1 << height16) : 1;
     int offset = range.offset() << height16;
     for(int i=0; i<wordwidth; i++) 
-        map16(bits16+i,bin64_t(height+4,offset+i));
+        map16(bits+i,bin64_t(height+4,offset+i));
 }
 
 
diff --git a/bins.h b/bins.h
index a1e66ac..88c248c 100644 (file)
--- a/bins.h
+++ b/bins.h
@@ -113,7 +113,14 @@ public:
         right change places. Twisting is mostly needed for randomization.  */
     void        twist (uint64_t mask);
     
-    void        to_coarse_bitmap (void* bits, bin64_t range, uint8_t height);
+    /** Convert binmap to a conventional flat bitmap; only bits corresponding
+        to solid filled bins are set to 1. Won't work for ranges < 16 bit.
+        @param range  the bin (the range) to cover
+        @param height aggregation level; use 2**height bins (2**height base
+                layer bits per one bitmap bit). 
+        @param bits   uint16_t array to put bitmap into; must have enough
+                      of space, i.e. 2**(range.layer()-height-4) cells.  */
+    void        to_coarse_bitmap (uint16_t* bits, bin64_t range, uint8_t height);
     
 private:
     
index 8217bc5..b30b5c2 100644 (file)
@@ -401,20 +401,20 @@ TEST(BinsTest,RangeOpTest) {
 TEST(BinsTest,CoarseBitmap) {
     binmap_t b;
     b.set(bin64_t(4,0));
-    uint32_t i32;
-    b.to_coarse_bitmap(&i32,bin64_t(5,0),0);
+    union {uint16_t i16[2]; uint32_t i32;};
+    b.to_coarse_bitmap(i16,bin64_t(5,0),0);
     EXPECT_EQ((1<<16)-1,i32);
     
     b.set(bin64_t(14,0));
     i32=0;
-    b.to_coarse_bitmap(&i32,bin64_t(15,0),10);
+    b.to_coarse_bitmap(i16,bin64_t(15,0),10);
     EXPECT_EQ((1<<16)-1,i32);
     
     binmap_t rough;
     rough.set(bin64_t(1,0));
     rough.set(bin64_t(0,2));
     i32=0;
-    rough.to_coarse_bitmap(&i32,bin64_t(6,0),1);
+    rough.to_coarse_bitmap(i16,bin64_t(6,0),1);
     EXPECT_EQ(1,i32);
     
     binmap_t ladder;
@@ -426,8 +426,20 @@ TEST(BinsTest,CoarseBitmap) {
     ladder.set(bin64_t(1,2));
     ladder.set(bin64_t(0,2));
     i32=0;
-    ladder.to_coarse_bitmap(&i32,bin64_t(8,0),3);
+    ladder.to_coarse_bitmap(i16,bin64_t(8,0),3);
     EXPECT_EQ(0x00ff0f34,i32);
+    
+    binmap_t bin;
+    bin.set(bin64_t(3,0));
+    bin.set(bin64_t(0,8));
+    i32 = 0;
+    bin.to_coarse_bitmap(i16,bin64_t(4,0),0);
+    EXPECT_EQ((1<<9)-1,i32);
+    
+    i32 = 0;
+    bin.to_coarse_bitmap(i16,bin64_t(7,0),3);
+    EXPECT_EQ(1,i32);
+    
 }
 
 /*TEST(BinsTest,AddSub) {