comp.lang.ada
 help / color / mirror / Atom feed
* Dynamic array allocation and STL equivalents?
@ 2005-02-11 13:06 brian.b.mcguinness
  2005-02-11 13:43 ` Jeff C
                   ` (6 more replies)
  0 siblings, 7 replies; 16+ messages in thread
From: brian.b.mcguinness @ 2005-02-11 13:06 UTC (permalink / raw)


Back in the mid 1980s, out of curiosity I bought books about a number
of programming languages, including LISP and Forth.  One of these books
was a turquoise and white paperback about Ada by J.G.P. Barnes.  During
a summer vacation trip, I read the Barnes book while riding for hours
down highways in the car.  I was impressed with features such as being
able to overload operators for newly defined types, the exception
handling facilities, and the ability to write constants in a wide
variety of bases.  My impression was that this was Pascal done right.
But at the time I didn't have access to an Ada compiler, so I couldn't
play with the language.

Recently, while looking through a list of GNU software, I was reminded
that there is a Gnu Ada compiler, so I installed it on a computer at
home, along with its library.  So far I have not found an rpm package
of GtkAda for Fedora Core 3, but I am still looking.

I would like to write a few Ada programs and play around with the
language a bit.  From the mid 1980s through the early 1990s I wrote
programs in Borland Turbo Pascal 3 through 6, which I was quite fond
of, so I shouldn't have much trouble picking up Ada, which has a
similar syntax.  But there are a few things I don't know how to do.
For one thing, I have looked through the Barnes book, the pages of
which have turned tan with age, and online, but can't find any
information on how to allocate arrays dynamically; there seems to be no
equivalent to the C malloc() function or the C++ dimensioned new.  If
someone would tell me how to do this, I would appreciate it.

It would also be useful to know if there is an Ada equivalent of the
C++ Standard Template Library, with classes for vectors, associative
arrays, and so on.

One interesting project would be to create an object class hierarchy to
implement APL arrays.  In APL, the lengths of an array's dimensions can
change, e.g. by concatenating new rows or columns onto a matrix, or
concatenating two matrices, and the number of dimensions can also
change, e.g. by "laminating" two 12x30 arrays to form a 2x12x30 array.
In C++, an obvious solution would be to use STL vectors, e.g.:

class RealArray {
  private:
    vector <unsigned long> dimensions;
    vector <double>        data;
  public:
    // define an indexing operator[] to use the dimensions
    // vector to translate sets of indices into offsets
    // into the data array
    ...
};

But I don't know how to do this in Ada.

I would appreciate suggestions.  These might be good topics to cover in
a FAQ.

