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
180
181
182
183
184
185
186
187
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
with Ada.Strings.Unbounded;
with Ada.Strings.Maps;
-- This source is licensed under Creative Commons CC0 v1.0.
--
-- To read the full text, see license.txt in the main directory of this repository
-- or go to https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
--
-- For a human readable summary, go to https://creativecommons.org/publicdomain/zero/1.0/
package body Preferences is
package S renames Ada.Strings;
package SU renames Ada.Strings.Unbounded;
package SM renames Ada.Strings.Maps;
generic
type Range_Type is range <>;
type Array_Type is array (Range_Type) of Natural;
procedure Index_And_Count
(Input : in Array_Type;
Value : in Natural;
Index : out Range_Type;
Count : out Natural);
procedure Index_And_Count
(Input : in Array_Type;
Value : in Natural;
Index : out Range_Type;
Count : out Natural) is
begin
Count := 0;
for I in Range_Type loop
if Input (I) = Value then
Index := I;
Count := Count + 1;
end if;
end loop;
end Index_And_Count;
function Extract_Formal
(Above_Input : in Above_Pref_Array;
Formal_Prefs : out Preference_Array)
return Boolean
is
procedure Above_IC is new Index_And_Count
(Above_Range, Above_Pref_Array);
Extracted : Natural := 0;
Working_Index : Above_Range;
Working_Count : Natural;
Formal_Index : Preference_Range := Preference_Range'First;
begin
Formal_Prefs := (others => Candidates.No_Candidate);
Pref_Loop :
for I in Above_Range loop
Above_IC (Above_Input, Integer (I), Working_Index, Working_Count);
exit when Working_Count /= 1;
Extracted := Extracted + 1;
for C of Above_Ballot.Element (Integer (Working_Index)) loop
exit Pref_Loop when Formal_Index > Preference_Range'Last;
Formal_Prefs (Formal_Index) := C;
Formal_Index := Formal_Index + 1;
end loop;
end loop Pref_Loop;
return Extracted >= Min_Above_Line;
end Extract_Formal;
function Extract_Formal
(Below_Input : in Below_Pref_Array;
Formal_Prefs : out Preference_Array)
return Boolean
is
procedure Below_IC is new Index_And_Count
(Below_Range, Below_Pref_Array);
Extracted : Natural := 0;
Working_Index : Below_Range;
Working_Count : Natural;
Formal_Index : Preference_Range := Preference_Range'First;
begin
Formal_Prefs := (others => Candidates.No_Candidate);
if Formal_Index > Preference_Range'Last then
return Extracted >= Min_Below_Line;
end if;
for I in Below_Range loop
Below_IC (Below_Input, Integer (I), Working_Index, Working_Count);
exit when Working_Count /= 1;
Formal_Prefs (Formal_Index) := Below_Ballot.Element (Integer (Working_Index));
Extracted := Extracted + 1;
exit when Formal_Index = Preference_Range'Last;
Formal_Index := Formal_Index + 1;
end loop;
return Extracted >= Min_Below_Line;
end Extract_Formal;
function Mark
(Input : in SU.Unbounded_String;
Output : out Natural;
Remaining : out SU.Unbounded_String)
return Boolean is
begin
if SU.Length (Input) > 0 and then
(SU.Element (Input, 1) = '/' or else
SU.Element (Input, 1) = '*')
then
Output := 1;
Remaining := SU.Tail (Input, SU.Length (Input) - 1);
return True;
else
return False;
end if;
end Mark;
function Number
(Input : in SU.Unbounded_String;
Output : out Natural;
Remaining : out SU.Unbounded_String)
return Boolean
is
First, Last : Natural;
begin
SU.Find_Token (Input, SM.To_Set ("1234567890"), S.Inside, First, Last);
if First = 1 and Last > 0 then
Output := Integer'Value (SU.Slice (Input, First, Last));
Remaining := SU.Unbounded_Slice (Input, Last + 1, SU.Length (Input));
return True;
else
return False;
end if;
end Number;
function Comma
(Input : in SU.Unbounded_String;
Remaining : out SU.Unbounded_String)
return Boolean is
begin
if SU.Length (Input) > 0 and then
SU.Element (Input, 1) = ','
then
Remaining := SU.Tail (Input, SU.Length (Input) - 1);
return True;
else
return False;
end if;
end Comma;
generic
type Range_Type is range <>;
type Array_Type is array (Range_Type) of Natural;
function Raw_Prefs
(Input : in SU.Unbounded_String;
Output : out Array_Type;
Remaining : out SU.Unbounded_String)
return Boolean;
function Raw_Prefs
(Input : in SU.Unbounded_String;
Output : out Array_Type;
Remaining : out SU.Unbounded_String)
return Boolean
is
Working_Num : Natural;
Index : Range_Type := Range_Type'First;
This_In, This_Remaining : SU.Unbounded_String;
begin
Output := (others => 0);
if Index > Range_Type'Last then
Remaining := Input;
return True;
end if;
This_In := Input;
loop
if Number (This_In, Working_Num, This_Remaining) or else
Mark (This_In, Working_Num, This_Remaining)
then
Output (Index) := Working_Num;
else
This_Remaining := This_In;
end if;
exit when Index = Range_Type'Last;
Index := Index + 1;
if not Comma (This_Remaining, This_In) then
return False;
end if;
end loop;
Remaining := This_Remaining;
return True;
end Raw_Prefs;
procedure Optional_Line_Ending
(Input : in SU.Unbounded_String;
Remaining : out SU.Unbounded_String) is
begin
if SU.Length (Input) > 1 and then
SU.Slice (Input, 1, 2) = Character'Val (13) & Character'Val (10)
then
Remaining := SU.Tail (Input, SU.Length (Input) - 2);
elsif
SU.Length (Input) > 0 and then
(SU.Element (Input, 1) = Character'Val (13) or else
SU.Element (Input, 1) = Character'Val (10))
then
Remaining := SU.Tail (Input, SU.Length (Input) - 1);
else
Remaining := Input;
end if;
end Optional_Line_Ending;
function Parse_Preferences
(Input : in String)
return Preference_Array
is
use type SU.Unbounded_String;
Above_Line : Above_Pref_Array;
Below_Line : Below_Pref_Array;
function Parse_Above_Line is new Raw_Prefs
(Above_Range, Above_Pref_Array);
function Parse_Below_Line is new Raw_Prefs
(Below_Range, Below_Pref_Array);
Result : Preference_Array;
This_In, This_Remaining : SU.Unbounded_String;
begin
This_In := SU.To_Unbounded_String (Input);
if not Parse_Above_Line (This_In, Above_Line, This_Remaining) then
return Empty_Array;
end if;
if not Comma (This_Remaining, This_In) then
return Empty_Array;
end if;
if not Parse_Below_Line (This_In, Below_Line, This_Remaining) then
return Empty_Array;
end if;
Optional_Line_Ending (This_Remaining, This_In);
if SU.Length (This_In) > 0 then
return Empty_Array;
end if;
if Extract_Formal (Below_Line, Result) or else
Extract_Formal (Above_Line, Result)
then
return Result;
else
return Empty_Array;
end if;
end Parse_Preferences;
end Preferences;
|