Skip to content

Instantly share code, notes, and snippets.

@VeerpalBrar
Created September 22, 2023 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VeerpalBrar/9fb5cb9b0963a1396f4e961f6be69922 to your computer and use it in GitHub Desktop.
Save VeerpalBrar/9fb5cb9b0963a1396f4e961f6be69922 to your computer and use it in GitHub Desktop.
A simple implementation of count min sketch in ruby
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