Skip to content

Instantly share code, notes, and snippets.

@seki
Created May 21, 2013 15:37
Show Gist options
  • Save seki/5620779 to your computer and use it in GitHub Desktop.
Save seki/5620779 to your computer and use it in GitHub Desktop.
merge sort for ikezawa-san
class Merge
def initialize(*fnames)
@file = fnames.collect {|fn| MergeFile.new(fn)}
delete_empty
end
def delete_empty
@file.delete_if {|f| f.empty?}
end
def empty?
delete_empty
@file.empty?
end
def pop
@file.min.pop
end
end
class Merge
class MergeFile
def initialize(fname)
@file = File.open(fname)
@curr = @file.gets
end
def empty?
@curr.nil?
end
def peek
@curr
end
def pop
@curr
ensure
@curr = @file.gets
end
def <=>(other)
peek <=> other.peek
end
end
end
if __FILE__ == $0
merge = Merge.new(*ARGV)
last = nil
while ! merge.empty?
curr = merge.pop
next if last == curr
last = curr
puts curr
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment