Created
October 4, 2011 00:10
-
-
Save dgraham/1260596 to your computer and use it in GitHub Desktop.
EM::PriorityQueue using RBTree
This file contains hidden or 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
module EventMachine | |
class PriorityQueue < Queue | |
def initialize(&comparator) | |
super | |
@items = Items.new(comparator) | |
end | |
class Items | |
def initialize(comparator) | |
require 'rbtree' | |
@items = MultiRBTree.new | |
@items.readjust(comparator) | |
end | |
def push(*args) | |
args.each do |obj| | |
@items[obj] = nil | |
end | |
end | |
def shift | |
value = @items.shift | |
value ? value[0] : nil | |
end | |
def method_missing(name, *args, &block) | |
@items.send(name, *args, &block) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment