prefixstorage 0.1.0
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.
- checksums.yaml +7 -0
- data/lib/storage.rb +156 -0
- data/lib/tree_node.rb +25 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 08dea8d443691e75e4c8b082a5f19a07bec791df
|
4
|
+
data.tar.gz: 254f3635a73b7aa7f7bb871be1187f9d1003e779
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aadc261a2a65cbf9931042c9d3b306e058f695740f51252a407ecf629104de86a91969c0b293abb2a013d717ade9f340213975e067cdd319050c8e5bf4eb4183
|
7
|
+
data.tar.gz: cebbf6981365b038a5f83ffdebe69338ef4a99b516be9f0fc38652db0821c1bc108d5e0f85cd3f4644f856a1a59747243bfff0fb960de918c5b4fd97591c95ab
|
data/lib/storage.rb
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "zip/zip"
|
3
|
+
|
4
|
+
class Storage
|
5
|
+
|
6
|
+
require_relative 'tree_node'
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@root = TreeNode.new
|
10
|
+
end
|
11
|
+
|
12
|
+
#adds new string to storage
|
13
|
+
def add(str)
|
14
|
+
|
15
|
+
words = (str.split ",").map! { |word| word.strip }
|
16
|
+
|
17
|
+
words.each do |word|
|
18
|
+
node = @root
|
19
|
+
word.split(//).each do |ch|
|
20
|
+
node.children[ch]=TreeNode.new if node.children[ch].nil?
|
21
|
+
|
22
|
+
node = node.children[ch]
|
23
|
+
|
24
|
+
end
|
25
|
+
node.is_leaf=true
|
26
|
+
end
|
27
|
+
@root.children
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
#checks if storage contains string
|
32
|
+
def contains(str)
|
33
|
+
node = @root
|
34
|
+
str.each_char do |ch|
|
35
|
+
|
36
|
+
return false if node.children[ch].nil?
|
37
|
+
|
38
|
+
node = node.children[ch]
|
39
|
+
end
|
40
|
+
node.is_leaf?
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
#returns all strings that start with str
|
46
|
+
def find(str)
|
47
|
+
node = @root
|
48
|
+
str.each_char do |ch|
|
49
|
+
|
50
|
+
return [] if node.children[ch].nil?
|
51
|
+
|
52
|
+
node = node.children[ch]
|
53
|
+
end
|
54
|
+
words = []
|
55
|
+
get_all_words node, str, words
|
56
|
+
|
57
|
+
words
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def load_from_file(file_name)
|
62
|
+
file_to_read = File.open(file_name, "r")
|
63
|
+
read_from_file file_to_read
|
64
|
+
file_to_read.close
|
65
|
+
end
|
66
|
+
|
67
|
+
def save_to_file(file_name)
|
68
|
+
file_to_write = File.new(file_name, "w+")
|
69
|
+
write_to_file file_to_write
|
70
|
+
file_to_write.close
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
def load_from_zip (zip_name)
|
76
|
+
zip_name = zip_name+".zip" unless zip_name.end_with? ".zip"
|
77
|
+
Zip::ZipFile.foreach(zip_name) do |file|
|
78
|
+
read_from_file file.get_input_stream
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
def save_to_zip (zip_name, result_file_name)
|
84
|
+
zip_name = zip_name+".zip" unless zip_name.end_with? ".zip"
|
85
|
+
Zip::ZipFile.open(zip_name, Zip::ZipFile::CREATE) do |zipfile|
|
86
|
+
zipfile.get_output_stream(result_file_name)do |f|
|
87
|
+
write_to_file f
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
private
|
95
|
+
def get_all_words(node, ch, words)
|
96
|
+
str = String.new(ch)
|
97
|
+
if node.is_leaf?
|
98
|
+
words.push str
|
99
|
+
end
|
100
|
+
if !node.children.empty?
|
101
|
+
|
102
|
+
|
103
|
+
node.children.each do |key, value|
|
104
|
+
get_all_words(value, str+key, words)
|
105
|
+
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
def read_from_file(file_to_read)
|
116
|
+
lines = file_to_read.readlines
|
117
|
+
lines.each {|line| add(line)}
|
118
|
+
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
def write_to_file(file_to_write)
|
123
|
+
words = find("")
|
124
|
+
if !words.empty?
|
125
|
+
last_word =
|
126
|
+
words.each {|word| file_to_write.write(word+", ")}
|
127
|
+
file_to_write.write last_word
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
#ant
|
134
|
+
# chor
|
135
|
+
# ient
|
136
|
+
|
137
|
+
=begin
|
138
|
+
storage = Storage.new
|
139
|
+
puts storage.add ""
|
140
|
+
puts storage.add "te, spec ,tetra, post, rock, postcard, trunk, pork"
|
141
|
+
|
142
|
+
puts storage.contains "te"
|
143
|
+
|
144
|
+
storage.load_from_file "../test_file"
|
145
|
+
puts storage.find ""
|
146
|
+
|
147
|
+
storage.save_to_file "../result.txt"
|
148
|
+
puts File.readline "../result.txt"
|
149
|
+
#storage.save_to_zip "result", "result.txt"
|
150
|
+
|
151
|
+
#storage.load_from_zip "result"
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
=end
|
156
|
+
|
data/lib/tree_node.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class TreeNode
|
2
|
+
|
3
|
+
def initialize
|
4
|
+
@children = {}
|
5
|
+
@is_leaf = false
|
6
|
+
end
|
7
|
+
|
8
|
+
def is_leaf?
|
9
|
+
@is_leaf
|
10
|
+
end
|
11
|
+
|
12
|
+
def is_leaf=(is_leaf)
|
13
|
+
@is_leaf = is_leaf
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
def children
|
18
|
+
@children
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_s
|
22
|
+
children.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prefixstorage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Artem Petrik
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2012-04-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Gem for storing strings in prefix tree (Trie)
|
14
|
+
email: earsonheart@gmeil.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/storage.rb
|
20
|
+
- lib/tree_node.rb
|
21
|
+
homepage: ''
|
22
|
+
licenses: []
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.3
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Prefix tree strings storage
|
44
|
+
test_files: []
|