class Storage::Storage

Class trie - storage

Attributes

trie[RW]

Public Class Methods

new() click to toggle source
# File lib/trie-storage.rb, line 6
def initialize
  @trie=Node.new
end

Public Instance Methods

add(str) click to toggle source

add str to storage

# File lib/trie-storage.rb, line 11
def add(str)
  raise ArgumentError if !str.match(/^[[:alpha:][,]]+$/)
  words = str.split(',').reject { |c| c.empty? }.each {|word| @trie.add_word(word)}
end
contains?(str) click to toggle source

check contains str in storage

# File lib/trie-storage.rb, line 22
def contains?(str)
  @trie.contains?(str)
end
find(str) click to toggle source

find all words starting from str

# File lib/trie-storage.rb, line 27
def find(str)
  raise ArgumentError if str.length<3
  st = str.dup
  @trie.find([],str,st)
end
load_from_file(filename) click to toggle source

load words in storage from file

# File lib/trie-storage.rb, line 34
def load_from_file(filename)
  lines = IO.readlines(filename)
  lines.map {|line| add(line.strip)}
end
load_from_zip(filename) click to toggle source

load words in storage from zip

# File lib/trie-storage.rb, line 45
def load_from_zip(filename)
  Zip::File.foreach(filename) do |entry|
    istream = entry.get_input_stream
    istream.read.split('\n').each do |line|
      add(line)
    end
  end
end
save_to_file(filename) click to toggle source

save words from storage to file

# File lib/trie-storage.rb, line 40
def save_to_file(filename)
  File.open(filename, 'w+') {|f| f.puts(self)}
end
save_to_zip(filename) click to toggle source

save words from storage to zip

# File lib/trie-storage.rb, line 55
def save_to_zip(filename)
  Zip::File.open(filename, Zip::File::CREATE) {
      |zipfile|
    zipfile.get_output_stream('storage.txt') { |f| f.puts  @trie.get_subtrie_words([], '').join(',') }
  }
end
to_s() click to toggle source

print all trie words

# File lib/trie-storage.rb, line 17
def to_s
  @trie.get_subtrie_words([], '').join(",")
end