bitary 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +6 -4
- data/lib/bitary/deptainer.rb +27 -0
- data/lib/bitary/factory.rb +27 -0
- data/lib/bitary/handler.rb +3 -0
- data/lib/bitary/version.rb +1 -1
- data/lib/bitary.rb +10 -4
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc3397b948d902fe5cba56205955a01b7ad9f0d5d6eb7a1bf74984005f693114
|
4
|
+
data.tar.gz: af6673d27575273094dc34a4c67c67fbaa603de8a4e04375b0719e247f8cbcd9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e44836498539b643e9b75ead2d63c9b91beda96008bc11d5e10a3b354d67ad4f604b1ceee4740665c60a7eeca7ca898e3f05b22aa48d2845f10fa71914a22998
|
7
|
+
data.tar.gz: 2cc01a7ba3ab7463ab64283fb6e4f82ab8dab61865595f93bbea1efdbf966463008faf2d9dab42f8e304e1f9e6bfa57d597b7d1826ef8521e7f480d2749403f1
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Ruby-based implementation of the bit array data structure.
|
4
4
|
|
5
|
-
It's still under development, but as of now, it implements simple and well-optimized logic allowing you to set, unset and retrieve bits.
|
5
|
+
It's still under development, but as of now, it implements simple and well-optimized logic allowing you to set, unset and retrieve bits (as well as some extra features, see below).
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -24,10 +24,12 @@ Documentation still needs to be written, but here is a breakdown
|
|
24
24
|
of the main capabilities brought by the current bit array implementation:
|
25
25
|
|
26
26
|
```ruby
|
27
|
+
require 'bitary'
|
28
|
+
|
27
29
|
bit_array_sz = Bitary.new(128) # give an explicit size. Defaults to 64 bits used per item
|
28
30
|
bit_array_ar = Bitary.new(
|
29
31
|
[255, 10, 20],
|
30
|
-
bits_per_item: 8
|
32
|
+
bits_per_item: Bitary::Size::BYTE # 8 bits
|
31
33
|
) # create based on some integer array
|
32
34
|
|
33
35
|
bit_array_sz.bits_per_item # 64
|
@@ -55,13 +57,13 @@ bit_array_ar.to_a # [127, 10, 20]
|
|
55
57
|
bit_array_ar.to_s # "01111111 00001010 00010100"
|
56
58
|
|
57
59
|
# increase/decrease bits used per item
|
58
|
-
bit_array_ar.bits_per_item = 64
|
60
|
+
bit_array_ar.bits_per_item = Bitary::Size::LONG # 64 bits
|
59
61
|
bit_array_ar.to_a # [8_325_652]
|
60
62
|
bit_array_ar.to_s # "0000000000000000000000000000000000000000011111110000101000010100"
|
61
63
|
|
62
64
|
bit_array_sz.bits_per_item # 64
|
63
65
|
bit_array_sz.to_a # [1_099_511_627_776, 0]
|
64
|
-
bit_array_sz.bits_per_item = 32
|
66
|
+
bit_array_sz.bits_per_item = Bitary::Size::INT # 32 bits
|
65
67
|
bit_array_sz.to_a # [256, 0, 0, 0]
|
66
68
|
```
|
67
69
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Bitary
|
4
|
+
class Deptainer
|
5
|
+
def initialize
|
6
|
+
@store = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](key)
|
10
|
+
raise ArgumentError unless key.is_a?(Symbol)
|
11
|
+
|
12
|
+
@store[key]
|
13
|
+
end
|
14
|
+
|
15
|
+
def []=(key, value)
|
16
|
+
raise ArgumentError unless key.is_a?(Symbol)
|
17
|
+
|
18
|
+
@store[key] = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def has?(key)
|
22
|
+
raise ArgumentError unless key.is_a?(Symbol)
|
23
|
+
|
24
|
+
@store.key?(key)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'deptainer'
|
4
|
+
|
5
|
+
class Bitary
|
6
|
+
class Factory
|
7
|
+
private_class_method :new
|
8
|
+
@container = Deptainer.new
|
9
|
+
|
10
|
+
def self.make(name, *, **)
|
11
|
+
raise ArgumentError unless name.is_a?(String)
|
12
|
+
|
13
|
+
name.split('::').reduce(Bitary) do |cls, str|
|
14
|
+
cls.const_get(str)
|
15
|
+
end.new(*, **)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.make_memo(name, *args, **kwargs)
|
19
|
+
raise ArgumentError unless name.is_a?(String)
|
20
|
+
if @container.has?(name.to_sym) && !args.empty? && !kwargs.empty?
|
21
|
+
raise ArgumentError
|
22
|
+
end
|
23
|
+
|
24
|
+
@container[name.to_sym] ||= make(name, *args, **kwargs)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/bitary/handler.rb
CHANGED
data/lib/bitary/version.rb
CHANGED
data/lib/bitary.rb
CHANGED
@@ -2,6 +2,9 @@
|
|
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
|
+
require_relative 'bitary/deptainer'
|
5
8
|
|
6
9
|
class Bitary
|
7
10
|
include Size
|
@@ -23,7 +26,7 @@ class Bitary
|
|
23
26
|
item_bit_size = compute_item_bit_size(item_index)
|
24
27
|
item = @internal_array[item_index]
|
25
28
|
|
26
|
-
Handler::Get
|
29
|
+
Factory.make('Handler::Get', item).execute(
|
27
30
|
index: index % @bits_per_item,
|
28
31
|
size: item_bit_size
|
29
32
|
)
|
@@ -39,12 +42,12 @@ class Bitary
|
|
39
42
|
|
40
43
|
@internal_array[item_index] =
|
41
44
|
if bit == 1
|
42
|
-
Handler::Set
|
45
|
+
Factory.make('Handler::Set', item).execute(
|
43
46
|
index: index % @bits_per_item,
|
44
47
|
size: item_bit_size
|
45
48
|
)
|
46
49
|
else
|
47
|
-
Handler::Unset
|
50
|
+
Factory.make('Handler::Unset', item).execute(
|
48
51
|
index: index % @bits_per_item,
|
49
52
|
size: item_bit_size
|
50
53
|
)
|
@@ -136,7 +139,10 @@ class Bitary
|
|
136
139
|
processed_bits = 0
|
137
140
|
end
|
138
141
|
|
139
|
-
acc[-1] = Handler::Append
|
142
|
+
acc[-1] = Factory.make('Handler::Append', acc[-1]).execute(
|
143
|
+
offset:,
|
144
|
+
value:
|
145
|
+
)
|
140
146
|
processed_bits += bpi
|
141
147
|
end
|
142
148
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maximilien Ballesteros
|
@@ -24,6 +24,8 @@ files:
|
|
24
24
|
- README.md
|
25
25
|
- Rakefile
|
26
26
|
- lib/bitary.rb
|
27
|
+
- lib/bitary/deptainer.rb
|
28
|
+
- lib/bitary/factory.rb
|
27
29
|
- lib/bitary/handler.rb
|
28
30
|
- lib/bitary/handler/append.rb
|
29
31
|
- lib/bitary/handler/get.rb
|