class Andromeda::Impl::XorId
Generator for random xorable ids (used for markers)
Generator for random xorable ids (used for markers)
Public Class Methods
new(len, random = true, init_data = nil)
click to toggle source
# File lib/andromeda/impl/id.rb, line 11 def initialize(len, random = true, init_data = nil) raise ArgumentError unless len.kind_of?(Fixnum) raise ArgumentError unless len >= 0 @data = if init_data init_data else if random then len.times.map { Id.rnd_byte } else len.times.map { 0 } end end end
Private Class Methods
rnd_byte()
click to toggle source
# File lib/andromeda/impl/id.rb, line 76 def self.rnd_byte ; Random.rand(256) end
two_char_hex_str(s)
click to toggle source
# File lib/andromeda/impl/id.rb, line 78 def self.two_char_hex_str(s) case s.length when 0 then '00' when 1 then "0#{s}" else s end end
Public Instance Methods
==(b)
click to toggle source
Compare self to b @param [Id] b
# File lib/andromeda/impl/id.rb, line 41 def ==(b) return true if self.equal? b return false if b.nil? return false unless b.class.equal? self.class return false unless same_length? b zip_bytes(b) { |i, j| return false if i != j } true end
[](key)
click to toggle source
# File lib/andromeda/impl/id.rb, line 32 def [](key) ; @data[key] end
clone_to_copy?()
click to toggle source
# File lib/andromeda/impl/id.rb, line 26 def clone_to_copy? ; false end
each() { |this| ... }
click to toggle source
# File lib/andromeda/impl/id.rb, line 36 def each ; this = self ; 0.upto(length-1).each { |i| yield this[i] } end
each_with_index() { |i, this| ... }
click to toggle source
# File lib/andromeda/impl/id.rb, line 37 def each_with_index ; this = self ; 0.upto(length-1).each { |i| yield i, this[i] } end
hash()
click to toggle source
# File lib/andromeda/impl/id.rb, line 50 def hash ; @data.hash end
identical_copy()
click to toggle source
# File lib/andromeda/impl/id.rb, line 27 def identical_copy ; self end
inspect()
click to toggle source
# File lib/andromeda/impl/id.rb, line 67 def inspect ; to_s end
length()
click to toggle source
# File lib/andromeda/impl/id.rb, line 29 def length ; @data.length end
same_length?(obj)
click to toggle source
# File lib/andromeda/impl/id.rb, line 34 def same_length?(obj) ; self.length == obj.length end
to_short_s()
click to toggle source
# File lib/andromeda/impl/id.rb, line 61 def to_short_s r = '' each { |b| r << Id.two_char_hex_str(b.to_s(16)) } r end
xor(b)
click to toggle source
xor self and b’s ids component-wise @param [Array<Fixnum>] b @return [Id]
# File lib/andromeda/impl/id.rb, line 55 def xor(b) r = [] zip_bytes(b) { |i,j| r << (i ^ j) } Id.new r.length, false, r end
zero?()
click to toggle source
# File lib/andromeda/impl/id.rb, line 30 def zero? ; each { |b| return false unless b == 0 } ; true end
Private Instance Methods
zip_bytes(b) { |a, b| ... }
click to toggle source
# File lib/andromeda/impl/id.rb, line 71 def zip_bytes(b) a = self 0.upto(length-1).each { |i| yield a[i], b[i] } end