bis 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6f97157ab82377beef29d536741bb736d150c429
4
+ data.tar.gz: 82d5750520e4f6f123518d6132228a2105def5cb
5
+ SHA512:
6
+ metadata.gz: 19ef835409a3461bfc4b28548369d5e810295dc29794ae4fa69489d9d6382f98c27da9dff9c2888999b0d0c8e55a8b9ed0ca38d6e1cacd692dc9a7275fa7a023
7
+ data.tar.gz: 286ebeb190985e8651a2380661a07dad21bb814bfa4a9e058a58818598a9856d7a3d2af27485921d2cc74c662704b8af6f2f40f919918a366d2af723bf274ea3
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Fuad Saud
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,19 @@
1
+ # Bis
2
+
3
+ A pure ruby bitset implementation.
4
+
5
+ ## Installation
6
+
7
+ $ gem install bis
8
+
9
+ ## Usage
10
+
11
+ TODO: Write usage instructions here
12
+
13
+ ## Contributing
14
+
15
+ 1. Fork it
16
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
17
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
18
+ 4. Push to the branch (`git push origin my-new-feature`)
19
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ require 'rubocop/rake_task'
5
+
6
+ task default: %i( spec rubocop )
7
+
8
+ RSpec::Core::RakeTask.new
9
+ Rubocop::RakeTask.new
@@ -0,0 +1,19 @@
1
+ class Bis
2
+ module Conversion
3
+ def Bis(obj)
4
+ if obj.kind_of? Bis
5
+ obj
6
+ elsif obj.kind_of? Enumerable
7
+ Bis.from_enum(obj)
8
+ elsif obj.respond_to? :to_i
9
+ Bis.new(Bis::WORD_SIZE, value: obj.to_i)
10
+ else
11
+ fail TypeError, "#{obj.class} can't be coerced to Bis"
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module Kernel
18
+ include Bis::Conversion
19
+ end
@@ -0,0 +1,3 @@
1
+ class Bis
2
+ VERSION = '0.1.0'
3
+ end
data/lib/bis.rb ADDED
@@ -0,0 +1,176 @@
1
+ require 'forwardable'
2
+
3
+ require 'bis/conversion'
4
+ require 'bis/version'
5
+
6
+ class Bis
7
+ WORD_SIZE = 0.size * 8
8
+
9
+ attr_reader :size
10
+
11
+ def self.from_enum(enum)
12
+ Bis.new(enum.size, value: enum.join.to_i(2))
13
+ end
14
+
15
+ def initialize(size, value: 0)
16
+ unless size.kind_of?(Integer) && size > 0
17
+ fail ArgumentError, 'size must be a positive integer'
18
+ end
19
+
20
+ @size = size
21
+ @store = Array.new(words_needed_for(size), 0)
22
+
23
+ self.value = value if value != 0
24
+ end
25
+
26
+ def set(index)
27
+ change_bit_at(index).(1)
28
+ end
29
+
30
+ def clear(index)
31
+ change_bit_at(index).(0)
32
+ end
33
+
34
+ def [](index)
35
+ x, y = offset_for(index)
36
+
37
+ @store[x][y]
38
+ end
39
+
40
+ # Not sure if it's a good idead to implement this.
41
+ def []=(index, value)
42
+ with_valid_bit value do |bit|
43
+ case bit
44
+ when 1 then set index
45
+ when 0 then clear index
46
+ end
47
+ end
48
+ end
49
+
50
+ def ==(other)
51
+ to_i == other
52
+ end
53
+
54
+ def <=>(other)
55
+ to_i <=> Bis(other).to_i
56
+ end
57
+
58
+ def +(value)
59
+ with_valid_bit value do |bit|
60
+ new_bis = new.(size: size + 1)
61
+
62
+ case bit
63
+ when 1 then new_bis.(value: to_i << 1 | bit)
64
+ when 0 then new_bis.(value: to_i << 1)
65
+ end
66
+ end
67
+ end
68
+
69
+ def &(other)
70
+ new_with_same_size.(value: other & to_i)
71
+ end
72
+
73
+ def |(other)
74
+ new_with_same_size.(value: other | to_i)
75
+ end
76
+
77
+ def ^(other)
78
+ new_with_same_size.(value: other ^ to_i)
79
+ end
80
+
81
+ def <<(amount)
82
+ new_with_same_size.(value: to_i << amount)
83
+ end
84
+
85
+ def >>(amount)
86
+ new_with_same_size.(value: to_i >> amount)
87
+ end
88
+
89
+ def each
90
+ return enum_for :each unless block_given?
91
+
92
+ size.times.reverse_each do |bit|
93
+ yield self[bit]
94
+ end
95
+ end
96
+
97
+ def to_a
98
+ each.to_a
99
+ end
100
+
101
+ def to_i
102
+ each.reduce(0) { |a, e| (a << 1) | e }
103
+ end
104
+
105
+ def to_s
106
+ to_a.join
107
+ end
108
+
109
+ def inspect
110
+ "<<#{ to_s }>> #{ to_i }"
111
+ end
112
+
113
+ protected
114
+
115
+ def store=(store)
116
+ @store = store
117
+ end
118
+
119
+ private
120
+
121
+ def value=(value)
122
+ @store.each_with_index do |_, i|
123
+ @store[i] |= Integer(value >> (i * WORD_SIZE))
124
+ end
125
+ end
126
+
127
+ def offset_for(index)
128
+ if index >= size
129
+ fail ArgumentError, "index #{index} out of boudaries for #{self}"
130
+ end
131
+
132
+ [index / WORD_SIZE, index % WORD_SIZE]
133
+ end
134
+
135
+
136
+ def words_needed_for(bits)
137
+ (bits - 1) / WORD_SIZE + 1
138
+ end
139
+
140
+ def with_valid_bit(bit)
141
+ case bit
142
+ when 0..1 then yield bit
143
+ else fail ArgumentError, 'bit must be either 0 or 1'
144
+ end
145
+ end
146
+
147
+ def change_bit_at(index)
148
+ ->(bit) {
149
+ return self if self[index] == bit
150
+
151
+ x, y = offset_for(index)
152
+
153
+ new_with_same_size.().tap { |bis|
154
+ bis.store = @store.dup.tap { |s|
155
+ s[x] = s[x].send(change_operation_for(bit), 1 << y)
156
+ }
157
+ }
158
+ }
159
+ end
160
+
161
+ def change_operation_for(bit)
162
+ bit.zero? ? :^ : :|
163
+ end
164
+
165
+ def new_with_same_size
166
+ new.(size: size)
167
+ end
168
+
169
+ def new(factory = self.class)
170
+ ->(size: size) {
171
+ ->(value: 0) {
172
+ factory.new(size, value: value)
173
+ }
174
+ }
175
+ end
176
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bis::Conversion do
4
+ describe '#Bis' do
5
+ subject { Bis(obj) }
6
+
7
+ context 'given a Bis object' do
8
+ let(:obj) { Bis.new(10, value: 7) }
9
+
10
+ it 'returns the given object' do
11
+ expect(subject).to eq obj
12
+ end
13
+ end
14
+
15
+ context 'given an Enumerable' do
16
+ let(:obj) { [0, 1, 1, 0, 1, 1, 0] }
17
+
18
+ it 'treats the enumerable as a list of bits' do
19
+ expect(subject).to eq 0b0110110
20
+ end
21
+ end
22
+
23
+ context 'given an object that quacks like an Integer' do
24
+ let(:obj) { 26 }
25
+
26
+ it 'returns a Bis with the internal value set to the given number' do
27
+ expect(subject).to eq 0b11010
28
+ end
29
+ end
30
+ end
31
+ end
data/spec/bis_spec.rb ADDED
@@ -0,0 +1,267 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bis do
4
+ let(:size) { Bis::WORD_SIZE }
5
+
6
+ it('has a version') { expect(Bis::VERSION).to be_a String }
7
+
8
+ describe '.new' do
9
+ context 'with size' do
10
+ let(:size) { 8 }
11
+
12
+ subject { Bis.new(size) }
13
+
14
+ it 'is as big as the passed size' do
15
+ expect(subject.size).to eq size
16
+ end
17
+
18
+ it 'has all bits off' do
19
+ expect(subject).to eq 0
20
+ end
21
+ end
22
+
23
+ context 'with size and value' do
24
+ let(:value) { 10 }
25
+
26
+ subject { Bis.new(size, value: value) }
27
+
28
+ it 'is as big as the passed size' do
29
+ expect(subject.size).to eq size
30
+ end
31
+
32
+ it 'has the bits set to the given value' do
33
+ expect(subject).to eq value
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#==' do
39
+ context 'with an integer' do
40
+ let(:value) { 16 }
41
+
42
+ subject { Bis.new(size, value: value) }
43
+
44
+ it 'compares to the bitset' do
45
+ expect(subject).to eq value
46
+ end
47
+
48
+ it 'compares properly' do
49
+ expect(subject).to_not eq value - 1
50
+ end
51
+ end
52
+ end
53
+
54
+ describe '#[]' do
55
+ context 'invalid index' do
56
+ let (:size) { 10 }
57
+
58
+ subject { Bis.new(size) }
59
+
60
+ it 'fails' do
61
+ expect { subject[11] }.to raise_error
62
+ end
63
+ end
64
+
65
+ context 'small numbers' do
66
+ let(:value) { 10 }
67
+
68
+ subject { Bis.new(size, value: value) }
69
+
70
+ it 'returns the bit at the given position' do
71
+ expect(subject[3]).to eq 1
72
+ end
73
+ end
74
+
75
+ context 'large numbers' do
76
+ let(:size) { 65 }
77
+
78
+ subject { Bis.new(size).set(64) }
79
+
80
+ it 'returns the bit at the given position' do
81
+ expect(subject[64]).to eq 1
82
+ end
83
+ end
84
+ end
85
+
86
+ describe '#[]=', pending: "Not sure if it's a good idea to implement this" do
87
+ context 'invalid argument' do
88
+ subject { Bis.new(size) }
89
+
90
+ it 'fails' do
91
+ expect { subject[4] = 'lol' }.to raise_error
92
+ end
93
+ end
94
+
95
+ context 'valid argument' do
96
+ let(:value) { 7 }
97
+
98
+ subject { Bis.new(size, value: 7)[index] = argument }
99
+
100
+ context '1' do
101
+ let(:index) { 4 }
102
+ let(:argument) { 1 }
103
+
104
+ it 'sets the given bit' do
105
+ expect(subject).to eq(value | (argument << index))
106
+ end
107
+ end
108
+
109
+ context '0' do
110
+ let(:index) { 1 }
111
+ let(:argument) { 0 }
112
+
113
+ it 'clears the given bit' do
114
+ expect(subject).to eq(value ^ (1 << index))
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ describe '#set' do
121
+ let(:before) { 0b101 }
122
+ let(:after) { 0b111 }
123
+
124
+ subject { Bis.new(size, value: before) }
125
+
126
+ it 'returns a new bitset with the given bit on' do
127
+ expect(subject.set(1)).to eq after
128
+ end
129
+ end
130
+
131
+ describe '#clear' do
132
+ let(:before) { 0b111 }
133
+ let(:after) { 0b101 }
134
+
135
+ subject { Bis.new(size, value: before) }
136
+
137
+ it 'returns a new bitset with the given bit off' do
138
+ expect(subject.clear(1)).to eq after
139
+ end
140
+ end
141
+
142
+ describe '#to_a' do
143
+ let(:size) { 8 }
144
+
145
+ subject { Bis.new(size, value: 0b01010110).to_a }
146
+
147
+ it 'returns a binary array representation of itself' do
148
+ expect(subject).to eq [0, 1, 0, 1, 0, 1, 1, 0]
149
+ end
150
+ end
151
+
152
+ describe '#to_i' do
153
+ let(:size) { 4 }
154
+
155
+ subject { Bis.new(size, value: 0b100) }
156
+
157
+ it "returns it's integer representation" do
158
+ expect(subject).to eq 4
159
+ end
160
+ end
161
+
162
+ describe '#to_s' do
163
+ let(:size) { 8 }
164
+
165
+ subject { Bis.new(size, value: 0b01010110).to_s }
166
+
167
+ it 'returns a binary array representation of itself' do
168
+ expect(subject).to eq '01010110'
169
+ end
170
+ end
171
+
172
+ shared_examples 'pushing a bit' do
173
+ it 'increases the size' do
174
+ expect(subject.size).to eq size + 1
175
+ end
176
+
177
+ it 'pushes the new bit to the end of the new bitset' do
178
+ expect(subject).to eq new_value
179
+ end
180
+ end
181
+
182
+ describe '#+' do
183
+ let(:size) { 8 }
184
+ let(:value) { 10 }
185
+
186
+ subject { Bis.new(size, value: value) + argument }
187
+
188
+ context '1' do
189
+ let(:argument) { 1 }
190
+ let(:new_value) { (value << 1) | argument }
191
+
192
+ it_behaves_like 'pushing a bit'
193
+ end
194
+
195
+ context '0' do
196
+ let(:argument) { 0 }
197
+ let(:new_value) { value << 1 }
198
+
199
+ it_behaves_like 'pushing a bit'
200
+ end
201
+ end
202
+
203
+ describe '#&' do
204
+ it 'evaluates to logic AND of the two bitsets' do
205
+ expect(Bis.new(size, value: 0b1100) &
206
+ Bis.new(size, value: 0b0101)).to eq Bis.new(size, value: 0b0100)
207
+ end
208
+ end
209
+
210
+ describe '#|' do
211
+ it 'evaluates to logic OR of the two bitsets' do
212
+ expect(Bis.new(size, value: 0b1100) |
213
+ Bis.new(size, value: 0b0110)).to eq Bis.new(size, value: 0b1110)
214
+ end
215
+ end
216
+
217
+ describe '#^' do
218
+ it 'evaluates to logic XOR of the two bitsets' do
219
+ expect(Bis.new(size, value: 0b1100) ^
220
+ Bis.new(size, value: 0b0110)).to eq Bis.new(size, value: 0b1010)
221
+ end
222
+ end
223
+
224
+ describe '#>>' do
225
+ let(:shift) { 1 }
226
+
227
+ context 'zero value' do
228
+ subject { Bis.new(size) }
229
+
230
+ it 'remains zero' do
231
+ expect(subject >> shift).to eq 0
232
+ end
233
+ end
234
+
235
+ context 'non-zero value' do
236
+ let(:value) { 10 }
237
+
238
+ subject { Bis.new(size, value: value) }
239
+
240
+ it 'shifts the internal value properly' do
241
+ expect(subject >> shift).to eq value >> shift
242
+ end
243
+ end
244
+ end
245
+
246
+ describe '#<<' do
247
+ let(:shift) { 1 }
248
+
249
+ context 'zero value' do
250
+ subject { Bis.new(size) }
251
+
252
+ it 'remains zero' do
253
+ expect(subject << shift).to eq 0
254
+ end
255
+ end
256
+
257
+ context 'non-zero value' do
258
+ let(:value) { 10 }
259
+
260
+ subject { Bis.new(size, value: value) }
261
+
262
+ it 'shifts the internal value properly' do
263
+ expect(subject << shift).to eq value << shift
264
+ end
265
+ end
266
+ end
267
+ end
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+
3
+ require_relative File.join(__dir__, '../lib/bis')
4
+
5
+ RSpec.configure do |config|
6
+ config.include Bis::Conversion
7
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fuad Saud
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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: |2
42
+ Feed me.
43
+ email: fuadksd@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - Rakefile
50
+ - LICENSE.md
51
+ - lib/bis/conversion.rb
52
+ - lib/bis/version.rb
53
+ - lib/bis.rb
54
+ - spec/bis/conversion_spec.rb
55
+ - spec/bis_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: http://github.com/fuadsaud/bis
58
+ licenses: []
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.0.3
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Pure ruby bitset implementation
80
+ test_files: []
81
+ has_rdoc: false