source: trunk/ada-2012/src/semantic/gela-peristent_lists.adb@ 552

Last change on this file since 552 was 426, checked in by Maxim Reznik, 7 years ago

Minor refactoring

Rename Count_Type to List in Gela.Peristent_Lists. Hide Index_Type

  • Property svn:keywords set to Author Date Revision
File size: 2.9 KB
Line 
1package body Gela.Peristent_Lists is
2
3 ------------
4 -- Delete --
5 ------------
6
7 procedure Delete
8 (Self : in out Container;
9 Input : List;
10 Value : Element_Type;
11 Output : out List)
12 is
13 procedure Count_Each (Item : Element_Type);
14
15 Count : Natural := 0;
16 Found : Boolean := False;
17
18 procedure Count_Each (Item : Element_Type) is
19 begin
20 if Value = Item then
21 Found := True;
22 end if;
23
24 Count := Count + 1;
25 end Count_Each;
26
27 begin
28 For_Each (Self, Input, Count_Each'Access);
29
30 if Found then
31 declare
32 procedure Fill_Each (Item : Element_Type);
33
34 Data : array (1 .. Count) of Element_Type;
35
36 procedure Fill_Each (Item : Element_Type) is
37 begin
38 if Value /= Item then
39 Count := Count + 1;
40 Data (Count) := Item;
41 end if;
42 end Fill_Each;
43 begin
44 Count := 0;
45 For_Each (Self, Input, Fill_Each'Access);
46 Output := 0;
47
48 for J in 1 .. Count loop
49 Prepend (Self, Data (J), Output, Output);
50 end loop;
51 end;
52 else
53 Output := Input;
54 end if;
55 end Delete;
56
57 --------------
58 -- For_Each --
59 --------------
60
61 procedure For_Each
62 (Self : Container;
63 Input : List;
64 Action : access procedure (Value : Element_Type))
65 is
66 Index : List := Input;
67 begin
68 while Index /= 0 loop
69 declare
70 X : constant Link := Self.Links.Element (Index);
71 begin
72 Action (Self.Elements.Element (X.Value));
73 Index := X.Next;
74 end;
75 end loop;
76 end For_Each;
77
78 ----------
79 -- Head --
80 ----------
81
82 function Head
83 (Self : Container;
84 Index : List) return Element_Type
85 is
86 X : constant Link := Self.Links.Element (Index);
87 begin
88 return Self.Elements.Element (X.Value);
89 end Head;
90
91 -------------
92 -- Prepend --
93 -------------
94
95 procedure Prepend
96 (Self : in out Container;
97 Value : Element_Type;
98 Input : List := Empty;
99 Output : out List)
100 is
101 Value_Index : Natural := Self.Elements.Find_Index (Value);
102 New_Link : Link;
103 Result : List;
104 begin
105 if Value_Index = 0 then
106 Self.Elements.Append (Value);
107 Value_Index := Self.Elements.Last_Index;
108 end if;
109
110 New_Link := (Value => Value_Index, Next => Input);
111 Result := Self.Links.Find_Index (New_Link);
112
113 if Result = 0 then
114 Self.Links.Append (New_Link);
115 Output := Self.Links.Last_Index;
116 else
117 Output := Result;
118 end if;
119 end Prepend;
120
121 ----------
122 -- Tail --
123 ----------
124
125 function Tail
126 (Self : Container;
127 Index : List) return List is
128 begin
129 return Self.Links.Element (Index).Next;
130 end Tail;
131
132end Gela.Peristent_Lists;
Note: See TracBrowser for help on using the repository browser.