comp.lang.ada
 help / color / mirror / Atom feed
* Aliasing or referencing assignment
@ 2005-09-09  0:20 David Trudgett
  2005-09-09  2:13 ` Steve
  2005-09-09 15:40 ` Jeffrey Carter
  0 siblings, 2 replies; 10+ messages in thread
From: David Trudgett @ 2005-09-09  0:20 UTC (permalink / raw)



I'm having a little difficulty with assign by reference and assign by
value (copy) semantics in a piece of code I'm working on. In the
chunk of code that follows below, I want to avoid repeating
"Self.List(Opponent_Win, Can_I_Play)" many times, and just replace it
with a variable name like "C".

"Self.List(Opponent_Win, Can_I_Play)" returns a Charles container
library Vector of Unbounded_String. The assignment:

      C := Self.List(Opponent_Win, Can_I_Play); -- "List" is 2D array

is ineffective in doing what I want because it is doing a value
copy. I've tried to turn the type definition of "C" into an access
type, but with no success. I also tried the "To_Access" function (in
charles.vectors.unbounded), but that returns a reference to an
"Element_Array" and not a Container_Type with which I can use
"Append".

Anyway, here is the code:



   procedure Initialize (Self : in out Comments)
   is
      C : String_List.Container_Type;
      package Random_Integer is new Ada.Numerics.Discrete_Random(Integer);
      use Random_Integer;
      G : Generator;
   begin

      Reset(G);
      Ada.Numerics.Float_Random.Reset(Self.PRNG, Random(G));

      -- OPPONENT WIN COMMENTS
      -- Can I Play difficulty
      C := Self.List(Opponent_Win, Can_I_Play);

      Append
        (C, To_Unbounded_String
         ("You're too good for me"));
      Append
        (C, To_Unbounded_String
         ("How did you manage that?"));

      ... and so on.

That works if I then finish with:

      Self.List(Opponent_Win, Can_I_Play) := C;


But that's copy overhead I want to avoid (fifteen times, what's
more). For the time being, I've just replaced all of the "C"s in the
Append procedure calls with "Self.List(Opponent_Win, Can_I_Play)" (and
variations thereof further down).

How can I make "C" act like a reference to the Unbounded_String Vector?

Also, is the 'To_Unbounded_String("this is a string")' really
necessary? I thought I read in the ARM that string literals were
defined for all the string types.

Thanks,

David


P.S.

Later on, I will probably read these comments from an XML file, but
for now I'm just hard coding them into the program.


-- 

David Trudgett
http://www.zeta.org.au/~wpower/

FORTH IF HONK THEN




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

* Re: Aliasing or referencing assignment
  2005-09-09  0:20 Aliasing or referencing assignment David Trudgett
@ 2005-09-09  2:13 ` Steve
  2005-09-09  7:33   ` David Trudgett
  2005-09-09 15:40 ` Jeffrey Carter
  1 sibling, 1 reply; 10+ messages in thread
From: Steve @ 2005-09-09  2:13 UTC (permalink / raw)


"David Trudgett" <wpower@zeta.org.au.nospamplease> wrote in message 
news:m3slwf3xcy.fsf@rr.trudgett...
>
> I'm having a little difficulty with assign by reference and assign by
> value (copy) semantics in a piece of code I'm working on. In the
> chunk of code that follows below, I want to avoid repeating
> "Self.List(Opponent_Win, Can_I_Play)" many times, and just replace it
> with a variable name like "C".
>
> "Self.List(Opponent_Win, Can_I_Play)" returns a Charles container
> library Vector of Unbounded_String. The assignment:
>
>      C := Self.List(Opponent_Win, Can_I_Play); -- "List" is 2D array
>

How about something like:

  declare
      C : ListElementType renames Self.List(Opponent_Win, Can_I_Play);
  begin
        ... code that uses C ...
       Append( C, To_Unbounded_String( "Blah blah" ) );
  end;


Steve
(The Duck)

> is ineffective in doing what I want because it is doing a value
> copy. I've tried to turn the type definition of "C" into an access
> type, but with no success. I also tried the "To_Access" function (in
> charles.vectors.unbounded), but that returns a reference to an
> "Element_Array" and not a Container_Type with which I can use
> "Append".
>
> Anyway, here is the code:
>
>
>
>   procedure Initialize (Self : in out Comments)
>   is
>      C : String_List.Container_Type;
>      package Random_Integer is new Ada.Numerics.Discrete_Random(Integer);
>      use Random_Integer;
>      G : Generator;
>   begin
>
>      Reset(G);
>      Ada.Numerics.Float_Random.Reset(Self.PRNG, Random(G));
>
>      -- OPPONENT WIN COMMENTS
>      -- Can I Play difficulty
>      C := Self.List(Opponent_Win, Can_I_Play);
>
>      Append
>        (C, To_Unbounded_String
>         ("You're too good for me"));
>      Append
>        (C, To_Unbounded_String
>         ("How did you manage that?"));
>
>      ... and so on.
>
> That works if I then finish with:
>
>      Self.List(Opponent_Win, Can_I_Play) := C;
>
>
> But that's copy overhead I want to avoid (fifteen times, what's
> more). For the time being, I've just replaced all of the "C"s in the
> Append procedure calls with "Self.List(Opponent_Win, Can_I_Play)" (and
> variations thereof further down).
>
> How can I make "C" act like a reference to the Unbounded_String Vector?
>
> Also, is the 'To_Unbounded_String("this is a string")' really
> necessary? I thought I read in the ARM that string literals were
> defined for all the string types.
>
> Thanks,
>
> David
>
>
> P.S.
>
> Later on, I will probably read these comments from an XML file, but
> for now I'm just hard coding them into the program.
>
>
> -- 
>
> David Trudgett
> http://www.zeta.org.au/~wpower/
>
> FORTH IF HONK THEN
> 





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

* Re: Aliasing or referencing assignment
  2005-09-09  2:13 ` Steve
@ 2005-09-09  7:33   ` David Trudgett
  0 siblings, 0 replies; 10+ messages in thread
From: David Trudgett @ 2005-09-09  7:33 UTC (permalink / raw)


"Steve" <nospam_steved94@comcast.net> writes:

> "David Trudgett" <wpower@zeta.org.au.nospamplease> wrote in message 
> news:m3slwf3xcy.fsf@rr.trudgett...
>>
>> I'm having a little difficulty with assign by reference and assign by
>> value (copy) semantics in a piece of code I'm working on. In the
>> chunk of code that follows below, I want to avoid repeating
>> "Self.List(Opponent_Win, Can_I_Play)" many times, and just replace it
>> with a variable name like "C".
>>
>> "Self.List(Opponent_Win, Can_I_Play)" returns a Charles container
>> library Vector of Unbounded_String. The assignment:
>>
>>      C := Self.List(Opponent_Win, Can_I_Play); -- "List" is 2D array
>>
>
> How about something like:
>
>   declare
>       C : ListElementType renames Self.List(Opponent_Win, Can_I_Play);
>   begin
>         ... code that uses C ...
>        Append( C, To_Unbounded_String( "Blah blah" ) );
>   end;

That's exactly what I needed, Steve! Thanks. I actually tried to use
"renames", but I guess I must have got it slightly wrong.

It's very handy to be able to declare variables of restricted scope
like that in Ada, too! Like in Lisp (with LET and LET*), and unlike
Pascal.

Anyway, for the record, this is what worked:

      declare
         C : String_List.Container_Type
           renames Self.List(Game_Start, Nightmare);
      begin

         Append
           (C, To_Unbounded_String
            ("Go ahead, make my day..."));

         ... and so on

      end;



Cheers,

David

-- 

David Trudgett
http://www.zeta.org.au/~wpower/

"The high office of the President has been used to foment a plot to
destroy the American's freedom and before I leave office, I must
inform the Citizen of his plight." 

    -- John F. Kennedy, speaking at Columbia University, 
       10 days before his assassination



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

* Re: Aliasing or referencing assignment
  2005-09-09  0:20 Aliasing or referencing assignment David Trudgett
  2005-09-09  2:13 ` Steve
@ 2005-09-09 15:40 ` Jeffrey Carter
  2005-09-10  7:38   ` David Trudgett
  1 sibling, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2005-09-09 15:40 UTC (permalink / raw)


David Trudgett wrote:
> 
> Also, is the 'To_Unbounded_String("this is a string")' really
> necessary? I thought I read in the ARM that string literals were
> defined for all the string types.

String literals are defined for all string types. However, 
Unbounded_String is not a string type. A string type is a 
one-dimensional array with components of a character type. 
Unbounded_String is a private type.

-- 
Jeffrey Carter
"Now go away or I shall taunt you a second time."
Monty Python and the Holy Grail
E-mail: jeffrey_r_carter-nr [commercial-at]
         raytheon [period | full stop] com



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

