bitset.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. //
  3. //
  4. 'use strict';
  5. /**
  6. * A bitset implementation, after that in java.util. Yes there
  7. * already exist such things, but none implement next{Clear|Set}Bit or
  8. * equivalent, and none involved me tooling about for an evening.
  9. */
  10. class BitSet {
  11. /**
  12. * @param {number} [size]
  13. */
  14. constructor(size) {
  15. if (size) {
  16. const numWords = Math.ceil(size / 32);
  17. this.words = new Array(numWords);
  18. }
  19. else {
  20. this.words = [];
  21. }
  22. this.wordsInUse = 0; // = number, not index
  23. }
  24. /**
  25. * @param {number} numWords
  26. */
  27. ensureSize(numWords) {
  28. const wordsPresent = this.words.length;
  29. if (wordsPresent < numWords) {
  30. this.words = this.words.concat(new Array(numWords - wordsPresent));
  31. }
  32. }
  33. /**
  34. * @param {number} bitIndex
  35. */
  36. set(bitIndex) {
  37. const w = wordIndex(bitIndex);
  38. if (w >= this.wordsInUse) {
  39. this.ensureSize(w + 1);
  40. this.wordsInUse = w + 1;
  41. }
  42. const bit = 1 << bitIndex;
  43. this.words[w] |= bit;
  44. }
  45. /**
  46. * @param {number} bitIndex
  47. */
  48. clear(bitIndex) {
  49. const w = wordIndex(bitIndex);
  50. if (w >= this.wordsInUse) return;
  51. const mask = ~(1 << bitIndex);
  52. this.words[w] &= mask;
  53. }
  54. /**
  55. * @param {number} bitIndex
  56. */
  57. get(bitIndex) {
  58. const w = wordIndex(bitIndex);
  59. if (w >= this.wordsInUse) return false; // >= since index vs size
  60. const bit = 1 << bitIndex;
  61. return !!(this.words[w] & bit);
  62. }
  63. /**
  64. * Give the next bit that is set on or after fromIndex, or -1 if no such bit
  65. *
  66. * @param {number} fromIndex
  67. */
  68. nextSetBit(fromIndex) {
  69. let w = wordIndex(fromIndex);
  70. if (w >= this.wordsInUse) return -1;
  71. // the right-hand side is shifted to only test the bits of the first
  72. // word that are > fromIndex
  73. let word = this.words[w] & (0xffffffff << fromIndex);
  74. while (true) {
  75. if (word) return (w * 32) + trailingZeros(word);
  76. w++;
  77. if (w === this.wordsInUse) return -1;
  78. word = this.words[w];
  79. }
  80. }
  81. /**
  82. * @param {number} fromIndex
  83. */
  84. nextClearBit(fromIndex) {
  85. let w = wordIndex(fromIndex);
  86. if (w >= this.wordsInUse) return fromIndex;
  87. let word = ~(this.words[w]) & (0xffffffff << fromIndex);
  88. while (true) {
  89. if (word) return (w * 32) + trailingZeros(word);
  90. w++;
  91. if (w == this.wordsInUse) return w * 32;
  92. word = ~(this.words[w]);
  93. }
  94. }
  95. }
  96. /**
  97. * @param {number} bitIndex
  98. */
  99. function wordIndex(bitIndex) {
  100. return Math.floor(bitIndex / 32);
  101. }
  102. /**
  103. * @param {number} i
  104. */
  105. function trailingZeros(i) {
  106. // From Hacker's Delight, via JDK. Probably far less effective here,
  107. // since bit ops are not necessarily the quick way to do things in
  108. // JS.
  109. if (i === 0) return 32;
  110. let y, n = 31;
  111. y = i << 16; if (y != 0) { n = n -16; i = y; }
  112. y = i << 8; if (y != 0) { n = n - 8; i = y; }
  113. y = i << 4; if (y != 0) { n = n - 4; i = y; }
  114. y = i << 2; if (y != 0) { n = n - 2; i = y; }
  115. return n - ((i << 1) >>> 31);
  116. }
  117. module.exports.BitSet = BitSet;