Created
May 27, 2009 15:45
-
-
Save seki/118701 to your computer and use it in GitHub Desktop.
concept code of NanoTable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rbtree' | |
class Nano | |
class Pivot | |
def initialize(ary) | |
@ary = ary | |
end | |
end | |
class Prefix < Pivot | |
def <=>(ary) | |
it = @ary <=> ary | |
return it if it >= 0 | |
return 1 if ary[0,@ary.size] == @ary | |
return -1 | |
end | |
end | |
class Forward < Prefix | |
def <=>(ary) | |
it = super(ary) | |
(it == 0) ? 1 : it | |
end | |
end | |
def initialize | |
@tree = MultiRBTree.new | |
end | |
def fetch(ary) | |
if block_given? | |
@tree.bound(ary, Prefix.new(ary)) do |k, v| | |
yield(k) | |
end | |
else | |
result = [] | |
fetch(ary) {|it| result << it} | |
result | |
end | |
end | |
def forward(ary) | |
it ,= @tree.lower_bound(Forward.new(ary)) | |
it | |
end | |
def store(ary) | |
@tree.store(ary, true) | |
end | |
end | |
nano = Nano.new | |
# build revert-index | |
while line = gets | |
line.scan(/\w+/) do |word| | |
nano.store([word, ARGF.filename, ARGF.file.lineno]) | |
end | |
end | |
# retrieve index | |
nano.fetch(['def']) do |it| | |
p it | |
end | |
# each word | |
word = '' | |
while word | |
word ,= nano.forward([word]) | |
p word if word | |
end | |
# each word, doc | |
word, doc = '' | |
while word | |
word, doc = nano.forward([word, doc]) | |
p [word, doc] if word | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment