summaryrefslogtreecommitdiff
path: root/misc/count_tables.ads
diff options
context:
space:
mode:
Diffstat (limited to 'misc/count_tables.ads')
-rw-r--r--misc/count_tables.ads124
1 files changed, 124 insertions, 0 deletions
diff --git a/misc/count_tables.ads b/misc/count_tables.ads
new file mode 100644
index 0000000..9f3055c
--- /dev/null
+++ b/misc/count_tables.ads
@@ -0,0 +1,124 @@
+
+
+with
+ Ada.Iterator_Interfaces;
+
+private with
+ Ada.Containers.Indefinite_Ordered_Maps,
+ Ada.Containers.Vectors,
+ Ada.Finalization;
+
+generic
+ type Key_Type (<>) is private;
+ with function "<" (Left, Right : Key_Type) return Boolean is <>;
+package Count_Tables is
+
+ -- This package imitates the basics of
+ -- the Nim CountTable table variant.
+
+ -- Functionality is incomplete and likely inefficient,
+ -- as this was only put together for demonstrative purposes.
+
+ type Map is tagged private
+ with Variable_Indexing => Reference,
+ Default_Iterator => Iterate,
+ Iterator_Element => Natural;
+
+ type Cursor is private;
+
+ Empty_Map : constant Map;
+ No_Element : constant Cursor;
+
+ function Has_Element
+ (Position : Cursor)
+ return Boolean;
+
+ function Key
+ (Position : Cursor)
+ return Key_Type;
+
+ function Element
+ (Position : Cursor)
+ return Natural;
+
+ package Map_Iterator_Interfaces is new
+ Ada.Iterator_Interfaces (Cursor, Has_Element);
+
+ type Reference_Type (Element : not null access Natural) is private
+ with Implicit_Dereference => Element;
+
+ function Reference
+ (Container : aliased in out Map;
+ Position : Cursor)
+ return Reference_Type;
+
+ procedure Increment
+ (Container : in out Map;
+ Key : in Key_Type;
+ Value : in Natural := 1);
+
+ procedure Sort
+ (Container : in out Map);
+
+ function Iterate
+ (Container : in Map)
+ return Map_Iterator_Interfaces.Reversible_Iterator'Class;
+
+private
+
+ type Map_Access is access all Map;
+
+ type Cursor is record
+ My_Map : Map_Access;
+ Index : Positive;
+ end record;
+
+ package Internal_Maps is new Ada.Containers.Indefinite_Ordered_Maps (Key_Type, Natural);
+ package Cursor_Vectors is new Ada.Containers.Vectors (Positive, Internal_Maps.Cursor, Internal_Maps."=");
+
+ type Map is tagged record
+ My_Map : Internal_Maps.Map;
+ My_Vec : Cursor_Vectors.Vector;
+ end record;
+
+ Empty_Map : constant Map :=
+ (My_Map => Internal_Maps.Empty_Map,
+ My_Vec => Cursor_Vectors.Empty_Vector);
+ No_Element : constant Cursor :=
+ (My_Map => null,
+ Index => 1);
+
+ function Less
+ (Left, Right : in Internal_Maps.Cursor)
+ return Boolean;
+
+ package Cursor_Sorting is new Cursor_Vectors.Generic_Sorting (Less);
+
+ type Reference_Type (Element : not null access Natural) is null record;
+
+ type Iterator is new Ada.Finalization.Limited_Controlled and
+ Map_Iterator_Interfaces.Reversible_Iterator with
+ record
+ Container : Map_Access;
+ end record;
+
+ overriding function First
+ (Object : in Iterator)
+ return Cursor;
+
+ overriding function Last
+ (Object : in Iterator)
+ return Cursor;
+
+ overriding function Next
+ (Object : in Iterator;
+ Position : in Cursor)
+ return Cursor;
+
+ overriding function Previous
+ (Object : in Iterator;
+ Position : in Cursor)
+ return Cursor;
+
+end Count_Tables;
+