class Canoser::HashT

Attributes

ktype[RW]
vtype[RW]

Public Class Methods

new(ktype=DEFAULT_KV, vtype=DEFAULT_KV) click to toggle source
# File lib/canoser/field.rb, line 141
def initialize(ktype=DEFAULT_KV, vtype=DEFAULT_KV)
  @ktype = ktype || DEFAULT_KV
  @vtype = vtype || DEFAULT_KV
end

Public Instance Methods

==(other) click to toggle source
# File lib/canoser/field.rb, line 171
def ==(other)
  return self.ktype == other.ktype && self.vtype == other.vtype
end
decode(cursor) click to toggle source
# File lib/canoser/field.rb, line 160
def decode(cursor)
  hash = {}
  len = Uint32.decode(cursor)
  len.times do
    k = ktype.decode(cursor)
    v = vtype.decode(cursor)
    hash[k] = v
  end
  hash
end
encode(hash) click to toggle source
# File lib/canoser/field.rb, line 146
def encode(hash)
  output = ""
  output << Uint32.encode(hash.size)
  sorted_map = {}
  hash.each do |k, v|
    sorted_map[ktype.encode(k)] = vtype.encode(v)
  end
  sorted_map.keys.sort.each do |k|
    output << k
    output << sorted_map[k]
  end
  output
end