Class: UnicodeNamecode::Trie

Inherits:
Object
  • Object
show all
Defined in:
lib/unicode_namecode/trie.rb

Overview

Trie (prefix tree) for efficient Unicode name lookups

Instance Method Summary collapse

Constructor Details

#initializeTrie

Returns a new instance of Trie.



6
7
8
# File 'lib/unicode_namecode/trie.rb', line 6

def initialize
  @root = TrieNode.new
end

Instance Method Details

#find(name) ⇒ Object

Find the exact codepoint for a Unicode name



22
23
24
25
# File 'lib/unicode_namecode/trie.rb', line 22

def find(name)
  node = find_node(name)
  node&.codepoint
end

#insert(name, codepoint) ⇒ Object

Insert a Unicode name and its codepoint into the Trie



11
12
13
14
15
16
17
18
19
# File 'lib/unicode_namecode/trie.rb', line 11

def insert(name, codepoint)
  node = @root
  name.each_char do |char|
    node.children[char] ||= TrieNode.new
    node = node.children[char]
  end
  node.codepoint = codepoint
  node.is_end = true
end

#prefix_search(prefix, limit = 100) ⇒ Object

Find all Unicode names that start with the given prefix



28
29
30
31
32
33
34
35
# File 'lib/unicode_namecode/trie.rb', line 28

def prefix_search(prefix, limit = 100)
  node = find_node(prefix)
  return [] unless node
  
  results = []
  collect_words(node, prefix, results, limit)
  results
end