Class: Rambling::Trie::CompressedNode

Inherits:
Node
  • Object
show all
Defined in:
lib/rambling/trie/compressed_node.rb

Overview

A representation of a node in an compressed trie data structure.

Instance Attribute Summary

Attributes inherited from Node

#children_tree, #letter, #parent

Instance Method Summary collapse

Methods inherited from Node

#children, #first_child, #initialize, #match_prefix, #root?, #terminal!, #terminal?

Methods included from Inspectable

#inspect

Methods included from Stringifyable

#as_word, #to_s

Methods included from Comparable

#==

Methods included from Enumerable

#each

Methods included from Compressable

#compressable?

Constructor Details

This class inherits a constructor from Rambling::Trie::Node

Instance Method Details

#add(word) ⇒ nil

Always raises InvalidOperation when trying to add a word to the current compressed trie node

Parameters:

  • word (String)

    the word to add to the trie.

Returns:

  • (nil)

    this never returns as it always raises an exception.

Raises:


10
11
12
# File 'lib/rambling/trie/compressed_node.rb', line 10

def add word
  raise Rambling::Trie::InvalidOperation, 'Cannot add word to compressed trie'
end

#compressed?Boolean

Always return `true` for a compressed node.

Returns:

  • (Boolean)

    always `true` for a compressed node.


39
40
41
# File 'lib/rambling/trie/compressed_node.rb', line 39

def compressed?
  true
end

#partial_word?(chars) ⇒ Boolean

Checks if a path for set a of characters exists in the trie.

Parameters:

  • chars (Array<String>)

    the characters to look for in the trie.

Returns:

  • (Boolean)

    `true` if the characters are found, `false` otherwise.


17
18
19
# File 'lib/rambling/trie/compressed_node.rb', line 17

def partial_word? chars
  chars.empty? || has_partial_word?(chars)
end

#scan(chars) ⇒ Node

Returns the node that starts with the specified characters.

Parameters:

  • chars (Array<String>)

    the characters to look for in the trie.

Returns:

  • (Node)

    the node that matches the specified characters. MissingNode when not found.


33
34
35
# File 'lib/rambling/trie/compressed_node.rb', line 33

def scan chars
  chars.empty? ? self : closest_node(chars)
end

#word?(chars) ⇒ Boolean

Checks if a path for set of characters represents a word in the trie.

Parameters:

  • chars (Array<String>)

    the characters to look for in the trie.

Returns:

  • (Boolean)

    `true` if the characters are found and form a word, `false` otherwise.


25
26
27
# File 'lib/rambling/trie/compressed_node.rb', line 25

def word? chars
  chars.empty? ? terminal? : has_word?(chars)
end