------------------------------------------------------------------------------
--                                                                          --
--                   AI-302 Reference Implementation                        --
--                                                                          --
--              Copyright (C) 2003-2004 Matthew J Heaney                    --
--                                                                          --
-- The AI-302 Reference Implementation is free software; you can            --
-- redistribute it and/or modify it under terms of the GNU General Public   --
-- License as published by the Free Software Foundation; either version 2,  --
-- or (at your option) any later version.  AI-302 is distributed in the     --
-- hope that it will be useful, but WITHOUT ANY WARRANTY; without even the  --
-- implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- See the GNU General Public License for more details.  You should have    --
-- received a copy of the GNU General Public License distributed with       --
-- AI-302;  see file COPYING.TXT.  If not, write to the Free Software       --
-- Foundation,  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                          --
-- As a special exception, if other files instantiate generics from this    --
-- unit, or you link this unit with other files to produce an executable,   --
-- this unit does not by itself cause the resulting executable to be        --
-- covered by the GNU General Public License.  This exception does not      --
-- however invalidate any other reasons why the executable file might be    --
-- covered by the GNU Public License.                                       --
--                                                                          --
-- The AI-302 Reference Implementation is maintained by Matthew J Heaney.   --
--                                                                          --
-- mailto:matthewjheaney@earthlink.net                                      --
-- http://home.earthlink.net/~matthewjheaney/index.html                     --
-- http://charles.tigris.org/                                               --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Unchecked_Deallocation;

with Ada.Containers.Hash_Tables.Generic_Operations;
pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Operations);

with Ada.Containers.Hash_Tables.Generic_Keys;
pragma Elaborate_All (Ada.Containers.Hash_Tables.Generic_Keys);

with System.Address_To_Access_Conversions; use type System.Address;
pragma Elaborate_All (System.Address_To_Access_Conversions);

with Ada.Containers.Prime_Numbers;

