From: ts Date: 2001-03-11T01:55:54+09:00 Subject: [ruby-talk:12384] Re: Q re looping structures >>>>> "B" == Benjamin J Tilly writes: B> Try this untangling code and see if performance isn't B> slightly better... Difficult to say which is faster. Actually it has this algorithm, you can choose your algorithm at compile time. With this example : pigeon% cat b.rb #!/usr/bin/ruby require 'tangle' ########################## # Examples ########################## l = ["a"]; r=["a"]; l << r ; r << l a =["a"]; b=["a"]; c = ["a"]; a << b; b << c; c << a aa = ["a"]; bb =["a"]; cc = ["a"]; aa << bb; bb << cc; cc << aa n = ["a"]; m =["a"]; o = ["a"]; p=["a"]; n << m ; m << o; o<< p; p << n; #aa << a ; aa << a; aa << a; ccl = cc.clone p "ops" #print ((a == ccl), "(a == ccl)") # will throw exceptions #print ((l == m), "(l == m)") p ccl p cc p n p a.untangle! p ccl.untangle! p ccl.tangle? ###################### x = [1];y = ["a",x]; z=["b"]; s = [x,y]; t= [s] ; x << t; # x << [[x,y]] y << [[z,x]]; z<< [x] xx =[1];yy=["a",xx];zz=["b"]; xx<<[[xx,y]];yy<<[[z,x]];zz<<[x] #p y == yy they are id_cache equal p y.untangle! p yy.tangle? p yy.untangle! p yy.tangle? ############################### p t p s def f(x); [x,x,x,x,x,x,x]; end def g(x); f f f f f f f f x; end def h(x); g g g g g g g g x; end def i(x); h h h h h h h h x; end def j(x); i i i i i i i x;end p h(1).untangle! # better be nil ... p (j(1) == j(1)) # change this to g(1) if linux can't handle this pigeon% pigeon% make distclean pigeon% ruby extconf.rb --with-hash-hash --disable-swap creating Makefile pigeon% make gcc -fPIC -g -O2 -DHASH_HASH -DNO_SWAP -I/usr/local/lib/ruby/1.6/i686-linux -I/usr/local/include -c -o tangle.o tangle.c gcc -shared -L/usr/local/lib/ruby/1.6/i686-linux -L/usr/local/lib -o tangle.so tangle.o -lc pigeon% pigeon% time b.rb "ops" ["a", ["a", ["a", ["a", [...]]]]] ["a", ["a", ["a", [...]]]] ["a", ["a", ["a", ["a", [...]]]]] ["a", ["a", ["a", ["a"]]]] ["a", ["a", ["a", ["a", ["a"]]]]] false ["a", [1, [[[1], ["a"]]]], [[["b", [[1, [[[1], ["a"]]]]]], [1, [[[1], ["a"]]]]]]] true ["a", [1, [[[1], ["a", [1, [[[1], ["a"]]]], [[["b", [[1, [[[1], ["a"]]]]]], [1, [[[1], ["a"]]]]]]]]]], [[["b", [[1, [[[1], ["a"]]]]]], [1, [[[1], ["a"]]]]]]] false [[[1], ["a"]]] [[1], ["a"]] nil true real 0m0.208s user 0m0.200s sys 0m0.010s pigeon% pigeon% make distclean pigeon% ruby extconf.rb --disable-swap creating Makefile pigeon% make gcc -fPIC -g -O2 -DNO_SWAP -I/usr/local/lib/ruby/1.6/i686-linux -I/usr/local/include -c -o tangle.o tangle.c gcc -shared -L/usr/local/lib/ruby/1.6/i686-linux -L/usr/local/lib -o tangle.so tangle.o -lc pigeon% pigeon% time b.rb "ops" ["a", ["a", ["a", ["a", [...]]]]] ["a", ["a", ["a", [...]]]] ["a", ["a", ["a", ["a", [...]]]]] ["a", ["a", ["a", ["a"]]]] ["a", ["a", ["a", ["a", ["a"]]]]] false ["a", [1, [[[1], ["a"]]]], [[["b", [[1, [[[1], ["a"]]]]]], [1, [[[1], ["a"]]]]]]] true ["a", [1, [[[1], ["a", [1, [[[1], ["a"]]]], [[["b", [[1, [[[1], ["a"]]]]]], [1, [[[1], ["a"]]]]]]]]]], [[["b", [[1, [[[1], ["a"]]]]]], [1, [[[1], ["a"]]]]]]] false [[[1], ["a"]]] [[1], ["a"]] nil true real 0m0.199s user 0m0.180s sys 0m0.010s pigeon% B> def eq? (other, cache) it don't have exactly this algorithm B> end Guy Decoux