--- Brian




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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 13:06 Dynamic array allocation and STL equivalents? brian.b.mcguinness
@ 2005-02-11 13:43 ` Jeff C
  2005-02-14 15:23   ` Marc A. Criley
  2005-02-11 14:41 ` Dmitry A. Kazakov
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 16+ messages in thread
From: Jeff C @ 2005-02-11 13:43 UTC (permalink / raw)


brian.b.mcguinness@lmco.com wrote:

> 
> Recently, while looking through a list of GNU software, I was reminded
> that there is a Gnu Ada compiler, so I installed it on a computer at
> home, along with its library.  So far I have not found an rpm package
> of GtkAda for Fedora Core 3, but I am still looking.
> 

You probably won't find it. I know people can be adverse to installing 
things via non-rpm means but there are times when you don't have an 
obvious choice. What I have done is to setup something like 
/opt/localmanaged as a root for packages like this.

Futher, I use Checkinstall http://asic-linux.com.mx/~izto/checkinstall/ 
to do the install/create the RPM. It is not a perfect approach but it 
generally works. I have not done this exact matching (GtkAda and Fedora 
Core 3) using this method (because sometimes I get lazy and just do an 
install into my home directory if I am doing some short term testing).



> I would like to write a few Ada programs and play around with the
> language a bit.  From the mid 1980s through the early 1990s I wrote
> programs in Borland Turbo Pascal 3 through 6, which I was quite fond
> of, so I shouldn't have much trouble picking up Ada, which has a
> similar syntax.  But there are a few things I don't know how to do.
> For one thing, I have looked through the Barnes book, the pages of
> which have turned tan with age, and online, but can't find any
> information on how to allocate arrays dynamically; there seems to be no
> equivalent to the C malloc() function or the C++ dimensioned new.  If
> someone would tell me how to do this, I would appreciate it.
> 

--
-- WARNING - NOT TESTED/NOT COMPILED SEMI-PSEUDO CODE TO ILLUSTRATE
-- POINT.
--
with Unchecked_Deallocation;


type My_Array is (integer range <>) of Float;
type Access_My_Array is access My_Array;
procedure Free is new Unchecked_Deallocation(My_Array, Access_My_Array);

A : Access_My_Array;
begin
   A := new My_Array(10 .. 1000); -- Made up bound, can be fully dynamic
   Free(A); -- Return storage....Since I figured that was the next question
end;


Note that you can more often than not avoid using access types 
(pointers) in Ada but clearly they still have lots of important uses.


> It would also be useful to know if there is an Ada equivalent of the
> C++ Standard Template Library, with classes for vectors, associative
> arrays, and so on.
> 


Ada is at a transition point with respect to this. Previously the 
standard libraries that shiped with Ada compilers did not include 
anything exactly like (or certainly as encompassing) as the STL.

The new Ada Standard (I think we are still calling it Ada 0Y) where Y 
may be = 5. Does include a larger set of predefined packages

Some of these packages have essentially been backported to operate with 
an Ada 95 compiler
http://www.martin.dowie.btinternet.co.uk/

There are also several others available (like the widely discussed Booch 
Components). Take a look at

http://www.adapower.com/index.php?Command=Class&ClassID=AdaLibs&Title=Ada+Libraries

(in fact, poke around the entire www.adapower.com site and 
www.adaworld.com sites for other good info)



> One interesting project would be to create an object class hierarchy to
> implement APL arrays.  In APL, the lengths of an array's dimensions can
> change, e.g. by concatenating new rows or columns onto a matrix, or
> concatenating two matrices, and the number of dimensions can also
> change, e.g. by "laminating" two 12x30 arrays to form a 2x12x30 array.
> In C++, an obvious solution would be to use STL vectors, e.g.:
> 
> class RealArray {
>   private:
>     vector <unsigned long> dimensions;
>     vector <double>        data;
>   public:
>     // define an indexing operator[] to use the dimensions
>     // vector to translate sets of indices into offsets
>     // into the data array
>     ...
> };
> 
> But I don't know how to do this in Ada.

This might be pretty fun. One thing that would have to be different is 
that Ada 95 does not allow one to overload the array index "operator" ().

Perhaps you should have a look at the Ada 95 for C++ tutorial at the
(essentially dead) website http://www.adahome.com/Ammo/cpp2ada.html
(Don't be fooled by the date on the front page of this website that 
updates every day...The site went static about 5 years ago..Still has 
some good old reference info)






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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 13:06 Dynamic array allocation and STL equivalents? brian.b.mcguinness
  2005-02-11 13:43 ` Jeff C
@ 2005-02-11 14:41 ` Dmitry A. Kazakov
  2005-02-11 15:50   ` Adrien Plisson
  2005-02-11 17:47   ` REH
  2005-02-11 17:24 ` Bobby D. Bryant
                   ` (4 subsequent siblings)
  6 siblings, 2 replies; 16+ messages in thread
From: Dmitry A. Kazakov @ 2005-02-11 14:41 UTC (permalink / raw)


On 11 Feb 2005 05:06:56 -0800, brian.b.mcguinness@lmco.com wrote:

[...]

Dynamic allocation of arrays is no problem in Ada.

> One interesting project would be to create an object class hierarchy to
> implement APL arrays.  In APL, the lengths of an array's dimensions can
> change, e.g. by concatenating new rows or columns onto a matrix, or
> concatenating two matrices, and the number of dimensions can also
> change, e.g. by "laminating" two 12x30 arrays to form a 2x12x30 array.
> In C++, an obvious solution would be to use STL vectors, e.g.:
> 
> class RealArray {
>   private:
>     vector <unsigned long> dimensions;
>     vector <double>        data;
>   public:
>     // define an indexing operator[] to use the dimensions
>     // vector to translate sets of indices into offsets
>     // into the data array

Index of a multi-dimensional array is a tuple. That won't work in C++ (you
cannot override "," to support: A[i,j,k]). Neither works it in Ada.

Though in Ada you could have a type:

   type Index is array (Integer range <>) of Integer; -- Tuple

then in Real_Array:

   type Real_Array is private;
   function Get (Container : Real_Array; Element : Index) return Float;

private
   type Index_Ptr is access Index;
   type Real_Array is new Ada.Finalization.Controlled with record
      Lower_Bound : Index_Ptr;
      Upper_Bound : Index_Ptr;
      Data : <whatsoever>
   ...
   procedure Finalize (X : in out Real_Array);
      -- Destructor is a handy thing

Then you could index your arrays like:

Get (My_Array, (I, J, K));

Further, if you wished to support slices sub-matrices etc, that would
require a lot of work. Alas, in Ada you cannot override either "()" or ".."
or "in" (membership test) or aggregates. So the interface would be rather
clumsy.

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



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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 14:41 ` Dmitry A. Kazakov
@ 2005-02-11 15:50   ` Adrien Plisson
  2005-02-11 17:47   ` REH
  1 sibling, 0 replies; 16+ messages in thread
From: Adrien Plisson @ 2005-02-11 15:50 UTC (permalink / raw)


Dmitry A. Kazakov wrote:
> Index of a multi-dimensional array is a tuple. That won't work in C++ (you
> cannot override "," to support: A[i,j,k]). Neither works it in Ada.

in C++, you can override "," ("," is an operator as any others). but 
you should really not do this: some people use this operator 
extensively for brevity or just because they want to put multiple 
expressions where they should only put one (eg, in the condition of a 
"if" statement).

but you may override operator [] to take a parameter of type 
std::vector, as suggested by the OP. it may render the element access 
syntax not obvious, but it is comparable to the Ada solution you wrote.

-- 
rien



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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 13:06 Dynamic array allocation and STL equivalents? brian.b.mcguinness
  2005-02-11 13:43 ` Jeff C
  2005-02-11 14:41 ` Dmitry A. Kazakov
