comp.lang.ada
 help / color / mirror / Atom feed
* Using generic package to store and retrieve string data
@ 2016-10-07 21:30 James Brewer
  2016-10-07 23:11 ` Jeffrey R. Carter
  2016-10-08 16:21 ` Stephen Leake
  0 siblings, 2 replies; 7+ messages in thread
From: James Brewer @ 2016-10-07 21:30 UTC (permalink / raw)


Hey guys, I am trying to create a generic package that will take strings of a fixed length store them in an array one at a time and later remove them one at a time. I was using a generic stack example I found on the net to get started but I can't seem to get it to work for string data.

Is there something that needs to be imported into the main program for string handling?
 
I tested it with character and integer data and it works with those.
Also, this is just a starting point for getting a generic to function with string data, ideally this will be converted into a queue.


Here is the code for the generic.

--ads

generic
    Size : Positive;
    type Item is private;
  package Generic_Stack is
    procedure Push(E : in  Item);
    procedure Pop (E : out Item);
    Overflow, Underflow : exception;
  end Generic_Stack;

--adb

package body Generic_Stack is
    type Table is array (Positive range <>) of Item;
    Space : Table(1 .. Size);
    Index : Natural := 0;

    procedure Push(E : in Item) is
    begin
      if Index >= Size then
        raise Overflow;
      end if;
      Index := Index + 1;
      Space(Index) := E;
    end Push;

    procedure Pop(E : out Item) is
    begin
      if Index = 0 then
        raise Underflow;
      end if;
      E := Space(Index);
      Index := Index - 1;
    end Pop;

  end Generic_Stack;


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using generic package to store and retrieve string data
  2016-10-07 21:30 Using generic package to store and retrieve string data James Brewer
@ 2016-10-07 23:11 ` Jeffrey R. Carter
  2016-10-08  7:42   ` Dmitry A. Kazakov
  2016-10-08 16:21 ` Stephen Leake
  1 sibling, 1 reply; 7+ messages in thread
From: Jeffrey R. Carter @ 2016-10-07 23:11 UTC (permalink / raw)


On 10/07/2016 02:30 PM, James Brewer wrote:
> Hey guys, I am trying to create a generic package that will take strings of a
> fixed length store them in an array one at a time and later remove them one
> at a time. I was using a generic stack example I found on the net to get
> started but I can't seem to get it to work for string data.
>
> Is there something that needs to be imported into the main program for string
> handling?

If your Strings are all the same length, then you need to instantiate the
generic with a subtype of that length:

subtype Stored_String is String (1 .. Length);

package Stored_String_Stack is new Generic_Stack
    (Size => Max_Stack_Size, Item => Stored_String);

If your Strings may have differing lengths, then you should either use 
Unbounded_String or one of the indefinite containers.

> Also, this is just a starting point for getting a generic to function with
> string data, ideally this will be converted into a queue.

I suggest you familiarize yourself with the standard container library in Annex 
A.18. One of them will probably do what you want.

http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-18.html

-- 
Jeff Carter
"It's symbolic of his struggle against reality."
Monty Python's Life of Brian
78


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using generic package to store and retrieve string data
  2016-10-07 23:11 ` Jeffrey R. Carter
@ 2016-10-08  7:42   ` Dmitry A. Kazakov
  2016-10-08  9:01     ` Simon Wright
  0 siblings, 1 reply; 7+ messages in thread
From: Dmitry A. Kazakov @ 2016-10-08  7:42 UTC (permalink / raw)


On 2016-10-08 01:11, Jeffrey R. Carter wrote:

> If your Strings may have differing lengths,  then you should either use
> Unbounded_String or one of the indefinite containers.

Unbounded_String is not self-contained. Normally it is important for a 
stack and containers to have this.

IMO, the best way to implement an indefinite elements LIFO is on top of 
a custom memory pool.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using generic package to store and retrieve string data
  2016-10-08  7:42   ` Dmitry A. Kazakov
@ 2016-10-08  9:01     ` Simon Wright
  2016-10-08 10:17       ` Dmitry A. Kazakov
  0 siblings, 1 reply; 7+ messages in thread
From: Simon Wright @ 2016-10-08  9:01 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> On 2016-10-08 01:11, Jeffrey R. Carter wrote:
>
>> If your Strings may have differing lengths, then you should either
>> use Unbounded_String or one of the indefinite containers.
>
> Unbounded_String is not self-contained. Normally it is important for a
> stack and containers to have this.

It looks self-contained to me. (I mean, if I 'with
Ada.Strings.Unbounded' I can pretty-much use it without further ado;
just a little complication with translating to/from String).

> IMO, the best way to implement an indefinite elements LIFO is on top
> of a custom memory pool.

I doubt this is going to help the OP.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using generic package to store and retrieve string data
  2016-10-08  9:01     ` Simon Wright
