nbitarray 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +8 -0
- data/LICENSE +22 -0
- data/README.md +37 -0
- data/Rakefile +2 -0
- data/lib/nbitarray/version.rb +3 -0
- data/lib/nbitarray.rb +98 -0
- data/nbitarray.gemspec +21 -0
- data/spec/lib/nbitarray_spec.rb +192 -0
- metadata +89 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :version => 2, :cli => "--color" do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Yusuke Mito
|
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,37 @@
|
|
1
|
+
# NBitArray
|
2
|
+
|
3
|
+
This is bitarray implementation which can assign arbitrary bit size per item.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'nbitarray'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install nbitarray
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
bitarray = NBitArray.new(3) # 3-bit array
|
23
|
+
bitarray[0] = 7
|
24
|
+
puts bitarray[0] #=> 7
|
25
|
+
bitarray[3] = 1
|
26
|
+
bitarray.size #=> 4
|
27
|
+
|
28
|
+
bitarray[1] = 8 #=> raise error
|
29
|
+
```
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/nbitarray.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'nbitarray/version'
|
2
|
+
require 'pry'
|
3
|
+
|
4
|
+
class NBitArray
|
5
|
+
MARSHAL_SPLIT_SIZE = 512
|
6
|
+
attr_reader :bit
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
def initialize(bit)
|
10
|
+
@bit = bit
|
11
|
+
@bit_array = 0
|
12
|
+
@mask = (1 << @bit) - 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def size
|
16
|
+
if @bit_array == 0
|
17
|
+
1
|
18
|
+
else
|
19
|
+
num_of_bits = Math.log2(@bit_array)
|
20
|
+
if num_of_bits == Float::INFINITY
|
21
|
+
# FIXME: this algorithm is too slow
|
22
|
+
count = 1
|
23
|
+
value = @bit_array
|
24
|
+
while (value = value >> 1) != 0
|
25
|
+
count += 1
|
26
|
+
end
|
27
|
+
num_of_bits = count
|
28
|
+
else
|
29
|
+
num_of_bits = num_of_bits.floor + 1
|
30
|
+
end
|
31
|
+
(num_of_bits - 1) / @bit + 1
|
32
|
+
end
|
33
|
+
end
|
34
|
+
alias length size
|
35
|
+
|
36
|
+
def each(&block)
|
37
|
+
size.times {|index| yield self[index] }
|
38
|
+
end
|
39
|
+
|
40
|
+
def []=(index, value)
|
41
|
+
raise ArgumentError, 'too large value' if value >= 1 << @bit
|
42
|
+
raise ArgumentError, 'value must be positive' if value < 0
|
43
|
+
bit_index = index * @bit
|
44
|
+
|
45
|
+
# 00 00 00 10 << 4 shift
|
46
|
+
# 00 10 00 00
|
47
|
+
assign_value = value << bit_index
|
48
|
+
|
49
|
+
# 00 00 00 11 mask
|
50
|
+
# 00 11 00 00 move to masking position
|
51
|
+
# 11 00 11 11 reverse mask
|
52
|
+
reverse_mask = ~(@mask << bit_index)
|
53
|
+
|
54
|
+
# 10 01 11 00 & 11 00 11 11 erase current value by reverse mask
|
55
|
+
# 10 00 11 00 | 00 10 00 00 assign new value
|
56
|
+
# 10 10 11 00
|
57
|
+
@bit_array = (@bit_array & reverse_mask) | assign_value
|
58
|
+
value
|
59
|
+
end
|
60
|
+
|
61
|
+
def [](index)
|
62
|
+
bit_index = index * @bit
|
63
|
+
|
64
|
+
# 10 10 11 00 >> 4 shift
|
65
|
+
# 00 00 10 10 | 00 00 00 11 mask
|
66
|
+
# 00 00 00 10 value
|
67
|
+
(@bit_array >> bit_index) & @mask
|
68
|
+
end
|
69
|
+
|
70
|
+
def marshal_dump
|
71
|
+
[@bit, @mask, splitted_bit_array(@bit_array, MARSHAL_SPLIT_SIZE), MARSHAL_SPLIT_SIZE]
|
72
|
+
end
|
73
|
+
|
74
|
+
def marshal_load(array)
|
75
|
+
@bit, @mask, splitted_array, split_size = array
|
76
|
+
@bit_array = joined_bit_array(splitted_array, split_size)
|
77
|
+
end
|
78
|
+
|
79
|
+
def splitted_bit_array(bit_array, split_size = 32)
|
80
|
+
mask = (1 << split_size) - 1
|
81
|
+
array = []
|
82
|
+
while bit_array > 0
|
83
|
+
array << (bit_array & mask)
|
84
|
+
bit_array = bit_array >> split_size
|
85
|
+
end
|
86
|
+
array
|
87
|
+
end
|
88
|
+
private :splitted_bit_array
|
89
|
+
|
90
|
+
def joined_bit_array(splitted_array, split_size)
|
91
|
+
value = 0
|
92
|
+
splitted_array.each_with_index do |splitted_value, index|
|
93
|
+
value += splitted_value << (split_size * index)
|
94
|
+
end
|
95
|
+
value
|
96
|
+
end
|
97
|
+
private :joined_bit_array
|
98
|
+
end
|
data/nbitarray.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/nbitarray/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Yusuke Mito"]
|
6
|
+
gem.email = ["y310.1984@gmail.com"]
|
7
|
+
gem.description = %q{Pure Ruby n-bit array implementation.}
|
8
|
+
gem.summary = %q{Pure Ruby n-bit array implementation.}
|
9
|
+
gem.homepage = "https://github.com/y310/nbitarray"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "nbitarray"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = NBitArray::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rspec'
|
19
|
+
gem.add_development_dependency 'guard-rspec'
|
20
|
+
gem.add_development_dependency 'pry-debugger'
|
21
|
+
end
|
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'nbitarray'
|
2
|
+
|
3
|
+
describe NBitArray do
|
4
|
+
describe '#new' do
|
5
|
+
it 'should initialized by bit' do
|
6
|
+
array = NBitArray.new(8)
|
7
|
+
expect { array[1] = 1 }.to change { array.size }.from(1).to(2)
|
8
|
+
array = NBitArray.new(33)
|
9
|
+
expect { array[1] = 1 }.to change { array.size }.from(1).to(2)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#size' do
|
14
|
+
shared_examples_for 'return correct array size for any bit' do
|
15
|
+
let(:bit_array) { NBitArray.new(bit) }
|
16
|
+
|
17
|
+
it 'should return correct size' do
|
18
|
+
expect { bit_array[1] = 1 }.to change { bit_array.size }.from(1).to(2)
|
19
|
+
expect { bit_array[10] = 1 }.to change { bit_array.size }.from(2).to(11)
|
20
|
+
expect { bit_array[100] = 1 }.to change { bit_array.size }.from(11).to(101)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'can be called by #length' do
|
25
|
+
expect { NBitArray.new(1).length }.to_not raise_error
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'small bit' do
|
29
|
+
let(:bit) { 1 }
|
30
|
+
it_should_behave_like 'return correct array size for any bit'
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'large bit' do
|
34
|
+
let(:bit) { 100 }
|
35
|
+
it_should_behave_like 'return correct array size for any bit'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#each' do
|
40
|
+
let(:bit_array) { NBitArray.new(3) }
|
41
|
+
let(:assign_values) { [0, 1, 2, 3, 4, 5, 6, 7] }
|
42
|
+
|
43
|
+
before do
|
44
|
+
assign_values.each_with_index do |v, i|
|
45
|
+
bit_array[i] = v
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should iterate all items' do
|
50
|
+
bit_array.each do |v|
|
51
|
+
v.should eq assign_values.shift
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'can use Enumerablue method' do
|
56
|
+
bit_array.map{|v| v * 2}.inject(0) {|v, sum| sum += v}.should eq 56
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#[]=, #[]' do
|
61
|
+
shared_examples_for 'manage value correctly for any bit' do
|
62
|
+
let(:bit_array) { NBitArray.new(bit) }
|
63
|
+
let(:max_value) { (1 << bit) - 1 }
|
64
|
+
|
65
|
+
it 'should assign value' do
|
66
|
+
expect { bit_array[0] = 0 }.to change { bit_array[0] }.by(0)
|
67
|
+
expect { bit_array[1] = max_value }.to change { bit_array[1] }.from(0).to(max_value)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should raise error' do
|
71
|
+
expect { bit_array[0] = 1 << bit }.to raise_error(ArgumentError)
|
72
|
+
expect { bit_array[0] = -1 }.to raise_error(ArgumentError)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'under 32bit array' do
|
77
|
+
let(:bit) { 1 }
|
78
|
+
it_should_behave_like 'manage value correctly for any bit'
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'over 32bit array' do
|
82
|
+
let(:bit) { 34 }
|
83
|
+
it_should_behave_like 'manage value correctly for any bit'
|
84
|
+
end
|
85
|
+
|
86
|
+
context 'too large bit array' do
|
87
|
+
let(:bit) { 100 }
|
88
|
+
it_should_behave_like 'manage value correctly for any bit'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe 'marshaling' do
|
93
|
+
let(:bit_array) { NBitArray.new(8) }
|
94
|
+
it 'can marshal' do
|
95
|
+
bit_array[0] = 1
|
96
|
+
bit_array[1] = 2
|
97
|
+
restored_bit_array = Marshal.load(Marshal.dump(bit_array))
|
98
|
+
restored_bit_array.bit.should eql bit_array.bit
|
99
|
+
restored_bit_array[0].should eql bit_array[0]
|
100
|
+
restored_bit_array[1].should eql bit_array[1]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe 'marshaling benchmark' do
|
105
|
+
shared_examples_for 'benchmark bit array with array' do
|
106
|
+
let(:max_value) { (1 << bit) - 1 }
|
107
|
+
let(:bit_array) { NBitArray.new(bit) }
|
108
|
+
let(:array) { Array.new }
|
109
|
+
let(:bit_array_size) { Marshal.dump(bit_array).size }
|
110
|
+
let(:array_size) { Marshal.dump(array).size }
|
111
|
+
|
112
|
+
it 'sparse array' do
|
113
|
+
index = 100
|
114
|
+
bit_array[index] = 1
|
115
|
+
array[index] = 1
|
116
|
+
(bit_array_size < array_size).should == sparse_won
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'filled array' do
|
120
|
+
1000.times do |i|
|
121
|
+
value = rand(max_value)
|
122
|
+
bit_array[i] = value
|
123
|
+
array[i] = value
|
124
|
+
end
|
125
|
+
(bit_array_size < array_size).should == filled_won
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context '1 bit' do
|
130
|
+
let(:bit) { 1 }
|
131
|
+
let(:sparse_won) { true }
|
132
|
+
let(:filled_won) { true }
|
133
|
+
it_should_behave_like 'benchmark bit array with array'
|
134
|
+
end
|
135
|
+
|
136
|
+
context '4 bit' do
|
137
|
+
let(:bit) { 4 }
|
138
|
+
let(:sparse_won) { true }
|
139
|
+
let(:filled_won) { true }
|
140
|
+
it_should_behave_like 'benchmark bit array with array'
|
141
|
+
end
|
142
|
+
|
143
|
+
context '8 bit' do
|
144
|
+
let(:bit) { 8 }
|
145
|
+
let(:sparse_won) { true }
|
146
|
+
let(:filled_won) { true }
|
147
|
+
it_should_behave_like 'benchmark bit array with array'
|
148
|
+
end
|
149
|
+
|
150
|
+
context '12 bit' do
|
151
|
+
let(:bit) { 12 }
|
152
|
+
let(:sparse_won) { true }
|
153
|
+
let(:filled_won) { true }
|
154
|
+
it_should_behave_like 'benchmark bit array with array'
|
155
|
+
end
|
156
|
+
|
157
|
+
context '16 bit' do
|
158
|
+
let(:bit) { 16 }
|
159
|
+
let(:sparse_won) { true }
|
160
|
+
let(:filled_won) { true }
|
161
|
+
it_should_behave_like 'benchmark bit array with array'
|
162
|
+
end
|
163
|
+
|
164
|
+
context '24 bit' do
|
165
|
+
let(:bit) { 24 }
|
166
|
+
let(:sparse_won) { true }
|
167
|
+
let(:filled_won) { true }
|
168
|
+
it_should_behave_like 'benchmark bit array with array'
|
169
|
+
end
|
170
|
+
|
171
|
+
context '32 bit' do
|
172
|
+
let(:bit) { 32 }
|
173
|
+
let(:sparse_won) { true }
|
174
|
+
let(:filled_won) { true }
|
175
|
+
it_should_behave_like 'benchmark bit array with array'
|
176
|
+
end
|
177
|
+
|
178
|
+
context '64 bit' do
|
179
|
+
let(:bit) { 64 }
|
180
|
+
let(:sparse_won) { true }
|
181
|
+
let(:filled_won) { true }
|
182
|
+
it_should_behave_like 'benchmark bit array with array'
|
183
|
+
end
|
184
|
+
|
185
|
+
context '128 bit' do
|
186
|
+
let(:bit) { 128 }
|
187
|
+
let(:sparse_won) { true }
|
188
|
+
let(:filled_won) { true }
|
189
|
+
it_should_behave_like 'benchmark bit array with array'
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nbitarray
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Yusuke Mito
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70151155133100 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70151155133100
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: guard-rspec
|
27
|
+
requirement: &70151155132240 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70151155132240
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: pry-debugger
|
38
|
+
requirement: &70151155131800 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70151155131800
|
47
|
+
description: Pure Ruby n-bit array implementation.
|
48
|
+
email:
|
49
|
+
- y310.1984@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- Guardfile
|
57
|
+
- LICENSE
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/nbitarray.rb
|
61
|
+
- lib/nbitarray/version.rb
|
62
|
+
- nbitarray.gemspec
|
63
|
+
- spec/lib/nbitarray_spec.rb
|
64
|
+
homepage: https://github.com/y310/nbitarray
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.15
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Pure Ruby n-bit array implementation.
|
88
|
+
test_files:
|
89
|
+
- spec/lib/nbitarray_spec.rb
|