@ 2005-02-11 17:24 ` Bobby D. Bryant
  2005-02-12  0:06   ` Robert A Duff
  2005-02-11 18:07 ` Matthew Heaney
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 16+ messages in thread
From: Bobby D. Bryant @ 2005-02-11 17:24 UTC (permalink / raw)


On Fri, 11 Feb 2005, brian.b.mcguinness@lmco.com wrote:

> Back in the mid 1980s, out of curiosity I bought books about a number
> of programming languages, including LISP and Forth.  One of these books
> was a turquoise and white paperback about Ada by J.G.P. Barnes.  During
> a summer vacation trip, I read the Barnes book while riding for hours
> down highways in the car.  I was impressed with features such as being
> able to overload operators for newly defined types, the exception
> handling facilities, and the ability to write constants in a wide
> variety of bases.  My impression was that this was Pascal done right.

Pascal pretty much _was_ "done right", given its pedagogical purpose.


> But at the time I didn't have access to an Ada compiler, so I couldn't
> play with the language.
> 
> Recently, while looking through a list of GNU software, I was reminded
> that there is a Gnu Ada compiler, so I installed it on a computer at
> home, along with its library.  So far I have not found an rpm package
> of GtkAda for Fedora Core 3, but I am still looking.

Google for 'gnat "fedora core 3"' and you'll get some hits which may
have what you want.  (Use your own judgement as to how safe a given
download site is.)


> I would like to write a few Ada programs and play around with the
> language a bit.  From the mid 1980s through the early 1990s I wrote
> programs in Borland Turbo Pascal 3 through 6, which I was quite fond
> of, so I shouldn't have much trouble picking up Ada, which has a
> similar syntax.  But there are a few things I don't know how to do.
> For one thing, I have looked through the Barnes book, the pages of
> which have turned tan with age, and online, but can't find any
> information on how to allocate arrays dynamically; there seems to be no
> equivalent to the C malloc() function or the C++ dimensioned new.  If
> someone would tell me how to do this, I would appreciate it.

Others have already answered this, but I'd like to add that your book
is apparently pre- Ada95, so you may want to get a newer book, or at
least consult some on-line sources to see what has changed.

Here's the Ada95 reference manual:
http://www.adahome.com/rm95/rm9x-toc.html, though it is probably easier
to learn from a book.

Here's a page about Ada83 vs. Ada95:
http://www.adaic.org/learn/tech/8395comp.html

-- 
Bobby Bryant
Austin, Texas



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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 14:41 ` Dmitry A. Kazakov
  2005-02-11 15:50   ` Adrien Plisson
@ 2005-02-11 17:47   ` REH
  2005-02-12  9:28     ` Dmitry A. Kazakov
  1 sibling, 1 reply; 16+ messages in thread
From: REH @ 2005-02-11 17:47 UTC (permalink / raw)



"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message
news:13r5d1sfsg55d$.1u0p9rdnt3zcy.dlg@40tude.net...
> On 11 Feb 2005 05:06:56 -0800, brian.b.mcguinness@lmco.com wrote:
> Index of a multi-dimensional array is a tuple. That won't work in C++ (you
> cannot override "," to support: A[i,j,k]). Neither works it in Ada.
>
Sure you can (though I don't avocate it).  Off the top of my head:

use enumeration type and operator overloading of ",".


enum I {i_first = 0, i_max = 10};
enum J {j_first = 0, j_max = 10};
enum K {k_first = 0, k_max = 10};
const size_t size = sizeof(int);

inline std::pair<size_t, size_t> operator, (I i, J j) {return
std::make_pair(size_t(i),  size_t(j));}
inline size_t operator, (const std::pair<size_t, size_t>& p, K k)
{
    size_t i = p.first * k_max * j_max * size;
    i += p.second * k_max * size;
    i += k;
    return i;
}

int* array = new int[i_max * j_max * k_max];

int* p = array[I(5), J(6), K(7)];

You can cleanup and "genericize" this more with templates.  My math may be
off, but the concept is the same.

As I said, I wouldn't do it, but it is possible.






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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 13:06 Dynamic array allocation and STL equivalents? brian.b.mcguinness
                   ` (2 preceding siblings ...)
  2005-02-11 17:24 ` Bobby D. Bryant
@ 2005-02-11 18:07 ` Matthew Heaney
  2005-02-11 18:36   ` Martin Krischik
  2005-02-11 19:35 ` brian.b.mcguinness
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 16+ messages in thread
From: Matthew Heaney @ 2005-02-11 18:07 UTC (permalink / raw)



brian.b.mcguinness@lmco.com wrote:
>
> It would also be useful to know if there is an Ada equivalent of the
> C++ Standard Template Library, with classes for vectors, associative
> arrays, and so on.

Ada 2005 will have a standard container library, very similar to the
C++ STL.  It will include vectors, lists, and hashed and ordered sets
and maps.

See the files a-c*.ad[sb] here:

http://charles.tigris.org/source/browse/charles/src/ai302/

Drop me a line (or post on CLA) if you have any container library
questions.

-Matt




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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 18:07 ` Matthew Heaney
@ 2005-02-11 18:36   ` Martin Krischik
  0 siblings, 0 replies; 16+ messages in thread
From: Martin Krischik @ 2005-02-11 18:36 UTC (permalink / raw)


Hello Matthew

Matthew Heaney wrote:

> Drop me a line (or post on CLA) if you have any container library
> questions.

When you are here, maybe you can help me out.

http://en.wikibooks.org/wiki/Programming:Ada:Packages:Ada.Containers

looks a bit empty and could do with some work. And who would be better then
you to describe the new container classes.

