bitary 0.1.7 → 0.1.9
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/CHANGELOG.md +9 -0
- data/lib/bitary/bitwarr.rb +24 -37
- data/lib/bitary/version.rb +1 -1
- data/lib/bitary.rb +17 -6
- metadata +1 -13
- data/lib/bitary/decorator/single_method/non_nil_enforcer.rb +0 -15
- data/lib/bitary/decorator/single_method.rb +0 -21
- data/lib/bitary/decorator.rb +0 -46
- data/lib/bitary/factory.rb +0 -25
- data/lib/bitary/handler/append.rb +0 -11
- data/lib/bitary/handler/get.rb +0 -11
- data/lib/bitary/handler/set.rb +0 -11
- data/lib/bitary/handler/unset.rb +0 -13
- data/lib/bitary/handler.rb +0 -20
- data/lib/bitary/mapper/int_to_bit.rb +0 -15
- data/lib/bitary/mapper/obj_to_bit.rb +0 -14
- data/lib/bitary/mapper.rb +0 -16
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e00e1deaf6796f8875294cb61799bc4ee1ae956c681489cee8243c9cad532224
|
|
4
|
+
data.tar.gz: 74088437d85300f562cb47011f17f9242329a0522a95878dfadce0a54803fffe
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 71b02ce18e1ad4decb42b8680fbcbb44cf44b9a4edb3637300aa40e1ef90825279b9b4c76e3eef24cf08b8b084cf248129f49e55dd98a191ac2223f185c3e7fb
|
|
7
|
+
data.tar.gz: a55ae4d18bd812a9d3a3ff1d6eafa263a5eb362978ed8dd47cfed2c0638927c6491f286f70f767a2cc0eeafea85704583cbee085ad450c8d533d5c086f9d8e1f
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## [0.1.8] - 2024-04-01
|
|
2
|
+
|
|
3
|
+
- inline bit operations to regain original perf
|
|
4
|
+
|
|
5
|
+
## [0.1.7] - 2024-04-01
|
|
6
|
+
|
|
7
|
+
- loads of refactoring and perf improvements
|
|
8
|
+
- Bitary now acts as the logical layer, whereas Bitwarr acts as the business layer
|
|
9
|
+
|
|
1
10
|
## [0.1.6] - 2024-03-31
|
|
2
11
|
|
|
3
12
|
- enhance global perf by remove useless and heavy error handling
|
data/lib/bitary/bitwarr.rb
CHANGED
|
@@ -7,21 +7,24 @@ class Bitary
|
|
|
7
7
|
def initialize(initial_data, bpi: Bitary::LONG)
|
|
8
8
|
@bitsize = init_bitsize(initial_data, bpi)
|
|
9
9
|
@array = init_array(initial_data, @bitsize, bpi)
|
|
10
|
-
@bpi =
|
|
11
|
-
|
|
12
|
-
self.bpi = bpi
|
|
10
|
+
@bpi = bpi
|
|
13
11
|
end
|
|
14
12
|
|
|
15
13
|
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(' ')
|
|
20
14
|
|
|
21
15
|
def []=(bit_index, value)
|
|
22
16
|
@array[item_index(bit_index)] = value
|
|
23
17
|
end
|
|
24
18
|
|
|
19
|
+
def bit_at(index) = (self[index] >> (@bpi - (index % @bpi) - 1)) & 0x1
|
|
20
|
+
def bit_at!(index) = self[index] |= 2**(@bpi - (index % @bpi) - 1)
|
|
21
|
+
|
|
22
|
+
def unbit_at!(index)
|
|
23
|
+
self[index] &= ((2**@bpi) - 1 - (2**(@bpi - (index % @bpi) - 1)))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_s = @array.map { |item| to_binstr(item) }.join(' ')
|
|
27
|
+
|
|
25
28
|
def each_byte(&proc)
|
|
26
29
|
@array.each do |item|
|
|
27
30
|
explode_item(item, Bitary::BYTE, @bpi, &proc)
|
|
@@ -45,17 +48,17 @@ class Bitary
|
|
|
45
48
|
|
|
46
49
|
private
|
|
47
50
|
|
|
48
|
-
def init_bpi(initial_data, bpi)
|
|
49
|
-
initial_data.is_a?(Array) ? Bitary::BYTE : bpi
|
|
50
|
-
end
|
|
51
|
-
|
|
52
51
|
def init_bitsize(initial_data, bpi)
|
|
53
52
|
initial_data.is_a?(Array) ? bpi * initial_data.length : initial_data
|
|
54
53
|
end
|
|
55
54
|
|
|
56
55
|
def init_array(initial_data, bitsize, bpi)
|
|
57
56
|
if initial_data.is_a?(Array)
|
|
58
|
-
|
|
57
|
+
if bpi == Bitary::BYTE
|
|
58
|
+
initial_data.clone
|
|
59
|
+
else
|
|
60
|
+
increase_items_size(initial_data, bpi, Bitary::BYTE)
|
|
61
|
+
end
|
|
59
62
|
else
|
|
60
63
|
[0] * (bitsize / bpi.to_f).ceil
|
|
61
64
|
end
|
|
@@ -69,28 +72,20 @@ class Bitary
|
|
|
69
72
|
format("%0#{@bpi}d", item.to_s(2))
|
|
70
73
|
end
|
|
71
74
|
|
|
72
|
-
def
|
|
73
|
-
|
|
74
|
-
.make("Handler::#{operation.capitalize}", self[index])
|
|
75
|
-
.execute(
|
|
76
|
-
index: index % @bpi,
|
|
77
|
-
size: @bpi
|
|
78
|
-
)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def operate_bit_at!(operation, index)
|
|
82
|
-
self[index] = operate_bit_at(operation, index)
|
|
75
|
+
def update_items_size!(new_size)
|
|
76
|
+
@array = update_items_size(@array, new_size, @bpi)
|
|
83
77
|
end
|
|
84
78
|
|
|
85
|
-
def update_items_size
|
|
86
|
-
|
|
79
|
+
def update_items_size(array, new_size, bpi)
|
|
80
|
+
if new_size > bpi
|
|
81
|
+
increase_items_size(array, new_size, bpi)
|
|
82
|
+
else
|
|
83
|
+
decrease_items_size(array, new_size, bpi)
|
|
84
|
+
end
|
|
87
85
|
end
|
|
88
86
|
|
|
89
87
|
def append_bits(item, bpi, addend)
|
|
90
|
-
|
|
91
|
-
offset: bpi,
|
|
92
|
-
value: addend
|
|
93
|
-
)
|
|
88
|
+
(item << bpi) | addend
|
|
94
89
|
end
|
|
95
90
|
|
|
96
91
|
def increase_items_size(array, new_size, bpi)
|
|
@@ -113,10 +108,6 @@ class Bitary
|
|
|
113
108
|
res
|
|
114
109
|
end
|
|
115
110
|
|
|
116
|
-
def increase_items_size!(value)
|
|
117
|
-
@array = increase_items_size(@array, value, @bpi)
|
|
118
|
-
end
|
|
119
|
-
|
|
120
111
|
def decrease_items_size(array, new_size, bpi)
|
|
121
112
|
array.each_with_object([]) do |item, acc|
|
|
122
113
|
explode_item(item, new_size, bpi) do |new_item|
|
|
@@ -125,10 +116,6 @@ class Bitary
|
|
|
125
116
|
end
|
|
126
117
|
end
|
|
127
118
|
|
|
128
|
-
def decrease_items_size!(value)
|
|
129
|
-
@array = decrease_items_size(@array, value, @bpi)
|
|
130
|
-
end
|
|
131
|
-
|
|
132
119
|
def explode_item(item, new_size, bpi)
|
|
133
120
|
mask = (2**new_size) - 1
|
|
134
121
|
(bpi / new_size).times do |i|
|
data/lib/bitary/version.rb
CHANGED
data/lib/bitary.rb
CHANGED
|
@@ -2,11 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'bitary/size'
|
|
4
4
|
require_relative 'bitary/version'
|
|
5
|
-
require_relative 'bitary/handler'
|
|
6
|
-
require_relative 'bitary/factory'
|
|
7
5
|
require_relative 'bitary/bitwarr'
|
|
8
|
-
require_relative 'bitary/decorator'
|
|
9
|
-
require_relative 'bitary/mapper'
|
|
10
6
|
|
|
11
7
|
class Bitary
|
|
12
8
|
include Size
|
|
@@ -15,7 +11,7 @@ class Bitary
|
|
|
15
11
|
check_initial_data(initial_data)
|
|
16
12
|
check_bpi(bpi)
|
|
17
13
|
|
|
18
|
-
@bitwarr =
|
|
14
|
+
@bitwarr = Bitwarr.new(initial_data, bpi:)
|
|
19
15
|
end
|
|
20
16
|
|
|
21
17
|
def [](index)
|
|
@@ -27,7 +23,7 @@ class Bitary
|
|
|
27
23
|
def []=(index, value)
|
|
28
24
|
check_bit_index(index)
|
|
29
25
|
|
|
30
|
-
case
|
|
26
|
+
case obj_to_bit(value)
|
|
31
27
|
when 0 then @bitwarr.unbit_at!(index)
|
|
32
28
|
else @bitwarr.bit_at!(index)
|
|
33
29
|
end
|
|
@@ -82,5 +78,20 @@ class Bitary
|
|
|
82
78
|
raise IndexError if bit_index.negative? || bit_index >= @bitwarr.bitsize
|
|
83
79
|
end
|
|
84
80
|
|
|
81
|
+
def obj_to_bit(value)
|
|
82
|
+
case !!value
|
|
83
|
+
when true then truthy_to_bit(value)
|
|
84
|
+
when false then 0
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def truthy_to_bit(value)
|
|
89
|
+
value.is_a?(Integer) ? int_to_bit(value) : 1
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def int_to_bit(value)
|
|
93
|
+
value.zero? ? 0 : 1
|
|
94
|
+
end
|
|
95
|
+
|
|
85
96
|
alias at []
|
|
86
97
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
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.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Maximilien Ballesteros
|
|
@@ -25,18 +25,6 @@ files:
|
|
|
25
25
|
- Rakefile
|
|
26
26
|
- lib/bitary.rb
|
|
27
27
|
- lib/bitary/bitwarr.rb
|
|
28
|
-
- lib/bitary/decorator.rb
|
|
29
|
-
- lib/bitary/decorator/single_method.rb
|
|
30
|
-
- lib/bitary/decorator/single_method/non_nil_enforcer.rb
|
|
31
|
-
- lib/bitary/factory.rb
|
|
32
|
-
- lib/bitary/handler.rb
|
|
33
|
-
- lib/bitary/handler/append.rb
|
|
34
|
-
- lib/bitary/handler/get.rb
|
|
35
|
-
- lib/bitary/handler/set.rb
|
|
36
|
-
- lib/bitary/handler/unset.rb
|
|
37
|
-
- lib/bitary/mapper.rb
|
|
38
|
-
- lib/bitary/mapper/int_to_bit.rb
|
|
39
|
-
- lib/bitary/mapper/obj_to_bit.rb
|
|
40
28
|
- lib/bitary/size.rb
|
|
41
29
|
- lib/bitary/version.rb
|
|
42
30
|
- sig/bitary.rbs
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Bitary
|
|
4
|
-
class Decorator
|
|
5
|
-
class SingleMethod < Bitary::Decorator
|
|
6
|
-
class NonNilEnforcer < Bitary::Decorator::SingleMethod
|
|
7
|
-
protected
|
|
8
|
-
|
|
9
|
-
def postcall(resp)
|
|
10
|
-
(resp.nil? and raise TypeError) || resp
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
end
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative 'single_method/non_nil_enforcer'
|
|
4
|
-
|
|
5
|
-
class Bitary
|
|
6
|
-
class Decorator
|
|
7
|
-
class SingleMethod < Bitary::Decorator
|
|
8
|
-
def initialize(wrappee, method)
|
|
9
|
-
super(wrappee) { |meth| meth == method }
|
|
10
|
-
check_method(wrappee, method)
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
private
|
|
14
|
-
|
|
15
|
-
def check_method(wrappee, method)
|
|
16
|
-
raise ArgumentError unless method.is_a?(Symbol)
|
|
17
|
-
raise NoMethodError unless wrappee.respond_to?(method)
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
data/lib/bitary/decorator.rb
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative 'decorator/single_method'
|
|
4
|
-
|
|
5
|
-
class Bitary
|
|
6
|
-
class Decorator
|
|
7
|
-
def initialize(wrappee, &proc)
|
|
8
|
-
@wrappee = wrappee
|
|
9
|
-
@predicate = proc || ->(_method) { true }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def method_missing(method, *, **, &)
|
|
13
|
-
if @wrappee.respond_to?(method)
|
|
14
|
-
if @predicate.call(method)
|
|
15
|
-
args, kwargs = precall(method, *, **)
|
|
16
|
-
resp = @wrappee.send(method, *args, **kwargs, &)
|
|
17
|
-
postcall(resp)
|
|
18
|
-
else
|
|
19
|
-
@wrappee.send(method, *, **, &)
|
|
20
|
-
end
|
|
21
|
-
else
|
|
22
|
-
super
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def respond_to_missing?(method, include_all = false)
|
|
27
|
-
@wrappee.respond_to?(method, include_all) || super
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def wrappee
|
|
31
|
-
res = @wrappee
|
|
32
|
-
res = res.wrappee while res.respond_to?(:wrappee)
|
|
33
|
-
res
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
protected
|
|
37
|
-
|
|
38
|
-
def precall(_method, *args, **kwargs)
|
|
39
|
-
[args, kwargs]
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def postcall(resp)
|
|
43
|
-
resp
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
data/lib/bitary/factory.rb
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Bitary
|
|
4
|
-
class Factory
|
|
5
|
-
private_class_method :new
|
|
6
|
-
@memo = {}
|
|
7
|
-
|
|
8
|
-
def self.make(name, *, **)
|
|
9
|
-
raise ArgumentError unless name.is_a?(String)
|
|
10
|
-
|
|
11
|
-
name.split('::').reduce(Bitary) do |cls, str|
|
|
12
|
-
cls.const_get(str)
|
|
13
|
-
end.new(*, **)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def self.make_memo(name, *args, **kwargs)
|
|
17
|
-
raise ArgumentError unless name.is_a?(String)
|
|
18
|
-
if @memo.key?(name.to_sym) && !args.empty? && !kwargs.empty?
|
|
19
|
-
raise ArgumentError
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
@memo[name.to_sym] ||= make(name, *args, **kwargs)
|
|
23
|
-
end
|
|
24
|
-
end
|
|
25
|
-
end
|
data/lib/bitary/handler/get.rb
DELETED
data/lib/bitary/handler/set.rb
DELETED
data/lib/bitary/handler/unset.rb
DELETED
data/lib/bitary/handler.rb
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative 'handler/set'
|
|
4
|
-
require_relative 'handler/unset'
|
|
5
|
-
require_relative 'handler/get'
|
|
6
|
-
require_relative 'handler/append'
|
|
7
|
-
|
|
8
|
-
class Bitary
|
|
9
|
-
class Handler
|
|
10
|
-
attr_reader :value
|
|
11
|
-
|
|
12
|
-
def initialize(value)
|
|
13
|
-
@value = value
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def execute(**kwargs)
|
|
17
|
-
raise NotImplementedError
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
data/lib/bitary/mapper.rb
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative 'mapper/int_to_bit'
|
|
4
|
-
require_relative 'mapper/obj_to_bit'
|
|
5
|
-
|
|
6
|
-
class Bitary
|
|
7
|
-
class Mapper
|
|
8
|
-
def self.new(*arg, **kwargs)
|
|
9
|
-
Decorator::SingleMethod::NonNilEnforcer.new(super, :map)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def map(value)
|
|
13
|
-
raise NotImplementedError
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
end
|