summaryrefslogtreecommitdiff
path: root/src/bundles-containers.adb
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2017-02-10 18:41:36 +1100
committerJed Barber <jjbarber@y7mail.com>2017-02-10 18:41:36 +1100
commit60b2207a469a5a1e7a7e5619a8eb1b01c67f314a (patch)
treec928299d78242f2b36798e8c1802914552626352 /src/bundles-containers.adb
parent964a28e91593c4bf1e1c132536828d87f8d12c84 (diff)
Preference data reads into Bundles properly, with packed memory and a few fixed bugs
Diffstat (limited to 'src/bundles-containers.adb')
-rw-r--r--src/bundles-containers.adb86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/bundles-containers.adb b/src/bundles-containers.adb
new file mode 100644
index 0000000..8ba0d30
--- /dev/null
+++ b/src/bundles-containers.adb
@@ -0,0 +1,86 @@
+
+
+with Ada.Strings.Unbounded;
+with Ada.Text_IO;
+with CSV;
+
+
+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;
+
+ procedure Update_Bundle
+ (B : in out Bundle) is
+ begin
+ Add (B, Item);
+ end Update_Bundle;
+
+ procedure Update_Vector
+ (C : in Candidates.CandidateID;
+ V : in out Bundle_Vectors.Vector) is
+ begin
+ V.Update_Element (V.First_Index, Update_Bundle'Access);
+ end Update_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
+ BMap.Update_Element (Current_Cursor, Update_Vector'Access);
+ else
+ declare
+ New_Bundle : Bundle := Empty_Bundle;
+ begin
+ Add (New_Bundle, 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 Ada.Containers.Count_Type;
+ 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 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;
+
+