| 123456789101112131415161718 |
- import {Buffer} from "buffer";
- // 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.
- // Precondition that is NOT CHECKED: buffers must be of equal length
- // Returns new buffer that is bitwise XOR of inputs.
- export function bufferXOR(a: Buffer, b: Buffer): Buffer {
- const result = Buffer.allocUnsafe(a.length);
- for (let i=0; i<a.length; i+=4) {
- // Little endian is fastest, because native to Intel CPUs
- result.writeInt32LE(a.readInt32LE(i) ^ b.readInt32LE(i), i);
- }
- return result;
- }
- export function buffersXOR(...args: Buffer[]): Buffer {
- return args.reduce((a,b) => bufferXOR(a,b), Buffer.alloc(32));
- }
|