bis 0.1.2 → 0.1.3
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 +4 -4
- data/README.md +8 -9
- data/lib/bis.rb +31 -25
- data/lib/bis/version.rb +1 -1
- data/spec/bis_spec.rb +9 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77a393ad380bbdc924d6c8421d4afc0a6573e4de
|
4
|
+
data.tar.gz: 56cc67115897fb0e197639e4e9176142596e0ac5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd7bcd70ca222cb942773cc7eb9efbb9cc10c0a75681993a6593bd2c1231fbc25d8894c0be4d03d2055fa83805bc24b71c47101f1fb20142cd0ba3843326aa18
|
7
|
+
data.tar.gz: 7ec004c8dac647d18bd7131dd27c9fafab0c41a8910b12dedec51439db1960da4791932f76786a5c293a491407c4b3552b2a2dae2df8ca85b1d6aeb68e1fc9bd
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
(https://codeclimate.com/github/fuadsaud/bis)
|
8
8
|
|
9
9
|
|
10
|
-
A pure ruby bitset implementation.
|
10
|
+
A pure ruby immutable bitset implementation.
|
11
11
|
|
12
12
|
## Installation
|
13
13
|
|
@@ -16,14 +16,13 @@ A pure ruby bitset implementation.
|
|
16
16
|
## Usage
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
bis = Bis.new(8)
|
20
|
-
|
21
|
-
bis.set(3) #=> <<
|
22
|
-
bis.set(3).set(2) #=> <<
|
23
|
-
|
24
|
-
|
25
|
-
bis3
|
26
|
-
bis3 + 1 #=> <<0010000001>> 65
|
19
|
+
bis = Bis.new(8) #=> <<00000000>> 0
|
20
|
+
bis.set(3) #=> <<00001000>> 8
|
21
|
+
bis.set(3).set(2) #=> <<00001100>> 12
|
22
|
+
bis2 = bis.set(3).set(2).clear(3) #=> <<00000100>> 4
|
23
|
+
bis3 = bis2 << 4 #=> <<01000000>> 64
|
24
|
+
bis3 + 1 #=> <<1000001>> 65
|
25
|
+
bis3.concat(10) #=> <<010000001010>> 1034
|
27
26
|
```
|
28
27
|
|
29
28
|
## Contributing
|
data/lib/bis.rb
CHANGED
@@ -2,21 +2,28 @@ require 'bis/conversion'
|
|
2
2
|
require 'bis/version'
|
3
3
|
|
4
4
|
class Bis
|
5
|
+
include Comparable
|
5
6
|
include Enumerable
|
6
7
|
|
7
8
|
def self.from_enum(enum)
|
8
|
-
|
9
|
+
from_string(enum.join)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.from_string(string)
|
13
|
+
Bis.new(string.size, value: string.to_i(2))
|
9
14
|
end
|
10
15
|
|
11
16
|
attr_reader :size
|
12
17
|
alias_method :length, :size
|
13
|
-
|
18
|
+
|
19
|
+
# Future versions of Ruby may have Fixnum#bitlength
|
20
|
+
alias_method :bitlength, :size
|
14
21
|
|
15
22
|
def initialize(size, value: 0)
|
16
23
|
fail ArgumentError, 'size must be >= 0' if size < 0
|
17
24
|
|
18
25
|
@size = size.to_i
|
19
|
-
@store = value & ((1 << size) - 1)
|
26
|
+
@store = (value & ((1 << size) - 1)).to_i
|
20
27
|
end
|
21
28
|
|
22
29
|
def set(index)
|
@@ -34,14 +41,14 @@ class Bis
|
|
34
41
|
end
|
35
42
|
|
36
43
|
# Not sure if it's a good idead to implement this.
|
37
|
-
def []=(index, value)
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
44
|
+
# def []=(index, value)
|
45
|
+
# with_valid_bit value do |bit|
|
46
|
+
# case bit
|
47
|
+
# when 1 then set index
|
48
|
+
# when 0 then clear index
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
# end
|
45
52
|
|
46
53
|
def concat(other)
|
47
54
|
size_and_value_for(other) do |other_size, other_value|
|
@@ -86,8 +93,15 @@ class Bis
|
|
86
93
|
def each_byte
|
87
94
|
return enum_for :each_byte unless block_given?
|
88
95
|
|
89
|
-
|
90
|
-
|
96
|
+
full_bitset = if size % 8 != 0
|
97
|
+
concat((1 << (8 - (size % 8))) - 1)
|
98
|
+
else
|
99
|
+
self
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
(full_bitset.size / 8).times.reverse_each do |offset|
|
104
|
+
yield Bis.new(8, value: (full_bitset >> offset * 8) & ((1 << 8) - 1))
|
91
105
|
end
|
92
106
|
end
|
93
107
|
|
@@ -107,14 +121,6 @@ class Bis
|
|
107
121
|
to_a.join
|
108
122
|
end
|
109
123
|
|
110
|
-
def ==(other)
|
111
|
-
other == to_i
|
112
|
-
end
|
113
|
-
|
114
|
-
def ===(other)
|
115
|
-
to_i === other
|
116
|
-
end
|
117
|
-
|
118
124
|
def <=>(other)
|
119
125
|
to_i <=> Bis(other).to_i
|
120
126
|
end
|
@@ -172,13 +178,13 @@ class Bis
|
|
172
178
|
|
173
179
|
def bitlenght_for(bitset_or_integer)
|
174
180
|
case bitset_or_integer
|
181
|
+
when Bis then bitset_or_integer.size
|
175
182
|
when 0..1 then 1
|
176
183
|
when 2 then 2
|
177
184
|
when Integer then Math.log2(bitset_or_integer).ceil
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
'or Bis'
|
185
|
+
else fail ArgumentError, 'cannot resolve a bitlength' +
|
186
|
+
"#{ bitset_or_integer }. Must be either Integer" +
|
187
|
+
'or Bis'
|
182
188
|
end
|
183
189
|
end
|
184
190
|
end
|
data/lib/bis/version.rb
CHANGED
data/spec/bis_spec.rb
CHANGED
@@ -423,21 +423,23 @@ describe Bis do
|
|
423
423
|
|
424
424
|
context 'bitset has less than 8 bits' do
|
425
425
|
let(:size) { 4 }
|
426
|
-
let(:value) {
|
426
|
+
let(:value) { 10 }
|
427
427
|
|
428
|
-
it "
|
429
|
-
expect { |b| subject.(b) }.
|
428
|
+
it "yields many 1's as needed to fill a byte" do
|
429
|
+
expect { |b| subject.(b) }.to yield_with_args(0b10101111)
|
430
430
|
end
|
431
431
|
end
|
432
432
|
|
433
|
-
context 'bitset has at least
|
433
|
+
context 'bitset has at least one byte' do
|
434
434
|
let(:size) { 16 }
|
435
|
-
let(:first_byte) {
|
436
|
-
let(:second_byte) {
|
435
|
+
let(:first_byte) { 0b00100110 }
|
436
|
+
let(:second_byte) { 0b11010101 }
|
437
437
|
let(:value) { (first_byte << 8) | second_byte }
|
438
438
|
|
439
439
|
it 'yields all the bytes' do
|
440
|
-
|
440
|
+
p Bis.new(size, value: value).each_byte.to_a
|
441
|
+
expect { |b| subject.(b) }.to yield_successive_args first_byte,
|
442
|
+
second_byte
|
441
443
|
end
|
442
444
|
end
|
443
445
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fuad Saud
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|