trie-storage 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/trie-storage.rb +32 -37
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8a3fd73429cb8c7b95251a46ae2f4b7c15930d8
4
- data.tar.gz: 25bd794fb5d8ca4191708c1962f3942e8e326431
3
+ metadata.gz: 12b94af64a8344e58c57f7c80225ce0f632f59ac
4
+ data.tar.gz: b3898b0c4e2b3bbf202c37835a764dd93caf3b35
5
5
  SHA512:
6
- metadata.gz: 53bf0d8059d6862aef12fd878be5271edc18af53da416377807ac2c2e2cdaed71d62f6cf594091ecbd8f60883f655fc82a5e28cf5ba29f8fdcffc2e6f9fec070
7
- data.tar.gz: b7724ffeede6923581b5a756c1effb168214fdbe45c8d45ce6830ec4b117f5929ae19ee1f929193e2161a7aa4430d5dabc45fd0127b83ae77a253a4e595926a6
6
+ metadata.gz: 13d6f214e24e454622b0be428f0fe1f87d83d00622704a2a60f693641e732e6b4ee56bb25a25f66135d4bddeb1f68974d013050b878d8f77aab2800f4c54312f
7
+ data.tar.gz: 22c629c77ca1d719ecf164f07b1b5d4da04b09d239a8ab43764878d1230b249863ab1776c500aaf7ceeac3f3ddad30c9a1362a1e5a49bd7935892f2426a8cc59
data/lib/trie-storage.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'zip'
2
+ module Storage
2
3
  #Class trie - storage
3
4
  class Storage
4
5
  attr_accessor :trie
@@ -8,18 +9,13 @@ class Storage
8
9
 
9
10
  #add str to storage
10
11
  def add(str)
11
- if !str.match(/^[[:alpha:][,]]+$/)
12
- raise ArgumentError
13
- end
14
- words = str.split(",").reject { |c| c.empty? }
15
- words.each do |word|
16
- @trie.add_word(word)
17
- end
12
+ raise ArgumentError if !str.match(/^[[:alpha:][,]]+$/)
13
+ words = str.split(',').reject { |c| c.empty? }.each {|word| @trie.add_word(word)}
18
14
  end
19
15
 
20
16
  #print all trie words
21
- def print
22
- puts @trie.get_subtrie_words([],"")
17
+ def to_s
18
+ @trie.get_subtrie_words([], '').join(",")
23
19
  end
24
20
 
25
21
  #check contains str in storage
@@ -29,31 +25,27 @@ class Storage
29
25
 
30
26
  #find all words starting from str
31
27
  def find(str)
32
- if str.length<3
33
- raise ArgumentError
34
- end
28
+ raise ArgumentError if str.length<3
35
29
  st = str.dup
36
30
  @trie.find([],str,st)
37
31
  end
38
32
 
39
33
  #load words in storage from file
40
34
  def load_from_file(filename)
41
- lines=IO.readlines(filename)
35
+ lines = IO.readlines(filename)
42
36
  lines.map {|line| add(line.strip)}
43
37
  end
44
38
 
45
39
  #save words from storage to file
46
40
  def save_to_file(filename)
47
- File.open(filename, "w+") do |f|
48
- f.puts(@trie.get_subtrie_words([],"").join(","))
49
- end
41
+ File.open(filename, 'w+') {|f| f.puts(self)}
50
42
  end
51
43
 
52
44
  #load words in storage from zip
53
45
  def load_from_zip(filename)
54
46
  Zip::File.foreach(filename) do |entry|
55
47
  istream = entry.get_input_stream
56
- istream.read.split("\n").each do |line|
48
+ istream.read.split('\n').each do |line|
57
49
  add(line)
58
50
  end
59
51
  end
@@ -63,7 +55,7 @@ class Storage
63
55
  def save_to_zip(filename)
64
56
  Zip::File.open(filename, Zip::File::CREATE) {
65
57
  |zipfile|
66
- zipfile.get_output_stream("storage.txt") { |f| f.puts @trie.get_subtrie_words([],"").join(",") }
58
+ zipfile.get_output_stream('storage.txt') { |f| f.puts @trie.get_subtrie_words([], '').join(',') }
67
59
  }
68
60
  end
69
61
 
@@ -72,7 +64,16 @@ end
72
64
  #Class Node of trie
73
65
  class Node
74
66
  # subtrie and property of ending word on this node
75
- attr_accessor :subtrie,:final
67
+ attr_accessor :subtrie
68
+
69
+ def final!
70
+ @final = true
71
+ end
72
+
73
+ def final?
74
+ @final
75
+ end
76
+
76
77
  def initialize
77
78
  @final = false
78
79
  @subtrie = {}
@@ -80,50 +81,43 @@ class Node
80
81
 
81
82
  #check contains str starting from current node
82
83
  def contains?(str)
83
- let=str[0]
84
+ let = str[0]
84
85
  if str.empty?
85
- @final
86
+ final?
86
87
  elsif @subtrie[let]
87
88
  @subtrie[let].contains?(str[1..-1])
88
- else
89
- return false
89
+ else false
90
90
  end
91
91
  end
92
92
 
93
93
  #add word in trie
94
94
  def add_word(word)
95
- let=word[0]
95
+ let = word[0]
96
96
  if !word.empty?
97
97
  if !@subtrie[let]
98
- @subtrie[let]=Node.new
98
+ @subtrie[let] = Node.new
99
99
  end
100
100
  @subtrie[let].add_word(word[1..-1])
101
101
  else
102
- @final=true
102
+ final!
103
103
  end
104
104
  end
105
105
 
106
106
  #get all words starting from current node
107
107
  def get_subtrie_words(res,str)
108
108
  @subtrie.each do |k,node|
109
- if node.final
110
- res.push(str+k)
111
- end
112
- if !node.subtrie.empty?
113
- node.get_subtrie_words(res,str+k)
114
- end
109
+ res.push(str+k) if node.final?
110
+ node.get_subtrie_words(res,str+k) if !node.subtrie.empty?
115
111
  end
116
112
  res
117
113
  end
118
114
 
119
115
  #find all words starting from current node and str
120
116
  def find(res,str,st)
121
- let=str[0]
117
+ let = str[0]
122
118
  if @subtrie[let]
123
- if str.length==1
124
- if @subtrie[let].final
125
- res.push(st)
126
- end
119
+ if str.length == 1
120
+ res.push(st) if @subtrie[let].final?
127
121
  @subtrie[let].get_subtrie_words(res,st)
128
122
  else
129
123
  @subtrie[let].find(res,str[1..-1],st)
@@ -133,3 +127,4 @@ class Node
133
127
  end
134
128
  end
135
129
  end
130
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trie-storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danila Potapov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-27 00:00:00.000000000 Z
11
+ date: 2016-10-31 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple trie storage gem
14
14
  email: potapovd36@gmail.com