Hello all
I'm writing a calendar application that needs to check for conflicts between recurring entries. Each Entry object has a recurrences() method which returns an array of ranges - each range contains the start and end times of each future occurrence.
I need to check for conflicts between new and existing entries. I'm doing this by checking that none of the future occurrences of the new entry clash with future occurrences of existing entries:
def conflicts?(other)
conflicts = 0
recurrences.each do |my_rec|
other.recurrences.each do |other_rec|
start, finish = other_rec.first, other_rec.last
conflicts += 1 if my_rec.include?(start) || my_rec.include?(finish)
end
end
conflicts > 0
end
recurrences() defaults to returning all occurrences between start time and start time + 1 year
the problem is this method is not very efficient. comparing just two entries, each with a daily recurrence over 1 year, leads to 365 * 365 comparisons (on my machine that takes 4+ seconds). There may be any number of existing entries to compare a new entry to so the method I have now is useless.
I don't have a computer science or maths background but I've been reading various textbooks on algorithms and I haven't been able to find a way to optimize the method. Does anyone else have any ideas?
thanks
Dave