Class: TrieFile::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/trie-file/node.rb

Constant Summary collapse

CHILD_FIELDS_LENGTH =
6
HEADER_FIELD_LENGTH =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
# File 'lib/trie-file/node.rb', line 11

def initialize(value = nil)
  @value = value
  @children = {}
  @byte_pos = 0
end

Instance Attribute Details

#byte_posObject

Returns the value of attribute byte_pos.



9
10
11
# File 'lib/trie-file/node.rb', line 9

def byte_pos
  @byte_pos
end

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/trie-file/node.rb', line 8

def children
  @children
end

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/trie-file/node.rb', line 9

def value
  @value
end

Instance Method Details

#add_child(char, node) ⇒ Object



25
26
27
# File 'lib/trie-file/node.rb', line 25

def add_child(char, node)
  @children[char] = node
end

#bytesizeObject



29
30
31
32
# File 'lib/trie-file/node.rb', line 29

def bytesize
  # add some constants here
  HEADER_FIELD_LENGTH + (children.size * CHILD_FIELDS_LENGTH) + value_bytesize
end

#child_at(char) ⇒ Object



21
22
23
# File 'lib/trie-file/node.rb', line 21

def child_at(char)
  children[char]
end

#has_child?(char) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/trie-file/node.rb', line 17

def has_child?(char)
  children.include?(char)
end

#value_bytesObject



38
39
40
# File 'lib/trie-file/node.rb', line 38

def value_bytes
  value ? value.bytes : []
end

#value_bytesizeObject



34
35
36
# File 'lib/trie-file/node.rb', line 34

def value_bytesize
  value ? value.bytesize : 0
end