Last active
December 15, 2015 02:38
-
-
Save bachue/5188255 to your computer and use it in GitHub Desktop.
My TSort Example
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
require 'tsort' | |
require 'pp' | |
Node = Struct.new :value, :before, :after | |
class Collection < Array | |
include TSort | |
alias :tsort_each_node :each | |
def tsort_each_child(current, &block) | |
select { |competitor| | |
current.after == competitor.value || # Is there any node be run after the current node? | |
competitor.before == current.value # Is there any node run before the current node? | |
}.each(&block) | |
end | |
end | |
collection = Collection.new | |
collection << Node.new(1, 2, 4) | |
collection << Node.new(2) | |
collection << Node.new(3, 4) | |
collection << Node.new(4, 1, 3) | |
pp collection.strongly_connected_components | |
# [[#<struct Node value=3, before=4, after=nil>], | |
# [#<struct Node value=4, before=1, after=3>], | |
# [#<struct Node value=1, before=2, after=4>], | |
# [#<struct Node value=2, before=nil, after=nil>]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is to test initializer tsort in rails 3 initialization process