iterators_0.2.0_18995a4d/src/iterators-root.adb

  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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package body Iterators.Root is

   -----------------
   -- As_Iterator --
   -----------------

   function As_Iterator (This : in out Holder) return Iterator_Reference is
     (Ptr => Iterator'Class (This.Reference.Element.all)'Access);

   ------------------------------------
   -- ADA STANDARD ITERATION SUPPORT --
   ------------------------------------

   package Ada_Iterators is

      type Ada_Iterator is new Ada_Iterator_Interfaces.Forward_Iterator with record
         Base : Holder;
      end record;

      overriding function First (This : Ada_Iterator) return Cursor is
        (Cursor (This.Base.Unchecked_Reference.Next));

      overriding function Next (This       : Ada_Iterator;
                                Pos_Unused : Cursor) return Cursor is
        (Cursor (This.Base.Unchecked_Reference.Next));

   end Ada_Iterators;

   -------------
   -- Iterate --
   -------------

   function Iterate (This : aliased Iterator'Class)
                     return Ada_Iterator_Interfaces.Forward_Iterator'Class is
     (Ada_Iterators.Ada_Iterator'(Base => This.To_Holder));

   -------------------
   -- Get_Const_Ref --
   -------------------

   function Get_Const_Ref (This : aliased Iterator'Class;
                           Pos  : Cursor'Class) return Const_Ref is (Pos.Get);

   -----------------
   -- Get_Var_Ref --
   -----------------

   function Get_Var_Ref (This : aliased in out Iterator'Class;
                         Pos  : Cursor'Class) return Reference is (Pos.Ref);

   --------------------
   -- REST OF THINGS --
   --------------------

   -------------
   -- Element --
   -------------

   function Element (This : Cursor) return Any_Element is
     (This.Get.Element.all);

   -----------------
   -- Has_Element --
   -----------------

   function Has_Element (This : Cursor) return Boolean is
     (This.Get.Element /= null);

   ---------
   -- Get --
   ---------

   function Get (This : Cursor) return Const_Ref is
     (Element => (if This.Data.Read_Only
                  then This.Data.Const_Ptr
                  else This.Data.Ptr));

   ---------
   -- Ref --
   ---------

   function Ref (This : Cursor) return Reference is
     (Element => (if This.Data.Read_Only
                  then raise Constraint_Error with "Iterator is read-only"
                  else This.Data.Ptr));

   ---------
   -- Val --
   ---------

   function Val (This : Cursor) return Any_Element is (This.Get.Element.all);

   ----------------
   -- New_Cursor --
   ----------------

   function New_Cursor (Element : aliased in out Any_Element) return Cursor is
     (Data => (Read_Only => False,
               Ptr       => Element'Unchecked_Access));

   ----------------------
   -- New_Const_Cursor --
   ----------------------

   function New_Const_Cursor (Element : aliased Any_Element) return Cursor is
     (Data => (Read_Only => True,
               Const_Ptr => Element'Unchecked_Access));

   ----------------------
   -- New_Empty_Cursor --
   ----------------------

   function New_Empty_Cursor return Cursor is
     (Data => (Read_Only => True,
               Const_Ptr => null));

   ---------------
   -- Print_Tag --
   ---------------

   function Print_Tag (This : Iterator'Class) return Iterator'Class is
   begin
      This.Print_Tag;
      return This;
   end Print_Tag;

   --------------------
   -- LIST ITERATORS --
   --------------------

   type List_Iterator is new Iterator with record
      Cont  : access List;
      Const : Boolean;
      Pos   : Lists.Cursor;
   end record;

   ----------
   -- Next --
   ----------

   overriding
   function Next (This : in out List_Iterator) return Cursor'Class
   is

      -----------------
      -- Next_Cursor --
      -----------------

      function Next_Cursor return Cursor'Class
      is (if This.Const then
             New_Const_Cursor (This.Cont.Constant_Reference (This.Pos))
          else
             New_Cursor (This.Cont.Reference (This.Pos).Element.all));

   begin
      if not Lists.Has_Element (This.Pos) then
         return New_Empty_Cursor;
      end if;

      return C : constant Cursor'Class := Next_Cursor do
         if Lists.Has_Element (This.Pos) then
            Lists.Next (This.Pos);
         end if;
      end return;
   end Next;

   function Const_Iter (This : List) return Iterator'Class
   is (List_Iterator'(Iterator with
                      Cont => This'Unrestricted_Access,
                      Const => True,
                      Pos   => This.First));

   function Iter (This : List) return Iterator'Class
   is (List_Iterator'(Iterator with
                      Cont => This'Unrestricted_Access,
                      Const => False,
                      Pos   => This.First));

end Iterators.Root;