bitary 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/bitary/bitwarr.rb +55 -87
- data/lib/bitary/version.rb +1 -1
- data/lib/bitary.rb +34 -10
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 537a58c702f90bfeade22439d4134d921df709d4d474279f29adae869ff08b0d
|
4
|
+
data.tar.gz: 90910632d7a7f40f89774a8d0ba2b1a6bbf04f5b281454ab18313359151f58b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c65d9bcd2b757bf0a3e2d8b25b51b6a50b239e589efba142eb3c1c7539d42ac88c311361aa934f6ecf5be06c18da53c4e3c138dda801f0c89330da7c030a745
|
7
|
+
data.tar.gz: 10169aa8520c4d2dc4ddc92e419b77b745688c14bc08214770f9da25ed35b66d66655db37ef32daa7967119514ad3cacb618d06be5bd3c69df45e2ed435e9fb1
|
data/CHANGELOG.md
CHANGED
data/lib/bitary/bitwarr.rb
CHANGED
@@ -5,101 +5,68 @@ class Bitary
|
|
5
5
|
attr_reader :bpi, :bitsize
|
6
6
|
|
7
7
|
def initialize(initial_data, bpi: Bitary::LONG)
|
8
|
-
|
9
|
-
|
8
|
+
@bitsize = init_bitsize(initial_data, bpi)
|
9
|
+
@array = init_array(initial_data, @bitsize, bpi)
|
10
|
+
@bpi = init_bpi(initial_data, bpi)
|
10
11
|
|
11
|
-
|
12
|
-
@bitsize = init_bitsize(initial_data)
|
13
|
-
@array = init_array(initial_data)
|
12
|
+
self.bpi = bpi
|
14
13
|
end
|
15
14
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
def
|
21
|
-
@array.respond_to?(method, include_all) || super
|
22
|
-
end
|
23
|
-
|
24
|
-
def [](bit_index)
|
25
|
-
@array[item_index(bit_index)]
|
26
|
-
end
|
15
|
+
def [](bit_index) = @array[item_index(bit_index)]
|
16
|
+
def bit_at(index) = operate_bit_at(:get, index)
|
17
|
+
def bit_at!(index) = operate_bit_at!(:set, index)
|
18
|
+
def unbit_at!(index) = operate_bit_at!(:unset, index)
|
19
|
+
def to_s = @array.map { |item| to_binstr(item) }.join(' ')
|
27
20
|
|
28
21
|
def []=(bit_index, value)
|
29
|
-
raise ArgumentError unless value.is_a?(Integer)
|
30
|
-
|
31
22
|
@array[item_index(bit_index)] = value
|
32
23
|
end
|
33
24
|
|
34
|
-
def
|
35
|
-
check_bit_index(bit_index)
|
36
|
-
|
37
|
-
bit_index / @bpi
|
38
|
-
end
|
39
|
-
|
40
|
-
def bit_at(index)
|
41
|
-
operate_bit_at(:get, index)
|
42
|
-
end
|
43
|
-
|
44
|
-
def bit_at!(index)
|
45
|
-
operate_bit_at!(:set, index)
|
46
|
-
end
|
47
|
-
|
48
|
-
def unbit_at!(index)
|
49
|
-
operate_bit_at!(:unset, index)
|
50
|
-
end
|
51
|
-
|
52
|
-
def each_byte
|
25
|
+
def each_byte(&proc)
|
53
26
|
@array.each do |item|
|
54
|
-
(
|
55
|
-
byte = (item >> (@bpi - (Bitary::BYTE * (i + 1))))
|
56
|
-
yield byte
|
57
|
-
end
|
27
|
+
explode_item(item, Bitary::BYTE, @bpi, &proc)
|
58
28
|
end
|
59
29
|
end
|
60
30
|
|
61
|
-
def to_s
|
62
|
-
@array.map { |item| format("%0#{@bpi}d", item.to_s(2)) }.join(' ')
|
63
|
-
end
|
64
|
-
|
65
31
|
def bpi=(value)
|
66
|
-
|
32
|
+
return if value == @bpi
|
67
33
|
|
68
34
|
update_items_size!(value)
|
69
|
-
|
70
35
|
@bpi = value
|
71
36
|
end
|
72
37
|
|
38
|
+
def method_missing(method, *, &)
|
39
|
+
@array.respond_to?(method) ? @array.send(method, *, &) : super
|
40
|
+
end
|
41
|
+
|
42
|
+
def respond_to_missing?(method, include_all = false)
|
43
|
+
@array.respond_to?(method, include_all) || super
|
44
|
+
end
|
45
|
+
|
73
46
|
private
|
74
47
|
|
75
|
-
def
|
76
|
-
initial_data.is_a?(Array) ?
|
48
|
+
def init_bpi(initial_data, bpi)
|
49
|
+
initial_data.is_a?(Array) ? Bitary::BYTE : bpi
|
77
50
|
end
|
78
51
|
|
79
|
-
def
|
52
|
+
def init_bitsize(initial_data, bpi)
|
53
|
+
initial_data.is_a?(Array) ? bpi * initial_data.length : initial_data
|
54
|
+
end
|
55
|
+
|
56
|
+
def init_array(initial_data, bitsize, bpi)
|
80
57
|
if initial_data.is_a?(Array)
|
81
58
|
initial_data.clone
|
82
59
|
else
|
83
|
-
[0] * (
|
60
|
+
[0] * (bitsize / bpi.to_f).ceil
|
84
61
|
end
|
85
62
|
end
|
86
63
|
|
87
|
-
def
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
def check_bit_index(bit_index)
|
92
|
-
raise ArgumentError unless bit_index.is_a?(Integer)
|
93
|
-
raise IndexError if bit_index.negative? || bit_index >= @bitsize
|
64
|
+
def item_index(bit_index)
|
65
|
+
bit_index / @bpi
|
94
66
|
end
|
95
67
|
|
96
|
-
def
|
97
|
-
|
98
|
-
Bitary::BYTE,
|
99
|
-
Bitary::SHORT,
|
100
|
-
Bitary::INT,
|
101
|
-
Bitary::LONG
|
102
|
-
].include?(bpi)
|
68
|
+
def to_binstr(item)
|
69
|
+
format("%0#{@bpi}d", item.to_s(2))
|
103
70
|
end
|
104
71
|
|
105
72
|
def operate_bit_at(operation, index)
|
@@ -116,29 +83,34 @@ class Bitary
|
|
116
83
|
end
|
117
84
|
|
118
85
|
def update_items_size!(value)
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
86
|
+
value > @bpi ? increase_items_size!(value) : decrease_items_size!(value)
|
87
|
+
end
|
88
|
+
|
89
|
+
def append_bits(item, bpi, addend)
|
90
|
+
Factory.make('Handler::Append', item).execute(
|
91
|
+
offset: bpi,
|
92
|
+
value: addend
|
93
|
+
)
|
124
94
|
end
|
125
95
|
|
126
96
|
def increase_items_size(array, new_size, bpi)
|
127
97
|
processed_bits = 0
|
128
|
-
array.each_with_object([0]) do |
|
129
|
-
|
130
|
-
if processed_bits >= new_size
|
131
|
-
offset = 0
|
98
|
+
res = array.each_with_object([0]) do |item, acc|
|
99
|
+
if processed_bits == new_size
|
132
100
|
acc << 0
|
133
101
|
processed_bits = 0
|
134
102
|
end
|
135
103
|
|
136
|
-
acc[-1] =
|
137
|
-
|
138
|
-
|
139
|
-
|
104
|
+
acc[-1] = append_bits(acc[-1], bpi, item)
|
105
|
+
processed_bits += bpi
|
106
|
+
end
|
107
|
+
|
108
|
+
while processed_bits < new_size
|
109
|
+
res[-1] = append_bits(res[-1], bpi, 0)
|
140
110
|
processed_bits += bpi
|
141
111
|
end
|
112
|
+
|
113
|
+
res
|
142
114
|
end
|
143
115
|
|
144
116
|
def increase_items_size!(value)
|
@@ -147,7 +119,9 @@ class Bitary
|
|
147
119
|
|
148
120
|
def decrease_items_size(array, new_size, bpi)
|
149
121
|
array.each_with_object([]) do |item, acc|
|
150
|
-
|
122
|
+
explode_item(item, new_size, bpi) do |new_item|
|
123
|
+
acc << new_item
|
124
|
+
end
|
151
125
|
end
|
152
126
|
end
|
153
127
|
|
@@ -156,16 +130,10 @@ class Bitary
|
|
156
130
|
end
|
157
131
|
|
158
132
|
def explode_item(item, new_size, bpi)
|
159
|
-
res = []
|
160
|
-
offset = bpi
|
161
133
|
mask = (2**new_size) - 1
|
162
|
-
|
163
|
-
|
164
|
-
offset -= new_size
|
165
|
-
res << ((item >> offset) & mask)
|
134
|
+
(bpi / new_size).times do |i|
|
135
|
+
yield ((item >> (bpi - (new_size * (i + 1)))) & mask)
|
166
136
|
end
|
167
|
-
|
168
|
-
res
|
169
137
|
end
|
170
138
|
end
|
171
139
|
end
|
data/lib/bitary/version.rb
CHANGED
data/lib/bitary.rb
CHANGED
@@ -12,17 +12,24 @@ class Bitary
|
|
12
12
|
include Size
|
13
13
|
|
14
14
|
def initialize(initial_data, bpi: LONG)
|
15
|
-
|
15
|
+
check_initial_data(initial_data)
|
16
|
+
check_bpi(bpi)
|
17
|
+
|
18
|
+
@bitwarr = Factory.make('Bitwarr', initial_data, bpi:)
|
16
19
|
end
|
17
20
|
|
18
21
|
def [](index)
|
19
|
-
|
22
|
+
check_bit_index(index)
|
23
|
+
|
24
|
+
@bitwarr.bit_at(index)
|
20
25
|
end
|
21
26
|
|
22
27
|
def []=(index, value)
|
28
|
+
check_bit_index(index)
|
29
|
+
|
23
30
|
case Factory.make('Mapper::ObjToBit').map(value)
|
24
|
-
when 0 then @
|
25
|
-
else @
|
31
|
+
when 0 then @bitwarr.unbit_at!(index)
|
32
|
+
else @bitwarr.bit_at!(index)
|
26
33
|
end
|
27
34
|
end
|
28
35
|
|
@@ -35,27 +42,44 @@ class Bitary
|
|
35
42
|
end
|
36
43
|
|
37
44
|
def each_byte(&)
|
38
|
-
@
|
45
|
+
@bitwarr.each_byte(&)
|
39
46
|
end
|
40
47
|
|
41
48
|
def to_a
|
42
|
-
@
|
49
|
+
@bitwarr.to_a
|
43
50
|
end
|
44
51
|
|
45
52
|
def to_s
|
46
|
-
@
|
53
|
+
@bitwarr.to_s
|
47
54
|
end
|
48
55
|
|
49
56
|
def bpi=(value)
|
50
|
-
|
57
|
+
check_bpi(value)
|
58
|
+
|
59
|
+
@bitwarr.bpi = value
|
51
60
|
end
|
52
61
|
|
53
62
|
def size
|
54
|
-
@
|
63
|
+
@bitwarr.bitsize
|
55
64
|
end
|
56
65
|
|
57
66
|
def bpi
|
58
|
-
@
|
67
|
+
@bitwarr.bpi
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def check_initial_data(initial_data)
|
73
|
+
raise ArgumentError unless [Array, Integer].include?(initial_data.class)
|
74
|
+
end
|
75
|
+
|
76
|
+
def check_bpi(bpi)
|
77
|
+
raise ArgumentError unless [BYTE, SHORT, INT, LONG].include?(bpi)
|
78
|
+
end
|
79
|
+
|
80
|
+
def check_bit_index(bit_index)
|
81
|
+
raise ArgumentError unless bit_index.is_a?(Integer)
|
82
|
+
raise IndexError if bit_index.negative? || bit_index >= @bitwarr.bitsize
|
59
83
|
end
|
60
84
|
|
61
85
|
alias at []
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maximilien Ballesteros
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby-based implementation of the bit array data structure
|
14
14
|
email:
|