From eb17c310f1602a9b5411edb4adccda1e57f7e800 Mon Sep 17 00:00:00 2001 From: Rod Kay Date: Mon, 1 May 2023 21:18:04 +1000 Subject: [PATCH] lace: Add a simple stack generic. --- 1-base/lace/source/lace-stack.adb | 45 +++++++++++++++++++++++++++++++ 1-base/lace/source/lace-stack.ads | 29 ++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 1-base/lace/source/lace-stack.adb create mode 100644 1-base/lace/source/lace-stack.ads diff --git a/1-base/lace/source/lace-stack.adb b/1-base/lace/source/lace-stack.adb new file mode 100644 index 0000000..8d5dcfa --- /dev/null +++ b/1-base/lace/source/lace-stack.adb @@ -0,0 +1,45 @@ +package body lace.Stack +is + use ada.Containers; + + + function to_Stack return Item + is + Self : Item; + begin + Self.reserve_Capacity (Count_type (initial_Capacity)); + return Self; + end to_Stack; + + + + + procedure push (Self : in out Item; E : in Element_T) + is + pragma assert (Check => Self.Capacity >= Count_type (initial_Capacity), + Message => "Stack has not been initialised."); + begin + Self.append (E); + end push; + + + + function pop (Self : in out Item) return Element_T + is + Top : constant Element_t := Self.last_Element; + begin + Self.delete_Last; + return Top; + end pop; + + + + + function getCount (Self : in Item) return Natural + is + begin + return Natural (Self.Length); + end getCount; + + +end lace.Stack; \ No newline at end of file diff --git a/1-base/lace/source/lace-stack.ads b/1-base/lace/source/lace-stack.ads new file mode 100644 index 0000000..a0b0f82 --- /dev/null +++ b/1-base/lace/source/lace-stack.ads @@ -0,0 +1,29 @@ +private +with + ada.Containers.Vectors; + + +generic + type Element_t is private; + initial_Capacity : Positive; + +package lace.Stack +is + type Item is private; + + function to_Stack return Item; + + + procedure push (Self : in out Item; E : in Element_T); + function pop (Self : in out Item) return Element_T; + + + function getCount (Self : in Item) return Natural; + + + +private + package Vectors is new ada.Containers.Vectors (Positive, Element_t); + type Item is new Vectors.Vector with null record; + +end lace.Stack;