* Re: Aliasing or referencing assignment
  2005-09-09 15:40 ` Jeffrey Carter
@ 2005-09-10  7:38   ` David Trudgett
  2005-09-10 10:01     ` Martin Dowie
  0 siblings, 1 reply; 10+ messages in thread
From: David Trudgett @ 2005-09-10  7:38 UTC (permalink / raw)


Jeffrey Carter <spam@spam.com> writes:

> David Trudgett wrote:
>> Also, is the 'To_Unbounded_String("this is a string")' really
>> necessary? I thought I read in the ARM that string literals were
>> defined for all the string types.
>
> String literals are defined for all string types. However,
> Unbounded_String is not a string type. A string type is a
> one-dimensional array with components of a character
> type. Unbounded_String is a private type.

Well that explains that little mystery, then. I just didn't want to be
using all those "To_Unbounded_String" calls if I could avoid them. I
suppose they get optimised away in the actual object code, though.

Thanks for that.

Cheers,

David



-- 

David Trudgett
http://www.zeta.org.au/~wpower/

A person cannot support the policies of the Bush administration
unless said person is lacking in either intelligence or decency -- or,
in the case of Bush himself, both. "I just didn't know" simply doesn't
cut it when your proclaimed ignorance is based on lies that are an
insult to the intelligence of a child.

    -- David McGowan, April 2003. 
       http://davesweb.cnchost.com/nwsltr34.html



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

* Re: Aliasing or referencing assignment
  2005-09-10  7:38   ` David Trudgett
@ 2005-09-10 10:01     ` Martin Dowie
  2005-09-10 10:33       ` David Trudgett
  0 siblings, 1 reply; 10+ messages in thread
From: Martin Dowie @ 2005-09-10 10:01 UTC (permalink / raw)


David Trudgett wrote:
> Jeffrey Carter <spam@spam.com> writes:
> 
> 
>>David Trudgett wrote:
>>
>>>Also, is the 'To_Unbounded_String("this is a string")' really
>>>necessary? I thought I read in the ARM that string literals were
>>>defined for all the string types.
>>
>>String literals are defined for all string types. However,
>>Unbounded_String is not a string type. A string type is a
>>one-dimensional array with components of a character
>>type. Unbounded_String is a private type.
> 
> 
> Well that explains that little mystery, then. I just didn't want to be
> using all those "To_Unbounded_String" calls if I could avoid them. I
> suppose they get optimised away in the actual object code, though.

Er, no - why would you think that?...



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

* Re: Aliasing or referencing assignment
  2005-09-10 10:01     ` Martin Dowie
@ 2005-09-10 10:33       ` David Trudgett
  2005-09-10 11:28         ` Ludovic Brenta
  2005-09-11  3:20         ` Jeffrey R. Carter
  0 siblings, 2 replies; 10+ messages in thread
From: David Trudgett @ 2005-09-10 10:33 UTC (permalink / raw)


Martin Dowie <martin.dowie@btopenworld.com> writes:

> David Trudgett wrote:
>> Jeffrey Carter <spam@spam.com> writes:
>>
>>>David Trudgett wrote:
>>>
>>>>Also, is the 'To_Unbounded_String("this is a string")' really
>>>>necessary? I thought I read in the ARM that string literals were
>>>>defined for all the string types.
>>>
>>>String literals are defined for all string types. However,
>>>Unbounded_String is not a string type. A string type is a
>>>one-dimensional array with components of a character
>>>type. Unbounded_String is a private type.
>> Well that explains that little mystery, then. I just didn't want to
>> be
>> using all those "To_Unbounded_String" calls if I could avoid them. I
>> suppose they get optimised away in the actual object code, though.
>
> Er, no - why would you think that?...

Without knowing the details of how they work, I was just assuming they
would be pre-elaborated during the compilation process.

David

-- 

David Trudgett
http://www.zeta.org.au/~wpower/

Patriotism is a pernicious, psychopathic form of idiocy.

    -- George Bernard Shaw (1856-1950)



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

* Re: Aliasing or referencing assignment
  2005-09-10 10:33       ` David Trudgett
@ 2005-09-10 11:28         ` Ludovic Brenta
  2005-09-11 20:43           ` David Trudgett
  2005-09-11  3:20         ` Jeffrey R. Carter
  1 sibling, 1 reply; 10+ messages in thread
From: Ludovic Brenta @ 2005-09-10 11:28 UTC (permalink / raw)


David Trudgett <wpower@zeta.org.au.nospamplease> writes:
> Without knowing the details of how they work, I was just assuming
> they would be pre-elaborated during the compilation process.

Internally, they may be implemented by an array and a used length.
When the used length grows beyond the length of the array, the array
is reallocated to grow in size.  The complexity of this is probably
just above what can be preelaborated automatically.

