summaryrefslogtreecommitdiff
path: root/misc/count_tables.adb
blob: 68cb9adc52109cce77d9d107c79fe60565f4541c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

package body Count_Tables is


    function Has_Element
           (Position : Cursor)
        return Boolean is
    begin
        return Position.My_Map /= null and then
            Position.Index >= Position.My_Map.My_Vec.First_Index and then
            Position.Index <= Position.My_Map.My_Vec.Last_Index;
    end Has_Element;


    function Key
           (Position : Cursor)
        return Key_Type is
    begin
        return Internal_Maps.Key
            (Position.My_Map.My_Vec.Element (Position.Index));
    end Key;


    function Element
           (Position : Cursor)
        return Natural is
    begin
        return Internal_Maps.Element
            (Position.My_Map.My_Vec.Element (Position.Index));
    end Element;


    function Reference
           (Container : aliased in out Map;
            Position  :                Cursor)
        return Reference_Type is
    begin
        return (Element => Container.My_Map.Reference
            (Container.My_Vec (Position.Index)).Element);
    end Reference;


    procedure Increment
           (Container : in out Map;
            Key       : in     Key_Type;
            Value     : in     Natural := 1)
    is
        Place   : Internal_Maps.Cursor;
        Success : Boolean;
    begin
        Container.My_Map.Insert (Key, Value, Place, Success);
        if not Success then
            Container.My_Map.Reference (Key) := Container.My_Map.Reference (Key) + Value;
        else
            Container.My_Vec.Append (Place);
        end if;
    end Increment;


    function Less
           (Left, Right : in Internal_Maps.Cursor)
        return Boolean is
    begin
        return Internal_Maps.Element (Right) < Internal_Maps.Element (Left);
    end Less;


    procedure Sort
           (Container : in out Map) is
    begin
        Cursor_Sorting.Sort (Container.My_Vec);
    end Sort;


    function Iterate
           (Container : in Map)
        return Map_Iterator_Interfaces.Reversible_Iterator'Class is
    begin
        return It : Iterator := (Ada.Finalization.Limited_Controlled
            with Container => Container'Unrestricted_Access);
    end Iterate;


    function First
           (Object : in Iterator) return Cursor is
    begin
        if Object.Container /= null and then
           not Object.Container.My_Map.Is_Empty
        then
            return (My_Map => Object.Container, Index => 1);
        else
            return No_Element;
        end if;
    end First;


    function Last
           (Object : in Iterator) return Cursor is
    begin
        if Object.Container /= null and then
           not Object.Container.My_Map.Is_Empty
        then
            return (My_Map => Object.Container, Index => Object.Container.My_Vec.Last_Index);
        else
            return No_Element;
        end if;
    end Last;


    function Next
           (Object   : in Iterator;
            Position : in Cursor) return Cursor is
    begin
        if Has_Element (Position) and then
           Position.Index < Position.My_Map.My_Vec.Last_Index
        then
            return (My_Map => Position.My_Map, Index => Position.Index + 1);
        else
            return No_Element;
        end if;
    end Next;


    function Previous
           (Object   : in Iterator;
            Position : in Cursor) return Cursor is
    begin
        if Has_Element (Position) and then
           Position.Index > Position.My_Map.My_Vec.First_Index
        then
            return (My_Map => Position.My_Map, Index => Position.Index - 1);
        else
            return No_Element;
        end if;
    end Previous;


end Count_Tables;