UNPKG

esds

Version:

ES6 JS lightweight data structures (Priority Queue, Binary Search Tree (BST), Graph, Bloom Filters, Trie, Queue, Stack, Linked-List)

109 lines (87 loc) 3.4 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: BloomFilter.mjs</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: BloomFilter.mjs</h1> <section> <article> <pre class="prettyprint source linenums"><code>import xxhash from "xxhash-wasm"; const { h32 } = await xxhash(); /** * BloomFilter Class * Represents a BloomFilter Object */ class BloomFilter { /** * BloomFilter Constructor * @param {number} [size=512] - Number of elements in the Bloom Filter, default 512 * @param {number} [size=3] - Number of rounds of hashing, default 3 */ constructor(size = 512, hash = 3) { if (size &lt;= 1) throw new Error("Size should be bigger than 1"); if (hash &lt; 1) throw new Error("Hash should be bigger than 1"); this.size = size; this.hash = hash; // Create ArrayBuffer this.view = new DataView(new ArrayBuffer(this.size)); } /** * Add a new element to the Bloom Filter * @param {string} input - Element to add - If array is provided all the elements will be added */ add(input) { if (!Array.isArray(input)) input = [input]; for (let value of input) { for (let i = 0; i &lt; this.hash; i++) { value = h32(value); this.view.setInt8(parseInt(value, 16) % this.size, 1); } } } /** * Returns if element may be present in the Bloom Filter * @param {string} input - Element to check - If array is provided all the elements will be checked * @returns {boolean} - True - If element may be present, False if element is definitely not present, if array was provided an boolean array will be returned */ has(input) { if (!Array.isArray(input)) { let value = input; for (let i = 0; i &lt; this.hash; i++) { value = h32(value); if (!this.view.getInt8(parseInt(value, 16) % this.size)) return false; } return true; } else { const output = []; for (let value of input) output.push(this.has(value)); return output; } } } export { BloomFilter }; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BloomFilter.html">BloomFilter</a></li><li><a href="BST.html">BST</a></li><li><a href="Element.html">Element</a></li><li><a href="Graph.html">Graph</a></li><li><a href="List.html">List</a></li><li><a href="Node.html">Node</a></li><li><a href="PriorityQueue.html">PriorityQueue</a></li><li><a href="Queue.html">Queue</a></li><li><a href="Stack.html">Stack</a></li><li><a href="TreeElement.html">TreeElement</a></li><li><a href="Trie.html">Trie</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Thu Sep 23 2021 00:49:18 GMT-0700 (Pacific Daylight Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>