cc 1.1.3 → 1.1.5
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 +4 -4
- data/cc.rb +2 -1
- data/enum.rb +141 -0
- data/logic.rb +13 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1f4f552fd0a586d4321f95a788b210de1ae859f187f03ff92dff524c16021d49
|
|
4
|
+
data.tar.gz: 857478152baa7bc3ced3d103f8f8eb6797989563d7242e25dc7b7e1564b98be2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: de7cb9814ffcc3b8acae9c5ad48b5a46e61d7524a28c4cd44ae7a8bd6de7ae8374bfb19474cd0cfcd7174e399c11acf1867a3bd48fb959f526bdf6cc113a8e9b
|
|
7
|
+
data.tar.gz: 81d1bcb0123efe87e9a037364cfc4da673ccf6804b0bcadab97f4da30d9385ac3c9035c5f61a65894169bde84aad2487c8f3f47e87c3c47d690714d5b2d349bf
|
data/cc.rb
CHANGED
data/enum.rb
CHANGED
|
@@ -14,6 +14,51 @@
|
|
|
14
14
|
puts "Enumberable objects could be added head or tail:"
|
|
15
15
|
pp [ 1 , [2], 3 ].unfold_head( :a, :b, :c )
|
|
16
16
|
pp [[1], 2 , [3]].unfold_tail(*[:x, :y, :z])
|
|
17
|
+
|
|
18
|
+
puts "Hash could project and serialize on a subscope as a sub hash or array:"
|
|
19
|
+
{a: 1 ,b: 2,c: 3}.project(:a, :c) # => {:a=>1, :c=>3}
|
|
20
|
+
{a: 1 ,b: 2,c: 3}.serialize(:a, :c) # => [1, 3]
|
|
21
|
+
|
|
22
|
+
puts "Array could mapping with a subscope as a key-val array:"
|
|
23
|
+
a = [[1,2,3], [4,5,6], [7,8]]
|
|
24
|
+
a.mapping [:a, :b, :c] # => [{:a=>1, :b=>2, :c=>3}, {:a=>4, :b=>5, :c=>6}, {:a=>7, :b=>8, :c=>nil}]
|
|
25
|
+
a.mapping [:a, :d] # => [{:a=>1, :d=>2}, {:a=>4, :d=>5}, {:a=>7, :d=>8}]
|
|
26
|
+
|
|
27
|
+
puts "Building a RangeTree to query a range of records:"
|
|
28
|
+
pp(rt = RangeTree.new(records = [
|
|
29
|
+
[[1, 3], :a],
|
|
30
|
+
[[2, 4], :b],
|
|
31
|
+
[[2, 7], :c],
|
|
32
|
+
[[6], :d],
|
|
33
|
+
[[4, 5], :e],
|
|
34
|
+
[[8], :f],
|
|
35
|
+
[[10], :g],
|
|
36
|
+
:y,
|
|
37
|
+
:z
|
|
38
|
+
]))
|
|
39
|
+
# {1=>{3=>[:a]},
|
|
40
|
+
# 2=>{4=>[:b], 7=>[:c]},
|
|
41
|
+
# 6=>{6=>[:d]},
|
|
42
|
+
# 4=>{5=>[:e]},
|
|
43
|
+
# 8=>{8=>[:f]},
|
|
44
|
+
# 10=>{10=>[:g]},
|
|
45
|
+
# 0=>{0=>[:y, :z]}}
|
|
46
|
+
|
|
47
|
+
pp rt.query( Range.new(1,2) )
|
|
48
|
+
# ["complete", {[1, 3]=>[:a]}]
|
|
49
|
+
|
|
50
|
+
pp rt.query( [4,7] )
|
|
51
|
+
# ["complete", {[2, 7]=>[:c]}]
|
|
52
|
+
|
|
53
|
+
pp rt.query( 6 )
|
|
54
|
+
# ["complete", {[2, 7]=>[:c], [6, 6]=>[:d]}]
|
|
55
|
+
|
|
56
|
+
pp rt.query( [1,6] )
|
|
57
|
+
# ["partial",
|
|
58
|
+
# {[1, 3]=>[:a], [2, 4]=>[:b], [2, 7]=>[:c], [6, 6]=>[:d], [4, 5]=>[:e]}]
|
|
59
|
+
|
|
60
|
+
pp rt.query( :a )
|
|
61
|
+
# ["unformal", {[0, 0]=>[]}]
|
|
17
62
|
=end
|
|
18
63
|
|
|
19
64
|
module Enumerable
|
|
@@ -75,3 +120,99 @@ class Array
|
|
|
75
120
|
return new_records
|
|
76
121
|
end
|
|
77
122
|
end
|
|
123
|
+
|
|
124
|
+
class RangeTree < Hash
|
|
125
|
+
attr_reader :a
|
|
126
|
+
|
|
127
|
+
# Records := [ [Range|Point, Record] | Record, ... ]
|
|
128
|
+
def initialize records
|
|
129
|
+
merge records
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def merge records
|
|
133
|
+
records.each do|record|
|
|
134
|
+
head, rest = record
|
|
135
|
+
if head.is_a?(Range)
|
|
136
|
+
starter, finisher = head.min, head.max
|
|
137
|
+
elsif head.is_a?(Array)
|
|
138
|
+
starter, finisher = head.sort.first, head.sort.last
|
|
139
|
+
elsif head.is_a?(Numeric)
|
|
140
|
+
starter, finisher = head, head
|
|
141
|
+
else
|
|
142
|
+
starter, finisher, rest = 0, 0, record
|
|
143
|
+
end
|
|
144
|
+
self[starter] ||= {}
|
|
145
|
+
self[starter][finisher] ||= []
|
|
146
|
+
self[starter][finisher] << rest
|
|
147
|
+
self[starter][finisher].uniq!
|
|
148
|
+
end
|
|
149
|
+
@start_keys = self.keys.sort
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def update_key
|
|
153
|
+
@start_keys = self.keys.sort
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def unikey target
|
|
157
|
+
if target.is_a?(Range)
|
|
158
|
+
skey, fkey = target.min, target.max
|
|
159
|
+
elsif target.is_a?(Array)
|
|
160
|
+
skey, fkey = target.sort.first, target.sort.last
|
|
161
|
+
elsif target.is_a?(Numeric)
|
|
162
|
+
skey, fkey = target, target
|
|
163
|
+
else
|
|
164
|
+
skey, fkey = 0, 0
|
|
165
|
+
end
|
|
166
|
+
return skey, fkey
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def locate target
|
|
170
|
+
skey, fkey = unikey(target)
|
|
171
|
+
keys = []
|
|
172
|
+
start_keys = self.keys.sort
|
|
173
|
+
candi_keys = start_keys.select{|sk|sk<=skey}
|
|
174
|
+
candi_keys.each do|candi_key|
|
|
175
|
+
finish_keys = self[candi_key].keys.sort
|
|
176
|
+
cand_keys = finish_keys.select{|fk|fk>=fkey}.map{|cand_key|[candi_key, cand_key]}
|
|
177
|
+
keys += cand_keys
|
|
178
|
+
end
|
|
179
|
+
return keys
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def search target
|
|
183
|
+
keys = []
|
|
184
|
+
skey, fkey = unikey(target)
|
|
185
|
+
self.each do|sk, candis|
|
|
186
|
+
candis.each do|fk, cands|
|
|
187
|
+
next if fkey < sk || fk < skey
|
|
188
|
+
keys << [sk,fk]
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
return keys
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def index target
|
|
195
|
+
keys = locate(target)
|
|
196
|
+
if keys.empty?
|
|
197
|
+
return 'partial', search(target)
|
|
198
|
+
elsif keys==[[0,0]]
|
|
199
|
+
return 'unformal', [[0,0]]
|
|
200
|
+
else
|
|
201
|
+
return 'complete', keys
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def query target
|
|
206
|
+
result = {}
|
|
207
|
+
state, keys = index(target)
|
|
208
|
+
keys.each do|keyp|
|
|
209
|
+
sk, fk = keyp
|
|
210
|
+
result[keyp] = self[sk][fk]
|
|
211
|
+
end
|
|
212
|
+
if state=='unformal'
|
|
213
|
+
self[0] ||= {}
|
|
214
|
+
result[[0,0]] = (self[0][0]||[]).include?(target) ? target : []
|
|
215
|
+
end
|
|
216
|
+
return state, result
|
|
217
|
+
end
|
|
218
|
+
end
|
data/logic.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#coding:utf-8
|
|
2
|
+
|
|
3
|
+
############################################################################################################
|
|
4
|
+
# HOW TO USE #
|
|
5
|
+
############################################################################################################
|
|
6
|
+
|
|
7
|
+
=begin
|
|
8
|
+
p (1..9).map{|i|flag{[i>3, i%2==0]}}
|
|
9
|
+
=end
|
|
10
|
+
|
|
11
|
+
def flag op=true, &conds
|
|
12
|
+
[(conds||lambda{}).call,[]].flatten(1).reduce(op){|f,c|op==:or ? (f || c) : (f && c)}
|
|
13
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cc
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.1.
|
|
4
|
+
version: 1.1.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matt
|
|
@@ -28,6 +28,7 @@ files:
|
|
|
28
28
|
- exception.rb
|
|
29
29
|
- file.rb
|
|
30
30
|
- kernel.rb
|
|
31
|
+
- logic.rb
|
|
31
32
|
- monkey-patch.rb
|
|
32
33
|
- number.rb
|
|
33
34
|
- regexp.rb
|