Class: Dcha::MPT::NibbleKey
- Inherits:
-
Array
- Object
- Array
- Dcha::MPT::NibbleKey
- Defined in:
- lib/dcha/mpt/nibble_key.rb
Overview
:nodoc:
Constant Summary collapse
- TERM_FLAG =
0b0010
- ODD_FLAG =
0b0001
- TERMINATOR =
16- HEX_VALUES =
Hash[(0..15).map { |i| [i.to_s(16), i]
Class Method Summary collapse
- .decode(bytes) ⇒ Object
- .encode(nibbles) ⇒ Object
- .from_string(s) ⇒ Object
- .terminator ⇒ Object
- .to_string(nibbles) ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #common_prefix(another_key) ⇒ Object
- #encode ⇒ Object
- #prefix?(another_key) ⇒ Boolean
- #terminate(flag) ⇒ Object
- #terminate? ⇒ Boolean
- #to_s ⇒ Object (also: #to_string)
Class Method Details
.decode(bytes) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/dcha/mpt/nibble_key.rb', line 26 def decode(bytes) o = from_string bytes flags = o[0] o.push TERMINATOR if flags & TERM_FLAG > 0 fill = flags & ODD_FLAG > 0 ? 1 : 2 new o[fill..-1] end |
.encode(nibbles) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/dcha/mpt/nibble_key.rb', line 12 def encode(nibbles) flags = 0 if nibbles.last == TERMINATOR flags |= TERM_FLAG nibbles = nibbles[0...-1] end odd = nibbles.size % 2 flags |= odd return pack [flags] + nibbles if odd == 1 pack [flags, 0b0000] + nibbles end |
.from_string(s) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/dcha/mpt/nibble_key.rb', line 35 def from_string(s) nibbles = s.unpack('H*') .first .each_char .map { |c| HEX_VALUES[c] } new nibbles end |
.terminator ⇒ Object
51 52 53 |
# File 'lib/dcha/mpt/nibble_key.rb', line 51 def terminator new [TERMINATOR] end |
.to_string(nibbles) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/dcha/mpt/nibble_key.rb', line 43 def to_string(nibbles) # TODO: Add error message raise ArgumentError unless nibbles.any? { |x| x.between?(0, 15) } raise ArgumentError if nibbles.size.odd? pack nibbles end |
Instance Method Details
#+(other) ⇒ Object
105 106 107 |
# File 'lib/dcha/mpt/nibble_key.rb', line 105 def +(other) self.class.new super(other) end |
#common_prefix(another_key) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dcha/mpt/nibble_key.rb', line 85 def common_prefix(another_key) prefix = [] [size, another_key.size].min.times do |i| break if self[i] != another_key[i] prefix.push self[i] end self.class.new prefix end |
#encode ⇒ Object
96 97 98 |
# File 'lib/dcha/mpt/nibble_key.rb', line 96 def encode self.class.encode self end |
#prefix?(another_key) ⇒ Boolean
80 81 82 83 |
# File 'lib/dcha/mpt/nibble_key.rb', line 80 def prefix?(another_key) return false if another_key.size < size another_key.take(size) == self end |
#terminate(flag) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/dcha/mpt/nibble_key.rb', line 70 def terminate(flag) dup.tap do |copy| if flag copy.push TERMINATOR unless copy.terminate? elsif copy.terminate? copy.pop end end end |
#terminate? ⇒ Boolean
66 67 68 |
# File 'lib/dcha/mpt/nibble_key.rb', line 66 def terminate? last == TERMINATOR end |
#to_s ⇒ Object Also known as: to_string
100 101 102 |
# File 'lib/dcha/mpt/nibble_key.rb', line 100 def to_s self.class.to_string self end |