maybe better union of intervals
- To: mathgroup at yoda.physics.unc.edu
- Subject: maybe better union of intervals
- From: fateman at mammoth.berkeley.edu (Richard Fateman)
- Date: Fri, 5 Jun 92 16:59:22 -0700
(* Take a list of intervals of the form {{low1, high1} ...{lown,highn}} and return a list of maximal intervals, sorted by lower endpoints *) (*an empty list is a list of maximal intervals. *) mi[{}]:={} (* a list of one interval is also *) mi[a:{_}]:=a (* given an interval a ={low,hi} and a list L of maximal intervals, sorted, with the lowest (first) element of L = {l,h}, and with low <= l, then a new list L' of maximal intervals consists either of appending a to L, or merging a with {l,h}, and appending that to the rest of L. The merge is necessary exactly when hi >=l *) mi[list:{a_,__}]:=mi1[a,mi[Rest[list]]] mi1[a_,Null]:={a} mi1[new:{low_,hi_},old:{{l_,h_},___} ]:= If [hi>=l, Prepend[Rest[old],{Min[l,low],Max[hi,h]}], Prepend[old,new]] UnionOfListOfIntervals[a_]:=mi[Sort[a]]