It is enough if you hack some raw text in - I will do the beautifying. But
it would be a great help for the wikibook project.

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com




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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 13:06 Dynamic array allocation and STL equivalents? brian.b.mcguinness
                   ` (3 preceding siblings ...)
  2005-02-11 18:07 ` Matthew Heaney
@ 2005-02-11 19:35 ` brian.b.mcguinness
  2005-02-12  1:04 ` Jeffrey Carter
  2005-02-12  6:19 ` Bobby D. Bryant
  6 siblings, 0 replies; 16+ messages in thread
From: brian.b.mcguinness @ 2005-02-11 19:35 UTC (permalink / raw)


Thanks to everyone for the help.  This will be very useful in getting
started with Ada.  

--- Brian




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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 17:24 ` Bobby D. Bryant
@ 2005-02-12  0:06   ` Robert A Duff
  2005-02-12  3:45     ` Bobby D. Bryant
  0 siblings, 1 reply; 16+ messages in thread
From: Robert A Duff @ 2005-02-12  0:06 UTC (permalink / raw)


bdbryant@mail.utexas.edu (Bobby D. Bryant) writes:

> On Fri, 11 Feb 2005, brian.b.mcguinness@lmco.com wrote:
> 
> > Back in the mid 1980s, out of curiosity I bought books about a number
> > of programming languages, including LISP and Forth.  One of these books
> > was a turquoise and white paperback about Ada by J.G.P. Barnes.

Barnes has also written books about Ada 95.  And he's currently
writing some articles on the new Ada 2005.

>...During
> > a summer vacation trip, I read the Barnes book while riding for hours
> > down highways in the car.  I was impressed with features such as being
> > able to overload operators for newly defined types, the exception
> > handling facilities, and the ability to write constants in a wide
> > variety of bases.  My impression was that this was Pascal done right.
> 
> Pascal pretty much _was_ "done right", given its pedagogical purpose.

Hmm...  Even back in those days, I would want to teach students about
encapsulation and information hiding, which Pascal doesn't support very
well.  And why should students have to deal with static-sized strings?
And why have dangling "else", instead of if/else/end if?  And why does
Read summarily kill the program on bad input?  Shouldn't students learn
about proper error recovery?  Separate compilation?  Etc.

My impression of Ada in 198x was the same as the original poster --
"Pascal done right".

> Others have already answered this, but I'd like to add that your book
> is apparently pre- Ada95, so you may want to get a newer book, or at
> least consult some on-line sources to see what has changed.

Yes.

> Here's the Ada95 reference manual:
> http://www.adahome.com/rm95/rm9x-toc.html, though it is probably easier
> to learn from a book.

Yes.  As one of the authors of that manual, I definitely agree it's
not an easy way to learn the language.  Read a textbook instead.

- Bob



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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 13:06 Dynamic array allocation and STL equivalents? brian.b.mcguinness
                   ` (4 preceding siblings ...)
  2005-02-11 19:35 ` brian.b.mcguinness
@ 2005-02-12  1:04 ` Jeffrey Carter
  2005-02-12  6:19 ` Bobby D. Bryant
  6 siblings, 0 replies; 16+ messages in thread
From: Jeffrey Carter @ 2005-02-12  1:04 UTC (permalink / raw)


brian.b.mcguinness@lmco.com wrote:

> I would like to write a few Ada programs and play around with the
> language a bit.  From the mid 1980s through the early 1990s I wrote
> programs in Borland Turbo Pascal 3 through 6, which I was quite fond
> of, so I shouldn't have much trouble picking up Ada, which has a
> similar syntax.  But there are a few things I don't know how to do.
> For one thing, I have looked through the Barnes book, the pages of
> which have turned tan with age, and online, but can't find any
> information on how to allocate arrays dynamically; there seems to be no
> equivalent to the C malloc() function or the C++ dimensioned new.  If
> someone would tell me how to do this, I would appreciate it.

Nobody addressed this, so I'll point out a simple way to create a 
dynamically sized array in Ada. I'll use the predefined array type 
String for simplicity:

Dynamic_Array : declare
    S : String (1 .. N);
begin -- Dynamic_Array
    -- Do something with S here
