bloomit
Version:
Space efficient bloom filter based on the bloom-filters npm package.
149 lines (144 loc) • 5.74 kB
JavaScript
/* file : utils.ts
MIT License
Copyright (c) 2017-2020 Thomas Minier & Arnaud Grall
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBitAtIndex = exports.setBitInByte = exports.getBitIndex = exports.getByteIndexInArray = exports.getUint8ArrayLength = exports.getDistinctIndices = exports.hashTwice = void 0;
var xxhashjs_1 = __importDefault(require("xxhashjs"));
/**
* Hash a value into two 32-bit values (in hex or integer format)
* @param value - The value to hash
* @param seed the seed used for hashing
* @return The results of the hash functions applied to the value (in hex or integer)
* @memberof Utils
* @author Arnaud Grall & Thomas Minier
*/
function hashTwice(value, seed) {
var f = xxhashjs_1.default.h32(value, seed + 1);
var l = xxhashjs_1.default.h32(value, seed + 2);
return {
first: f.toNumber(),
second: l.toNumber(),
};
}
exports.hashTwice = hashTwice;
/**
* Generate a set of distinct indexes on interval [0, size) using the double hashing technique
* @param element - The element to hash
* @param size - the range on which we can generate an index [0, size) = size
* @param number - The number of indexes desired
* @param seed - The seed used
* @param maxIterations - throw if we exceed this without finding a result of the
* requested size; avoids a hard busy loop in the event of an algorithm failure. Defaults
* to size*100
* @return A array of indexes
* @author Arnaud Grall
*/
function getDistinctIndices(element, size, number, seed, maxIterations) {
var _a;
if (seed === void 0) { seed = 0; }
if (maxIterations === void 0) { maxIterations = size * 100; }
var _b = hashTwice(element, seed), first = _b.first, second = _b.second;
var index, i = 1;
var indices = [];
// Implements enhanced double hashing algorithm from
// http://peterd.org/pcd-diss.pdf s.6.5.4
while (indices.length < number) {
index = first % size;
if (!indices.includes(index)) {
indices.push(index);
}
first = (first + second) % size;
second = (second + i) % size;
i++;
if (i > size) {
// Enhanced double hashing stops cycles of length less than `size` in the case where
// size is coprime with the second hash. But you still get cycles of length `size`.
// So if we reach there and haven't finished, append a prime to the input and
// rehash.
seed++;
(_a = hashTwice(element, seed), first = _a.first, second = _a.second);
}
if (maxIterations && i > maxIterations) {
throw new Error('max iterations exceeded');
}
}
return indices;
}
exports.getDistinctIndices = getDistinctIndices;
/**
* Return the amount of bytes needed to fit the input bits
* @return Length of Unit8Array to use
* @param bitCount - amount of bits the filter uses
*/
function getUint8ArrayLength(bitCount) {
var remainder = bitCount % 8;
var bitFill = 8 - remainder;
return (bitCount + bitFill) / 8;
}
exports.getUint8ArrayLength = getUint8ArrayLength;
/**
* Return the index of the byte to be edited within the array
* @return Array index of the byte to be edited
* @param bitIndex - index of the bit to be set
* @author Kolja Blauhut
*/
function getByteIndexInArray(bitIndex) {
return Math.floor(bitIndex / 8);
}
exports.getByteIndexInArray = getByteIndexInArray;
/**
* Return the index of the bit in the byte to edit
* @return Array index of the byte to be edited
* @param bitIndex - index of the bit to be set
* @author Kolja Blauhut
*/
function getBitIndex(bitIndex) {
return bitIndex % 8;
}
exports.getBitIndex = getBitIndex;
/**
* Set a certain bit in the byte to 1
* @return Edited byte
* @param indexInByte - Index of the bit in the byte to be set
* @param byte - Current byte
* @author Kolja Blauhut
*/
function setBitInByte(indexInByte, byte) {
var byteOR = 1 << indexInByte;
return byte | byteOR;
}
exports.setBitInByte = setBitInByte;
/**
* Returns a bit at a given index
* @return Bit 1 | 0
* @param array - Uint8Array containing bloom filter
* @param bitIndex - Index of bit to read
* @author Kolja Blauhut
*/
function getBitAtIndex(array, bitIndex) {
var byte = array[getByteIndexInArray(bitIndex)];
var indexInByte = getBitIndex(bitIndex);
var byteAND = setBitInByte(indexInByte, 0);
return ((byte & byteAND) >> indexInByte);
}
exports.getBitAtIndex = getBitAtIndex;