Sha256: 838d64bd587996f7586700a9a92012600814f10cc708c2b756f3da117cf07944

Contents?: true

Size: 845 Bytes

Versions: 1

Compression:

Stored size: 845 Bytes

Contents

module Rambling
  class Trie < TrieNode
    def initialize(filename = nil)
      super(nil)

      @filename = filename
      @is_compressed = false
      add_all_nodes if filename
    end

    def compress!
      unless compressed?
        compress_own_tree!
        @is_compressed = true
      end

      self
    end

    def compressed?
      @is_compressed = @is_compressed.nil? ? false : @is_compressed
    end

    def has_branch_for?(word = '')
      compressed? ? has_compressed_branch_for?(word) : has_uncompressed_branch_for?(word)
    end

    def is_word?(word = '')
      compressed? ? is_compressed_word?(word) : is_uncompressed_word?(word)
    end

    private
    def add_all_nodes
      File.open(@filename) do |file|
        while word = file.gets
          add_branch_from(word.chomp)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rambling-trie-0.3.2 ./lib/trie.rb