Created
May 21, 2013 15:37
-
-
Save seki/5620779 to your computer and use it in GitHub Desktop.
merge sort for ikezawa-san
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
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