Natools

Diff
Login

Differences From Artifact [e2501f2237]:

To Artifact [814ac130dc]:


1
2
3
4
5
6
7
8
9
------------------------------------------------------------------------------
-- Copyright (c) 2014, Natacha Porté                                        --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --

|







1
2
3
4
5
6
7
8
9
------------------------------------------------------------------------------
-- Copyright (c) 2014-2015, Natacha Porté                                   --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --
650
651
652
653
654
655
656


















































































































































































































































































































































657
658
659
660
661
662
663

      for I in reverse Container.Backend.Query.Data.Nodes'Range loop
         Position.Index := I;
         Process.all (Position);
      end loop;
   end Reverse_Iterate;





















































































































































































































































































































































   ------------------------------
   -- Updatable Map Operations --
   ------------------------------

   function Reference







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001

      for I in reverse Container.Backend.Query.Data.Nodes'Range loop
         Position.Index := I;
         Process.all (Position);
      end loop;
   end Reverse_Iterate;



   ----------------------------------------
   -- Constant Map "Update" Constructors --
   ----------------------------------------

   function Insert
     (Source : in Constant_Map;
      Key : in Key_Type;
      New_Item : in Element_Type;
      Position : out Cursor;
      Inserted : out Boolean)
     return Constant_Map
   is
      Floor, Ceiling : Count_Type;
   begin
      if Source.Is_Empty then
         declare
            Backend : constant Backend_Refs.Data_Access := new Backend_Array'
              (Ada.Finalization.Limited_Controlled with
               Size => 1,
               Nodes => (1 => (Key => new Key_Type'(Key),
                               Element => new Element_Type'(New_Item))),
               Finalized => False);
            Result : constant Constant_Map
              := (Backend => Backend_Refs.Create (Backend));
         begin
            Position := (Is_Empty => False,
               Backend => Result.Backend,
               Index => 1);
            Inserted := True;
            return Result;
         end;
      end if;

      Search (Source.Backend.Query.Data.Nodes, Key, Floor, Ceiling);

      if Floor = Ceiling then
         Position := (Is_Empty => False,
            Backend => Source.Backend,
            Index => Floor);
         Inserted := False;
         return Source;
      end if;

      declare
         function Key_Factory (Index : Index_Type) return Key_Type;
         function Element_Factory (Index : Index_Type) return Element_Type;

         Accessor : constant Backend_Refs.Accessor := Source.Backend.Query;

         function Key_Factory (Index : Index_Type) return Key_Type is
         begin
            if Index <= Floor then
               return Accessor.Nodes (Index).Key.all;
            elsif Index = Floor + 1 then
               return Key;
            else
               return Accessor.Nodes (Index - 1).Key.all;
            end if;
         end Key_Factory;

         function Element_Factory (Index : Index_Type) return Element_Type is
         begin
            if Index <= Floor then
               return Accessor.Nodes (Index).Element.all;
            elsif Index = Floor + 1 then
               return New_Item;
            else
               return Accessor.Nodes (Index - 1).Element.all;
            end if;
         end Element_Factory;

         Result : constant Constant_Map := (Backend => Make_Backend
           (Accessor.Size + 1, Key_Factory'Access, Element_Factory'Access));
      begin
         Position := (Is_Empty => False,
            Backend => Result.Backend,
            Index => Floor + 1);
         Inserted := True;
         return Result;
      end;
   end Insert;


   function Insert
     (Source : in Constant_Map;
      Key : in Key_Type;
      New_Item : in Element_Type)
     return Constant_Map
   is
      Position : Cursor;
      Inserted : Boolean;
      Result : constant Constant_Map
        := Insert (Source, Key, New_Item, Position, Inserted);
   begin
      if not Inserted then
         raise Constraint_Error with "Inserted key already in Constant_Map";
      end if;

      return Result;
   end Insert;


   function Include
     (Source : in Constant_Map;
      Key : in Key_Type;
      New_Item : in Element_Type)
     return Constant_Map
   is
      Position : Cursor;
      Inserted : Boolean;
      Result : constant Constant_Map
        := Insert (Source, Key, New_Item, Position, Inserted);
   begin
      if Inserted then
         return Result;
      end if;

      declare
         function Key_Factory (Index : Index_Type) return Key_Type;
         function Element_Factory (Index : Index_Type) return Element_Type;

         Accessor : constant Backend_Refs.Accessor := Source.Backend.Query;

         function Key_Factory (Index : Index_Type) return Key_Type is
         begin
            if Index = Position.Index then
               return Key;
            else
               return Accessor.Nodes (Index).Key.all;
            end if;
         end Key_Factory;

         function Element_Factory (Index : Index_Type) return Element_Type is
         begin
            if Index = Position.Index then
               return New_Item;
            else
               return Accessor.Nodes (Index).Element.all;
            end if;
         end Element_Factory;

         Result : constant Constant_Map := (Backend => Make_Backend
           (Accessor.Size, Key_Factory'Access, Element_Factory'Access));
      begin
         return Result;
      end;
   end Include;


   function Replace
     (Source : in Constant_Map;
      Key : in Key_Type;
      New_Item : in Element_Type)
     return Constant_Map
   is
      Floor, Ceiling : Count_Type;
   begin
      if Source.Is_Empty then
         raise Constraint_Error with "Replace called on empty Constant_Map";
      end if;

      Search (Source.Backend.Query.Data.Nodes, Key, Floor, Ceiling);

      if Floor /= Ceiling then
         raise Constraint_Error
           with "Replace called with key not in Constant_Map";
      end if;

      return Replace_Element
        (Source => Source,
         Position =>
           (Is_Empty => False,
            Backend => Source.Backend,
            Index => Floor),
         New_Item => New_Item);
   end Replace;


   function Replace_Element
     (Source : in Constant_Map;
      Position : in Cursor;
      New_Item : in Element_Type)
     return Constant_Map
   is
      use type Backend_Refs.Immutable_Reference;
   begin
      if Position.Is_Empty then
         raise Constraint_Error
           with "Constant_Map.Replace_Element called with empty cursor";
      end if;

      if Source.Backend /= Position.Backend then
         raise Program_Error with "Constant_Map.Replace_Element "
           & "with unrelated container and cursor";
      end if;

      declare
         function Key_Factory (Index : Index_Type) return Key_Type;
         function Element_Factory (Index : Index_Type) return Element_Type;

         Accessor : constant Backend_Refs.Accessor := Source.Backend.Query;

         function Key_Factory (Index : Index_Type) return Key_Type is
         begin
            return Accessor.Nodes (Index).Key.all;
         end Key_Factory;

         function Element_Factory (Index : Index_Type) return Element_Type is
         begin
            if Index = Position.Index then
               return New_Item;
            else
               return Accessor.Nodes (Index).Element.all;
            end if;
         end Element_Factory;

         Result : constant Constant_Map := (Backend => Make_Backend
           (Accessor.Size, Key_Factory'Access, Element_Factory'Access));
      begin
         return Result;
      end;
   end Replace_Element;


   function Replace_Element
     (Source : in Constant_Map;
      Position : in Cursor;
      New_Item : in Element_Type;
      New_Position : out Cursor)
     return Constant_Map
   is
      Result : constant Constant_Map
        := Replace_Element (Source, Position, New_Item);
   begin
      New_Position :=
        (Is_Empty => False,
         Backend => Result.Backend,
         Index => Position.Index);
      return Result;
   end Replace_Element;


   function Exclude
     (Source : in Constant_Map;
      Key : in Key_Type)
     return Constant_Map
   is
      Floor, Ceiling : Count_Type;
   begin
      if Source.Is_Empty then
         return Source;
      end if;

      Search (Source.Backend.Query.Data.Nodes, Key, Floor, Ceiling);

      if Floor = Ceiling then
         return Delete
           (Source,
            Cursor'(Is_Empty => False,
                    Backend => Source.Backend,
                    Index => Floor));
      else
         return Source;
      end if;
   end Exclude;


   function Delete
     (Source : in Constant_Map;
      Key : in Key_Type)
     return Constant_Map
   is
      Floor, Ceiling : Count_Type;
   begin
      if Source.Is_Empty then
         raise Constraint_Error with "Delete called on empty Constant_Map";
      end if;

      Search (Source.Backend.Query.Data.Nodes, Key, Floor, Ceiling);

      if Floor /= Ceiling then
         raise Constraint_Error with "Deleted key not in Constant_Map";
      end if;

      return Delete (Source,
        (Is_Empty => False, Backend => Source.Backend, Index => Floor));
   end Delete;


   function Delete
     (Source : in Constant_Map;
      Position : in Cursor)
     return Constant_Map
   is
      use type Backend_Refs.Immutable_Reference;
   begin
      if Position.Is_Empty then
         raise Constraint_Error with "Constant_Map.Delete with empty cursor";
      end if;

      if Source.Backend /= Position.Backend then
         raise Program_Error
           with "Constant_Map.Delete with unrelated container and cursor";
      end if;

      declare
         function Key_Factory (Index : Index_Type) return Key_Type;
         function Element_Factory (Index : Index_Type) return Element_Type;

         Accessor : constant Backend_Refs.Accessor := Source.Backend.Query;

         function Key_Factory (Index : Index_Type) return Key_Type is
         begin
            if Index < Position.Index then
               return Accessor.Nodes (Index).Key.all;
            else
               return Accessor.Nodes (Index + 1).Key.all;
            end if;
         end Key_Factory;

         function Element_Factory (Index : Index_Type) return Element_Type is
         begin
            if Index < Position.Index then
               return Accessor.Nodes (Index).Element.all;
            else
               return Accessor.Nodes (Index + 1).Element.all;
            end if;
         end Element_Factory;

         Result : constant Constant_Map := (Backend => Make_Backend
           (Accessor.Size - 1, Key_Factory'Access, Element_Factory'Access));
      begin
         return Result;
      end;
   end Delete;



   ------------------------------
   -- Updatable Map Operations --
   ------------------------------

   function Reference