Sha256: 8ad53e4b9a4b62d70ce4a3cf8064b3c8114349c3443043e4016d8f1ee68bb8bc

Contents?: true

Size: 761 Bytes

Versions: 13

Compression:

Stored size: 761 Bytes

Contents

require File.join(File.dirname(__FILE__), 'tree')

# A Trie[http://en.wikipedia.org/wiki/Trie] is an associative access tree structure.
#
# @example
#   trie = Trie.new
#   trie[:a, :b] = 1
#   trie[:a, :b] #=> 1
#   trie[:a, :c] #=> nil
class Trie
  # Creates a new empty Trie.
  def initialize
    @tree = Tree.new
  end

  # @return the value at the given trie path
  def [](*path)
    tree = @tree[nil, *path]
    tree.value if tree
  end

  # @return the top_level Tree for this trie
  def to_tree
    @tree
  end

  # Sets the value for a node path.
  #
  # @example
  #   trie = Trie.new
  #   trie[:a, :b] = 1
  #   trie[:a, :b] #=> 1
  def []=(*path_and_value)
    value = path_and_value.pop
    @tree.fill(nil, *path_and_value).value = value
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
caruby-core-1.5.5 lib/caruby/util/trie.rb
caruby-core-1.5.4 lib/caruby/util/trie.rb
caruby-core-1.5.3 lib/caruby/util/trie.rb
caruby-core-1.5.2 lib/caruby/util/trie.rb
caruby-core-1.5.1 lib/caruby/util/trie.rb
caruby-core-1.4.9 lib/caruby/util/trie.rb
caruby-core-1.4.7 lib/caruby/util/trie.rb
caruby-core-1.4.6 lib/caruby/util/trie.rb
caruby-core-1.4.5 lib/caruby/util/trie.rb
caruby-core-1.4.4 lib/caruby/util/trie.rb
caruby-core-1.4.3 lib/caruby/util/trie.rb
caruby-core-1.4.2 lib/caruby/util/trie.rb
caruby-core-1.4.1 lib/caruby/util/trie.rb