comp.lang.ada
 help / color / mirror / Atom feed
From: hreba <f_hreba@yahoo.com.br>
Subject: Array Index: substitute integer with enumeration
Date: Thu, 4 Jun 2020 18:54:54 +0200
Date: 2020-06-04T18:54:54+02:00	[thread overview]
Message-ID: <hjsnauF9ikeU1@mid.individual.net> (raw)

Just as an example, let's assume there is a general geometry package 
which requires a function from the client which transforms cartesian 
coordinates into a client-specific coordinate system:

type Point is array (range 1..3) of Float;

type Transform_T is access Function (p: Point) return Point;

As a client who implements spherical coordinates, I would then write a 
function:

function To_Spherical (p: Point) return Point is
begin
   return
     ( Sqrt(p(1)**2 + p(2)**2 + p(3)**2),	-- r
       Arctan(p(2)/p(1)),			-- phi
       ...
     )
end To_Spherical.

Enumerations would make this less error-prone, but somewhat clumsy. With

type Cart_Coord is (x, y, z);

p(1) could be written as p(x'Pos+1). This is safer, but if I would 
define additionally

  type Spherical_Coord is (r, phi, theta)

the compiler wouldn't notice a mix-up of x'Pos and r'Pos. But now I 
could write

type Cart_Point is array (Cart_Coord) of Float;

function To_Spherical (p: Point) return Point is
   pc: Cart_Point:= p;
begin
   return
     ( Sqrt(pc(x)**2 + pc(y)**2 + pc(z)**2),	-- r
       Arctan(p(y)/p(x)),			-- phi
       ...
     )
end To_Spherical.

My questions are now:

1. Does the initialization of pc involve any copying at run time?
2. If so, how can it be avoided?

-- 
Frank Hrebabetzky, Kronach	+49 / 9261 / 950 0565

             reply	other threads:[~2020-06-04 16:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-04 16:54 hreba [this message]
2020-06-04 17:49 ` Array Index: substitute integer with enumeration Jeffrey R. Carter
2020-06-05 15:16   ` hreba
2020-06-05 17:15     ` Jeffrey R. Carter
2020-06-09 17:25 ` Shark8
replies disabled

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