From: cruiserdan Date: 2007-12-20T08:00:03+09:00 Subject: Re: Database-like Algorithmic Problem Okay, here's a fixed version. There were 2 problems. The first was that I forgot to remove the bad (unmatched) hashes before returning a result. The second was that you have to go through every key in the template to find a match (I was returning after the first match). Thus find is no more efficient than select due to the way the data is stored. I also took out the 'unless bad.include? hash' clause from the compare because I think it actually is slower that way, depending on what you are comparing. I thought it would be faster than the original version posted because I don't have to generate a set of candidates first, so the initial benchmarks were disappointing. But maybe this version will do even better. I look forward to seeing your results. --- # new version of select def select(template) good = Set.new; bad = Set.new template.each do |key,val1| (@c[key]).each do |val2,hash| ( val1 === val2 ? good : bad ) << hash # unless bad.include? hash end end return (good - bad).to_a end # new version of find def find(template) r = select template r.first unless r.empty? end --- Regards, Dan On Dec 19, 2:23 pm, Christophe Mckeon wrote: > there's a bug in yours. not sure where yet. try > > d = DB.new > d << {:a => 2, :b => 1, :c => 'b'} > puts d.find({ :a => Numeric, :b => 'a' }).inspect > > returns {:a => 2, :b => 1, :c => 'b'} > > -- > Posted viahttp://www.ruby-forum.com/.