Sha256: c2ac77ad0b91bfc5ba6dc48398b9caa4ece4dc074c9d181891a9fbc89bb94fa6

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

JsonBloomfilter.BitArray =
    size: null
    field: null

    ELEMENT_WIDTH: 32

    new: (size, field = null) ->
      @size = size
      arrayLength = Math.floor(((size - 1) / @ELEMENT_WIDTH) + 1)
      @field = field || []
      @field[i] = 0 for i in [0..arrayLength] unless field
      this

    add: (position) ->
      @set(position, 1)

    remove: (position) ->
      @set(position, 0)

    set: (position, value) ->
      aPos = @arrayPosition(position)
      bChange = @bitChange(position)
      if value == 1
        @field[aPos] |= bChange
      else if @field[aPos] & bChange != 0
        @field[aPos] ^= bChange
      true

    get: (position) ->
      aPos = @arrayPosition(position)
      bChange = @bitChange(position)
      if (@field[aPos] & bChange) > 0
        return 1
      else
        return 0

    arrayPosition: (position) ->
      Math.floor(position / @ELEMENT_WIDTH)

    bitChange: (position) ->
      Math.abs(1 << position % @ELEMENT_WIDTH)

    toString: ->
      output = ""
      output += @get(i) for i in [0..@size-1]
      output

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json-bloomfilter-0.0.2 coffee/bitarray.coffee