summaryrefslogtreecommitdiff
path: root/src/candidates.adb
blob: 33d3901d576c17afd1f91b3b6cdc8f92caa4cd32 (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
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


with Ada.Text_IO;
with CSV;


package body Candidates is


    function To_String
           (Input_Candidate : in Candidate;
            Delimiter       : in Character := ',')
        return String
    is
        package My_CSV is new CSV (Delimiter => Delimiter);
        use type My_CSV.String_Vectors.Vector;

        My_Record : My_CSV.CSV_Record;
    begin
        My_Record :=
            My_CSV.String_Vectors.Empty_Vector &
            Input_Candidate.Group &
            Input_Candidate.Group_Rank &
            Input_Candidate.First_Name &
            Input_Candidate.Last_Name &
            Input_Candidate.Party;
        return My_CSV.Unparse_Record (My_Record);
    end To_String;




    procedure Read_Candidates
           (Filename       : in     String;
            State          : in     State_Name;
            Candidate_List :    out Candidate_Vector)
    is
        package My_CSV is new CSV;
        use Ada.Text_IO;
        use type Ada.Containers.Count_Type;
        use type SU.Unbounded_String;

        Input_File        : File_Type;
        Current_Record    : My_CSV.CSV_Record;
        Current_Candidate : Candidate;
    begin
        Open (Input_File, In_File, Filename);
        Candidate_List := (Vec => Candidate_Vectors.Empty_Vector);

        while not End_Of_File (Input_File) loop
            Current_Record := My_CSV.Parse_Line (Get_Line (Input_File));

            --  all the field numbers here correspond to how
            --  AEC Senate candidate data is arranged in csv format
            if  Current_Record.Length = 25 and then
                Current_Record.Element (2) = "S" and then
                Current_Record.Element (3) = State_Name'Image (State)
            then
                Current_Candidate :=
                   (First_Name => Current_Record.Element (8),
                    Last_Name  => Current_Record.Element (7),
                    Group      => Current_Record.Element (5),
                    Group_Rank => Current_Record.Element (6),
                    Party      => Current_Record.Element (9));
                Candidate_List.Vec.Append (Current_Candidate);
            end if;
        end loop;

        Close (Input_File);
    end Read_Candidates;




    function First
           (Candidate_List : in Candidate_Vector)
        return CandidateID is
    begin
        return Candidate_List.Vec.First_Index;
    end First;




    function Last
           (Candidate_List : in Candidate_Vector)
        return CandidateID is
    begin
        return Candidate_List.Vec.Last_Index;
    end Last;




    function Lookup
           (Candidate_List : in Candidate_Vector;
            Index          : in CandidateID)
        return Candidate is
    begin
        return Candidate_List.Vec.Element (Index);
    end Lookup;




    function First
           (CandidateID_List : in CandidateID_Vector)
        return Positive is
    begin
        return CandidateID_List.Vec.First_Index;
    end First;




    function Last
           (CandidateID_List : in CandidateID_Vector)
        return Positive is
    begin
        return CandidateID_List.Vec.Last_Index;
    end Last;




    function Lookup
           (CandidateID_List : in CandidateID_Vector;
            Index            : in Positive)
        return CandidateID is
    begin
        return CandidateID_List.Vec.Element (Index);
    end Lookup;




    function "<"
           (Left, Right : Candidate)
        return Boolean
    is
        use type SU.Unbounded_String;
    begin
        if SU.Length (Left.Group) = SU.Length (Right.Group) then
            if Left.Group = Right.Group then
                return Left.Group_Rank < Right.Group_Rank;
            else
                return Left.Group < Right.Group;
            end if;
        else
            return SU.Length (Left.Group) < SU.Length (Right.Group);
        end if;
    end "<";




    function Generate_Above
           (Candidate_List : in Candidate_Vector)
        return Above_Line_Ballot
    is
        use type Ada.Containers.Count_Type;
        use type SU.Unbounded_String;

        Result : Above_Line_Ballot;
        Working_Vector : CandidateID_Vector;
        Current_Group : SU.Unbounded_String;
        Current_Index : CandidateID;
    begin
        Result := (Vec => Above_Line_Ballots.Empty_Vector);

        if Candidate_List.Vec.Length = 0 then
            return Result;
        end if;

        Current_Index := Candidate_List.Vec.First_Index;
        while Current_Index <= Candidate_List.Vec.Last_Index loop
            Current_Group := Candidate_List.Vec.Element (Current_Index).Group;

            --  the assumption is that the "UG" group is always last
            --  a fairly safe assumption given alphabetical group order
            exit when Current_Group = "UG";

            Working_Vector := (Vec => CandidateID_Vectors.Empty_Vector);
            loop
                Working_Vector.Vec.Append (Current_Index);
                Current_Index := Current_Index + 1;
                exit when Current_Index > Candidate_List.Vec.Last_Index or else
                    Current_Group /= Candidate_List.Vec.Element (Current_Index).Group;
            end loop;
            Result.Vec.Append (Working_Vector);
        end loop;

        return Result;
    end Generate_Above;




    function Generate_Below
           (Candidate_List : in Candidate_Vector)
        return Below_Line_Ballot
    is
        Result : Below_Line_Ballot;
    begin
        Result := (Vec => CandidateID_Vectors.Empty_Vector);
        for ID in CandidateID range Candidate_List.Vec.First_Index .. Candidate_List.Vec.Last_Index loop
            Result.Vec.Append (ID);
        end loop;
        return Result;
    end Generate_Below;




    procedure Generate_Ballots
           (Candidate_List : in     Candidate_Vector;
            Above_Ballot   :    out Above_Line_Ballot;
            Below_Ballot   :    out Below_Line_Ballot)
    is
        package Sorting is new Candidate_Vectors.Generic_Sorting;

        My_Candidate_List : Candidate_Vector;
    begin
        My_Candidate_List := Candidate_List;
        Sorting.Sort (My_Candidate_List.Vec);
        Above_Ballot := Generate_Above (My_Candidate_List);
        Below_Ballot := Generate_Below (My_Candidate_List);
    end Generate_Ballots;




    function First
           (Above_Ballot : in Above_Line_Ballot)
        return Positive is
    begin
        return Above_Ballot.Vec.First_Index;
    end First;




    function Last
           (Above_Ballot : in Above_Line_Ballot)
        return Positive is
    begin
        return Above_Ballot.Vec.Last_Index;
    end Last;




    function Lookup
           (Above_Ballot : in Above_Line_Ballot;
            Index        : in Positive)
        return CandidateID_Vector is
    begin
        return Above_Ballot.Vec.Element (Index);
    end Lookup;




    function First
           (Below_Ballot : in Below_Line_Ballot)
        return Positive is
    begin
        return Below_Ballot.Vec.First_Index;
    end First;




    function Last
           (Below_Ballot : in Below_Line_Ballot)
        return Positive is
    begin
        return Below_Ballot.Vec.Last_Index;
    end Last;




    function Lookup
           (Below_Ballot : in Below_Line_Ballot;
            Index        : in Positive)
        return CandidateID is
    begin
        return Below_Ballot.Vec.Element (Index);
    end Lookup;


end Candidates;