@ 2016-10-08 10:17       ` Dmitry A. Kazakov
  2016-10-08 16:07         ` Jeffrey R. Carter
  0 siblings, 1 reply; 7+ messages in thread
From: Dmitry A. Kazakov @ 2016-10-08 10:17 UTC (permalink / raw)


On 2016-10-08 11:01, Simon Wright wrote:
> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:
>
>> On 2016-10-08 01:11, Jeffrey R. Carter wrote:
>>
>>> If your Strings may have differing lengths, then you should either
>>> use Unbounded_String or one of the indefinite containers.
>>
>> Unbounded_String is not self-contained. Normally it is important for a
>> stack and containers to have this.
>
> It looks self-contained to me. (I mean, if I 'with
> Ada.Strings.Unbounded' I can pretty-much use it without further ado;
> just a little complication with translating to/from String).

The string body will be allocated in the pool not on the stack. That, 
not always, but usually defeats the purpose of having a stack of strings.

>> IMO, the best way to implement an indefinite elements LIFO is on top
>> of a custom memory pool.
>
> I doubt this is going to help the OP.

My comment was addressed to Jeffrey's about indefinite elements. (The OP 
wanted fixed strings)

A stack of indefinite objects sitting on the stack would require either 
custom allocation or custom serialization (e.g. using stream 
attributes). In both cases it is important to grow the stack towards 
lesser addresses/indices to have access to the beginning of the last 
element.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using generic package to store and retrieve string data
  2016-10-08 10:17       ` Dmitry A. Kazakov
@ 2016-10-08 16:07         ` Jeffrey R. Carter
  0 siblings, 0 replies; 7+ messages in thread
From: Jeffrey R. Carter @ 2016-10-08 16:07 UTC (permalink / raw)


On 10/08/2016 03:17 AM, Dmitry A. Kazakov wrote:
>
> The string body will be allocated in the pool not on the stack. That, not
> always, but usually defeats the purpose of having a stack of strings.

I fail to see this as an issue; the stack's behavior will be what the OP wants. 
I was using Unbounded_String simply as a holder for String that provides a 
definite type that can be used to instantiate the generic stack pkg given. 
Indefinite_Holders would have also worked.

-- 
Jeff Carter
"You empty-headed animal-food-trough wiper."
Monty Python & the Holy Grail
04

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Using generic package to store and retrieve string data
  2016-10-07 21:30 Using generic package to store and retrieve string data James Brewer
  2016-10-07 23:11 ` Jeffrey R. Carter
@ 2016-10-08 16:21 ` Stephen Leake
  1 sibling, 0 replies; 7+ messages in thread
From: Stephen Leake @ 2016-10-08 16:21 UTC (permalink / raw)


On Friday, October 7, 2016 at 4:30:27 PM UTC-5, James Brewer wrote:
> Hey guys, I am trying to create a generic package that will take strings of a
> fixed length store them in an array one at a time and later remove them one 
> at a time. I was using a generic stack example I found on the net to get 
> started but I can't seem to get it to work for string data.

You did not show your instantiation of this, so I can't say what your problem might be.

Here is an instantiation that works:

with Ada.Text_Io; use Ada.Text_Io;
with Generic_Stack;
procedure Stack_Main
is

   subtype String_10 is String (1 .. 10);

   package Stack is new Generic_Stack
     (Size => 10,
      Item => String_10);

   Popped : String_10;
begin
   Put_Line ("Push '0123456789'");
   Stack.Push ("0123456789");
   Put_Line ("Push 'Hello.....'");
   Stack.Push ("Hello.....");

   Stack.Pop (Popped);
   Put_Line ("Popped: '" & Popped & "'");
   Stack.Pop (Popped);
   Put_Line ("Popped: '" & Popped & "'");
end Stack_Main;


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-10-08 16:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-07 21:30 Using generic package to store and retrieve string data James Brewer
2016-10-07 23:11 ` Jeffrey R. Carter
2016-10-08  7:42   ` Dmitry A. Kazakov
2016-10-08  9:01     ` Simon Wright
2016-10-08 10:17       ` Dmitry A. Kazakov
2016-10-08 16:07         ` Jeffrey R. Carter
2016-10-08 16:21 ` Stephen Leake

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox