trie-storage 0.0.2
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 +117 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 85e93644fb5f2752bae30d4abf768045de14729e
|
4
|
+
data.tar.gz: d4f3ecbc1abd9c311381ee2cd8628102ec0fb045
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 403a931c9d58715bd9be69739a7c554c0f51c7cdf3a4f07bfeede3277497160d9355e5fb20c83bd92e45c6819f9f5c66a4a72d2c10286b5a19563fbe8ef0d3b5
|
7
|
+
data.tar.gz: c0dc8c73a86f9d6c8e24c246617d5ea51a3474d8d175c99e8043b29417ec7643f2d6dd32478f54f88ca87f5bcdc53e32fbd27d0b1d0af0a99b79542a5f94677a
|
data/lib/storage.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'zip'
|
2
|
+
class Storage
|
3
|
+
attr_accessor :trie
|
4
|
+
def initialize
|
5
|
+
@trie=Node.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def add(str)
|
9
|
+
words = str.split(",")
|
10
|
+
words.each do |word|
|
11
|
+
@trie.add_word(word)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def print
|
16
|
+
puts @trie.get_subtrie_words([],"")
|
17
|
+
end
|
18
|
+
|
19
|
+
def contains?(str)
|
20
|
+
@trie.contains?(str)
|
21
|
+
end
|
22
|
+
|
23
|
+
def find(str)
|
24
|
+
if str.length<3
|
25
|
+
raise ArgumentError
|
26
|
+
end
|
27
|
+
st = str.dup
|
28
|
+
@trie.find([],str,st)
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_from_file(filename)
|
32
|
+
lines=IO.readlines(filename)
|
33
|
+
lines.map {|line| add(line)}
|
34
|
+
end
|
35
|
+
|
36
|
+
def save_to_file(filename)
|
37
|
+
File.open(filename, "w+") do |f|
|
38
|
+
f.puts(@trie.get_subtrie_words([],"").join(","))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def load_from_zip(filename)
|
43
|
+
Zip::File.foreach(filename) do |entry|
|
44
|
+
istream = entry.get_input_stream
|
45
|
+
istream.read.split("\n").each do |line|
|
46
|
+
add(line)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def save_to_zip(filename)
|
52
|
+
Zip::File.open(filename, Zip::File::CREATE) {
|
53
|
+
|zipfile|
|
54
|
+
zipfile.get_output_stream("storage.txt") { |f| f.puts @trie.get_subtrie_words([],"").join(",") }
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
class Node
|
61
|
+
attr_accessor :subtrie,:final
|
62
|
+
def initialize
|
63
|
+
@final = false
|
64
|
+
@subtrie = {}
|
65
|
+
end
|
66
|
+
|
67
|
+
def contains?(str)
|
68
|
+
let=str[0]
|
69
|
+
if str.empty?
|
70
|
+
@final
|
71
|
+
elsif @subtrie[let]
|
72
|
+
@subtrie[let].contains?(str[1..-1])
|
73
|
+
else
|
74
|
+
return false
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def add_word(word)
|
79
|
+
let=word[0]
|
80
|
+
if !word.empty?
|
81
|
+
if !@subtrie[let]
|
82
|
+
@subtrie[let]=Node.new
|
83
|
+
end
|
84
|
+
@subtrie[let].add_word(word[1..-1])
|
85
|
+
else
|
86
|
+
@final=true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_subtrie_words(res,str)
|
91
|
+
@subtrie.each do |k,node|
|
92
|
+
if node.final
|
93
|
+
res.push(str+k)
|
94
|
+
end
|
95
|
+
if !node.subtrie.empty?
|
96
|
+
node.get_subtrie_words(res,str+k)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
res
|
100
|
+
end
|
101
|
+
|
102
|
+
def find(res,str,st)
|
103
|
+
let=str[0]
|
104
|
+
if @subtrie[let]
|
105
|
+
if str.length==1
|
106
|
+
if @subtrie[let].final
|
107
|
+
res.push(st)
|
108
|
+
end
|
109
|
+
@subtrie[let].get_subtrie_words(res,st)
|
110
|
+
else
|
111
|
+
@subtrie[let].find(res,str[1..-1],st)
|
112
|
+
end
|
113
|
+
else
|
114
|
+
res
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trie-storage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Danila Potapov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple trie storage gem
|
14
|
+
email: potapovd36@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/storage.rb
|
20
|
+
homepage: http://rubygems.org/gems/trie-storage
|
21
|
+
licenses:
|
22
|
+
- MIT
|
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.6.7
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Trie storage
|
44
|
+
test_files: []
|