From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d3b3a6fb3be6d395,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-11-24 23:19:13 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!arclight.uoregon.edu!wn14feed!wn12feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!rwcrnsc51.ops.asp.att.net.POSTED!not-for-mail From: Stapler Subject: A tiny little integer stack package from a novice. Newsgroups: comp.lang.ada User-Agent: Pan/0.11.4 (Unix) Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Comment-To: ALL Message-ID: NNTP-Posting-Host: 12.241.145.39 X-Complaints-To: abuse@attbi.com X-Trace: rwcrnsc51.ops.asp.att.net 1038208754 12.241.145.39 (Mon, 25 Nov 2002 07:19:14 GMT) NNTP-Posting-Date: Mon, 25 Nov 2002 07:19:14 GMT Organization: AT&T Broadband Date: Mon, 25 Nov 2002 07:19:14 GMT Xref: archiver1.google.com comp.lang.ada:31204 Date: 2002-11-25T07:19:14+00:00 List-Id: I just coded up this stack package. The type system is alot simpler than in the past. I want to get the core logic right before I start adding features to it. As a newbie, I would appreciate any critiques the more experienced among you might have to offer. Although I call it a stack, it's more like a fixed buffer, a psuedo-stack if you will. with Ada.Unchecked_Deallocation; generic Size : Positive; package int_stack is type Int_Stack is limited private; type Stack_Ptr is limited private; procedure Push(X : in Integer); procedure Free_Stack; function Pop return Integer; private type Int_Stack is array(1..Size) of Integer; type Stack_Ptr is access all Int_Stack; The_Stack : Stack_Ptr := new Int_Stack; -- Provides access for De-allocation of entire stack -- Is also the sole allocation point for this package. procedure Free is new Ada.Unchecked_Deallocation(Int_Stack, Stack_Ptr); end int_stack; package body int_stack is Counter : Positive := 1; procedure Push(X : in Integer) is begin The_Stack.all(Counter) := X; Counter := Counter + 1; end Push; procedure Free_Stack is begin Free(The_Stack); -- I'm learning to make the commands -- self-explanatory here. Heh. end Free_Stack; function Pop return Integer is X : Integer := 0; begin Counter := Counter - 1; -- The Counter op is placed here because -- the Counter variable will point to 1 element -- past the end of the array on the first -- call. X := The_Stack(Counter); return X; end Pop; end int_stack; And here is the first full test program, which will expanded. with int_stack; with Ada.Text_IO; procedure test_int_stack is Bar_Int : Integer := 0; S_Size : Positive := 10000; package My_Stack is new int_stack( Size => S_Size); use My_Stack; begin for J in 1..S_Size loop Ada.Text_IO.Put_Line("Pushing "&J'img&" onto the stack."); Push(J); end loop; Ada.Text_IO.New_Line; for H in 1..S_Size loop Bar_Int := Pop; Ada.Text_IO.Put_Line("Got "&Bar_Int'img&" from the stack."); end loop; Free_Stack; end test_int_stack; Gmem reports no memory leaks. But seeing as there is only a single allocation and de-allocation here, that's really nothing to get excited about. Any features I should add on to make it more useful? I'm gonna be funning around with this package for a while to see where it breaks down, so I'll have plenty of time to plan new stuff. More importantly, I'm looking for places this thing breaks. Any advice would be greatly appreciated. Stapler.