Class SimpleVector
In: lib/bloomfilter/simplevector.rb
Parent: Object

This is a slower and more limited bitvector class. Please use the BitSet C-extension library from below instead:

Methods

&   ==   []   []=   clear   empty   from_str   inspect   intersect   new   restore   store   to_str   union   |  

External Aliases

length -> size

Attributes

bvec  [RW] 
length  [RW] 

Public Class methods

[Source]

    # File lib/bloomfilter/simplevector.rb, line 17
17:   def SimpleVector.from_str(n,hexstr)
18:     bv = SimpleVector.new(n)
19:     bv.bvec = hexstr.hex
20:     bv
21:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 12
12:   def initialize(arg)
13:     @length = arg
14:     @bvec = 1<<@length
15:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 78
78:   def SimpleVector.restore(bfstr,n)
79:     bytes = bfstr.size
80:     sv = bfstr.reverse.unpack("H*")[0]
81:     sv = sv.hex | 1<<n
82:     nv = SimpleVector.new(n)
83:     nv.bvec = sv
84:     nv
85:   end

Public Instance methods

[Source]

    # File lib/bloomfilter/simplevector.rb, line 45
45:   def &(other)
46:     self.dup.intersect(other)
47:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 49
49:   def ==(other)
50:     self.bvec == other.bvec
51:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 61
61:   def [](pos)
62:     (@bvec & 1<<pos ) == 0 ? 0 : 1
63:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 53
53:   def []=(pos,val=1)
54:     case val
55:       when 1: @bvec |= 1<<pos
56:       when 0: @bvec &= ~(1<<(length+1) | 1<<pos)
57:     end
58:     self
59:   end
clear()

Alias for empty

[Source]

    # File lib/bloomfilter/simplevector.rb, line 23
23:   def empty
24:     @bvec = 1 << size
25:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 65
65:   def inspect
66:     (@bvec).to_s(2)[1..-1]
67:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 40
40:   def intersect(other)
41:     self.bvec &= other.bvec
42:     self
43:   end

store/restore to/from BitSet style bytestrings

[Source]

    # File lib/bloomfilter/simplevector.rb, line 71
71:   def store
72:     sv = self.bvec & ~(1<<(length+1) | 1<<size)
73:     sv = sv.to_s(16)
74:     sv[0,0] = '0' * (((size+7)/8) * 2 - sv.size)
75:     [sv].pack('H*').reverse
76:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 28
28:   def to_str
29:     @bvec.to_s(16)
30:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 32
32:   def union(other)
33:     self.bvec |= other.bvec
34:     self
35:   end

[Source]

    # File lib/bloomfilter/simplevector.rb, line 36
36:   def |(other)
37:     self.dup.union(other)
38:   end

[Validate]