On 5/5/10, Intransition <> wrote:
> Have a look at this class:
>
> http://github.com/proutils/lemon/blo...on/snapshot.rb
>
> I am storing a collection of OfModule instances in a hash (@modules)
> indexed by the modules they correspond to. I'd rather just use an
> array, since that is the basic intent. But I ran into difficulty when
> implementing the #- method, which led me to use the hash instead.
>
> Is there an efficient way to implement the #- method if @modules were
> an array instead of a hash?
So, am I understanding you correctly? The core problem in on line 77?
You need an efficient way to tell if c has a particular module in it
or not, so you can know whether to fool around with the methods it
advertises?
Why not create a temporary hash before the other.modules.each loop and
use it to tell which modules are present.... something like (assuming
you rewrite @modules as an array):
known_mods={}
c.modules.each{|mod| known_mods[mod.base]=mod }
other.modules.each do |ofmod|
if known_mods[ofmod.base]
...
end
end
Your Snapshot#- won't be as efficient as it is now, since you have to
build that index up every time its called, but it should be a fairly
minor performance degradation, I would think.
> Feel free to offer any other critiques of this code too, btw.
I am curious. Why do you need this Snapshot class? And what does it do exactly?