bit2vec.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. *
  3. * Copyright (c) 2011, Jue Ruan <ruanjue@gmail.com>
  4. *
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __BIT2_VEC_RJ_H
  20. #define __BIT2_VEC_RJ_H
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include "mem_share.h"
  26. typedef struct {
  27. uint8_t *bits;
  28. uint64_t size;
  29. uint64_t cap;
  30. } Bit2Vec;
  31. static inline Bit2Vec* init_bit2vec(uint64_t size){
  32. Bit2Vec *vec;
  33. vec = malloc(sizeof(Bit2Vec));
  34. vec->size = 0;
  35. vec->cap = size;
  36. vec->bits = calloc(1, (size * 2 + 7) / 8);
  37. return vec;
  38. }
  39. static inline size_t bit2vec_obj_desc_cnt(void *obj, int idx){
  40. return (((Bit2Vec*)obj)->cap * 2 + 7) / 8;
  41. idx = idx;
  42. }
  43. static const obj_desc_t bit2vec_obj_desc = {"bit2vec_obj_desc", sizeof(Bit2Vec), 1, {1}, {offsetof(Bit2Vec, bits)}, {(obj_desc_t*)&OBJ_DESC_DATA}, bit2vec_obj_desc_cnt, NULL};
  44. static inline void clear_bit2vec(Bit2Vec *vec){
  45. memset(vec->bits, 0, (vec->cap * 2 + 7) / 8);
  46. vec->size = 0;
  47. }
  48. static inline void free_bit2vec(Bit2Vec *vec){
  49. free(vec->bits);
  50. free(vec);
  51. }
  52. static inline int encap_bit2vec(Bit2Vec *vec, uint32_t n){
  53. uint64_t cap;
  54. if(vec->size + n <= vec->cap) return 0;
  55. cap = vec->cap;
  56. while(vec->size + n > vec->cap){
  57. if(vec->cap < 1024 * 1024){
  58. vec->cap <<= 1;
  59. } else {
  60. vec->cap += 1024 * 1024;
  61. }
  62. }
  63. vec->bits = realloc(vec->bits, (vec->cap * 2 + 7) / 8);
  64. memset(vec->bits + (cap * 2 + 7) / 8, 0, (vec->cap * 2 + 7) / 8 - (cap * 2 + 7) / 8);
  65. return 1;
  66. }
  67. static inline void set_bit2vec(Bit2Vec *vec, uint64_t idx, uint8_t dat){
  68. vec->bits[idx >> 2] = (vec->bits[idx >> 2] & (~(3U << ((idx & 0x03U) << 1)))) | ((dat & 0x03) << ((idx & 0x03U) << 1));
  69. }
  70. static inline void push_bit2vec(Bit2Vec *vec, uint8_t dat){
  71. encap_bit2vec(vec, 1);
  72. set_bit2vec(vec, vec->size, dat);
  73. vec->size ++;
  74. }
  75. static inline uint8_t get_bit2vec(Bit2Vec *vec, uint64_t idx){
  76. return (vec->bits[idx >> 2] >> ((idx & 0x03U) << 1)) & 0x03;
  77. }
  78. static inline int pop_bit2vec(Bit2Vec *vec){
  79. if(vec->size == 0) return -1;
  80. vec->size --;
  81. return get_bit2vec(vec, vec->size);
  82. }
  83. #endif