Class: Stribog::ByteVector
- Inherits:
-
Object
- Object
- Stribog::ByteVector
- Includes:
- Enumerable
- Defined in:
- lib/stribog/byte_vector.rb
Overview
byte_array
Instance Attribute Summary collapse
-
#vector ⇒ Object
readonly
Returns the value of attribute vector.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #[](index) ⇒ Object
- #^(other) ⇒ Object
- #addition(size: 64) ⇒ Object
- #bit64 ⇒ Object
- #byte8 ⇒ Object
- #each ⇒ Object
-
#initialize(vector) ⇒ ByteVector
constructor
A new instance of ByteVector.
- #padding(size: 64) ⇒ Object
- #reverse ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object
- #to_dec ⇒ Object
- #to_hex ⇒ Object
- #to_s ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(vector) ⇒ ByteVector
Returns a new instance of ByteVector.
22 23 24 |
# File 'lib/stribog/byte_vector.rb', line 22 def initialize(vector) @vector = vector end |
Instance Attribute Details
#vector ⇒ Object (readonly)
Returns the value of attribute vector.
8 9 10 |
# File 'lib/stribog/byte_vector.rb', line 8 def vector @vector end |
Class Method Details
.convert(vector) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/stribog/byte_vector.rb', line 10 def self.convert(vector) case vector when String new(vector.unpack('C*')) when Numeric # TODO: REFACTOR bin = vector.to_s(2) size = 2**Math.log2(vector.size * 8).ceil new(['0' * (size - bin.size) + bin].pack('B*').unpack('C*')) end end |
Instance Method Details
#+(other) ⇒ Object
32 33 34 |
# File 'lib/stribog/byte_vector.rb', line 32 def +(other) self.class.new vector + other.to_a end |
#[](index) ⇒ Object
73 74 75 |
# File 'lib/stribog/byte_vector.rb', line 73 def [](index) vector[index] end |
#^(other) ⇒ Object
26 27 28 29 30 |
# File 'lib/stribog/byte_vector.rb', line 26 def ^(other) vec = [other, self].sort_by(&:size).reverse.map(&:reverse) self.class.new vec[0].map .with_index { |bit, index| bit ^ (vec[1][index] || 0) }.reverse end |
#addition(size: 64) ⇒ Object
36 37 38 39 |
# File 'lib/stribog/byte_vector.rb', line 36 def addition(size: 64) return self if vector.size >= size self.class.new(Array.new(size - vector.size, 0) + vector) end |
#bit64 ⇒ Object
51 52 53 |
# File 'lib/stribog/byte_vector.rb', line 51 def bit64 @bit64 ||= byte8.map { |b| [b].pack('Q*').unpack('B*') }.flatten end |
#byte8 ⇒ Object
47 48 49 |
# File 'lib/stribog/byte_vector.rb', line 47 def byte8 @byte8 ||= vector.pack('C*').unpack('Q*') end |
#each ⇒ Object
77 78 79 80 81 82 |
# File 'lib/stribog/byte_vector.rb', line 77 def each return vector.each unless block_given? vector.each do |v| yield(v) end end |
#padding(size: 64) ⇒ Object
41 42 43 44 45 |
# File 'lib/stribog/byte_vector.rb', line 41 def padding(size: 64) return self if vector.size >= size # (self.class.new([1]) + vector).addition_by_zeros(size: 512) self.class.new([1] + vector).addition(size: size) end |
#reverse ⇒ Object
92 93 94 |
# File 'lib/stribog/byte_vector.rb', line 92 def reverse self.class.new vector.reverse end |
#size ⇒ Object
69 70 71 |
# File 'lib/stribog/byte_vector.rb', line 69 def size @size ||= vector.size end |
#to_a ⇒ Object
84 85 86 |
# File 'lib/stribog/byte_vector.rb', line 84 def to_a vector end |
#to_dec ⇒ Object
55 56 57 58 |
# File 'lib/stribog/byte_vector.rb', line 55 def to_dec # to_s.to_i(2) vector.pack('C*').unpack('B*').first.to_i(2) end |
#to_hex ⇒ Object
60 61 62 |
# File 'lib/stribog/byte_vector.rb', line 60 def to_hex to_s.to_i(2).to_s(16) end |
#to_s ⇒ Object
64 65 66 67 |
# File 'lib/stribog/byte_vector.rb', line 64 def to_s # vector.pack('C*') vector end |
#zero? ⇒ Boolean
88 89 90 |
# File 'lib/stribog/byte_vector.rb', line 88 def zero? vector.any?(&:zero?) end |