end Dynamic_Array;

N can come from the command line, input, a function, or anything else. 
There's no messing with pointers, no possibility of memory leaks or 
buffer overflows.

-- 
Jeff Carter
"If you think you got a nasty taunting this time,
you ain't heard nothing yet!"
Monty Python and the Holy Grail
23



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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-12  0:06   ` Robert A Duff
@ 2005-02-12  3:45     ` Bobby D. Bryant
  0 siblings, 0 replies; 16+ messages in thread
From: Bobby D. Bryant @ 2005-02-12  3:45 UTC (permalink / raw)


On Sat, 12 Feb 2005, Robert A Duff <bobduff@shell01.TheWorld.com> wrote:

> bdbryant@mail.utexas.edu (Bobby D. Bryant) writes:
> 
>> Pascal pretty much _was_ "done right", given its pedagogical purpose.
> 
> Hmm...  Even back in those days, I would want to teach students about
> encapsulation and information hiding, which Pascal doesn't support very
> well.  And why should students have to deal with static-sized strings?
> And why have dangling "else", instead of if/else/end if?  And why does
> Read summarily kill the program on bad input?  Shouldn't students learn
> about proper error recovery?  Separate compilation?  Etc.
> 
> My impression of Ada in 198x was the same as the original poster --
> "Pascal done right".

OK, I concede.

-- 
Bobby Bryant
Austin, Texas



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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 13:06 Dynamic array allocation and STL equivalents? brian.b.mcguinness
                   ` (5 preceding siblings ...)
  2005-02-12  1:04 ` Jeffrey Carter
@ 2005-02-12  6:19 ` Bobby D. Bryant
  6 siblings, 0 replies; 16+ messages in thread
From: Bobby D. Bryant @ 2005-02-12  6:19 UTC (permalink / raw)


On Fri, 11 Feb 2005, brian.b.mcguinness@lmco.com wrote:

> One interesting project would be to create an object class hierarchy to
> implement APL arrays.  In APL, the lengths of an array's dimensions can
> change, e.g. by concatenating new rows or columns onto a matrix, or
> concatenating two matrices, and the number of dimensions can also
> change, e.g. by "laminating" two 12x30 arrays to form a 2x12x30 array.
> In C++, an obvious solution would be to use STL vectors, e.g.:
> 
> class RealArray {
>   private:
>     vector <unsigned long> dimensions;
>     vector <double>        data;
>   public:
>     // define an indexing operator[] to use the dimensions
>     // vector to translate sets of indices into offsets
>     // into the data array
>     ...
> };
> 
> But I don't know how to do this in Ada.

If you can work out the recursive data typing issues, you might be
able to do it by overloading the '&' catenation operator to bind a new
row/column/layer onto an array or matrix.

The indexing would be tricky, but if you want think of an array access
as a sort of function call (send an argument, get a result), you might
be able to kludge together a function to do the work.  Let the
function's first argument be the data structure, and the second a
pointer to an (unconstrained) array type that holds the (variable
length) value of the indices of interest.

I think the concept is sound, if you can get everything past the type
checking, though specifying the indices in a program might be tedious.

-- 
Bobby Bryant
Austin, Texas



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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 17:47   ` REH
@ 2005-02-12  9:28     ` Dmitry A. Kazakov
  2005-02-12 18:52       ` Robert A Duff
  0 siblings, 1 reply; 16+ messages in thread
From: Dmitry A. Kazakov @ 2005-02-12  9:28 UTC (permalink / raw)


On Fri, 11 Feb 2005 12:47:48 -0500, REH wrote:

