Module: Pacer::Utils::Trie::Vertex

Defined in:
lib/pacer/utils/trie.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) add(array)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pacer/utils/trie.rb', line 50

def add(array)
  found = find_partial(array)
  if found.length == array.length
    result = found.last.in_vertex
  else
    found[array.length] ||= nil
    result = array.zip(found).inject(self) do |vertex, (part, edge)|
      if edge
        edge.in_vertex
      else
        new_vertex = graph.create_vertex :type => 'Trie'
        graph.create_edge nil, vertex, new_vertex, "trie:#{part.to_s}"
        new_vertex
      end
    end
  end
  result[:end] = true
  result.graph = graph
  result.add_extensions [Trie]
end

- (Object) add_word(word)



46
47
48
# File 'lib/pacer/utils/trie.rb', line 46

def add_word(word)
  add word.scan(/./)
end

- (Object) array



84
85
86
# File 'lib/pacer/utils/trie.rb', line 84

def array
  path.e.labels.map { |label| label[/^trie:(.*)$/, 1] }.to_a
end

- (Object) find(array)



23
24
25
26
27
28
29
30
# File 'lib/pacer/utils/trie.rb', line 23

def find(array)
  found = find_partial(array)
  if found.length == array.length
    result = found.last.in_vertex.add_extensions [Trie]
    result.graph = graph
    result
  end
end

- (Object) find_partial(array)



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pacer/utils/trie.rb', line 32

def find_partial(array)
  return [] if array.empty?
  strings = array.map &:to_s
  max_depth = array.length - 1
  found = []
  v.out_e(strings.first).loop { |e| e.in_v.out_e }.while do |e, depth|
    if e.label == "trie:#{strings[depth]}" and depth <= max_depth
      found << e
      :loop
    end
  end.to_a
  found
end

- (Object) find_word(word)



19
20
21
# File 'lib/pacer/utils/trie.rb', line 19

def find_word(word)
  find word.scan(/./)
end

- (Object) path



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pacer/utils/trie.rb', line 71

def path
  result = []
  v.in_e.chain_route(:pipe_class => Pacer::Pipes::LabelPrefixPipe, :pipe_args => 'trie:').loop do |e|
    e.out_v.in_e.chain_route(:pipe_class => Pacer::Pipes::LabelPrefixPipe, :pipe_args => 'trie:')
  end.while do |e, d|
    if e.out_vertex[:type] == 'Trie'
      :emit
    else
      :loop
    end
  end.paths.to_a.first.reverse.to_route(:element_type => :mixed, :graph => graph)
end

- (Object) word



88
89
90
# File 'lib/pacer/utils/trie.rb', line 88

def word
  array.join
end