Overview
Comment: | constant_indefinite_ordered_maps: add constructors that provide mutation semantics |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
c0a78f48d7afd387bca7b57c6dd927ab |
User & Date: | nat on 2015-03-17 22:06:18 |
Other Links: | manifest | tags |
Context
2015-03-18
| ||
20:25 | constant_indefinite_ordered_map_tests: add a fully covering test suite for the new "mutation" constructors check-in: 6a2a4d94a0 user: nat tags: trunk | |
2015-03-17
| ||
22:06 | constant_indefinite_ordered_maps: add constructors that provide mutation semantics check-in: c0a78f48d7 user: nat tags: trunk | |
2015-02-28
| ||
14:51 | constant_indefinite_ordered_maps: add constants for empty maps check-in: f17f93813b user: nat tags: trunk | |
Changes
Modified src/natools-constant_indefinite_ordered_maps.adb from [e2501f2237] to [814ac130dc].
1 | ------------------------------------------------------------------------------ | | | 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 |
︙ | ︙ |
Modified src/natools-constant_indefinite_ordered_maps.ads from [3be65a27de] to [f137970892].
︙ | ︙ | |||
23 24 25 26 27 28 29 | -- cheap and uses the same actual object. It is as task-safe as the current -- -- implementation of Natools.References. -- -- Cursors also hold a reference, which is used to identify the parent map, -- -- so after an assignment or a call to Clear or Move, the link between the -- -- map object and the cursor is broken, but the cursor is still usable and -- -- behaves as the original version of the maps. -- -- -- | | < < < | > > > | > | | 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | -- cheap and uses the same actual object. It is as task-safe as the current -- -- implementation of Natools.References. -- -- Cursors also hold a reference, which is used to identify the parent map, -- -- so after an assignment or a call to Clear or Move, the link between the -- -- map object and the cursor is broken, but the cursor is still usable and -- -- behaves as the original version of the maps. -- -- -- -- There are two types defined here, depending on their restrictions and -- -- safety against concurrent accesses: -- -- * Constant_Map cannot be changed in any way, but is completely -- -- task-safe (unless some referential magic is performed, like -- -- tampering checks in standard containers) -- -- * Updatable_Map allows read-write operations on stored elements, but -- -- it is up to the client to ensure there operations are task-safe, -- -- e.g. by using an atomic or protected Element_Type. -- -- -- -- Insertion and deletion primitives are provided as function rather than -- -- procedures, to emphasize that they actually create a new map with the -- -- requested change. Since most of the map is blindly duplicated, they are -- -- all in O(n) time, which makes them a quite inefficient way to build -- -- maps. For a significant number of changes, it's probably better to go -- -- through an unsafe map. -- -- -- -- All the subprograms here have the semantics of standard indefinite -- -- ordered maps (see ARM A.18.6), except for tampering, which becomes -- -- irrelevant. -- ------------------------------------------------------------------------------ with Ada.Containers.Indefinite_Ordered_Maps; |
︙ | ︙ | |||
187 188 189 190 191 192 193 194 195 196 197 198 199 200 | return Constant_Reference_Type; function Constant_Reference (Container : aliased in Constant_Map; Key : in Key_Type) return Constant_Reference_Type; type Updatable_Map is new Constant_Map with private with Constant_Indexing => Constant_Reference, Variable_Indexing => Reference, Default_Iterator => Iterate, Iterator_Element => Element_Type; pragma Preelaborable_Initialization (Updatable_Map); | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | return Constant_Reference_Type; function Constant_Reference (Container : aliased in Constant_Map; Key : in Key_Type) return Constant_Reference_Type; function Insert (Source : in Constant_Map; Key : in Key_Type; New_Item : in Element_Type; Position : out Cursor; Inserted : out Boolean) return Constant_Map; function Insert (Source : in Constant_Map; Key : in Key_Type; New_Item : in Element_Type) return Constant_Map; function Include (Source : in Constant_Map; Key : in Key_Type; New_Item : in Element_Type) return Constant_Map; function Replace (Source : in Constant_Map; Key : in Key_Type; New_Item : in Element_Type) return Constant_Map; function Replace_Element (Source : in Constant_Map; Position : in Cursor; New_Item : in Element_Type) return Constant_Map; function Replace_Element (Source : in Constant_Map; Position : in Cursor; New_Item : in Element_Type; New_Position : out Cursor) return Constant_Map; function Exclude (Source : in Constant_Map; Key : in Key_Type) return Constant_Map; function Delete (Source : in Constant_Map; Key : in Key_Type) return Constant_Map; function Delete (Source : in Constant_Map; Position : in Cursor) return Constant_Map; type Updatable_Map is new Constant_Map with private with Constant_Indexing => Constant_Reference, Variable_Indexing => Reference, Default_Iterator => Iterate, Iterator_Element => Element_Type; pragma Preelaborable_Initialization (Updatable_Map); |
︙ | ︙ |