Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BloomFilter {
- constructor(size, hashCount) {
- this.size = size;
- this.hashCount = hashCount;
- this.bits = new Uint8Array(size);
- }
- add(value) {
- for (let i = 0; i < this.hashCount; i++) {
- const hash = this.hash(value, i);
- this.bits[hash] = 1;
- }
- }
- has(value) {
- for (let i = 0; i < this.hashCount; i++) {
- const hash = this.hash(value, i);
- if (this.bits[hash] !== 1) {
- return false;
- }
- }
- return true;
- }
- hash(value, seed) {
- let hash = 0;
- for (let i = 0; i < value.length; i++) {
- hash = (hash << 5) + hash + value.charCodeAt(i);
- hash = hash & hash; // Konvertierung in 32-Bit-Integer
- hash = Math.abs(hash);
- }
- return hash % this.size;
- }
- }
- // Beispiel für die Verwendung des Bloom-Filters:
- const bloomFilter = new BloomFilter(100, 3);
- bloomFilter.add('hello');
- console.log(bloomFilter.has('hello')); // gibt "true" aus
- console.log(bloomFilter.has('world')); // gibt "false" aus (mit hoher Wahrscheinlichkeit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement