summaryrefslogtreecommitdiff
path: root/src/candidates-containers.adb
blob: 64a03ca3c0df83a60e9a84cec1c774a13ee8cea2 (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


with Ada.Containers.Vectors;
with Ada.Text_IO;
with CSV;


package body Candidates.Containers is


    procedure Read_Candidates
           (Filename       : in     String;
            State          : in     State_Name;
            Candidate_Data :    out Candidate_Map)
    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;

        Next_ID : CandidateID := CandidateID'First;
    begin
        Open (Input_File, In_File, Filename);
        Candidate_Data := Candidate_Maps.Empty_Map;

        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_Data.Insert (Next_ID, Current_Candidate);
                Next_ID := Next_ID + 1;
            end if;
        end loop;

        Close (Input_File);
    end Read_Candidates;




    --  these two types exist because I can't think of an easier
    --  way to sort a Candidate_Map into the appropriate order at
    --  the moment

    type Cand_Sort_Data is record
        Cand_ID    : CandidateID;
        Group      : SU.Unbounded_String;
        Group_Rank : SU.Unbounded_String;
    end record;

    package Cand_Sort_Data_Vectors is new Ada.Containers.Vectors
           (Index_Type   => Positive,
            Element_Type => Cand_Sort_Data);




    function "<"
           (Left, Right : Cand_Sort_Data)
        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_Data : in Cand_Sort_Data_Vectors.Vector)
        return Above_Line_Ballot
    is
        use type Ada.Containers.Count_Type;
        use type SU.Unbounded_String;

        Result : Above_Line_Ballot := CandidateID_Map_Maps.Empty_Map;
        Working_Map : CandidateID_Maps.Map;
        Current_Group : SU.Unbounded_String;
        Current_Index : Positive;
        Next_ID, Working_ID : Positive;
    begin
        if Candidate_Data.Length = 0 then
            return Result;
        end if;

        Next_ID := 1;
        Current_Index := Candidate_Data.First_Index;
        while Current_Index <= Candidate_Data.Last_Index loop
            Current_Group := Candidate_Data.Element (Current_Index).Group;

            --  the assumption is that the "UG" group is always last
            --  a fairly safe assumption given alphabetical group order
            --  but will break down should there be more than... 553 grouped candidates
            exit when Current_Group = "UG";

            Working_Map := CandidateID_Maps.Empty_Map;
            Working_ID := 1;
            loop
                Working_Map.Insert (Working_ID, Candidate_Data.Element (Current_Index).Cand_ID);
                Working_ID := Working_ID + 1;
                Current_Index := Current_Index + 1;
                exit when Current_Index > Candidate_Data.Last_Index or else
                    Current_Group /= Candidate_Data.Element (Current_Index).Group;
            end loop;
            Result.Insert (Next_ID, Working_Map);
            Next_ID := Next_ID + 1;
        end loop;

        return Result;
    end Generate_Above;




    function Generate_Below
           (Candidate_Data : in Cand_Sort_Data_Vectors.Vector)
        return Below_Line_Ballot
    is
        Result : Below_Line_Ballot := CandidateID_Maps.Empty_Map;
        Next_ID : Positive := 1;
    begin
        for Item of Candidate_Data loop
            Result.Insert (Next_ID, Item.Cand_ID);
            Next_ID := Next_ID + 1;
        end loop;
        return Result;
    end Generate_Below;




    procedure Generate_Ballots
           (Candidate_Data : in     Candidate_Map;
            Above_Ballot   :    out Above_Line_Ballot;
            Below_Ballot   :    out Below_Line_Ballot)
    is
        package Sorting is new Cand_Sort_Data_Vectors.Generic_Sorting;

        My_Candidate_Data : Cand_Sort_Data_Vectors.Vector;
        Working_Candidate : Candidate;
    begin
        My_Candidate_Data := Cand_Sort_Data_Vectors.Empty_Vector;

        for Cursor in Candidate_Data.Iterate loop
            Working_Candidate := Candidate_Maps.Element (Cursor);
            My_Candidate_Data.Append
                  ((Cand_ID    => Candidate_Maps.Key (Cursor),
                    Group      => Working_Candidate.Group,
                    Group_Rank => Working_Candidate.Group_Rank));
        end loop;

        Sorting.Sort (My_Candidate_Data);

        Above_Ballot := Generate_Above (My_Candidate_Data);
        Below_Ballot := Generate_Below (My_Candidate_Data);
    end Generate_Ballots;




    function To_String
           (Above_Ballot : in Above_Line_Ballot)
        return String
    is
        Result : SU.Unbounded_String := SU.To_Unbounded_String (0);
    begin
        for Group_Cursor in Above_Ballot.Iterate loop
            SU.Append (Result, Integer'Image (CandidateID_Map_Maps.Key (Group_Cursor)) & ": ");
            for Box of CandidateID_Map_Maps.Element (Group_Cursor) loop
                SU.Append (Result, CandidateID'Image (Box) & " ");
            end loop;
            SU.Append (Result, ASCII.LF);
        end loop;
        return SU.To_String (Result);
    end To_String;




    function To_String
           (Below_Ballot : in Below_Line_Ballot)
        return String
    is
        Result : SU.Unbounded_String := SU.To_Unbounded_String (0);
    begin
        for Box of Below_Ballot loop
            SU.Append (Result, CandidateID'Image (Box) & " ");
        end loop;
        return SU.To_String (Result);
    end To_String;


end Candidates.Containers;