-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bloom filters #162
Merged
Merged
Bloom filters #162
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3cc813d
update: used fill() to fill array with null
ashokdey c5e89e8
update: initial Set implementation
ashokdey 92be475
update: entry in TOC
ashokdey ec38f2e
update: hidden storage
ashokdey 8e33980
update: added BloomFilters
ashokdey b097303
refactor: improvemnts in _hash()
ashokdey b3ef051
update: entry in TOC
ashokdey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
class BloomFilters { | ||
constructor(size = 101) { | ||
this.size = size; | ||
this.data = this.getStorage(size); | ||
} | ||
|
||
add(element) { | ||
const indices = this.getIndices(element); | ||
|
||
for (let i = 0; i < indices.length; i += 1) { | ||
this.data.setBit(indices[i]); | ||
} | ||
} | ||
|
||
contains(element) { | ||
const indices = this.getIndices(element); | ||
|
||
for (let i = 0; i < indices.length; i += 1) { | ||
const index = indices[i]; | ||
if (!this.data.getBit(index)) { | ||
return false; // item is definately not there | ||
} | ||
} | ||
return true; // item may be there | ||
} | ||
|
||
getIndices(element) { | ||
return [this.hashOne(element), this.hashTwo(element), this.hashThree(element)]; | ||
} | ||
|
||
hashOne(value) { | ||
const stringValue = String(value); | ||
let hashVal = 0; | ||
|
||
for (let i = 0; i < stringValue.length; i += 1) { | ||
hashVal += stringValue.charCodeAt(i) - 96; | ||
} | ||
|
||
// eslint-disable-next-line no-bitwise | ||
hashVal &= hashVal; | ||
|
||
return Math.abs(hashVal % this.size); | ||
} | ||
|
||
hashTwo(value) { | ||
const stringValue = String(value); | ||
const PRIME_MULTIPLIER = 1801; // Random prime number | ||
let hashVal = 0; | ||
|
||
for (let i = 0; i < stringValue.length; i += 1) { | ||
hashVal += stringValue.charCodeAt(i) - 96; | ||
hashVal *= PRIME_MULTIPLIER; | ||
} | ||
|
||
return Math.abs(hashVal % this.size); | ||
} | ||
|
||
hashThree(value) { | ||
const stringValue = String(value); | ||
const PRIME_MULTIPLIER = 1801; // Random prime number | ||
const PRIME_ADDER = 2029; // Random prime number | ||
let hashVal = 0; | ||
|
||
for (let i = 0; i < stringValue.length; i += 1) { | ||
hashVal += stringValue.charCodeAt(i) - 96; | ||
hashVal *= PRIME_MULTIPLIER; | ||
hashVal += PRIME_ADDER; | ||
} | ||
// eslint-disable-next-line no-bitwise | ||
hashVal &= hashVal; | ||
return Math.abs(hashVal % this.size); | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
getStorage(size) { | ||
const data = new Array(size).fill(0); | ||
|
||
return { | ||
setBit(index) { | ||
data[index] = 1; | ||
}, | ||
getBit(index) { | ||
return data[index]; | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
// const b = new BloomFilters(); | ||
|
||
// b.add('React.js'); | ||
// b.add('Node.js'); | ||
|
||
// console.log(b.contains('JavaScript')); | ||
// console.log(b.contains('React.js')); | ||
|
||
module.exports = BloomFilters; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// XSet because ES6 already has a Set class | ||
class XSet { | ||
constructor() { | ||
this.data = this.getStore(); | ||
} | ||
|
||
add(element) { | ||
this.data.push(element); | ||
} | ||
|
||
remove(element) { | ||
this.data.pop(element); | ||
} | ||
|
||
has(element) { | ||
return this.data.contains(element); | ||
} | ||
|
||
values() { | ||
return this.data.val(); | ||
} | ||
|
||
union(givenSet) { | ||
const result = new Set(); | ||
const firstSetValues = this.values(); | ||
const givenSetValues = givenSet.values(); | ||
|
||
// eslint-disable-next-line no-restricted-syntax | ||
for (const e of firstSetValues) result.add(e); | ||
|
||
// eslint-disable-next-line no-restricted-syntax | ||
for (const e of givenSetValues) result.add(e); | ||
|
||
return result; | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
getStore() { | ||
const store = {}; | ||
|
||
return { | ||
push(el) { | ||
if (!store[el]) { | ||
store[el] = true; | ||
} | ||
}, | ||
pop(el) { | ||
if (store[el]) { | ||
delete store[el]; | ||
} | ||
}, | ||
contains(el) { | ||
return !!store[el]; | ||
}, | ||
val() { | ||
return Object.keys(store); | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
// const s = new XSet(); | ||
|
||
// s.add(10); | ||
// s.add(20); | ||
// s.add(90); | ||
|
||
// console.log(s.has(1)); | ||
// console.log(s.has(10)); | ||
// console.log(s.has(90)); | ||
|
||
// console.log(s.values()); | ||
// s.remove(90); | ||
// console.log(s.has(90)); | ||
// console.log(s.data); | ||
|
||
module.exports = XSet; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why to use bitwise AND operation ??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merely an operation can be skipped