Permalink
Browse files

immutable drip ph2

  • Loading branch information...
1 parent 8227c88 commit f0a792b699b051e3a0c4b423958b2a6583f97fa6 @seki committed Sep 28, 2011
Showing with 16 additions and 8 deletions.
  1. +16 −8 lib/drip.rb
View
@@ -9,20 +9,28 @@ def inspect; to_s; end
class ImmutableDrip
class Generator
- def initialize(pool=[], tag=[])
- @pool = pool
- @tag = tag
+ def initialize
+ @pool = []
+ @tag = []
+ @shared = Hash.new {|h, k| h[k] = k; k}
end
def add(key, value, *tag)
@pool << [key, value]
+ idx = @pool.size - 1
tag.uniq.each do |t|
- @tag << [[t, key], value]
+ @tag << [[@shared[t], key], idx]
end
end
def generate
- ImmutableDrip.new(@pool.sort, @tag.sort)
+ tag = @tag.sort
+ tag.inject(nil) do |last, kv|
+ k = kv[0]
+ k[0] = last if k[0] == last
+ k[0]
+ end
+ ImmutableDrip.new(@pool.sort, tag)
end
end
@@ -51,7 +59,7 @@ def read_tag(key, tag, n=1)
idx = lower_boundary(@tag, [tag, key + 1])
return [] unless idx
@tag[idx, n].find_all {|kv| kv[0][0] == tag}.collect {|kv|
- [kv[0][1], *kv[1].to_a]
+ [kv[0][1], *@pool[kv[1]][1].to_a]
}
end
@@ -60,7 +68,7 @@ def head_tag(n, tag)
upper = upper_boundary(@tag, [tag, INF])
lower = [lower, upper - n].max
@tag[lower ... upper].collect {|kv|
- [kv[0][1], *kv[1].to_a]
+ [kv[0][1], *@pool[kv[1]][1].to_a]
}
end
@@ -75,7 +83,7 @@ def head(n=1, tag=nil)
def older_tag(key, tag)
idx = upper_boundary(@tag, [tag, key-1])
k, v = @tag[idx - 1]
- k && k[0] == tag ? [k[1], *v.to_a] : nil
+ k && k[0] == tag ? [k[1], *@pool[v][1].to_a] : nil
end
def older(key, tag=nil)

0 comments on commit f0a792b

Please sign in to comment.