comp.lang.ada
 help / color / mirror / Atom feed
* Random Input-Size and recognizing ENTER-press
@ 2004-12-19  8:44 xadian
  2004-12-19 14:50 ` Marius Amado Alves
  2004-12-19 15:52 ` Jeffrey Carter
  0 siblings, 2 replies; 8+ messages in thread
From: xadian @ 2004-12-19  8:44 UTC (permalink / raw)


Hi all

Here's my problem:
I need to to Get() a random number of characters (typed by the user) in 
an array.
I could loop with a Get() statement and exit when all the inputs are in 
the array, but for that I'd need to know the number of inputs before. Is 
there a way to do that without knowing the input number?!
It would be possible if there is a way to recognize when the user 
presses the ENTER button (s.th. like captureEvent()). Is that possible?

Ty already
Xadian




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

* Re: Random Input-Size and recognizing ENTER-press
  2004-12-19  8:44 Random Input-Size and recognizing ENTER-press xadian
@ 2004-12-19 14:50 ` Marius Amado Alves
  2004-12-19 16:01   ` Jeffrey Carter
  2004-12-19 15:52 ` Jeffrey Carter
  1 sibling, 1 reply; 8+ messages in thread
From: Marius Amado Alves @ 2004-12-19 14:50 UTC (permalink / raw)
  To: comp.lang.ada

> I need to to Get() a random number of characters (typed by the user) in 
> an array.
> I could loop with a Get() statement and exit when all the inputs are in 
> the array, but for that I'd need to know the number of inputs before.

If this is your only problem then it its no problem. Use an unbounded 
array. For characters that would be an Unbounded_String.

And use Get_Immediate if you want to test explicitly for CR (as the 
usual representation of Enter).

Since you say you want an array (for characters that would be a String), 
encapsulate the reading procedure inside a function returning a String.

On a silver plater:

    function Get_String return String is
       U : Unbounded_String;
    begin
       loop
          Get_Immediate (C);
          exit when C = CR;
          Append (U, C);
       end loop;
       return To_String (U);
    end;





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

* Re: Random Input-Size and recognizing ENTER-press
  2004-12-19  8:44 Random Input-Size and recognizing ENTER-press xadian
  2004-12-19 14:50 ` Marius Amado Alves
@ 2004-12-19 15:52 ` Jeffrey Carter
  1 sibling, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2004-12-19 15:52 UTC (permalink / raw)


xadian wrote:
> 
> Here's my problem:
> I need to to Get() a random number of characters (typed by the user) in 
> an array.
> I could loop with a Get() statement and exit when all the inputs are in 
> the array, but for that I'd need to know the number of inputs before. Is 
> there a way to do that without knowing the input number?!

An array of characters is a string. In Ada, you can use the predefined 
type String.

Input of an arbitrary length String is easily done with a function such 
as PragmARC.Get_Line.

You can obtain the PragmAda Reusable Components at

http://home.earthlink.net/~jrcarter010/pragmarc.htm

-- 
Jeff Carter
"C++: The power, elegance and simplicity of a hand grenade."
Ole-Hjalmar Kristensen
90



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

* Re: Random Input-Size and recognizing ENTER-press
  2004-12-19 14:50 ` Marius Amado Alves
@ 2004-12-19 16:01   ` Jeffrey Carter
  2004-12-19 16:03     ` Pascal Obry
  2004-12-20 14:36     ` Steve
  0 siblings, 2 replies; 8+ messages in thread
From: Jeffrey Carter @ 2004-12-19 16:01 UTC (permalink / raw)


Marius Amado Alves wrote:

>    function Get_String return String is
>       U : Unbounded_String;
>    begin
>       loop
>          Get_Immediate (C);
>          exit when C = CR;
>          Append (U, C);
>       end loop;
>       return To_String (U);
>    end;

You might want to take a look at the source for Ada.Strings.Unbounded 
from GNAT before advocating an approach such as this. Every addition of 
a Character allocates a new String, copies the old String + new 
Character into the new String, and deallocates the old String. For a 
string of N Characters, that's N allocations and deallocations. Using 
Get_Line to obtain the Characters can improve that to N / Buffer'Length + 1.

It's not clear that the OP really requires Get_Immediate; the only 
requirement stated is the need to read an arbitrary-length String. The 
discussion of recognizing when the Enter key is pressed is part of a 
proposed solution, not the problem.

-- 
Jeff Carter
"C++: The power, elegance and simplicity of a hand grenade."
Ole-Hjalmar Kristensen
90



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

* Re: Random Input-Size and recognizing ENTER-press
  2004-12-19 16:01   ` Jeffrey Carter
@ 2004-12-19 16:03     ` Pascal Obry
  2004-12-19 23:47       ` Jeffrey Carter
  2004-12-20 14:36     ` Steve
  1 sibling, 1 reply; 8+ messages in thread
From: Pascal Obry @ 2004-12-19 16:03 UTC (permalink / raw)



Jeffrey Carter <spam@spam.com> writes:

> You might want to take a look at the source for Ada.Strings.Unbounded from
> GNAT before advocating an approach such as this. Every addition of a
> Character allocates a new String, copies the old String + new Character into
> the new String, and deallocates the old String. For a string of N
> Characters, that's N allocations and deallocations. Using Get_Line to obtain
> the Characters can improve that to N / Buffer'Length + 1.

That's not right. There is a buffer that is allocated when there is no more
space into an Unbounded_String. So there is far less that N allocation when
adding N characters into an Unbounded_String.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|              http://www.obry.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595



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

* Re: Random Input-Size and recognizing ENTER-press
  2004-12-19 16:03     ` Pascal Obry
@ 2004-12-19 23:47       ` Jeffrey Carter
  0 siblings, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2004-12-19 23:47 UTC (permalink / raw)


Pascal Obry wrote:

> That's not right. There is a buffer that is allocated when there is no more
> space into an Unbounded_String. So there is far less that N allocation when
> adding N characters into an Unbounded_String.

Here's what I have:

    Null_String : aliased String := "";

    function To_Unbounded (S : String) return Unbounded_String
      renames To_Unbounded_String;

    type Unbounded_String is new AF.Controlled with record
       Reference : String_Access := Null_String'Access;
    end record;

    procedure Append
      (Source   : in out Unbounded_String;
       New_Item : in Character)
    is
       S_Length : constant Integer := Source.Reference.all'Length;
       Length   : constant Integer := S_Length + 1;
       Tmp      : String_Access;

    begin
       Tmp := new String (1 .. Length);
       Tmp (1 .. S_Length) := Source.Reference.all;
       Tmp (S_Length + 1) := New_Item;
       Free (Source.Reference);
       Source.Reference := Tmp;
    end Append;

Starting with a null string, that certainly looks like N allocations.

-- 
Jeff Carter
"I was hobbling along, minding my own business, all of a
sudden, up he comes, cures me! One minute I'm a leper with
a trade, next minute my livelihood's gone! Not so much as a
'by your leave!' You're cured, mate. Bloody do-gooder!"
Monty Python's Life of Brian
76



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

* Re: Random Input-Size and recognizing ENTER-press
  2004-12-19 16:01   ` Jeffrey Carter
  2004-12-19 16:03     ` Pascal Obry
@ 2004-12-20 14:36     ` Steve
  2004-12-20 23:45       ` Jeffrey Carter
  1 sibling, 1 reply; 8+ messages in thread
From: Steve @ 2004-12-20 14:36 UTC (permalink / raw)


IMHO it's silly to worry about the number of allocations an deallocations 
when you're talking about keeping up with someone typing at a keyboard 
(especially at todays processor speeds).

Steve
(The Duck)

"Jeffrey Carter" <spam@spam.com> wrote in message 
news:qBhxd.4359$RH4.2409@newsread1.news.pas.earthlink.net...
> Marius Amado Alves wrote:
>
>>    function Get_String return String is
>>       U : Unbounded_String;
>>    begin
>>       loop
>>          Get_Immediate (C);
>>          exit when C = CR;
>>          Append (U, C);
>>       end loop;
>>       return To_String (U);
>>    end;
>
> You might want to take a look at the source for Ada.Strings.Unbounded from 
> GNAT before advocating an approach such as this. Every addition of a 
> Character allocates a new String, copies the old String + new Character 
> into the new String, and deallocates the old String. For a string of N 
> Characters, that's N allocations and deallocations. Using Get_Line to 
> obtain the Characters can improve that to N / Buffer'Length + 1.
>
> It's not clear that the OP really requires Get_Immediate; the only 
> requirement stated is the need to read an arbitrary-length String. The 
> discussion of recognizing when the Enter key is pressed is part of a 
> proposed solution, not the problem.
>
> -- 
> Jeff Carter
> "C++: The power, elegance and simplicity of a hand grenade."
> Ole-Hjalmar Kristensen
> 90 





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

* Re: Random Input-Size and recognizing ENTER-press
  2004-12-20 14:36     ` Steve
@ 2004-12-20 23:45       ` Jeffrey Carter
  0 siblings, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2004-12-20 23:45 UTC (permalink / raw)


Steve wrote:

> IMHO it's silly to worry about the number of allocations an deallocations 
> when you're talking about keeping up with someone typing at a keyboard 
> (especially at todays processor speeds).

True, but fragmentation of the heap is a different matter.

-- 
Jeff Carter
"This trial is a travesty. It's a travesty of a mockery of a
sham of a mockery of a travesty of two mockeries of a sham. ...
Do you realize there's not a single homosexual on that jury?"
Bananas
27



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

end of thread, other threads:[~2004-12-20 23:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-19  8:44 Random Input-Size and recognizing ENTER-press xadian
2004-12-19 14:50 ` Marius Amado Alves
2004-12-19 16:01   ` Jeffrey Carter
2004-12-19 16:03     ` Pascal Obry
2004-12-19 23:47       ` Jeffrey Carter
2004-12-20 14:36     ` Steve
2004-12-20 23:45       ` Jeffrey Carter
2004-12-19 15:52 ` Jeffrey Carter

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