项目作者: sug0

项目描述 :
Bitmap C Library
高级语言: C
项目地址: git://github.com/sug0/bitmap.git
创建时间: 2017-10-27T20:09:16Z
项目社区:https://github.com/sug0/bitmap

开源协议:

下载


Bitmap

A bitmap is a data structure that implements an array of booleans in a very
memory efficient way; albeit the time complexity of this implementation
isn’t constant, given the fact cpu registers will store the same bytes in
them for a prolonged period of time with accesses restricted to the
same memory region, this data structure often outperforms others with
better time complexity.

API

  1. typedef struct _bm {
  2. uint8_t *bits;
  3. size_t max_index;
  4. } Bitmap_t;
  5. extern void Bitmap_init(Bitmap_t *bm, size_t sz);
  6. extern uint8_t Bitmap_at(Bitmap_t *bm, size_t index);
  7. extern void Bitmap_set(Bitmap_t *bm, size_t index, uint8_t set_bit);
  8. extern void Bitmap_free(Bitmap_t *bm);

Example usage

  1. #include <stdio.h>
  2. #include <bitmap.h>
  3. int main(void)
  4. {
  5. Bitmap_t bm;
  6. /* initialize bitmap with 8 spaces */
  7. Bitmap_init(&bm, 8);
  8. /* set the first bit */
  9. Bitmap_set(&bm, 0, 1);
  10. /* set the third bit */
  11. Bitmap_set(&bm, 2, 1);
  12. /* set the last bit */
  13. Bitmap_set(&bm, 7, 1);
  14. /* print set bits */
  15. for (int i = 0; i < 8; i++)
  16. putchar((Bitmap_at(&bm, i)) ? '1' : '0');
  17. putchar('\n');
  18. /* free the allocated space in memory */
  19. Bitmap_free(&bm);
  20. return 0;
  21. }