> "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message
> news:13r5d1sfsg55d$.1u0p9rdnt3zcy.dlg@40tude.net...
>> On 11 Feb 2005 05:06:56 -0800, brian.b.mcguinness@lmco.com wrote:
>> Index of a multi-dimensional array is a tuple. That won't work in C++ (you
>> cannot override "," to support: A[i,j,k]). Neither works it in Ada.
>>
> Sure you can (though I don't avocate it). 

OK

> Off the top of my head:
> 
> use enumeration type and operator overloading of ",".
> 
> enum I {i_first = 0, i_max = 10};
> enum J {j_first = 0, j_max = 10};
> enum K {k_first = 0, k_max = 10};
> const size_t size = sizeof(int);
> 
> inline std::pair<size_t, size_t> operator, (I i, J j) {return
> std::make_pair(size_t(i),  size_t(j));}
> inline size_t operator, (const std::pair<size_t, size_t>& p, K k)
> {
>     size_t i = p.first * k_max * j_max * size;
>     i += p.second * k_max * size;
>     i += k;
>     return i;
> }
> 
> int* array = new int[i_max * j_max * k_max];
> 
> int* p = array[I(5), J(6), K(7)];

This is still static and also because there are explicit type
specifications.

Should we try

class IndexTuple
{
public :
   IndexTuple (int);
   IndexTuple operator, (int);

protected :
   // some unbounded index array implementation
};

Then I'm not sure if any C++ compiler would be able to resolve

"Array [1, 2]" to

   IndexTuple::operator, (IndexTuple::IndexTuple (1), 2)

keeping "SomethingUnrelated (1, 2)" resolved to

   SomethingUnrelated (1, 2) to

I have an impression that C++ resolution rules are strictly bottom-up, so
it wouldn't work without explicit type specifications here and there. So in
the end it would be even worse than my variant with Ada's aggregates.

I would really like Ada 2010 to respond to these issues.

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



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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-12  9:28     ` Dmitry A. Kazakov
@ 2005-02-12 18:52       ` Robert A Duff
  0 siblings, 0 replies; 16+ messages in thread
From: Robert A Duff @ 2005-02-12 18:52 UTC (permalink / raw)


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

> I have an impression that C++ resolution rules are strictly bottom-up,
> ...

Yes.  I believe that's why C++ has the kludge of putting "L" or "U" at
the end of an integer literal.  The resolution rules can't tell the
intended type from the context.  (The implicit conversion rules add
some confusion on top of all that.)

That kind of kludge wouldn't work for Ada, where there are more than a
few integer types around the place!

It's also nice that Ada allows overloaded enumeration literals
and string literals.

- Bob



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

* Re: Dynamic array allocation and STL equivalents?
  2005-02-11 13:43 ` Jeff C
@ 2005-02-14 15:23   ` Marc A. Criley
  0 siblings, 0 replies; 16+ messages in thread
From: Marc A. Criley @ 2005-02-14 15:23 UTC (permalink / raw)


Jeff C wrote:

> Perhaps you should have a look at the Ada 95 for C++ tutorial at the
> (essentially dead) website http://www.adahome.com/Ammo/cpp2ada.html
> (Don't be fooled by the date on the front page of this website that 
> updates every day...The site went static about 5 years ago..Still has 
> some good old reference info)

Simon Johnton's "Ada 95 for C and C++ Programmers" is a good reference 
and does justice to its title.  Unfortunately the book is out of print, 
but there do still seem to be some copies floating around.

Marc A. Criley
McKae Technologies
www.mckae.com



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

end of thread, other threads:[~2005-02-14 15:23 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-11 13:06 Dynamic array allocation and STL equivalents? brian.b.mcguinness
2005-02-11 13:43 ` Jeff C
2005-02-14 15:23   ` Marc A. Criley
2005-02-11 14:41 ` Dmitry A. Kazakov
2005-02-11 15:50   ` Adrien Plisson
2005-02-11 17:47   ` REH
2005-02-12  9:28     ` Dmitry A. Kazakov
2005-02-12 18:52       ` Robert A Duff
2005-02-11 17:24 ` Bobby D. Bryant
2005-02-12  0:06   ` Robert A Duff
2005-02-12  3:45     ` Bobby D. Bryant
2005-02-11 18:07 ` Matthew Heaney
2005-02-11 18:36   ` Martin Krischik
2005-02-11 19:35 ` brian.b.mcguinness
2005-02-12  1:04 ` Jeffrey Carter
2005-02-12  6:19 ` Bobby D. Bryant

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