Created
September 22, 2023 19:44
-
-
Save VeerpalBrar/9fb5cb9b0963a1396f4e961f6be69922 to your computer and use it in GitHub Desktop.
A simple implementation of count min sketch in ruby
This file contains hidden or 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
require 'digest' | |
class CountMinSketch | |
def initialize(width, length, hashes) | |
@width = width | |
@length = length | |
@hashes = hashes | |
@table = Array.new(length) { Array.new(width) { 0 } } | |
end | |
def increment(item) | |
for row in 0..@length - 1 | |
hash = @hashes[row] | |
index = indexFromHash(hash, item) | |
@table[row][index] += 1 | |
end | |
end | |
def getCount(item) | |
for row in 0..@length - 1 | |
hash = @hashes[row] | |
index = indexFromHash(hash, item) | |
count = @table[row][index] | |
result = result ? [result, count].min : count | |
end | |
result | |
end | |
def printSketch | |
pp @table | |
end | |
private | |
def indexFromHash(hash, item) | |
hash.digest(item).sum % @width | |
end | |
end | |
sketch = CountMinSketch.new(3, 3, [Digest::SHA256.new, Digest::MD5.new, Digest::SHA384.new]) | |
item = "item" | |
anotherItem = "anotherItem" | |
sketch.increment(item) | |
sketch.increment(item) | |
sketch.increment(item) | |
sketch.increment(anotherItem) | |
sketch.increment(anotherItem) | |
sketch.printSketch | |
puts "item count: #{sketch.getCount(item)} expected: 3" | |
puts "anotherItem count: #{sketch.getCount(anotherItem)} expected: 2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment