comp.lang.ada
 help / color / mirror / Atom feed
* Initialize with aggregate?
@ 2005-11-21 19:40 ejijott
  2005-11-21 21:38 ` Gautier Write-only
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: ejijott @ 2005-11-21 19:40 UTC (permalink / raw)


Hi there!

Regarding the code below, what syntax do I use to initialize tempstore
with an aggregate?
[code]
.
.
.
    type node;
    type storage is access node;
    type node is
        record
            next:  storage;       -- Pekare till nästa nod.
            item:  string(1..50); -- En textsträng
            len:   integer :=-1;  -- Längden på strängen.
            count: integer := 0;  -- Antalet förekomster av strängen
        end record;
.
.
.
        tempstore:=new Node;
        tempstore.item(1 .. Val'length):=Val;
        tempstore.len:=Val'length;
        tempstore.count:=1;
        if S /= null then
            tempstore.next:=S;
        end if;
        S:=tempstore;
[/code]




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

* Re: Initialize with aggregate?
  2005-11-21 19:40 Initialize with aggregate? ejijott
@ 2005-11-21 21:38 ` Gautier Write-only
  2005-11-21 21:49 ` Simon Wright
  2005-11-21 22:21 ` Robert A Duff
  2 siblings, 0 replies; 8+ messages in thread
From: Gautier Write-only @ 2005-11-21 21:38 UTC (permalink / raw)


ejijott@gmail.com:

> Regarding the code below, what syntax do I use to initialize tempstore
> with an aggregate?

The syntax is there (The Maunal, 4.8: Allocators) with examples:
  http://www.adaic.com/standards/95lrm/html/RM-4-8.html

Maybe that exmple can also help
  http://homepage.sunrise.ch/mysunrise/gdm/e3d_html/unzip__adb.htm#567_15

It should be enough to do the job, isn't it ?
______________________________________________________________ 
Gautier     --     http://www.mysunrise.ch/users/gdm/index.htm 
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm



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

* Re: Initialize with aggregate?
  2005-11-21 19:40 Initialize with aggregate? ejijott
  2005-11-21 21:38 ` Gautier Write-only
@ 2005-11-21 21:49 ` Simon Wright
  2005-11-22 17:10   ` Adam Beneschan
  2005-11-21 22:21 ` Robert A Duff
  2 siblings, 1 reply; 8+ messages in thread
From: Simon Wright @ 2005-11-21 21:49 UTC (permalink / raw)


ejijott@gmail.com writes:

> Regarding the code below, what syntax do I use to initialize tempstore
> with an aggregate?
> [code]
> .
> .
> .
>     type node;
>     type storage is access node;
>     type node is
>         record
>             next:  storage;       -- Pekare till n�sta nod.
>             item:  string(1..50); -- En textstr�ng
>             len:   integer :=-1;  -- L�ngden p� str�ngen.
>             count: integer := 0;  -- Antalet f�rekomster av str�ngen
>         end record;
> .
> .
> .
>         tempstore:=new Node;
>         tempstore.item(1 .. Val'length):=Val;
>         tempstore.len:=Val'length;
>         tempstore.count:=1;
>         if S /= null then
>             tempstore.next:=S;
>         end if;
>         S:=tempstore;
> [/code]

S := new Node'(Next => S,
               Item => <<a function padding Val to 50 characters>>,
               Len => Val'Length,
               Count => 1);

If S is null then it can do no harm to assign it to the new node's
Next field.

You might consider replacing Item and Len by a bounded string of
length up to 50.



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

* Re: Initialize with aggregate?
  2005-11-21 19:40 Initialize with aggregate? ejijott
  2005-11-21 21:38 ` Gautier Write-only
  2005-11-21 21:49 ` Simon Wright
@ 2005-11-21 22:21 ` Robert A Duff
  2005-11-22 16:57   ` Adam Beneschan
  2 siblings, 1 reply; 8+ messages in thread
From: Robert A Duff @ 2005-11-21 22:21 UTC (permalink / raw)


ejijott@gmail.com writes:

> Hi there!
> 
> Regarding the code below, what syntax do I use to initialize tempstore
> with an aggregate?
> [code]
> .
> .
> .
>     type node;
>     type storage is access node;
>     type node is
>         record
>             next:  storage;       -- Pekare till n�sta nod.
>             item:  string(1..50); -- En textstr�ng
>             len:   integer :=-1;  -- L�ngden p� str�ngen.
>             count: integer := 0;  -- Antalet f�rekomster av str�ngen
>         end record;
> .
> .
> .
>         tempstore:=new Node;
>         tempstore.item(1 .. Val'length):=Val;
>         tempstore.len:=Val'length;
>         tempstore.count:=1;
>         if S /= null then
>             tempstore.next:=S;
>         end if;
>         S:=tempstore;
> [/code]

How about something like this:

    type node;
    type storage is access node;
    type node(len: natural) is
        record
            next:  storage;       -- Pekare till n�sta nod.
            item:  string(1..len); -- En textstr�ng
            count: integer := 0;  -- Antalet f�rekomster av str�ngen
        end record;

    tempstore:=new Node'(
            len => val'length,
            next => S,
            item => val,
            count => 1);
    S:=tempstore;

Hardwiring Item'Length to 50 isn't a good idea, and it makes the
programming harder, because you have to carefully avoid the junk
at the end.

- Bob



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

* Re: Initialize with aggregate?
  2005-11-21 22:21 ` Robert A Duff
@ 2005-11-22 16:57   ` Adam Beneschan
  2005-11-22 20:07     ` Robert A Duff
  0 siblings, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2005-11-22 16:57 UTC (permalink / raw)


Robert A Duff wrote:
> ejijott@gmail.com writes:
>
> > Hi there!
> >
> > Regarding the code below, what syntax do I use to initialize tempstore
> > with an aggregate?
> > [code]
> > .
> > .
> > .
> >     type node;
> >     type storage is access node;
> >     type node is
> >         record
> >             next:  storage;       -- Pekare till nästa nod.
> >             item:  string(1..50); -- En textsträng
> >             len:   integer :=-1;  -- Längden på strängen.
> >             count: integer := 0;  -- Antalet förekomster av strängen
> >         end record;
> > .
> > .
> > .
> >         tempstore:=new Node;
> >         tempstore.item(1 .. Val'length):=Val;
> >         tempstore.len:=Val'length;
> >         tempstore.count:=1;
> >         if S /= null then
> >             tempstore.next:=S;
> >         end if;
> >         S:=tempstore;
> > [/code]
>
> How about something like this:
>
>     type node;
>     type storage is access node;
>     type node(len: natural) is
>         record
>             next:  storage;       -- Pekare till nästa nod.
>             item:  string(1..len); -- En textsträng
>             count: integer := 0;  -- Antalet förekomster av strängen
>         end record;
>
>     tempstore:=new Node'(
>             len => val'length,
>             next => S,
>             item => val,
>             count => 1);
>     S:=tempstore;
>
> Hardwiring Item'Length to 50 isn't a good idea, and it makes the
> programming harder, because you have to carefully avoid the junk
> at the end.

I don't see how you can make a blanket statement like this.  If you
code it the way you've shown, you can't change "len" in a node once
you've allocated it.  But sometimes changing "item" and "len" in an
existing node to change the length of the string, without changing the
other fields in the node, is what you want to do, so there are cases
where the original poster's implementation is more appropriate than
using a discriminant record.  (Using Ada.Strings.Bounded may make
programming easier still, but you still have to hardwire a maximum
length.)

                                    -- Adam




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

* Re: Initialize with aggregate?
  2005-11-21 21:49 ` Simon Wright
