anchor.diff:                      AvlTree.Node (newRange, left, right, h)
                       |> addRange measurer newRange'
   
  +    let rec private union_helper (measurer : IMeasurer<_>) left (rangeAB : Range<_>) right limit head stream =
  +        let inline compare a b =
  +            measurer.Compare (a, b)
  +        
  +        match head with
  +        | None ->
  +            let joined =
  +                AvlTree.join rangeAB left right
  +                |> ensureCorrectlyFormed measurer
  +            joined, None, AvlTree.Empty
  +        | Some (rangeXY : Range<_>) ->
  +            let greater_limit z =
  +                match limit with
  +                | None -> false
  +                | Some u ->
  +                    compare z u >= 0
  +                
  +            if compare rangeXY.MaxValue rangeAB.MinValue < 0 && compare rangeXY.MaxValue (measurer.Previous rangeAB.MinValue) < 0 then
  +                let left' =
  +                    addRange measurer rangeXY left
  +                    |> ensureCorrectlyFormed measurer
  +                let head, stream =
  +                    AvlTree.tryExtractMin measurer stream
  +                    |> ensureCorrectlyFormed2Of2 measurer
  +                union_helper measurer left' rangeAB right limit head stream
  +                |> ensureCorrectlyFormed1stAnd3rd measurer
  +
  +            elif compare rangeXY.MinValue rangeAB.MaxValue > 0 && compare rangeXY.MinValue (measurer.Next rangeAB.MaxValue) > 0 then
  +                let right', head, stream =
  +                    union' measurer right limit head stream
  +                    |> ensureCorrectlyFormed1stAnd3rd measurer
  +                let joined =
  +                    AvlTree.join rangeAB left right'
  +                    |> ensureCorrectlyFormed measurer
  +                joined, head, stream
  +
  +            elif compare rangeAB.MaxValue rangeXY.MaxValue >= 0 then
  +                let head, stream =
  +                    AvlTree.tryExtractMin measurer stream
  +                    |> ensureCorrectlyFormed2Of2 measurer
  +                let newRange = Range (min measurer rangeAB.MinValue rangeXY.MinValue, rangeAB.MaxValue)
  +                union_helper measurer left newRange right limit head stream
  +                |> ensureCorrectlyFormed1stAnd3rd measurer
  +
  +            elif greater_limit rangeXY.MaxValue then
  +                let newRange = Range (min measurer rangeAB.MinValue rangeXY.MinValue, rangeXY.MaxValue)
  +                (left, Some newRange, stream)
  +
  +            else
  +                let right', head, stream =
  +                    let newRange = Range (min measurer rangeAB.MinValue rangeXY.MinValue, rangeXY.MaxValue)
  +                    union' measurer right limit (Some newRange) stream
  +                    |> ensureCorrectlyFormed1stAnd3rd measurer
  +                let rerooted =
  +                    AvlTree.reroot measurer left right'
  +                    |> ensureCorrectlyFormed measurer
  +                rerooted, head, stream
  +
  +    and private union' (measurer : IMeasurer<_>) (input : Diet<_>) limit head (stream : Diet<_>) =
  +        let inline compare a b =
  +            measurer.Compare (a, b)
  +
  +        match head with
  +        | None ->
  +            input, None, AvlTree.Empty
  +        | Some (rangeXY : Range<_>) ->
  +            match input with
  +            | AvlTree.Empty ->
  +                AvlTree.Empty, head, stream
  +            | AvlTree.Node (rangeAB, left, right, _) ->
  +                let left', head, stream =
  +                    if compare rangeXY.MinValue rangeAB.MinValue < 0 then
  +                        union' measurer left (Some (measurer.Previous rangeAB.MinValue)) head stream
  +                        |> ensureCorrectlyFormed1stAnd3rd measurer
  +                    else
  +                        left, head, stream
  +
  +                union_helper measurer left' rangeAB right limit head stream
  +                |> ensureCorrectlyFormed1stAnd3rd measurer
  +
       /// Computes the union of the two sets.
       let rec union (measurer : IMeasurer<_>) (input : Diet<_>) (stream : Diet<_>) : Diet<'T> =
           let inline compare a b =
               measurer.Compare (a, b)
   
  -        let rec union' (input : Diet<_>) limit head (stream : Diet<_>) =
  -            match head with
  -            | None ->
  -                input, None, AvlTree.Empty
  -            | Some (rangeXY : Range<_>) ->
  -                match input with
  -                | AvlTree.Empty ->
  -                    (AvlTree.Empty, head, stream)
  -                | AvlTree.Node (rangeAB, left, right, _) ->
  -                    let left', head, stream =
  -                        if compare rangeXY.MinValue rangeAB.MinValue < 0 then
  -                            union' left (Some (measurer.Previous rangeAB.MinValue)) head stream
  -                        else (left, head, stream)
  -
  -                    union_helper left' rangeAB right limit head stream
  -
  -        and union_helper left (rangeAB : Range<_>) right limit head stream =
  -            match head with
  -            | None ->
  -                (AvlTree.join rangeAB left right, None, AvlTree.Empty)
  -            | Some (rangeXY : Range<_>) ->
  -                let greater_limit z =
  -                    match limit with
  -                    | None -> false
  -                    | Some u ->
  -                        compare z u >= 0
  -                
  -                if compare rangeXY.MaxValue rangeAB.MinValue < 0 && compare rangeXY.MaxValue (measurer.Previous rangeAB.MinValue) < 0 then
  -                    let left' = addRange measurer rangeXY left
  -                    let head, stream = AvlTree.tryExtractMin measurer stream
  -                    union_helper left' rangeAB right limit head stream
  -
  -                elif compare rangeXY.MinValue rangeAB.MaxValue > 0 && compare rangeXY.MinValue (measurer.Next rangeAB.MaxValue) > 0 then
  -                    let right', head, stream = union' right limit head stream
  -                    (AvlTree.join rangeAB left right', head, stream)
  -                elif compare rangeAB.MaxValue rangeXY.MaxValue >= 0 then
  -                    let head, stream = AvlTree.tryExtractMin measurer stream
  -                    let newRange = Range (min measurer rangeAB.MinValue rangeXY.MinValue, rangeAB.MaxValue)
  -                    union_helper left newRange right limit head stream
  -                elif greater_limit rangeXY.MaxValue then
  -                    let newRange = Range (min measurer rangeAB.MinValue rangeXY.MinValue, rangeXY.MaxValue)
  -                    (left, Some newRange, stream)
  -                else
  -                    let (right', head, stream) =
  -                        let newRange = Range (min measurer rangeAB.MinValue rangeXY.MinValue, rangeXY.MaxValue)
  -                        union' right limit (Some newRange) stream
  -                    (AvlTree.reroot measurer left right', head, stream)
  -        
           if AvlTree.height stream > AvlTree.height input then
               union measurer stream input
           else
  
positive.code:                 GenericMaximum inputCount streamCount
              #endif
  
              let head, stream' = AvlTree.tryExtractMin measurer stream
              let result, head', stream'' = union' input None head stream'
              let result =
                  match head' with
                  | None ->
                      result
                  | Some i ->
                      AvlTree.join i result stream''
  
              #if DEBUG
              let resultCount = count measurer result
              let inputArr =
                  if resultCount >= minPossibleResultCount then Array.empty
                  else toArray measurer input
              let streamArr =
                  if resultCount >= minPossibleResultCount then Array.empty
                  else toArray measurer stream
                      
              Debug.Assert (
                  resultCount >= minPossibleResultCount,
                  sprintf "The result set should not contain fewer than %i elements, but it contains only %i elements."
                      minPossibleResultCount resultCount)
              #endif
  
              result
  
      /// Computes the intersection of the two sets.
  
negative.code:             AvlTree.singleton rangePQ
          | AvlTree.Node (rangeXY, left, right, _) ->
              if compare rangePQ.MaxValue (measurer.Previous rangeXY.MinValue) < 0 then
                  let left =
                      addRange' measurer rangePQ left
                      |> ensureCorrectlyFormed measurer
  
                  AvlTree.join rangeXY left right
                  |> ensureCorrectlyFormed measurer
  
              elif compare rangePQ.MinValue (measurer.Next rangeXY.MaxValue) > 0 then
                  let right =
                      addRange' measurer rangePQ right
                      |> ensureCorrectlyFormed measurer
                  
                  AvlTree.join rangeXY left right
                  |> ensureCorrectlyFormed measurer
              else
                  let x', left' =
                      if compare rangePQ.MinValue rangeXY.MinValue >= 0 then
                          rangeXY.MinValue, left
                      else
                          find_del_left measurer rangePQ.MinValue left
                          |> ensureCorrectlyFormed2Of2 measurer
  
                  let y', right' =
                      if compare rangePQ.MaxValue rangeXY.MaxValue <= 0 then
                          rangeXY.MaxValue, right
                      else
                          find_del_right measurer rangePQ.MaxValue right
                          |> ensureCorrectlyFormed2Of2 measurer
  
                  let rangeXY' = Range (x', y')
                  AvlTree.join rangeXY' left' right'
                  |> ensureCorrectlyFormed measurer
  
      // TEMP
      let rec addRange (measurer : IMeasurer<_>) rangePQ (tree : Diet<'T>) : Diet<_> =
          addRange' measurer rangePQ tree
          //|> ensureAllElementsUnique measurer
          |> ensureCorrectlyFormed measurer
  
      /// Returns a new set with the given element removed.
      /// No exception is thrown if the set doesn't contain the specified element.
  