Class: Classbench::Tier

Inherits:
Object
  • Object
show all
Defined in:
lib/classbench/tier.rb

Overview

Class for representation of a n-ary prefix tree - trie.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rootObject

Returns the value of attribute root.



5
6
7
# File 'lib/classbench/tier.rb', line 5

def root
  @root
end

Instance Method Details

#get_statsObject

Erase not implemented/neccessary



36
37
38
39
40
# File 'lib/classbench/tier.rb', line 36

def get_stats
  return if not root

  root.compute_weights
end

#insert(prefix) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/classbench/tier.rb', line 7

def insert(prefix)
  self.root = TierNode.new(0) if not root

  # Empty prefix
  if prefix.size == 0
    root.increment_prefixes
    return
  end

  current_node = self.root
  next_node = nil

  # For each char
  prefix.split('').each_with_index do |ch, i|
    next_node = current_node.subtree[ch]

    if next_node.nil?
      next_node = TierNode.new(i+1)
      current_node.subtree[ch] = next_node
    end

    current_node = next_node
  end

  current_node.increment_prefixes
end