Class: Xbd::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/xbd/xbd_dictionary.rb

Overview

********************************* Xbd::Dictionary ********************************* Consists of:

@hash:  a map from values to IDs and IDs to values
@array: a list of values; their indexes == their IDs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_values = []) ⇒ Dictionary

Returns a new instance of Dictionary.



12
13
14
15
16
# File 'lib/xbd/xbd_dictionary.rb', line 12

def initialize(initial_values=[])
  @hash={}
  @array=[]
  initial_values.each {|v| add v, false}
end

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



10
11
12
# File 'lib/xbd/xbd_dictionary.rb', line 10

def array
  @array
end

#hashObject (readonly)

Returns the value of attribute hash.



10
11
12
# File 'lib/xbd/xbd_dictionary.rb', line 10

def hash
  @hash
end

Class Method Details

.parse(source, index = 0) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/xbd/xbd_dictionary.rb', line 38

def Dictionary.parse(source,index=0)
  encoded_dictionary, index = source.read_asi_string index
  encoded_dictionary = StringIO.new(encoded_dictionary)
  num_entries = encoded_dictionary.read_asi
  lengths = num_entries.times.collect {encoded_dictionary.read_asi}
  strings = lengths.collect {|len| encoded_dictionary.read len}
  [Dictionary.new(strings), index]
end

.sanitize_string(str) ⇒ Object



23
24
25
26
27
28
# File 'lib/xbd/xbd_dictionary.rb', line 23

def Dictionary.sanitize_string(str)
  case str
  when String then  "#{str}".force_encoding("BINARY")
  else              str.to_s.force_encoding("BINARY")
  end
end

Instance Method Details

#<<(str) ⇒ Object

add a String to the dictionary



31
# File 'lib/xbd/xbd_dictionary.rb', line 31

def <<(str) add str end

#[](i) ⇒ Object

return String given an ID, or ID given a String



21
# File 'lib/xbd/xbd_dictionary.rb', line 21

def [](i) @hash[i] end

#lengthObject



18
# File 'lib/xbd/xbd_dictionary.rb', line 18

def length; @array.length end

#to_binaryObject

convert to binary string



34
35
36
# File 'lib/xbd/xbd_dictionary.rb', line 34

def to_binary
  [@array.length.to_asi, @array.collect{|v| v.length.to_asi}, @array].join.to_asi_string
end