Re: Functional solution to union of intervals
- To: MathGroup at yoda.physics.unc.edu
- Subject: Re: Functional solution to union of intervals
- From: fateman at peoplesparc.berkeley.edu (Richard Fateman)
- Date: Fri, 5 Jun 92 14:58:57 PDT
I think this works, and it seems to me to be far simpler to understand. If Lists in Mathematica were implemented as linked lists, instead of fixed-length arrays which have to be copied over at every Prepend, it would probably be quite fast. (* Take a list of intervals of the form {{low1, high1} ...{lown,highn}} and return a list of maximal intervals *) mi[{}]:={} mi[list:{a_,__}]:=mi1[a,mi[Rest[list]]] mi1[a_,Null]:={a} mi1[new:{low_,hi_},old:{{l_,h_},___} ]:= If [(l<=low<=h || l<=hi<=h || low< l<hi || low <h <hi), Prepend[Rest[old],{Min[l,low],Max[hi,h]}], Prepend[old,new]] UnionOfListOfIntervals[a_]:=mi[Sort[a]] (* I think the test can be simplified to omit several of the conditions... *) RJF