Permalink
Browse files

Added clear method

  • Loading branch information...
1 parent 2cb6058 commit f61583260532c530569ac38b095ebef1b1198b05 @dcadenas dcadenas committed Feb 5, 2010
Showing with 44 additions and 10 deletions.
  1. +6 −10 benchmark.rb
  2. +14 −0 ext/rankable_graph.c
  3. +24 −0 spec/rankable_graph_spec.rb
View
@@ -1,30 +1,26 @@
-#require 'rubygems'
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'ext'))
-
+#Not a benchmark, just a very unscientific test
require 'ext/rankable_graph'
require "benchmark"
include Benchmark
n = 1000000
bmbm(12) do |test|
- r = RankableGraph.new
+ r = RankableGraph.new
+ test.report("c:") do
srand(5)
(0..(n-1)).map do |i|
#each node has an average of 30 links
rand(60).times do
j = rand(n)
- #first three nodes are more linked to than the rest
+ #first three nodes have more links than the rest
r.link(i, (j > 800000 ? rand(3) : j))
end
end
-
- test.report("c:") do
result = []
r.rank(0.85, 0.001){|key, val| result << [key, val]}
puts "7 first values are #{result[0..6].map{|(k,v)| "[#{k}]=#{"%.4f" % (v * 100)}, "}}"
+
+ r.clear
end
end
-
-
-
View
@@ -213,6 +213,19 @@ static VALUE rankable_graph_init_copy(VALUE copy, VALUE orig){
return copy;
}
+static VALUE clear(VALUE self){
+ RNStruct* rn;
+ Data_Get_Struct(self, RNStruct, rn);
+
+ rn->current_available_index = -1;
+ g_hash_table_remove_all(rn->index_to_key);
+ g_hash_table_remove_all(rn->key_to_index);
+ g_ptr_array_set_size(rn->number_out_links, 0);
+ g_ptr_array_set_size(rn->in_links, 0);
+
+ return Qnil;
+}
+
static VALUE rb_cRankableGraph;
void Init_rankable_graph(){
@@ -221,6 +234,7 @@ void Init_rankable_graph(){
rb_define_method(rb_cRankableGraph, "initialize", init, 0);
rb_define_method(rb_cRankableGraph, "initialize_copy", rankable_graph_init_copy, 1);
rb_define_method(rb_cRankableGraph, "link", link, 2);
+ rb_define_method(rb_cRankableGraph, "clear", clear, 0);
rb_define_method(rb_cRankableGraph, "rank", rank, 2);
}
@@ -12,6 +12,30 @@ def assert_rank(rankable_graph, expected_rank)
end
describe RankableGraph do
+ it "should be possible to recalculate the ranks after a new link is added" do
+ rankable_graph = RankableGraph.new
+ rankable_graph.link(0, 1)
+ assert_rank(rankable_graph, { 0 => 35.1, 1 => 64.9 })
+ rankable_graph.link(1, 2)
+ assert_rank(rankable_graph, { 0 => 18.4, 1 => 34.1, 2 => 47.4 })
+ end
+
+ it "should be possible to clear the graph" do
+ rankable_graph = RankableGraph.new
+ rankable_graph.link(0, 1)
+ rankable_graph.link(1, 2)
+ rankable_graph.clear
+ rankable_graph.link(0, 1)
+ assert_rank(rankable_graph, { 0 => 35.1, 1 => 64.9 })
+ end
+
+ it "should not raise an exception when calculating the rank of an empty graph" do
+ rankable_graph = RankableGraph.new
+ lambda do
+ rankable_graph.rank(0.85, 0.0001){|label, rank| raise "This should not be raised!" }
+ end.should_not raise_error
+ end
+
it "should return correct results when having a dangling node" do
rankable_graph = RankableGraph.new
#node 2 is a dangling node because it has no outbound links

0 comments on commit f615832

Please sign in to comment.