anchor.diff:  
       /// The set containing the elements in the given range.
       static member FromRange (lowerBound, upperBound) : CharSet =
  -        CharSet (CharDiet.ofRange lowerBound upperBound)
  +        // For compatibility with the F# range operator, return an empty
  +        // set for an inverted range (i.e., when lowerBound > upperBound).
  +        if lowerBound > upperBound then empty
  +        elif lowerBound = upperBound then
  +            // This is a single-element range.
  +            CharSet.FromElement lowerBound
  +        else
  +            CharSet (CharDiet.ofRange lowerBound upperBound)
   
       //
       static member IsEmpty (charSet : CharSet) : bool =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.isEmpty charSet.Tree
   
       /// Returns a new set with an element added to the set.
       /// No exception is raised if the set already contains the given element.
       static member Add (value, charSet : CharSet) : CharSet =
  -        CharSet (CharDiet.add value charSet.Tree)
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
  +        // OPTIMIZATION : If the resulting tree is the same as the original, return
  +        // the input set instead of creating a new set.
  +        let result = CharDiet.add value charSet.Tree
  +        if charSet.Tree === result then charSet
  +        else CharSet (result)
   
       //
       static member AddRange (lower, upper, charSet : CharSet) : CharSet =
  -        CharSet (CharDiet.addRange (lower, upper) charSet.Tree)
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
  +        // For compatibility with the F# range operator, we don't raise
  +        // an exception if lower > upper; that is considered an empty range,
  +        // so here we just return the input set without modifying it.
  +        if lower > upper then charSet
  +        elif lower = upper then
  +            // Add the single value to the tree.
  +            CharSet.Add (lower, charSet)
  +        else
  +            // lower < upper
  +            // OPTIMIZATION : If the resulting tree is the same as the original,
  +            // return the input set instead of creating a new set.
  +            let result = CharDiet.addRange (lower, upper) charSet.Tree
  +            if charSet.Tree === result then charSet
  +            else CharSet (result)
   
       //
       static member Remove (value, charSet : CharSet) : CharSet =
  -        CharSet (CharDiet.remove value charSet.Tree)
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
  +        // OPTIMIZATION : If the resulting tree is the same as the original,
  +        // return the input set instead of creating a new set.
  +        let result = CharDiet.remove value charSet.Tree
  +        if charSet.Tree === result then charSet
  +        else CharSet (result)
   
       //
       static member Contains (value, charSet : CharSet) : bool =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.contains value charSet.Tree
   
       //
       static member OfSeq (sequence : seq<char>) : CharSet =
  -        CharSet (CharDiet.ofSeq sequence)
  +        // Preconditions
  +        checkNonNull "sequence" sequence
  +
  +        // If the sequence is empty, return immediately.
  +        if Seq.isEmpty sequence then empty
  +        else
  +            let tree =
  +                (Empty, sequence)
  +                ||> Seq.fold (fun tree el ->
  +                    CharDiet.add el tree)
  +            CharSet (tree)
   
       //
       static member OfList (list : char list) : CharSet =
  -        CharSet (CharDiet.ofList list)
  +        // Preconditions
  +        checkNonNull "list" list
  +
  +        // If the list is empty, return immediately.
  +        if List.isEmpty list then empty
  +        else
  +            let tree =
  +                (Empty, list)
  +                ||> List.fold (fun tree el ->
  +                    CharDiet.add el tree)
  +            CharSet (tree)
   
       //
       static member OfArray (array : char[]) : CharSet =
  -        CharSet (CharDiet.ofArray array)
  +        // Preconditions
  +        checkNonNull "array" array
  +
  +        // If the array is empty, return immediately.
  +        if Array.isEmpty array then empty
  +        else
  +            let tree =
  +                (Empty, array)
  +                ||> Array.fold (fun tree el ->
  +                    CharDiet.add el tree)
  +            CharSet (tree)
   
       //
       static member OfSet (set : Set<char>) : CharSet =
  -        CharSet (CharDiet.ofSet set)
  +        // Preconditions
  +        checkNonNull "set" set
  +
  +        // If the set is empty, return immediately.
  +        if Set.isEmpty set then empty
  +        else
  +            let tree =
  +                (Empty, set)
  +                ||> Set.fold (fun tree el ->
  +                    CharDiet.add el tree)
  +            CharSet (tree)
   
       //
       static member ToSeq (charSet : CharSet) : seq<char> =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.toSeq charSet.Tree
   
       //
       static member ToList (charSet : CharSet) : char list =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.toList charSet.Tree
   
       //
       static member ToArray (charSet : CharSet) : char[] =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.toArray charSet.Tree
   
       //
       static member ToSet (charSet : CharSet) : Set<char> =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.toSet charSet.Tree
   
       //
       static member Difference (charSet1 : CharSet, charSet2 : CharSet) : CharSet =
  -        CharSet (CharDiet.difference charSet1.Tree charSet2.Tree)
  +        // Preconditions
  +        checkNonNull "charSet1" charSet1
  +        checkNonNull "charSet2" charSet2
  +
  +        // OPTIMIZATION : If the result is the same as either input set's tree,
  +        // return that set instead of creating a new one.
  +        let result = CharDiet.difference charSet1.Tree charSet2.Tree
  +        if charSet1.Tree === result then charSet1
  +        elif charSet2.Tree === result then charSet2
  +        else CharSet (result)
   
       //
       static member Intersect (charSet1 : CharSet, charSet2 : CharSet) : CharSet =
  -        CharSet (CharDiet.intersect charSet1.Tree charSet2.Tree)
  +        // Preconditions
  +        checkNonNull "charSet1" charSet1
  +        checkNonNull "charSet2" charSet2
  +
  +        // OPTIMIZATION : If the result is the same as either input set's tree,
  +        // return that set instead of creating a new one.
  +        let result = CharDiet.intersect charSet1.Tree charSet2.Tree
  +        if charSet1.Tree === result then charSet1
  +        elif charSet2.Tree === result then charSet2
  +        else CharSet (result)
   
       //
       static member Union (charSet1 : CharSet, charSet2 : CharSet) : CharSet =
  -        CharSet (CharDiet.union charSet1.Tree charSet2.Tree)
  +        // Preconditions
  +        checkNonNull "charSet1" charSet1
  +        checkNonNull "charSet2" charSet2
  +
  +        // OPTIMIZATION : If the result is the same as either input set's tree,
  +        // return that set instead of creating a new one.
  +        let result = CharDiet.union charSet1.Tree charSet2.Tree
  +        if charSet1.Tree === result then charSet1
  +        elif charSet2.Tree === result then charSet2
  +        else CharSet (result)
   
       //
       static member Exists (predicate, charSet : CharSet) : bool =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.exists predicate charSet.Tree
   
       //
       static member Forall (predicate, charSet : CharSet) : bool =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.forall predicate charSet.Tree
   
       //
       static member Fold (folder : 'State -> _ -> 'State, state, charSet : CharSet) : 'State =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.fold folder state charSet.Tree
   
       //
       static member FoldBack (folder : _ -> 'State -> 'State, state, charSet : CharSet) : 'State =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.foldBack folder charSet.Tree state
   
       //
       static member Iter (action, charSet : CharSet) : unit =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           CharDiet.iter action charSet.Tree
   
       //
       static member IterIntervals (action, charSet : CharSet) : unit =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           let action = FSharpFunc<_,_,_>.Adapt action
           charSet.Tree |> AvlTree.Iter action.Invoke
   
       //
       static member Map (mapping : char -> char, charSet : CharSet) : CharSet =
  -        notImpl "CharSet.Map"
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
  +        let mappedTree =
  +            (CharDiet.empty, charSet.Tree)
  +            ||> CharDiet.fold (fun mappedTree ch ->
  +                CharDiet.add (mapping ch) mappedTree)
  +
  +        CharSet (mappedTree)
   
       //
       static member Filter (predicate : char -> bool, charSet : CharSet) : CharSet =
  -        notImpl "CharSet.Filter"
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
  +        let filteredTree =
  +            (charSet.Tree, charSet.Tree)
  +            ||> CharDiet.fold (fun filteredTree ch ->
  +                if predicate ch then
  +                    filteredTree
  +                else
  +                    CharDiet.remove ch filteredTree)
  +
  +        // OPTIMIZATION : If the filtered tree is the same as the input set's tree,
  +        // return the input set instead of creating a new CharSet with the same tree.
  +        if charSet.Tree === filteredTree then charSet
  +        else CharSet (filteredTree)
   
       //
       static member Partition (predicate : char -> bool, charSet : CharSet) : CharSet * CharSet =
  +        // Preconditions
  +        checkNonNull "charSet" charSet
  +
           notImpl "CharSet.Partition"
   
       interface System.IComparable with
  