You can have preelaborated unbounded strings if they are constant and
you declare them in a package which is preelaborated, e.g.

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package Constants is
   pragma Preelaborate; -- or better yet, Pure

   function "+" (R : in String) return Unbounded_String
      renames To_Unbounded_String;

   S : constant Unbounded_String := +"Some fixed string";
end Constants;

HTH

(The above package can be declared Pure.  In addition to being
preelaborated, Pure packages promise that they have no side effects:
no visible global variables, no access parameters, and no use of
System.Address.).

-- 
Ludovic Brenta.



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

* Re: Aliasing or referencing assignment
  2005-09-10 10:33       ` David Trudgett
  2005-09-10 11:28         ` Ludovic Brenta
@ 2005-09-11  3:20         ` Jeffrey R. Carter
  1 sibling, 0 replies; 10+ messages in thread
From: Jeffrey R. Carter @ 2005-09-11  3:20 UTC (permalink / raw)


David Trudgett wrote:
> Without knowing the details of how they work, I was just assuming they
> would be pre-elaborated during the compilation process.

GNAT in gcc 3.4.2 implements Unbounded_String as

    package AF renames Ada.Finalization;

    Null_String : aliased String := "";

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

and To_Unbounded_String as

    function To_Unbounded_String (Source : String) return Unbounded_String is
       Result : Unbounded_String;

    begin
       Result.Last          := Source'Length;
       Result.Reference     := new String (1 .. Source'Length);
       Result.Reference.all := Source;
       return Result;
    end To_Unbounded_String;

Other implementations are possible.

-- 
Jeff Carter
"I'm a lumberjack and I'm OK."
Monty Python's Flying Circus
54



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

* Re: Aliasing or referencing assignment
  2005-09-10 11:28         ` Ludovic Brenta
@ 2005-09-11 20:43           ` David Trudgett
  0 siblings, 0 replies; 10+ messages in thread
From: David Trudgett @ 2005-09-11 20:43 UTC (permalink / raw)


Ludovic Brenta <ludovic@ludovic-brenta.org> writes:

> David Trudgett <wpower@zeta.org.au.nospamplease> writes:
>> Without knowing the details of how they work, I was just assuming
>> they would be pre-elaborated during the compilation process.
>
> Internally, they may be implemented by an array and a used length.
> When the used length grows beyond the length of the array, the array
> is reallocated to grow in size.  

I suppose that's the general idea I had in the back of my mind.


> The complexity of this is probably just above what can be
> preelaborated automatically.

It might be tricky, but I'm no compiler writer, so I'll keep my mouth
shut! ;-)


>
> You can have preelaborated unbounded strings if they are constant and
> you declare them in a package which is preelaborated, e.g.

That's useful to know. Thank you.


>
> with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
> package Constants is
>    pragma Preelaborate; -- or better yet, Pure
>
>    function "+" (R : in String) return Unbounded_String
>       renames To_Unbounded_String;
>
>    S : constant Unbounded_String := +"Some fixed string";
> end Constants;
>
> HTH

Yes, it is helpful, thanks. I also like the way you've renamed
To_Unbounded_String to a right associative "+". Very interesting
indeed. Another person posted a code snippet showing a long package
name being renamed to something shorter. It seems 'rename' has more
uses than I at first thought... :-)


>
> (The above package can be declared Pure.  In addition to being
> preelaborated, Pure packages promise that they have no side effects:
> no visible global variables, no access parameters, and no use of
> System.Address.).

I'll keep it in mind the next time I have need of some purity! ;-)

Thanks again for your comments.

David



-- 

David Trudgett
http://www.zeta.org.au/~wpower/

"That a careful reader looking for a fact can sometimes find it with
diligence and a skeptical eye tells us nothing about whether that fact
received the attention and context it deserved, whether it was
intelligible to the reader or effectively distorted or suppressed.
What level of attention it deserved may be debatable, but there is no
merit to the pretense that because certain facts may be found in the
media by a diligent and skeptical researcher, the absence of radical
bias and de facto suppression is thereby demonstrated."
    
    -- Herman and Chomsky, Manufacturing Consent, pp. xv.



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

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

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-09  0:20 Aliasing or referencing assignment David Trudgett
2005-09-09  2:13 ` Steve
2005-09-09  7:33   ` David Trudgett
2005-09-09 15:40 ` Jeffrey Carter
2005-09-10  7:38   ` David Trudgett
2005-09-10 10:01     ` Martin Dowie
2005-09-10 10:33       ` David Trudgett
2005-09-10 11:28         ` Ludovic Brenta
2005-09-11 20:43           ` David Trudgett
2005-09-11  3:20         ` Jeffrey R. Carter

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