@ 2005-11-22 17:10   ` Adam Beneschan
  2005-11-22 20:00     ` Simon Wright
  0 siblings, 1 reply; 8+ messages in thread
From: Adam Beneschan @ 2005-11-22 17:10 UTC (permalink / raw)


Simon Wright wrote:

> >     type node;
> >     type storage is access node;
> >     type node is
> >         record
> >             next:  storage;       -- Pekare till nästa nod.
> >             item:  string(1..50); -- En textsträng
> >             len:   integer :=-1;  -- Längden på strängen.
> >             count: integer := 0;  -- Antalet förekomster av strängen
> >         end record;
> > .
> > .
> > .
> >         tempstore:=new Node;
> >         tempstore.item(1 .. Val'length):=Val;
> >         tempstore.len:=Val'length;
> >         tempstore.count:=1;
> >         if S /= null then
> >             tempstore.next:=S;
> >         end if;
> >         S:=tempstore;
> > [/code]
>
> S := new Node'(Next => S,
>                Item => <<a function padding Val to 50 characters>>,
>                Len => Val'Length,
>                Count => 1);

You don't need a separate function.  This should work:

S := new Node' (Next => S,
                        Item => Val & ((Val'Length + 1) .. 50 => ' '),
                        Len => Val'Length,
                        Count => 1);

[Apologies for any spacing problems.  Google Groups' latest improvement
prevents me from using a fixed font when composing a post.]

                                         -- Adam




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

* Re: Initialize with aggregate?
  2005-11-22 17:10   ` Adam Beneschan
@ 2005-11-22 20:00     ` Simon Wright
  0 siblings, 0 replies; 8+ messages in thread
From: Simon Wright @ 2005-11-22 20:00 UTC (permalink / raw)


"Adam Beneschan" <adam@irvine.com> writes:

> You don't need a separate function.  This should work:
>
> S := new Node' (Next => S,
>                         Item => Val & ((Val'Length + 1) .. 50 => ' '),
>                         Len => Val'Length,
>                         Count => 1);

Neat! (the coding, not the layout :-)

--S



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

* Re: Initialize with aggregate?
  2005-11-22 16:57   ` Adam Beneschan
@ 2005-11-22 20:07     ` Robert A Duff
  0 siblings, 0 replies; 8+ messages in thread
From: Robert A Duff @ 2005-11-22 20:07 UTC (permalink / raw)


"Adam Beneschan" <adam@irvine.com> writes:

> > Hardwiring Item'Length to 50 isn't a good idea, and it makes the
> > programming harder, because you have to carefully avoid the junk
> > at the end.
> 
> I don't see how you can make a blanket statement like this.

You're right.  Perhaps I should say, "Hardwiring Item'Length to 50 is
_almost_never_ a good idea."  The way I showed is fine if the string
doesn't change its length.  If it does, Strings.Unbounded is most likely
what you want.  In the unlikely event that the length changes, and "50"
is an appropriate upper-bound, Strings.Bounded is probably best, as you
mention below.

A blank-padded string is unlikely to be a good idea.

>...If you
> code it the way you've shown, you can't change "len" in a node once
> you've allocated it.  But sometimes changing "item" and "len" in an
> existing node to change the length of the string, without changing the
> other fields in the node, is what you want to do, so there are cases
> where the original poster's implementation is more appropriate than
> using a discriminant record.  (Using Ada.Strings.Bounded may make
> programming easier still, but you still have to hardwire a maximum
> length.)

- Bob



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

end of thread, other threads:[~2005-11-22 20:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-21 19:40 Initialize with aggregate? ejijott
2005-11-21 21:38 ` Gautier Write-only
2005-11-21 21:49 ` Simon Wright
2005-11-22 17:10   ` Adam Beneschan
2005-11-22 20:00     ` Simon Wright
2005-11-21 22:21 ` Robert A Duff
2005-11-22 16:57   ` Adam Beneschan
2005-11-22 20:07     ` Robert A Duff

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