int main() { int i; for(i=0;i<11;i++) //initializing the array to 0 T[i]=0; update(4,3); for(i=1;i<11;i++) //printing out array printf("%d ",T[i]); printf("\n"); update(6,3); //updating bucket 6 to value 3 update(10,3); //updating bucket 10 to value 3 update(9,3); //updating bucket 9 to value 3 update(8,3); //updating bucket 8 to value 3 for(i=1;i<11;i++) printf("%d ",T[i]); //printing out T values i.e according to binary indexed trees printf("\n"); for(i=1;i<11;i++) printf("%d ",query(i)); //if we want initial buckets to have values a[1]=1, a[2]=3,a[3]=3... than query(3)=a[1]+a[2]+a[3] printf("\n"); for(i=1;i<11;i++) printf("%d ",readSingle(i)); //this gives original values put in buckets printf("\n"); return 0; }
int TEENSYSENSE::ioctl(struct file *filp, int cmd, unsigned long arg) { int ret = -EINVAL; switch (cmd) { case TEENSY_SENSOR_START: start(); ret = OK; break; case TEENSY_SENSOR_STOP: stop(); ret = OK; break; case TEENSY_SENSOR_TEST: warnx("Starting test"); ret = test(); break; case TEENSY_SENSOR_READ: warnx("Requesting single value"); readSingle(); ret = OK; break; default: ret = CDev::ioctl(filp, cmd, arg); break; } return ret; }
TEST(FenwickTest, FenwickTree) { for (int i = 1; i <= 16; ++i) { ASSERT_EQ(0, readSingle(i)); ASSERT_EQ(0, read(i)); } update(1, 3); ASSERT_EQ(3, read(1)); update(2, 2); ASSERT_EQ(3, read(1)); ASSERT_EQ(5, read(2)); ASSERT_EQ(5, read(16)); update(16, -5); ASSERT_EQ(0, read(16)); ASSERT_EQ(5, read(15)); ASSERT_EQ(3, read(1)); }
void update(int index, T new_value) { increase(index, new_value - readSingle(index)); }