bitvector 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +10 -0
- data/bench.rb +53 -0
- data/bitvector.gemspec +21 -0
- data/lib/bit_vector.rb +96 -0
- data/lib/bit_vector/version.rb +3 -0
- data/lib/bitvector.rb +1 -0
- data/spec/bit_vector_spec.rb +418 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2c507e779cafcf95b841c23a00215cd3b1b64480
|
4
|
+
data.tar.gz: 8204d3d2d80258cd2d6fdbe152ae054e8d6de42b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78882aa419df48bee5fbedec56163c4fca2942e84b9f24122cbeeee23cb181fac164323f205f649fc215aae4d385f7ed9985bd60581a98d738e439cb07f92b13
|
7
|
+
data.tar.gz: 56b42cd6cd5658b0dcd35132c6e6b08f64966e183821ab2b81e8512e66e9bfc8041eb349ea14dcba9f61efeb0cea045dea3e8f0d8aca4119a3deb2adbc11287c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Juan Azambuja
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# BitVector
|
2
|
+
|
3
|
+
Small gem to manage integer fields as bit vectors of size 32.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'bitvector'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install bitvector
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Rails Example:
|
22
|
+
|
23
|
+
You want to represent your data as a bit vector, for example, lets say you want to represent days of the
|
24
|
+
week and store them as a bit vector (assuming days_of_week is an int attribute):
|
25
|
+
|
26
|
+
```
|
27
|
+
class Schedule < ActiveRecord::Base
|
28
|
+
serialize days_of_week, BitVector::BitVector
|
29
|
+
end
|
30
|
+
|
31
|
+
schedule = Schedule.new
|
32
|
+
vector = schedule.days_of_week # => This is a BitVector!
|
33
|
+
|
34
|
+
# Mark monday as true
|
35
|
+
vector[0] = 1
|
36
|
+
|
37
|
+
puts vector # => '00000000000000000000000000000001'
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
If you save a schedule instance, the value will be persisted as integer.
|
42
|
+
|
43
|
+
```
|
44
|
+
schedule.days_of_week = BitVector::BitVector.new(1).
|
45
|
+
schedule.save # => days_of_week fields is serialized as 1 in database.
|
46
|
+
```
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it ( https://github.com/juanazam/bitvector/fork )
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bench.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require "benchmark"
|
2
|
+
require "bundler/setup"
|
3
|
+
require "bitvector"
|
4
|
+
|
5
|
+
N = 100_000
|
6
|
+
|
7
|
+
def sample
|
8
|
+
@sample ||= BitVector::BitVector.new
|
9
|
+
end
|
10
|
+
|
11
|
+
Benchmark.bmbm do |x|
|
12
|
+
x.report "Initialize vector" do
|
13
|
+
N.times do
|
14
|
+
BitVector::BitVector.new
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
x.report "Load from number" do
|
19
|
+
N.times do
|
20
|
+
BitVector::BitVector.load 123
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
x.report "Load from string" do
|
25
|
+
N.times do
|
26
|
+
BitVector::BitVector.load "123"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
x.report "Dump vector" do
|
31
|
+
N.times do
|
32
|
+
BitVector::BitVector.dump sample
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
x.report "Set at index" do
|
37
|
+
N.times do
|
38
|
+
sample[16] = 1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
x.report "Clear at index" do
|
43
|
+
N.times do
|
44
|
+
sample[16] = 0
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
x.report "Get at index" do
|
49
|
+
N.times do
|
50
|
+
sample[16]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/bitvector.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "bitvector"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bitvector"
|
8
|
+
spec.version = BitVector::VERSION
|
9
|
+
spec.authors = ["Juan Azambuja"]
|
10
|
+
spec.email = ["juan@wyeworks.com"]
|
11
|
+
spec.summary = %q{Bit Vector representation and managing of integer fields.}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
20
|
+
spec.add_development_dependency "rake"
|
21
|
+
end
|
data/lib/bit_vector.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "bit_vector/version"
|
2
|
+
|
3
|
+
module BitVector
|
4
|
+
# Provides a class to represent a bit vector using a number and size.
|
5
|
+
class BitVector
|
6
|
+
DEFAULT_SIZE = 32
|
7
|
+
|
8
|
+
attr_reader :number, :size
|
9
|
+
|
10
|
+
# Returns new bit vector initialized to optional number and size.
|
11
|
+
def initialize(number = 0, size = DEFAULT_SIZE)
|
12
|
+
@number, @size = number, size
|
13
|
+
raise ArgumentError, "number must be =< #{max_number}" if number > max_number
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns a string representation.
|
17
|
+
def to_s
|
18
|
+
"%0#{size}b" % number
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns an integer representation.
|
22
|
+
alias_method :to_i, :number
|
23
|
+
|
24
|
+
# Sets or clears the bit at index.
|
25
|
+
def []=(index, value)
|
26
|
+
raise ArgumentError, "index must be < #{size}" if index >= size
|
27
|
+
mask = 1 << index
|
28
|
+
@number = value && value != 0 ? number | mask : number & ~mask
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns the bit at index.
|
32
|
+
def [](index)
|
33
|
+
raise ArgumentError, "index must be < #{size}" if index >= size
|
34
|
+
number[index]
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns true if equal to other vector. Two vectors are considered equal if
|
38
|
+
# their numbers and sizes are equal.
|
39
|
+
def ==(other)
|
40
|
+
number == other.number && size == other.size
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns true if equal to other vector and of the same class.
|
44
|
+
def eql?(other)
|
45
|
+
other.is_a?(self.class) && self == other
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns a new vector containing the combined bits of both vectors.
|
49
|
+
def |(other)
|
50
|
+
raise ArgumentError, "size mismatch" unless other.size == size
|
51
|
+
self.class.new number | other.number, size
|
52
|
+
end
|
53
|
+
alias_method :+, :|
|
54
|
+
|
55
|
+
# Returns a new vector containing the common bits with other vector.
|
56
|
+
def &(other)
|
57
|
+
raise ArgumentError, "size mismatch" unless other.size == size
|
58
|
+
self.class.new number & other.number, size
|
59
|
+
end
|
60
|
+
alias_method :-, :&
|
61
|
+
|
62
|
+
# Returns true if all the bits at indexes are set, false otherwise.
|
63
|
+
def include?(indexes)
|
64
|
+
indexes.all? { |index| self[index] == 1 }
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns true if all the bits at indexes are unset, false otherwise.
|
68
|
+
def exclude?(indexes)
|
69
|
+
indexes.all? { |index| self[index] == 0 }
|
70
|
+
end
|
71
|
+
|
72
|
+
# This should be good for now.
|
73
|
+
alias_method :hash, :number
|
74
|
+
|
75
|
+
# Loads vector from value.
|
76
|
+
def self.load(value)
|
77
|
+
new value.to_i
|
78
|
+
end
|
79
|
+
|
80
|
+
# Dumps vector to value.
|
81
|
+
def self.dump(vector)
|
82
|
+
vector.to_i
|
83
|
+
end
|
84
|
+
|
85
|
+
# Creates a new vector with bits at indexes set.
|
86
|
+
def self.from_indexes(indexes, size = DEFAULT_SIZE)
|
87
|
+
new indexes.inject(0) { |number, index| number |= 1 << index; number }, size
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def max_number
|
93
|
+
2 ** size - 1
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/bitvector.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bit_vector"
|
@@ -0,0 +1,418 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'bitvector'
|
4
|
+
|
5
|
+
module BitVector
|
6
|
+
describe BitVector do
|
7
|
+
let(:number) { 0 }
|
8
|
+
let(:bit_vector) { BitVector.new number }
|
9
|
+
|
10
|
+
describe "#to_s" do
|
11
|
+
subject { bit_vector.to_s }
|
12
|
+
|
13
|
+
describe "for an empty vector" do
|
14
|
+
let(:bit_vector) { BitVector.new }
|
15
|
+
|
16
|
+
it "returns all zeros" do
|
17
|
+
subject.must_equal("00000000000000000000000000000000")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "with a known number" do
|
22
|
+
let(:number) { 0b101 }
|
23
|
+
|
24
|
+
it "returns a formatted string" do
|
25
|
+
subject.must_equal("00000000000000000000000000000101")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#[]=' do
|
31
|
+
describe "with a too large index" do
|
32
|
+
let(:index) { bit_vector.size + 1 }
|
33
|
+
|
34
|
+
it "raises" do
|
35
|
+
lambda { bit_vector[index] = 1 }.must_raise(ArgumentError)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "with a bit set" do
|
40
|
+
let(:number) { 0b100 }
|
41
|
+
|
42
|
+
subject { bit_vector[2] }
|
43
|
+
|
44
|
+
describe "when set to 0" do
|
45
|
+
before do
|
46
|
+
bit_vector[2] = 0
|
47
|
+
end
|
48
|
+
|
49
|
+
it "clears bit" do
|
50
|
+
subject.must_equal 0
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "when set to false" do
|
55
|
+
before do
|
56
|
+
bit_vector[2] = false
|
57
|
+
end
|
58
|
+
|
59
|
+
it "clears bit" do
|
60
|
+
subject.must_equal 0
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "with no bits set" do
|
66
|
+
let(:number) { 0b0 }
|
67
|
+
|
68
|
+
subject { bit_vector[0] }
|
69
|
+
|
70
|
+
describe "when set to 1" do
|
71
|
+
before do
|
72
|
+
bit_vector[0] = 1
|
73
|
+
end
|
74
|
+
|
75
|
+
it "sets bit" do
|
76
|
+
subject.must_equal 1
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "when set to true" do
|
81
|
+
before do
|
82
|
+
bit_vector[0] = true
|
83
|
+
end
|
84
|
+
|
85
|
+
it "sets bit" do
|
86
|
+
subject.must_equal 1
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe '[]' do
|
93
|
+
let(:number) { 0b011 }
|
94
|
+
|
95
|
+
it "returns values at given positions" do
|
96
|
+
bit_vector[0].must_equal 1
|
97
|
+
bit_vector[1].must_equal 1
|
98
|
+
bit_vector[2].must_equal 0
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "with a too large index" do
|
102
|
+
let(:index) { bit_vector.size + 1 }
|
103
|
+
|
104
|
+
it "raises" do
|
105
|
+
lambda { bit_vector[index] }.must_raise(ArgumentError)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe '#to_i' do
|
111
|
+
subject { bit_vector.to_i }
|
112
|
+
|
113
|
+
before do
|
114
|
+
bit_vector[2] = 1
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'returns integer representation of vector' do
|
118
|
+
subject.must_equal(4)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "#size" do
|
123
|
+
subject { bit_vector.size }
|
124
|
+
|
125
|
+
it "returns the default size" do
|
126
|
+
subject.must_equal 32
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "#==" do
|
131
|
+
let(:other_number) { bit_vector.number }
|
132
|
+
let(:other_size) { bit_vector.size }
|
133
|
+
let(:other) { BitVector.new other_number, other_size }
|
134
|
+
|
135
|
+
subject { bit_vector == other }
|
136
|
+
|
137
|
+
describe "with same number and size" do
|
138
|
+
it "returns true" do
|
139
|
+
assert subject
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe "with different number and same size" do
|
144
|
+
let(:other_number) { bit_vector.number + 1 }
|
145
|
+
|
146
|
+
it "returns false" do
|
147
|
+
refute subject
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe "with same number and different size" do
|
152
|
+
let(:other_size) { bit_vector.size + 1 }
|
153
|
+
|
154
|
+
it "returns false" do
|
155
|
+
refute subject
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
describe "with different number and different size" do
|
160
|
+
let(:other_number) { bit_vector.number + 1 }
|
161
|
+
let(:other_size) { bit_vector.size + 1 }
|
162
|
+
|
163
|
+
it "returns false" do
|
164
|
+
refute subject
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
describe "with a size argument" do
|
170
|
+
let(:size) { 48 }
|
171
|
+
let(:bit_vector) { BitVector.new(number, size) }
|
172
|
+
|
173
|
+
describe "#initialize" do
|
174
|
+
describe "with a too large number" do
|
175
|
+
let(:number) { 2 ** size }
|
176
|
+
|
177
|
+
it "raises" do
|
178
|
+
lambda { bit_vector }.must_raise(ArgumentError)
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe "#size" do
|
184
|
+
subject { bit_vector.size }
|
185
|
+
|
186
|
+
it "return the size" do
|
187
|
+
subject.must_equal size
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
describe "#to_s" do
|
192
|
+
subject { bit_vector.to_s }
|
193
|
+
|
194
|
+
it "returns a formatted string" do
|
195
|
+
subject.must_equal "000000000000000000000000000000000000000000000000"
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
describe "#+" do
|
201
|
+
let(:other_size) { bit_vector.size }
|
202
|
+
let(:other) { BitVector.new other_number, other_size }
|
203
|
+
|
204
|
+
subject { bit_vector + other }
|
205
|
+
|
206
|
+
describe "with known numbers" do
|
207
|
+
let(:number) { 0b101 }
|
208
|
+
let(:other_number) { 0b110 }
|
209
|
+
|
210
|
+
it "returns the or'ed number" do
|
211
|
+
subject.number.must_equal 0b111
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe "#-" do
|
217
|
+
let(:other_size) { bit_vector.size }
|
218
|
+
let(:other) { BitVector.new other_number, other_size }
|
219
|
+
|
220
|
+
subject { bit_vector - other }
|
221
|
+
|
222
|
+
describe "with known numbers" do
|
223
|
+
let(:number) { 0b101 }
|
224
|
+
let(:other_number) { 0b110 }
|
225
|
+
|
226
|
+
it "returns the and'ed number" do
|
227
|
+
subject.number.must_equal 0b100
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
describe "#include?" do
|
233
|
+
let(:number) { 0b111 }
|
234
|
+
|
235
|
+
subject { bit_vector.include? indexes }
|
236
|
+
|
237
|
+
describe "with included indexes" do
|
238
|
+
let(:indexes) { [0, 1] }
|
239
|
+
|
240
|
+
it "returns true" do
|
241
|
+
assert subject
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
describe "with partially included indexes" do
|
246
|
+
let(:indexes) { [0, 1, 3] }
|
247
|
+
|
248
|
+
it "returns false" do
|
249
|
+
refute subject
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
describe "with excluded indexes" do
|
254
|
+
let(:indexes) { [3, 4] }
|
255
|
+
|
256
|
+
it "returns false" do
|
257
|
+
refute subject
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
describe "with no indexes" do
|
262
|
+
let(:indexes) { [] }
|
263
|
+
|
264
|
+
it "returns true" do
|
265
|
+
assert subject
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
describe "#exclude?" do
|
271
|
+
let(:number) { 0b111 }
|
272
|
+
|
273
|
+
subject { bit_vector.exclude? indexes }
|
274
|
+
|
275
|
+
describe "with excluded indexes" do
|
276
|
+
let(:indexes) { [3, 4] }
|
277
|
+
|
278
|
+
it "returns true" do
|
279
|
+
assert subject
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
describe "with partially excluded indexes" do
|
284
|
+
let(:indexes) { [3, 4, 0] }
|
285
|
+
|
286
|
+
it "returns false" do
|
287
|
+
refute subject
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
describe "with included indexes" do
|
292
|
+
let(:indexes) { [0, 1] }
|
293
|
+
|
294
|
+
it "returns false" do
|
295
|
+
refute subject
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
describe "with no indexes" do
|
300
|
+
let(:indexes) { [] }
|
301
|
+
|
302
|
+
it "returns true" do
|
303
|
+
assert subject
|
304
|
+
end
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
describe "#hash" do
|
309
|
+
let(:other) { BitVector.new other_number }
|
310
|
+
|
311
|
+
subject { bit_vector.hash }
|
312
|
+
|
313
|
+
describe "with the same number" do
|
314
|
+
let(:other_number) { number }
|
315
|
+
|
316
|
+
it "is equal" do
|
317
|
+
subject.must_equal other.hash
|
318
|
+
end
|
319
|
+
end
|
320
|
+
|
321
|
+
describe "with a different number" do
|
322
|
+
let(:other_number) { number + 1 }
|
323
|
+
|
324
|
+
it "is not equal" do
|
325
|
+
subject.wont_equal other.hash
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
describe "when used with a hash and known values" do
|
330
|
+
let(:hash) do
|
331
|
+
{
|
332
|
+
BitVector.new(0b100) => 1,
|
333
|
+
BitVector.new(0b100) => 2,
|
334
|
+
BitVector.new(0b101) => 3
|
335
|
+
}
|
336
|
+
end
|
337
|
+
|
338
|
+
it "has the right size" do
|
339
|
+
hash.size.must_equal 2
|
340
|
+
end
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
describe ".from_indexes" do
|
345
|
+
subject { BitVector.from_indexes indexes }
|
346
|
+
|
347
|
+
describe "with no indexes" do
|
348
|
+
let(:indexes) { [] }
|
349
|
+
|
350
|
+
it "returns the zero vector" do
|
351
|
+
subject.must_equal BitVector.new(0)
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
describe "with known indexes" do
|
356
|
+
let(:indexes) { [1, 2] }
|
357
|
+
|
358
|
+
it "returns the expected vector" do
|
359
|
+
subject.must_equal BitVector.new(0b110)
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
describe "with known, sparse and repeted indexes" do
|
364
|
+
let(:indexes) { [0, 2, 4, 4] }
|
365
|
+
|
366
|
+
it "returns the expected vector" do
|
367
|
+
subject.must_equal BitVector.new(0b10101)
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
describe "with a too large index" do
|
372
|
+
let(:indexes) { [1, 2, 48] }
|
373
|
+
|
374
|
+
it "raises" do
|
375
|
+
lambda { subject }.must_raise(ArgumentError)
|
376
|
+
end
|
377
|
+
end
|
378
|
+
end
|
379
|
+
|
380
|
+
describe '.load' do
|
381
|
+
subject { BitVector.load(value) }
|
382
|
+
|
383
|
+
describe 'when value is nil' do
|
384
|
+
let(:value) { nil }
|
385
|
+
|
386
|
+
it 'initializes array with all elements as FALSE' do
|
387
|
+
subject.must_equal(BitVector.new(0))
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
describe "when value is blank" do
|
392
|
+
let(:value) { "" }
|
393
|
+
|
394
|
+
it "initializes vector to 0" do
|
395
|
+
subject.must_equal(BitVector.new(0))
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
describe 'when value is int (4)' do
|
400
|
+
let(:value) { 4 }
|
401
|
+
|
402
|
+
it 'initializes array representing 4 in binary' do
|
403
|
+
subject.must_equal(BitVector.new(4))
|
404
|
+
end
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
describe '.dump' do
|
409
|
+
let(:number) { 5 }
|
410
|
+
|
411
|
+
subject { BitVector.dump(bit_vector) }
|
412
|
+
|
413
|
+
it 'dumps de parameter bit vector in its integer form' do
|
414
|
+
subject.must_equal(5)
|
415
|
+
end
|
416
|
+
end
|
417
|
+
end
|
418
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bitvector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Juan Azambuja
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- juan@wyeworks.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bench.rb
|
54
|
+
- bitvector.gemspec
|
55
|
+
- lib/bit_vector.rb
|
56
|
+
- lib/bit_vector/version.rb
|
57
|
+
- lib/bitvector.rb
|
58
|
+
- spec/bit_vector_spec.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.2.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Bit Vector representation and managing of integer fields.
|
83
|
+
test_files:
|
84
|
+
- spec/bit_vector_spec.rb
|