positive.code:             #endif
              result
  
      /// Comparison function for DIETs.
      let rec comparison (t1 : CharDiet) (t2 : CharDiet) =
          match t1, t2 with
          | Node (_,_,_,_), Empty -> 1
          | Empty, Empty -> 0
          | Empty, Node (_,_,_,_) -> -1
          | Node (_,_,_,_), Node (_,_,_,_) ->
              let (ix1, iy1), r1 = AvlTree.ExtractMin t1
              let (ix2, iy2), r2 = AvlTree.ExtractMin t2
              let c =
                  match compare ix1 ix2 with
                  | 0 -> compare iy1 iy2
                  | d -> -d
              match c with
              | 0 -> comparison r1 r2
              | c -> c
  
      /// Equality function for DIETs.
      let equal (t1 : CharDiet) (t2 : CharDiet) =
          comparison t1 t2 = 0
  
      /// Applies the given accumulating function to all elements in a DIET.
      let fold (folder : 'State -> char -> 'State) (state : 'State) (tree : CharDiet) =
          // Preconditions
  
negative.code:         | tree ->
              minElement tree, maxElement tree
  
      /// Comparison function for DIETs.
      let rec comparison (t1 : CharDiet) (t2 : CharDiet) =
          match t1, t2 with
          | Node (_,_,_,_), Empty -> 1
          | Empty, Empty -> 0
          | Empty, Node (_,_,_,_) -> -1
          | Node (_,_,_,_), Node (_,_,_,_) ->
              let (ix1, iy1), r1 = AvlTree.ExtractMin t1
              let (ix2, iy2), r2 = AvlTree.ExtractMin t2
              let c =
                  match compare ix1 ix2 with
                  | 0 -> compare iy1 iy2
                  | d -> -d
              match c with
              | 0 -> comparison r1 r2
              | c -> c
  
      /// Equality function for DIETs.
      let equal (t1 : CharDiet) (t2 : CharDiet) =
          comparison t1 t2 = 0
  
      /// Returns the number of elements in the set.
      let count (t : CharDiet) =
  