BitField.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __values = (this && this.__values) || function(o) {
  18. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  19. if (m) return m.call(o);
  20. if (o && typeof o.length === "number") return {
  21. next: function () {
  22. if (o && i >= o.length) o = void 0;
  23. return { value: o && o[i++], done: !o };
  24. }
  25. };
  26. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  27. };
  28. var __read = (this && this.__read) || function (o, n) {
  29. var m = typeof Symbol === "function" && o[Symbol.iterator];
  30. if (!m) return o;
  31. var i = m.call(o), r, ar = [], e;
  32. try {
  33. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  34. }
  35. catch (error) { e = { error: error }; }
  36. finally {
  37. try {
  38. if (r && !r.done && (m = i["return"])) m.call(i);
  39. }
  40. finally { if (e) throw e.error; }
  41. }
  42. return ar;
  43. };
  44. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  45. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  46. if (ar || !(i in from)) {
  47. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  48. ar[i] = from[i];
  49. }
  50. }
  51. return to.concat(ar || Array.prototype.slice.call(from));
  52. };
  53. Object.defineProperty(exports, "__esModule", { value: true });
  54. exports.BitFieldClass = exports.BitField = void 0;
  55. var BitField = (function () {
  56. function BitField() {
  57. this.bits = 0;
  58. }
  59. BitField.allocate = function () {
  60. var e_1, _a;
  61. var names = [];
  62. for (var _i = 0; _i < arguments.length; _i++) {
  63. names[_i] = arguments[_i];
  64. }
  65. try {
  66. for (var names_1 = __values(names), names_1_1 = names_1.next(); !names_1_1.done; names_1_1 = names_1.next()) {
  67. var name_1 = names_1_1.value;
  68. if (this.has(name_1)) {
  69. throw new Error('Bit already allocated for ' + name_1);
  70. }
  71. if (this.next === BitField.MAXBIT) {
  72. throw new Error('Maximum number of bits already allocated');
  73. }
  74. this.names.set(name_1, this.next);
  75. this.next <<= 1;
  76. }
  77. }
  78. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  79. finally {
  80. try {
  81. if (names_1_1 && !names_1_1.done && (_a = names_1.return)) _a.call(names_1);
  82. }
  83. finally { if (e_1) throw e_1.error; }
  84. }
  85. };
  86. BitField.has = function (name) {
  87. return this.names.has(name);
  88. };
  89. BitField.prototype.set = function (name) {
  90. this.bits |= this.getBit(name);
  91. };
  92. BitField.prototype.clear = function (name) {
  93. this.bits &= ~this.getBit(name);
  94. };
  95. BitField.prototype.isSet = function (name) {
  96. return !!(this.bits & this.getBit(name));
  97. };
  98. BitField.prototype.reset = function () {
  99. this.bits = 0;
  100. };
  101. BitField.prototype.getBit = function (name) {
  102. var bit = this.constructor.names.get(name);
  103. if (!bit) {
  104. throw new Error('Unknown bit-field name: ' + name);
  105. }
  106. return bit;
  107. };
  108. BitField.MAXBIT = 1 << 31;
  109. BitField.next = 1;
  110. BitField.names = new Map();
  111. return BitField;
  112. }());
  113. exports.BitField = BitField;
  114. function BitFieldClass() {
  115. var names = [];
  116. for (var _i = 0; _i < arguments.length; _i++) {
  117. names[_i] = arguments[_i];
  118. }
  119. var Bits = (function (_super) {
  120. __extends(Bits, _super);
  121. function Bits() {
  122. return _super !== null && _super.apply(this, arguments) || this;
  123. }
  124. return Bits;
  125. }(BitField));
  126. Bits.allocate.apply(Bits, __spreadArray([], __read(names), false));
  127. return Bits;
  128. }
  129. exports.BitFieldClass = BitFieldClass;
  130. //# sourceMappingURL=BitField.js.map