comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: OO Style with Ada Containers
Date: Wed, 21 Nov 2007 04:51:23 +0000
Date: 2007-11-21T04:51:23+00:00	[thread overview]
Message-ID: <uoddojj5g.fsf@earthlink.net> (raw)
In-Reply-To: 400347d7-aa93-4175-a3dc-e415ad0d9ca3@i29g2000prf.googlegroups.com

Maciej Sobczak <see.my.homepage@gmail.com> writes:

> You cannot do arithmetics with cursors. They don't have appropriate
> operators and the examples you shown work with indices, not with
> cursors. Cursors themselves can be only incremented and decremented.

Actually, I thought about this more and concluded it doesn't matter that much.
C++ requires a uniform interface to allow the instantiation to happen, because
of the way it binds operations (by name).  But in Ada you can build a type
adapter on-the-fly (that's the locally declared subprogram stuff, which you
must often do anyway), and furthermore the actual name can differ from the
formal.  So not having cursor-based arithmetic isn't such a great loss, since
you can always instantiate the algorithm using the index subtype instead of
using the cursor (as I showed earlier, to pass an array to a generic
algorithm).

Here's the first method using a package that synthesizes cursor-based
arithmetic operators:

   procedure Op (V : Vector) is
      package Cursor_Ops is
         new Vector_Cursor_Operations (Integer_Vectors, V'Access);
      use Cursor_Ops;
      
      procedure Sort is
         new Generic_Sort 
        (Cursor_Type => Cursor, 
         Element_Type => Integer, 
         Index_Type => Positive);

      M : Cursor := V.First + (No_Element - V.First) / 2;
   begin
      Sort (V.First, M);
      Sort (M, No_Element);
   end Op;


Here's the second method that uses the index subtype to instantiate the generic
sort algorithm:

   procedure Op2 (V : Vector) is
      function Element (I : Index_Type'Base) return Integer is
      begin
         return V.Element (I);
      end;
      procedure Sort is
         new Generic_Sort 
        (Cursor_Type => Integer'Base,  -- or possibly Extended_Index
         Element_Type => Integer,
         Index_Type => Positive);

      M : Extended_Index := V.First_Index + Count_Type'Pos (V.Length / 2);

   begin
      Sort (V.First_Index, M);
      Sort (M, V.First_Index + Count_Type'Pos (V.Length));
   end Op2;

It's not that much different.

If you feel strongly that vector cursors should support some kind of
arithmetic, then you might want to post a message to the ada-comment list so
that the request gets logged, and eventually put on the ARG agenda.

Cheers,
Matt


--STX
with Ada.Containers.Vectors; use Ada.Containers;

package Test_Vector_Operations is
   pragma Preelaborate;

   package Integer_Vectors is new Vectors (Positive, Integer);
   use Integer_Vectors;

   procedure Op (V : Vector);
   procedure Op2 (V : Vector);

end Test_Vector_Operations;

with Vector_Cursor_Operations;
with Generic_Sort;

package body Test_Vector_Operations is
   
   procedure Op (V : Vector) is
      package Cursor_Ops is
         new Vector_Cursor_Operations (Integer_Vectors, V'Access);
      use Cursor_Ops;
      
      procedure Sort is
         new Generic_Sort 
        (Cursor_Type => Cursor, 
         Element_Type => Integer, 
         Index_Type => Positive);

      M : Cursor := V.First + (No_Element - V.First) / 2;
   begin
      Sort (V.First, M);
      Sort (M, No_Element);
   end Op;
      
      
   procedure Op2 (V : Vector) is
      function Element (I : Index_Type'Base) return Integer is
      begin
         return V.Element (I);
      end;
      procedure Sort is
         new Generic_Sort 
        (Cursor_Type => Integer'Base,
         Element_Type => Integer,
         Index_Type => Positive);

      M : Extended_Index := V.First_Index + Count_Type'Pos (V.Length / 2);

   begin
      Sort (V.First_Index, M);
      Sort (M, V.First_Index + Count_Type'Pos (V.Length));
   end Op2;


end Test_Vector_Operations;


generic
   type Cursor_Type is private;
   type Element_Type is private;
   type Index_Type is (<>);
   with function Element (C : Cursor_Type) return Element_Type is <>;
   with function "+" (C : Cursor_Type; I : Index_Type'Base) return Cursor_Type is <>;
   with function "-" (L, R : Cursor_Type) return Index_Type'Base is <>;
procedure Generic_Sort (First, Back : Cursor_Type);
pragma Pure (Generic_Sort);



procedure Generic_Sort (First, Back : Cursor_Type) is
begin
   null;
end Generic_Sort;


with Ada.Containers.Vectors; use Ada.Containers;

generic
   with package Vector_Types is new Vectors (<>);
   use Vector_Types;

   V : access constant Vector;

package Vector_Cursor_Operations is
   pragma Preelaborate;

   function "+" (L : Cursor; R : Index_Type'Base) return Cursor;
   function "-" (L, R : Cursor) return Index_Type'Base;

end Vector_Cursor_Operations;


package body Vector_Cursor_Operations is

   function "+" (L : Cursor; R : Index_Type'Base) return Cursor is
      I : Index_Type'Base;

   begin
      if Has_Element (L) then
         I := To_Index (L);
      else
         I := V.Last_Index + 1;
      end if;
      I := I + R;
      return To_Cursor (V.all, I);
   end "+";

   function "-" (L, R : Cursor) return Index_Type'Base is
      I, J : Index_Type'Base;

   begin
      if Has_Element (L) then
         I := To_Index (L);
      else
         I := V.Last_Index + 1;
      end if;
      
      if Has_Element (R) then
         J := To_Index (R);
      else
         J := V.Last_Index + 1;
      end if;
      
      return I - J;
   end "-";
      

end Vector_Cursor_Operations;






  parent reply	other threads:[~2007-11-21  4:51 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-14 23:28 OO Style with Ada Containers braver
2007-11-14 23:50 ` Adam Beneschan
2007-11-14 23:59   ` braver
2007-11-15  0:24     ` braver
2007-11-15  9:36       ` Ludovic Brenta
2007-11-15 10:36         ` braver
2007-11-15 11:35           ` Ludovic Brenta
2007-11-15 13:50             ` braver
2007-11-19  2:45               ` Matthew Heaney
2007-11-15 18:22             ` braver
2007-11-15 20:18               ` Ludovic Brenta
2007-11-19  2:48                 ` Matthew Heaney
2007-11-19  2:47               ` Matthew Heaney
2007-11-19  2:39             ` Matthew Heaney
2007-11-19  2:38           ` Matthew Heaney
2007-11-19  2:36         ` Matthew Heaney
2007-11-19  2:24       ` Matthew Heaney
2007-11-23 10:28         ` braver
2007-11-23 13:29           ` Martin Krischik
2007-11-23 14:19             ` Georg Bauhaus
2007-11-25 13:38           ` Ludovic Brenta
2007-11-26  3:58             ` Matthew Heaney
2007-11-26  3:55           ` Matthew Heaney
2007-11-23 22:25         ` braver
2007-11-23 22:46           ` Pascal Obry
2007-11-23 22:52             ` braver
2007-11-26  4:09               ` Matthew Heaney
2007-11-26  4:07             ` Matthew Heaney
2007-11-26  4:03           ` Matthew Heaney
2007-11-26 13:45             ` Matthew Heaney
2007-11-26 19:09               ` braver
2007-11-26 20:29                 ` Matthew Heaney
2007-11-27 19:31                   ` Georg Bauhaus
2007-11-27 20:12                     ` Matthew Heaney
2007-11-25 14:08         ` braver
2007-11-26  4:21           ` Matthew Heaney
2007-11-19  1:04   ` Matthew Heaney
2007-11-15  8:43 ` Dmitry A. Kazakov
2007-11-15 14:04   ` Maciej Sobczak
2007-11-19  2:53     ` Matthew Heaney
2007-11-19 13:44       ` Maciej Sobczak
2007-11-19 14:44         ` Martin
2007-11-19 15:51         ` Matthew Heaney
2007-11-19 17:33           ` Markus E L
2007-11-19 21:29           ` Maciej Sobczak
2007-11-19 22:16             ` Matthew Heaney
2007-11-19 22:22               ` Matthew Heaney
2007-11-20 14:11               ` Maciej Sobczak
2007-11-20 17:00                 ` Matthew Heaney
2007-11-20 17:17                   ` Matthew Heaney
2007-11-20 21:13                   ` Maciej Sobczak
2007-11-20 21:57                     ` Matthew Heaney
2007-11-21  4:51                     ` Matthew Heaney [this message]
2007-11-21  9:18                       ` Georg Bauhaus
2007-11-21 15:59                         ` Maciej Sobczak
2007-11-21 17:41                           ` Georg Bauhaus
2007-11-21 22:25                         ` Jeffrey R. Carter
2007-11-20 18:06                 ` Georg Bauhaus
2007-11-19 16:19         ` Dmitry A. Kazakov
2007-11-19 20:45           ` Maciej Sobczak
2007-11-20  2:24             ` Matthew Heaney
2007-11-20  9:06             ` Dmitry A. Kazakov
2007-11-20 12:16               ` Georg Bauhaus
2007-11-21 15:17                 ` Dmitry A. Kazakov
2007-11-19  2:50   ` Matthew Heaney
2007-11-19  1:03 ` Matthew Heaney
replies disabled

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