bitfield-rb 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/bitfield.rb +86 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ec149110915b30e5f2a3f8f2ff5729961fb1bf7ea64265dbb655c9063be8108e
4
+ data.tar.gz: 760e0eb296542267085255ed973246652c1baac8561cde7bb66b77cc7ce9b53a
5
+ SHA512:
6
+ metadata.gz: 618a8407480299dbb1da19f30c73a4d3d1d427aa28d79af47ba37bfb9f39af47e7f8aa038b5902f8eff63cc2e17b0077dc2d1e803851f2acb8b51a4f69e4f859
7
+ data.tar.gz: 321cabf3ab4d97dadfb7e077bc963b453fcf6356c56ff72999a7ff8b69f86ca2851bea352650e879ac923283b4b2a810395cca8c93bc2588931f44af2c24d905
data/lib/bitfield.rb ADDED
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A BitField is a small 8-bit object whose bits can be modified and retrieved. It serves as a (hopefully) reliable, memory efficient, constrained collection of flags.
4
+ class BitField
5
+
6
+ # Constructs a new BitField
7
+ # @return [BitField] the constructed BitField.
8
+ def initialize
9
+ @source = [0xff].pack('C')
10
+ end
11
+
12
+ # Toggles the bit at the specified index.
13
+ # @param bit_idx [Integer] the bit index
14
+ def set(bit_idx)
15
+ current = to_bin_str
16
+ current[bit_idx] = current[bit_idx] == '0' ? '1' : '0'
17
+ @source = [from_bin_rep(current.reverse)].pack('C')
18
+ nil
19
+ end
20
+
21
+ alias toggle set
22
+
23
+ # Retrieves the bit at the specified index
24
+ # @param bit_idx [Integer] the bit index
25
+ # @return [Boolean] bit value
26
+ def get(bit_idx)
27
+ to_bin_rep(to_int).reverse[bit_idx] == '0'
28
+ end
29
+
30
+ alias retrieve get
31
+
32
+ private
33
+
34
+ # Uses {String#unpack} to convert a String type to a 8-bit unsigned char
35
+ # @return [Integer] the integer
36
+ def to_int
37
+ @source.unpack1('C')
38
+ end
39
+
40
+ # Returns a String representation of the binary source.
41
+ # @return [String] the string
42
+ def to_bin_str
43
+ to_bin_rep(to_int).reverse
44
+ end
45
+
46
+ # Returns a binary representation in the form of an array of 1's and 0's in their respective digits.
47
+ # @return [String] the binary representation
48
+ def to_bin_rep(value)
49
+ Kernel.sprintf('%08b', value)
50
+ end
51
+
52
+ # Returns a base 2 integer provided a representation.
53
+ # @param representation [String] the representation
54
+ def from_bin_rep(representation)
55
+ representation.to_i(2)
56
+ end
57
+ end
58
+
59
+ # Copyright (c) 2022, Patrick W.
60
+ # All rights reserved.
61
+ #
62
+ # Redistribution and use in source and binary forms, with or without
63
+ # modification, are permitted provided that the following conditions are met:
64
+ #
65
+ # * Redistributions of source code must retain the above copyright notice, this
66
+ # list of conditions and the following disclaimer.
67
+ #
68
+ # * Redistributions in binary form must reproduce the above copyright notice,
69
+ # this list of conditions and the following disclaimer in the documentation
70
+ # and/or other materials provided with the distribution.
71
+ #
72
+ # * Neither the name of the copyright holder nor the names of its
73
+ # contributors may be used to endorse or promote products derived from
74
+ # this software without specific prior written permission.
75
+ #
76
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
77
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
79
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
80
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
82
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
83
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
84
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
85
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bitfield-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Patrick W.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-03-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: Sickday@pm.me
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/bitfield.rb
20
+ homepage: https://git.repos.pw/Sickday/bitfield-rb
21
+ licenses:
22
+ - BSD-3-Clause
23
+ metadata:
24
+ source_code_uri: https://git.repos.pw/Sickday/bitfield-rb
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.1.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: String-backed BitField object written in Ruby.
44
+ test_files: []