golombset.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2015 Kazuho Oku
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #ifndef GOLOMBSET_H
  23. #define GOLOMBSET_H
  24. #include <inttypes.h>
  25. struct st_golombset_encode_t {
  26. unsigned char *dst;
  27. unsigned char *dst_max;
  28. unsigned dst_shift;
  29. };
  30. struct st_golombset_decode_t {
  31. const unsigned char *src;
  32. const unsigned char *src_max;
  33. unsigned src_shift;
  34. };
  35. static int golombset_encode_bit(struct st_golombset_encode_t *ctx, int bit)
  36. {
  37. if (ctx->dst_shift == 0) {
  38. if (++ctx->dst == ctx->dst_max)
  39. return -1;
  40. *ctx->dst = 0;
  41. ctx->dst_shift = 8;
  42. }
  43. --ctx->dst_shift;
  44. if (bit)
  45. *ctx->dst |= 1 << ctx->dst_shift;
  46. return 0;
  47. }
  48. static int golombset_encode_bits(struct st_golombset_encode_t *ctx, unsigned bits, uint64_t value)
  49. {
  50. if (bits != 0) {
  51. do {
  52. --bits;
  53. if (golombset_encode_bit(ctx, (value >> bits) & 1) != 0)
  54. return -1;
  55. } while (bits != 0);
  56. }
  57. return 0;
  58. }
  59. static int golombset_decode_bit(struct st_golombset_decode_t *ctx)
  60. {
  61. if (ctx->src_shift == 0) {
  62. if (++ctx->src == ctx->src_max)
  63. return -1;
  64. ctx->src_shift = 8;
  65. }
  66. return (*ctx->src >> --ctx->src_shift) & 1;
  67. }
  68. static int golombset_decode_bits(struct st_golombset_decode_t *ctx, unsigned bits, uint64_t *value)
  69. {
  70. int bit;
  71. *value = 0;
  72. for (; bits != 0; --bits) {
  73. if ((bit = golombset_decode_bit(ctx)) == -1)
  74. return -1;
  75. *value = (*value * 2) + bit;
  76. }
  77. return 0;
  78. }
  79. static int golombset_encode_value(struct st_golombset_encode_t *ctx, unsigned fixed_bits, uint64_t value)
  80. {
  81. /* emit quontient */
  82. uint64_t unary_bits = value >> fixed_bits;
  83. for (; unary_bits != 0; --unary_bits)
  84. if (golombset_encode_bit(ctx, 0) != 0)
  85. return -1;
  86. if (golombset_encode_bit(ctx, 1) != 0)
  87. return -1;
  88. /* emit remainder */
  89. return golombset_encode_bits(ctx, fixed_bits, value);
  90. }
  91. static int golombset_decode_value(struct st_golombset_decode_t *ctx, unsigned fixed_bits, uint64_t *value)
  92. {
  93. uint64_t q;
  94. int bit;
  95. /* decode quontient */
  96. for (q = 0; ; ++q) {
  97. if ((bit = golombset_decode_bit(ctx)) == -1)
  98. return -1;
  99. if (bit)
  100. break;
  101. }
  102. /* decode remainder */
  103. if (golombset_decode_bits(ctx, fixed_bits, value) == -1)
  104. return -1;
  105. /* merge q and r */
  106. *value += q << fixed_bits;
  107. return 0;
  108. }
  109. static int golombset_encode(unsigned fixed_bits, uint64_t *keys, size_t num_keys, void *buf, size_t *bufsize)
  110. {
  111. struct st_golombset_encode_t ctx = {(unsigned char *)buf - 1, (unsigned char *)buf + *bufsize};
  112. size_t i;
  113. uint64_t next_min = 0;
  114. for (i = 0; i != num_keys; ++i) {
  115. if (golombset_encode_value(&ctx, fixed_bits, keys[i] - next_min) != 0)
  116. return -1;
  117. next_min = keys[i] + 1;
  118. }
  119. *bufsize = ctx.dst + 1 - (unsigned char *)buf;
  120. return 0;
  121. }
  122. static int golombset_decode(unsigned fixed_bits, const void *buf, size_t bufsize, uint64_t *keys, size_t *num_keys)
  123. {
  124. struct st_golombset_decode_t ctx = {buf, (unsigned char *)buf + bufsize, 8};
  125. size_t index = 0;
  126. uint64_t next_min = 0;
  127. while (1) {
  128. uint64_t value;
  129. if (golombset_decode_value(&ctx, fixed_bits, &value) != 0)
  130. break;
  131. if (index == *num_keys) {
  132. /* not enough space */
  133. return -1;
  134. }
  135. value += next_min;
  136. keys[index++] = value;
  137. next_min = value + 1;
  138. }
  139. *num_keys = index;
  140. return 0;
  141. }
  142. #endif