summaryrefslogtreecommitdiff
path: root/src/bundles-containers.adb
blob: 2dc97129432fb2ddf977976b2dd191b04b0a7618 (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


with Ada.Strings.Unbounded;
with Ada.Text_IO;
with CSV;


--  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 Bundles.Containers is


    package SU renames Ada.Strings.Unbounded;




    procedure Add_To_Map
           (BMap : in out Bundle_Maps.Map;
            Item : in     Given_Prefs.Preference_Array)
    is
        use type Bundle_Maps.Cursor;
        use type Bundle_Vectors.Vector;

        Place : Candidates.CandidateID := Item (Given_Prefs.Preference_Range'First);
        Current_Cursor : Bundle_Maps.Cursor := BMap.Find (Place);
    begin
        if Current_Cursor /= Bundle_Maps.No_Element then
            declare
                Vec_Ref : Bundle_Maps.Reference_Type :=
                    BMap.Reference (Current_Cursor);
                Bundle_Ref : Bundle_Vectors.Reference_Type :=
                    Vec_Ref.Reference (Vec_Ref.First_Index);
            begin
                Bundle_Ref.Papers.Append (Item);
            end;
        else
            declare
                New_Bundle : Bundle := Empty_Bundle;
            begin
                New_Bundle.Papers.Append (Item);
                BMap.Insert (Place, Bundle_Vectors.Empty_Vector & New_Bundle);
            end;
        end if;
    end Add_To_Map;




    procedure Read_Bundles
           (Filename : in     String;
            Result   :    out Bundle_Maps.Map)
    is
        package My_CSV is new CSV;
        use Ada.Text_IO;
        use type Candidates.CandidateID;

        Input_File : File_Type;
        Current_Record : My_CSV.CSV_Record;
        Current_Prefs : Given_Prefs.Preference_Array;
    begin
        Open (Input_File, In_File, Filename);

        Result := Bundle_Maps.Empty_Map;
        while not End_Of_File (Input_File) loop
            Current_Record := My_CSV.Parse_Line (Get_Line (Input_File));
            if Integer (Current_Record.Length) > 0 then
                Current_Prefs := Given_Prefs.Parse_Preferences (SU.To_String (Current_Record.Last_Element));
                if Current_Prefs (Given_Prefs.Preference_Range'First) /= Candidates.No_Candidate then
                    Add_To_Map (Result, Current_Prefs);
                end if;
            end if;
        end loop;

        Close (Input_File);
    end Read_Bundles;


end Bundles.Containers;