-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathccflay.rb
64 lines (51 loc) · 1.09 KB
/
ccflay.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
require "flay"
require "concurrent"
require "digest"
##
# A thread-safe and stable hash subclass of Flay.
class CCFlay < Flay
def initialize(option = nil)
super
@hashes = Concurrent::Hash.new do |hash, key|
hash[key] = Concurrent::Array.new
end
self.identical = Concurrent::Hash.new
self.masses = Concurrent::Hash.new
end
def post_filter *patterns
return if patterns.empty?
self.hashes.delete_if { |_, sexps|
sexps.any? { |sexp|
patterns.any? { |pattern|
# pattern =~ sexp
pattern.satisfy? sexp
}
}
}
end
def prune
post_filter(*option[:post_filters])
super
end
end
class Sexp
attr_writer :mass
def flatter
result = dup.clear
result.mass = mass
each_with_object(result) do |s, r|
if s.is_a?(Sexp)
ss = s.flatter
# s(:a, s(:b, s(:c, 42))) => s(:a, :b, s(:c, 42))
if ss.size == 2 && ss[1].is_a?(Sexp)
r.concat ss
else
r << ss
end
else
r << s
end
end
end
end