buffer_xor.js 1005 B

123456789101112131415161718192021
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.buffersXOR = exports.bufferXOR = void 0;
  4. const buffer_1 = require("buffer");
  5. // Often in the code, when we want to calculate a content-based ID, where the content is an unordered set, we compute the XOR of the content-based IDs of the set elements. This is because XOR is insensitive to order.
  6. // Precondition that is NOT CHECKED: buffers must be of equal length
  7. // Returns new buffer that is bitwise XOR of inputs.
  8. function bufferXOR(a, b) {
  9. const result = buffer_1.Buffer.allocUnsafe(a.length);
  10. for (let i = 0; i < a.length; i += 4) {
  11. // Little endian is fastest, because native to Intel CPUs
  12. result.writeInt32LE(a.readInt32LE(i) ^ b.readInt32LE(i), i);
  13. }
  14. return result;
  15. }
  16. exports.bufferXOR = bufferXOR;
  17. function buffersXOR(...args) {
  18. return args.reduce((a, b) => bufferXOR(a, b), buffer_1.Buffer.alloc(32));
  19. }
  20. exports.buffersXOR = buffersXOR;
  21. //# sourceMappingURL=buffer_xor.js.map