package body Ada.Containers.Hashed_Sets is

   function Hash_Node
     (Node : Node_Access) return Hash_Type;
   pragma Inline (Hash_Node);

   function Hash_Node
     (Node : Node_Access) return Hash_Type is
   begin
      return Hash (Node.Element);
   end Hash_Node;

   function Next
     (Node : Node_Access) return Node_Access;
   pragma Inline (Next);

   function Next
     (Node : Node_Access) return Node_Access is
   begin
      return Node.Next;
   end Next;

   procedure Set_Next
     (Node : Node_Access;
      Next : Node_Access);
   pragma Inline (Set_Next);

   procedure Set_Next
     (Node : Node_Access;
      Next : Node_Access) is
   begin
      Node.Next := Next;
   end Set_Next;

   function Equivalent_Keys
     (Key  : Element_Type;
      Node : Node_Access) return Boolean;
   pragma Inline (Equivalent_Keys);

   function Equivalent_Keys
     (Key  : Element_Type;
      Node : Node_Access) return Boolean is
   begin
      return Equivalent_Elements (Key, Node.Element);
   end Equivalent_Keys;

   function Copy_Node
     (Source : Node_Access) return Node_Access;
   pragma Inline (Copy_Node);

   function Copy_Node
     (Source : Node_Access) return Node_Access is

      Target : constant Node_Access :=
        new Node_Type'(Element => Source.Element,
                       Next    => null);
   begin
      return Target;
   end Copy_Node;


   procedure Free is
      new Ada.Unchecked_Deallocation (Node_Type, Node_Access);

   package HT_Ops is
      new Hash_Tables.Generic_Operations
       (HT_Types          => HT_Types,
        Hash_Node         => Hash_Node,
        Next              => Next,
        Set_Next          => Set_Next,
        Copy_Node         => Copy_Node,
        Free              => Free);

   package Element_Keys is
      new Hash_Tables.Generic_Keys
       (HT_Types  => HT_Types,
        Next      => Next,
        Set_Next  => Set_Next,
        Key_Type  => Element_Type,
        Hash      => Hash,
        Equivalent_Keys => Equivalent_Keys);


   package Pointer_Types is
      new System.Address_To_Access_Conversions (Set);

   use Pointer_Types;


   procedure Adjust (Container : in out Set) is
   begin
      HT_Ops.Adjust (Container.HT);
   end Adjust;

   procedure Finalize (Container : in out Set) is
   begin
      HT_Ops.Finalize (Container.HT);
   end Finalize;


   function Find_Equal_Key
     (R_HT   : Hash_Table_Type;
      L_Node : Node_Access) return Boolean;

   function Find_Equal_Key
     (R_HT   : Hash_Table_Type;
      L_Node : Node_Access) return Boolean is

      R_Index : constant Hash_Type :=
        Element_Keys.Index (R_HT, L_Node.Element);

      R_Node  : Node_Access := R_HT.Buckets (R_Index);

   begin

      loop

         if R_Node = null then
            return False;
         end if;

         if L_Node.Element = R_Node.Element then
            return True;
         end if;

         R_Node := Next (R_Node);

      end loop;

   end Find_Equal_Key;

   function Is_Equal is
      new HT_Ops.Generic_Equal (Find_Equal_Key);

   function "=" (Left, Right : Set) return Boolean is
   begin
      return Is_Equal (Left.HT, Right.HT);
   end "=";


   function Find_Equivalent_Key
     (R_HT   : Hash_Table_Type;
      L_Node : Node_Access) return Boolean;

   function Find_Equivalent_Key
     (R_HT   : Hash_Table_Type;
      L_Node : Node_Access) return Boolean is

      R_Index : constant Hash_Type :=
        Element_Keys.Index (R_HT, L_Node.Element);

      R_Node  : Node_Access := R_HT.Buckets (R_Index);

   begin

      loop

         if R_Node = null then
            return False;
         end if;

         if Equivalent_Elements (L_Node.Element, R_Node.Element) then
            return True;
         end if;

         R_Node := Next (R_Node);

      end loop;

   end Find_Equivalent_Key;

   function Is_Equivalent is
      new HT_Ops.Generic_Equal (Find_Equivalent_Key);

   function Equivalent_Sets (Left, Right : Set) return Boolean is
   begin
      return Is_Equivalent (Left.HT, Right.HT);
   end Equivalent_Sets;


   function Length (Container : Set) return Count_Type is
   begin
      return Container.HT.Length;
   end Length;


   function Is_Empty (Container : Set) return Boolean is
   begin
      return Container.HT.Length = 0;
   end Is_Empty;


   procedure Clear (Container : in out Set) is
   begin
      HT_Ops.Clear (Container.HT);
   end Clear;


   function Element (Position : Cursor) return Element_Type is
   begin
      return Position.Node.Element;
   end Element;


   procedure Query_Element
     (Position : in Cursor;
      Process  : not null access procedure (Element : in Element_Type)) is

      E : Element_Type renames Position.Node.Element;

      C : Set renames To_Pointer (Position.Container.all'Address).all;
      B : Natural renames C.HT.Busy;
      L : Natural renames C.HT.Lock;

   begin

      B := B + 1;
      L := L + 1;

      begin
         Process (E);
      exception
         when others =>
            L := L - 1;
            B := B - 1;
            raise;
      end;

      L := L - 1;
      B := B - 1;

   end Query_Element;


   procedure Replace_Element (HT      : in out Hash_Table_Type;
                              Node    : in     Node_Access;
                              Element : in     Element_Type);

   procedure Replace_Element (HT      : in out Hash_Table_Type;
                              Node    : in     Node_Access;
                              Element : in     Element_Type) is
   begin

      if Equivalent_Elements (Node.Element, Element) then

         if HT.Lock > 0 then
            raise Program_Error;
         end if;

         pragma Assert (Hash (Node.Element) = Hash (Element));

         Node.Element := Element;  --  NOTE: assignment can fail

         return;

      end if;

      if HT.Busy > 0 then
         raise Program_Error;
      end if;

      HT_Ops.Delete_Node_Sans_Free (HT, Node);

      declare
         function New_Node (Next : Node_Access) return Node_Access;
         pragma Inline (New_Node);

         function New_Node (Next : Node_Access) return Node_Access is
         begin
            Node.Element := Element;  -- NOTE: assignment can fail
            Node.Next := Next;
            return Node;
         end New_Node;

         procedure Insert is
            new Element_Keys.Generic_Conditional_Insert (New_Node);

         Result   : Node_Access;
         Inserted : Boolean;
      begin
         Insert
           (HT       => HT,
            Key      => Element,
            Node     => Result,
            Inserted => Inserted);

         if Inserted then
            pragma Assert (Result = Node);
            return;
         end if;
      exception
         when others =>
            null;   -- assignment must have failed
      end;

      declare
         function New_Node (Next : Node_Access) return Node_Access;
         pragma Inline (New_Node);

         function New_Node (Next : Node_Access) return Node_Access is
         begin
            Node.Next := Next;
            return Node;
         end New_Node;

         procedure Insert is
            new Element_Keys.Generic_Conditional_Insert (New_Node);

         Result   : Node_Access;
         Inserted : Boolean;
      begin
         Insert
           (HT       => HT,
            Key      => Node.Element,
            Node     => Result,
            Inserted => Inserted);
      exception
         when others =>
            null;
      end;

      raise Program_Error;

   end Replace_Element;


   procedure Replace_Element (Container : in Set;
                              Position  : in Cursor;
                              By        : in Element_Type) is

      C : Set renames To_Pointer (Container'Address).all;

   begin

      if Position.Node = null then
         raise Constraint_Error;
      end if;

      if Position.Container /= Set_Access'(Container'Unchecked_Access) then
         raise Program_Error;
      end if;

      Replace_Element (C.HT, Position.Node, By);

   end Replace_Element;


   procedure Move (Target : in out Set;
                   Source : in out Set) is
   begin
      HT_Ops.Move (Target.HT, Source.HT);
   end Move;


   procedure Insert (Container : in out Set;
                     New_Item  : in     Element_Type;
                     Position  :    out Cursor;
                     Inserted  :    out Boolean) is

      function New_Node (Next : Node_Access) return Node_Access;
      pragma Inline (New_Node);

      function New_Node (Next : Node_Access) return Node_Access is
         Node : constant Node_Access := new Node_Type'(New_Item, Next);
      begin
         return Node;
      end New_Node;

      procedure Insert is
        new Element_Keys.Generic_Conditional_Insert (New_Node);

      HT : Hash_Table_Type renames Container.HT;

   begin

      if HT_Ops.Capacity (HT) = 0 then
         HT_Ops.Reserve_Capacity (HT, 1);
      end if;

      Insert (HT, New_Item, Position.Node, Inserted);

      if Inserted
        and then HT.Length > HT_Ops.Capacity (HT)
      then
         HT_Ops.Reserve_Capacity (HT, HT.Length);
      end if;

      Position.Container := Container'Unchecked_Access;

   end Insert;


   procedure Insert (Container : in out Set;
                     New_Item  : in     Element_Type) is

      Position : Cursor;
      Inserted : Boolean;

   begin

      Insert (Container, New_Item, Position, Inserted);

      if not Inserted then
         raise Constraint_Error;
      end if;

   end Insert;


   procedure Replace (Container : in out Set;
                      New_Item  : in     Element_Type) is

      Node : constant Node_Access :=
        Element_Keys.Find (Container.HT, New_Item);

   begin

      if Node = null then
         raise Constraint_Error;
      end if;

      if Container.HT.Lock > 0 then
         raise Program_Error;
      end if;

      Node.Element := New_Item;

   end Replace;


   procedure Include (Container : in out Set;
                      New_Item  : in     Element_Type) is

      Position : Cursor;
      Inserted : Boolean;

   begin

      Insert (Container, New_Item, Position, Inserted);

      if not Inserted then

         if Container.HT.Lock > 0 then
            raise Program_Error;
         end if;

         Position.Node.Element := New_Item;

      end if;

   end Include;


   procedure Delete (Container : in out Set;
                     Item      : in     Element_Type) is

      X : Node_Access;

   begin

      Element_Keys.Delete_Key_Sans_Free (Container.HT, Item, X);

      if X = null then
         raise Constraint_Error;
      end if;

      Free (X);

   end Delete;


   procedure Exclude (Container : in out Set;
                      Item      : in     Element_Type) is

      X : Node_Access;

   begin

      Element_Keys.Delete_Key_Sans_Free (Container.HT, Item, X);

      Free (X);

   end Exclude;


   procedure Delete (Container : in out Set;
                     Position  : in out Cursor) is
   begin

      if Position.Node = null then  --  TODO: need ruling from ARG
         raise Constraint_Error;
      end if;

      if Position.Container /= Set_Access'(Container'Unchecked_Access) then
         raise Program_Error;
      end if;

      if Container.HT.Busy > 0 then
         raise Program_Error;
      end if;

      HT_Ops.Delete_Node_Sans_Free (Container.HT, Position.Node);

      Free (Position.Node);

      Position.Container := null;

   end Delete;



   procedure Union (Target : in out Set;
                    Source : in     Set) is

      procedure Process (Src_Node : in Node_Access);

      procedure Process (Src_Node : in Node_Access) is

         function New_Node (Next : Node_Access) return Node_Access;
         pragma Inline (New_Node);

         function New_Node (Next : Node_Access) return Node_Access is
            Node : constant Node_Access :=
              new Node_Type'(Src_Node.Element, Next);
         begin
            return Node;
         end New_Node;

         procedure Insert is
            new Element_Keys.Generic_Conditional_Insert (New_Node);

         Tgt_Node : Node_Access;
         Success  : Boolean;

      begin

         Insert (Target.HT, Src_Node.Element, Tgt_Node, Success);

      end Process;

      procedure Iterate is
         new HT_Ops.Generic_Iteration (Process);

   begin

      if Target'Address = Source'Address then
         return;
      end if;

      if Target.HT.Busy > 0 then
         raise Program_Error;
      end if;

      declare
         N : constant Count_Type := Target.Length + Source.Length;
      begin
         if N > HT_Ops.Capacity (Target.HT) then
            HT_Ops.Reserve_Capacity (Target.HT, N);
         end if;
      end;

      Iterate (Source.HT);

   end Union;



   function Union (Left, Right : Set) return Set is

      Buckets : HT_Types.Buckets_Access;
      Length  : Count_Type;

   begin

      if Left'Address = Right'Address then
         return Left;
      end if;

      if Right.Length = 0 then
         return Left;
      end if;

      if Left.Length = 0 then
         return Right;
      end if;

      declare
         Size : constant Hash_Type :=
           Prime_Numbers.To_Prime (Left.Length + Right.Length);
      begin
         Buckets := new Buckets_Type (0 .. Size - 1);
      end;

      declare
         procedure Process (L_Node : Node_Access);

         procedure Process (L_Node : Node_Access) is
            I : constant Hash_Type :=
              Hash (L_Node.Element) mod Buckets'Length;
         begin
            Buckets (I) := new Node_Type'(L_Node.Element, Buckets (I));
         end Process;

         procedure Iterate is
            new HT_Ops.Generic_Iteration (Process);
      begin
         Iterate (Left.HT);
      exception
         when others =>
            HT_Ops.Free_Hash_Table (Buckets);
            raise;
      end;

      Length := Left.Length;

      declare
         procedure Process (Src_Node : Node_Access);

         procedure Process (Src_Node : Node_Access) is

            I : constant Hash_Type :=
              Hash (Src_Node.Element) mod Buckets'Length;

            Tgt_Node : Node_Access := Buckets (I);

         begin

            while Tgt_Node /= null loop

               if Equivalent_Elements (Src_Node.Element, Tgt_Node.Element) then
                  return;
               end if;

               Tgt_Node := Next (Tgt_Node);

            end loop;

            Buckets (I) := new Node_Type'(Src_Node.Element, Buckets (I));
            Length := Length + 1;

         end Process;

         procedure Iterate is
            new HT_Ops.Generic_Iteration (Process);
      begin
         Iterate (Right.HT);
      exception
         when others =>
            HT_Ops.Free_Hash_Table (Buckets);
            raise;
      end;

      return (Controlled with HT => (Buckets, Length, 0, 0));

   end Union;


   function Is_In
     (HT  : Hash_Table_Type;
      Key : Node_Access) return Boolean;
   pragma Inline (Is_In);

   function Is_In
     (HT  : Hash_Table_Type;
      Key : Node_Access) return Boolean is
   begin
      return Element_Keys.Find (HT, Key.Element) /= null;
   end Is_In;


   procedure Intersection (Target : in out Set;
                           Source : in     Set) is

      Tgt_Node : Node_Access;

   begin

      if Target'Address = Source'Address then
         return;
      end if;

      if Source.Length = 0 then
         Clear (Target);
         return;
      end if;

      if Target.HT.Busy > 0 then
         raise Program_Error;
      end if;

      --  TODO: optimize this to use an explicit
      --  loop instead of an active iterator
      --  (similar to how a passive iterator is
      --  implemented).
      --
      --  Another possibility is to test which
      --  set is smaller, and iterate over the
      --  smaller set.

      Tgt_Node := HT_Ops.First (Target.HT);

      while Tgt_Node /= null loop

         if Is_In (Source.HT, Tgt_Node) then

            Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);

         else

            declare
               X : Node_Access := Tgt_Node;
            begin
               Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
               HT_Ops.Delete_Node_Sans_Free (Target.HT, X);
               Free (X);
            end;

         end if;

      end loop;

   end Intersection;


   function Intersection (Left, Right : Set) return Set is

      Buckets : HT_Types.Buckets_Access;
      Length  : Count_Type;

   begin

      if Left'Address = Right'Address then
         return Left;
      end if;

      Length := Count_Type'Min (Left.Length, Right.Length);

      if Length = 0 then
         return Empty_Set;
      end if;

      declare
         Size : constant Hash_Type := Prime_Numbers.To_Prime (Length);
      begin
         Buckets := new Buckets_Type (0 .. Size - 1);
      end;

      Length := 0;

      declare
         procedure Process (L_Node : Node_Access);

         procedure Process (L_Node : Node_Access) is
         begin
            if Is_In (Right.HT, L_Node) then

               declare
                  I : constant Hash_Type :=
                    Hash (L_Node.Element) mod Buckets'Length;
               begin
                  Buckets (I) := new Node_Type'(L_Node.Element, Buckets (I));
               end;

               Length := Length + 1;

            end if;
         end Process;

         procedure Iterate is
            new HT_Ops.Generic_Iteration (Process);
      begin
         Iterate (Left.HT);
      exception
         when others =>
            HT_Ops.Free_Hash_Table (Buckets);
            raise;
      end;

      return (Controlled with (Buckets, Length, 0, 0));

   end Intersection;


   procedure Difference (Target : in out Set;
                         Source : in     Set) is


      Tgt_Node : Node_Access;

   begin

      if Target'Address = Source'Address then
         Clear (Target);
         return;
      end if;

      if Source.Length = 0 then
         return;
      end if;

      if Target.HT.Busy > 0 then
         raise Program_Error;
      end if;

      --  TODO: As I noted above, this can be
      --  written in terms of a loop instead as
      --  active-iterator style, sort of like a
      --  passive iterator.

      Tgt_Node := HT_Ops.First (Target.HT);

      while Tgt_Node /= null loop

         if Is_In (Source.HT, Tgt_Node) then

            declare
               X : Node_Access := Tgt_Node;
            begin
               Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);
               HT_Ops.Delete_Node_Sans_Free (Target.HT, X);
               Free (X);
            end;

         else

            Tgt_Node := HT_Ops.Next (Target.HT, Tgt_Node);

         end if;

      end loop;

   end Difference;



   function Difference (Left, Right : Set) return Set is

      Buckets : HT_Types.Buckets_Access;
      Length  : Count_Type;

   begin

      if Left'Address = Right'Address then
         return Empty_Set;
      end if;

      if Left.Length = 0 then
         return Empty_Set;
      end if;

      if Right.Length = 0 then
         return Left;
      end if;

      declare
         Size : constant Hash_Type := Prime_Numbers.To_Prime (Left.Length);
      begin
         Buckets := new Buckets_Type (0 .. Size - 1);
      end;

      Length := 0;

      declare
         procedure Process (L_Node : Node_Access);

         procedure Process (L_Node : Node_Access) is
         begin
            if not Is_In (Right.HT, L_Node) then

               declare
                  I : constant Hash_Type :=
                    Hash (L_Node.Element) mod Buckets'Length;
               begin
                  Buckets (I) := new Node_Type'(L_Node.Element, Buckets (I));
               end;

               Length := Length + 1;

            end if;
         end Process;

         procedure Iterate is
            new HT_Ops.Generic_Iteration (Process);
      begin
         Iterate (Left.HT);
      exception
         when others =>
            HT_Ops.Free_Hash_Table (Buckets);
            raise;
      end;

      return (Controlled with (Buckets, Length, 0, 0));

   end Difference;



   procedure Symmetric_Difference (Target : in out Set;
                                   Source : in     Set) is
   begin

      if Target'Address = Source'Address then
         Clear (Target);
         return;
      end if;

      if Target.HT.Busy > 0 then
         raise Program_Error;
      end if;

      declare
         N : constant Count_Type := Target.Length + Source.Length;
      begin
         if N > HT_Ops.Capacity (Target.HT) then
            HT_Ops.Reserve_Capacity (Target.HT, N);
         end if;
      end;

      if Target.Length = 0 then

         declare
            procedure Process (Src_Node : Node_Access);

            procedure Process (Src_Node : Node_Access) is
               E : Element_Type renames Src_Node.Element;
               B : Buckets_Type renames Target.HT.Buckets.all;
               I : constant Hash_Type := Hash (E) mod B'Length;
               N : Count_Type renames Target.HT.Length;
            begin
               B (I) := new Node_Type'(E, B (I));
               N := N + 1;
            end Process;

            procedure Iterate is
               new HT_Ops.Generic_Iteration (Process);
         begin
            Iterate (Source.HT);
         end;

      else

         declare
            procedure Process (Src_Node : Node_Access);

            procedure Process (Src_Node : Node_Access) is
               E : Element_Type renames Src_Node.Element;
               B : Buckets_Type renames Target.HT.Buckets.all;
               I : constant Hash_Type := Hash (E) mod B'Length;
               N : Count_Type renames Target.HT.Length;
            begin
               if B (I) = null then

                  B (I) := new Node_Type'(E, null);
                  N := N + 1;

               elsif Equivalent_Elements (E, B (I).Element) then

                  declare
                     X : Node_Access := B (I);
                  begin
                     B (I) := B (I).Next;
                     N := N - 1;
                     Free (X);
                  end;

               else

                  declare
                     Prev : Node_Access := B (I);
                     Curr : Node_Access := Prev.Next;
                  begin
                     while Curr /= null loop
                        if Equivalent_Elements (E, Curr.Element) then
                           Prev.Next := Curr.Next;
                           N := N - 1;
                           Free (Curr);
                           return;
                        end if;

                        Prev := Curr;
                        Curr := Prev.Next;
                     end loop;

                     B (I) := new Node_Type'(E, B (I));
                     N := N + 1;
                  end;

               end if;
            end Process;

            procedure Iterate is
               new HT_Ops.Generic_Iteration (Process);
         begin
            Iterate (Source.HT);
         end;

      end if;

   end Symmetric_Difference;


   function Symmetric_Difference (Left, Right : Set) return Set is

      Buckets : HT_Types.Buckets_Access;
      Length  : Count_Type;

   begin

      if Left'Address = Right'Address then
         return Empty_Set;
      end if;

      if Right.Length = 0 then
         return Left;
      end if;

      if Left.Length = 0 then
         return Right;
      end if;

      declare
         Size : constant Hash_Type :=
           Prime_Numbers.To_Prime (Left.Length + Right.Length);
      begin
         Buckets := new Buckets_Type (0 .. Size - 1);
      end;

      Length := 0;

      declare
         procedure Process (L_Node : Node_Access);

         procedure Process (L_Node : Node_Access) is
         begin
            if not Is_In (Right.HT, L_Node) then
               declare
                  E : Element_Type renames L_Node.Element;
                  I : constant Hash_Type := Hash (E) mod Buckets'Length;
               begin
                  Buckets (I) := new Node_Type'(E, Buckets (I));
                  Length := Length + 1;
               end;
            end if;
         end Process;

         procedure Iterate is
            new HT_Ops.Generic_Iteration (Process);
      begin
         Iterate (Left.HT);
      exception
         when others =>
            HT_Ops.Free_Hash_Table (Buckets);
            raise;
      end;

      declare
         procedure Process (R_Node : Node_Access);

         procedure Process (R_Node : Node_Access) is
         begin
            if not Is_In (Left.HT, R_Node) then
               declare
                  E : Element_Type renames R_Node.Element;
                  I : constant Hash_Type := Hash (E) mod Buckets'Length;
               begin
                  Buckets (I) := new Node_Type'(E, Buckets (I));
                  Length := Length + 1;
               end;
            end if;
         end Process;

         procedure Iterate is
            new HT_Ops.Generic_Iteration (Process);
      begin
         Iterate (Right.HT);
      exception
         when others =>
            HT_Ops.Free_Hash_Table (Buckets);
            raise;
      end;

      return (Controlled with (Buckets, Length, 0, 0));

   end Symmetric_Difference;


   function Is_Subset (Subset : Set;
                       Of_Set : Set) return Boolean is

      Subset_Node : Node_Access;

   begin

      if Subset'Address = Of_Set'Address then
         return True;
      end if;

      if Subset.Length > Of_Set.Length then
         return False;
      end if;

      --  TODO: rewrite this to loop in the
      --  style of a passive iterator.

      Subset_Node := HT_Ops.First (Subset.HT);

      while Subset_Node /= null loop
         if not Is_In (Of_Set.HT, Subset_Node) then
            return False;
         end if;

         Subset_Node := HT_Ops.Next (Subset.HT, Subset_Node);
      end loop;

      return True;

   end Is_Subset;


   function Overlap (Left, Right : Set) return Boolean is

      Left_Node : Node_Access;

   begin

      if Right.Length = 0 then
         return False;
      end if;

      if Left'Address = Right'Address then
         return True;
      end if;

      --  TODO: iterate over the smaller set

      Left_Node := HT_Ops.First (Left.HT);

      while Left_Node /= null loop
         if Is_In (Right.HT, Left_Node) then
            return True;
         end if;

         Left_Node := HT_Ops.Next (Left.HT, Left_Node);
      end loop;

      return False;

   end Overlap;


   function Find (Container : Set;
                  Item      : Element_Type) return Cursor is

      Node : constant Node_Access := Element_Keys.Find (Container.HT, Item);

   begin

      if Node = null then
         return No_Element;
      end if;

      return Cursor'(Container'Unchecked_Access, Node);

   end Find;


   function Contains (Container : Set;
                      Item      : Element_Type) return Boolean is
   begin
      return Find (Container, Item) /= No_Element;
   end Contains;



   function First (Container : Set) return Cursor is
      Node : constant Node_Access := HT_Ops.First (Container.HT);
   begin
      if Node = null then
         return No_Element;
      end if;

      return Cursor'(Container'Unchecked_Access, Node);
   end First;


   function Next (Position : Cursor) return Cursor is
   begin
      if Position.Container = null
        or else Position.Node = null
      then
         return No_Element;
      end if;

      declare
         S : Set renames Position.Container.all;
         Node : constant Node_Access := HT_Ops.Next (S.HT, Position.Node);
      begin
         if Node = null then
            return No_Element;
         end if;

         return Cursor'(Position.Container, Node);
      end;
   end Next;


   procedure Next (Position : in out Cursor) is
   begin
      Position := Next (Position);
   end Next;


   function Has_Element (Position : Cursor) return Boolean is
   begin
      if Position.Container = null then
         return False;
      end if;

      if Position.Node = null then
         return False;
      end if;

      return True;
   end Has_Element;


   function Equivalent_Elements (Left, Right : Cursor)
     return Boolean is
   begin
      return Equivalent_Elements (Left.Node.Element, Right.Node.Element);
   end Equivalent_Elements;


   function Equivalent_Elements (Left  : Cursor;
                                 Right : Element_Type)
    return Boolean is
   begin
      return Equivalent_Elements (Left.Node.Element, Right);
   end Equivalent_Elements;


   function Equivalent_Elements (Left  : Element_Type;
                                 Right : Cursor)
    return Boolean is
   begin
      return Equivalent_Elements (Left, Right.Node.Element);
   end Equivalent_Elements;


   procedure Iterate
     (Container : in Set;
      Process   : not null access procedure (Position : in Cursor)) is

      procedure Process_Node (Node : in Node_Access);
      pragma Inline (Process_Node);

      procedure Process_Node (Node : in Node_Access) is
      begin
         Process (Cursor'(Container'Unchecked_Access, Node));
      end Process_Node;

      procedure Iterate is
         new HT_Ops.Generic_Iteration (Process_Node);

      C : Set renames To_Pointer (Container'Address).all;
      B : Natural renames C.HT.Busy;

   begin

      B := B + 1;

      begin
         Iterate (C.HT);
      exception
         when others =>
            B := B - 1;
            raise;
      end;

      B := B - 1;

   end Iterate;


   function Capacity (Container : Set) return Count_Type is
   begin
      return HT_Ops.Capacity (Container.HT);
   end Capacity;

   procedure Reserve_Capacity
     (Container : in out Set;
      Capacity  : in     Count_Type) is
   begin
      HT_Ops.Reserve_Capacity (Container.HT, Capacity);
   end Reserve_Capacity;


   procedure Write_Node
     (Stream : access Root_Stream_Type'Class;
      Node   : in     Node_Access);
   pragma Inline (Write_Node);

   procedure Write_Node
     (Stream : access Root_Stream_Type'Class;
      Node   : in     Node_Access) is
   begin
      Element_Type'Write (Stream, Node.Element);
   end Write_Node;

   procedure Write_Nodes is
      new HT_Ops.Generic_Write (Write_Node);

   procedure Write
     (Stream    : access Root_Stream_Type'Class;
      Container : in     Set) is
   begin
      Write_Nodes (Stream, Container.HT);
   end Write;


   function Read_Node (Stream : access Root_Stream_Type'Class)
     return Node_Access;
   pragma Inline (Read_Node);

   function Read_Node (Stream : access Root_Stream_Type'Class)
     return Node_Access is

      Node : Node_Access := new Node_Type;
   begin
      Element_Type'Read (Stream, Node.Element);
      return Node;
   exception
      when others =>
         Free (Node);
         raise;
   end Read_Node;

   procedure Read_Nodes is
      new HT_Ops.Generic_Read (Read_Node);

   procedure Read
     (Stream    : access Root_Stream_Type'Class;
      Container :    out Set) is
   begin
      Read_Nodes (Stream, Container.HT);
   end Read;


   package body Generic_Keys is

      function Equivalent_Keys (Left  : Cursor;
                                Right : Key_Type)
        return Boolean is
      begin
         return Equivalent_Keys (Right, Left.Node.Element);
      end Equivalent_Keys;

      function Equivalent_Keys (Left  : Key_Type;
                                Right : Cursor)
        return Boolean is
      begin
         return Equivalent_Keys (Left, Right.Node.Element);
      end Equivalent_Keys;

      function Equivalent_Keys
        (Key  : Key_Type;
         Node : Node_Access) return Boolean;
      pragma Inline (Equivalent_Keys);

      function Equivalent_Keys
        (Key  : Key_Type;
         Node : Node_Access) return Boolean is
      begin
         return Equivalent_Keys (Key, Node.Element);
      end Equivalent_Keys;

      package Key_Keys is
         new Hash_Tables.Generic_Keys
          (HT_Types  => HT_Types,
           Next      => Next,
           Set_Next  => Set_Next,
           Key_Type  => Key_Type,
           Hash      => Hash,
           Equivalent_Keys => Equivalent_Keys);


      function Find (Container : Set;
                     Key       : Key_Type)
         return Cursor is

         Node : constant Node_Access :=
           Key_Keys.Find (Container.HT, Key);

      begin

         if Node = null then
            return No_Element;
         end if;

         return Cursor'(Container'Unchecked_Access, Node);

      end Find;


      function Contains (Container : Set;
                         Key       : Key_Type) return Boolean is
      begin
         return Find (Container, Key) /= No_Element;
      end Contains;


      function Element (Container : Set;
                        Key       : Key_Type)
        return Element_Type is

         Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);
      begin
         return Node.Element;
      end Element;


      function Key (Position : Cursor) return Key_Type is
      begin
         return Key (Position.Node.Element);
      end Key;


      procedure Replace (Container : in out Set;       --  TODO: why inout?
                         Key       : in     Key_Type;
                         New_Item  : in     Element_Type) is

         Node : constant Node_Access := Key_Keys.Find (Container.HT, Key);

      begin

         if Node = null then
            raise Constraint_Error;
         end if;

         Replace_Element (Container.HT, Node, New_Item);

      end Replace;


      procedure Delete (Container : in out Set;
                        Key       : in     Key_Type) is

         X : Node_Access;

      begin

         Key_Keys.Delete_Key_Sans_Free (Container.HT, Key, X);

         if X = null then
            raise Constraint_Error;
         end if;

         Free (X);

      end Delete;


      procedure Exclude (Container : in out Set;
                         Key       : in     Key_Type) is

         X : Node_Access;

      begin

         Key_Keys.Delete_Key_Sans_Free (Container.HT, Key, X);
         Free (X);

      end Exclude;


      procedure Update_Element_Preserving_Key
        (Container : in out Set;
         Position  : in     Cursor;
         Process   : not null access
           procedure (Element : in out Element_Type)) is

         HT : Hash_Table_Type renames Container.HT;
         I  : Hash_Type;

      begin

         if Position.Node = null then
            raise Constraint_Error;
         end if;

         if Position.Container /= Set_Access'(Container'Unchecked_Access) then
            raise Program_Error;
         end if;

         if HT.Buckets = null
           or else HT.Buckets'Length = 0
           or else HT.Length = 0
           or else Position.Node.Next = Position.Node
         then
            raise Program_Error;
         end if;

         I := HT_Ops.Index (HT, Position.Node);

         declare
            E : Element_Type renames Position.Node.Element;
            K : Key_Type renames Key (E);

            B : Natural renames HT.Busy;
            L : Natural renames HT.Lock;
         begin
            B := B + 1;
            L := L + 1;

            begin
               Process (E);
            exception
               when others =>
                  L := L - 1;
                  B := B - 1;
                  raise;
            end;

            L := L - 1;
            B := B - 1;

            if Equivalent_Keys (K, E) then
               pragma Assert (Hash (K) = Hash (E));
               return;
            end if;
         end;

         if HT.Buckets (I) = Position.Node then
            HT.Buckets (I) := Position.Node.Next;
         else
            declare
               Prev : Node_Access := HT.Buckets (I);
            begin
               while Prev.Next /= Position.Node loop
                  Prev := Prev.Next;

                  if Prev = null then
                     raise Program_Error;
                  end if;
               end loop;

               Prev.Next := Position.Node.Next;
            end;
         end if;

         HT.Length := HT.Length - 1;

         declare
            X : Node_Access := Position.Node;
         begin
            Free (X);
         end;

         raise Program_Error;

      end Update_Element_Preserving_Key;

   end Generic_Keys;

end Ada.Containers.Hashed_Sets;

