Sha256: baac5a765b6a9c60485f5579c68a7eb579063db6b38e81252cfc2426116e19b1

Contents?: true

Size: 1011 Bytes

Versions: 2

Compression:

Stored size: 1011 Bytes

Contents

JsonBloomfilter = (options = {}) ->
  @options =
    size: 100,
    hashes: 4,
    seed: (new Date().getTime() / 1000),
    bits: null

  @options[key] = value for key, value of options
  @bits = new JsonBloomfilter.BitArray(@options["size"], @options["bits"])
  this

JsonBloomfilter.prototype.add = (key) ->
  @bits.add(index) for index in @indexesFor(key)
  return

JsonBloomfilter.prototype.test = (key) ->
  for index in @indexesFor(key)
    return false if @bits.get(index) == 0
  true

JsonBloomfilter.prototype.clear = ->
  @bits = new JsonBloomfilter.BitArray(@options["size"])
  return

JsonBloomfilter.prototype.toHash = ->
  hash = {}
  hash[key] = value for key, value of @options
  hash["bits"] = @bits.field
  hash

JsonBloomfilter.prototype.toJson = ->
  JSON.stringify @toHash()

JsonBloomfilter.prototype.indexesFor = (key) ->
  indexes = []
  for index in [0..@options["hashes"]-1]
    indexes.push (JsonBloomfilter.Zlib.crc32("#{key}:#{index+@options["seed"]}") % @options["size"])
  indexes

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
json-bloomfilter-0.0.5 coffee/bloomfilter.coffee
json-bloomfilter-0.0.4 coffee/bloomfilter.coffee