comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: A few questions
Date: Mon, 02 Nov 2015 17:45:31 +0000
Date: 2015-11-02T17:45:31+00:00	[thread overview]
Message-ID: <ly611kmh1g.fsf@pushface.org> (raw)
In-Reply-To: f487cd6f-f4f0-437e-9077-0495d17156f3@googlegroups.com

Laurent <lutgenl@icloud.com> writes:

> Hi
>
> I need a range of Dates.

I wrote the attached, using generalized iteration, which bears a strong
resemblance to the standard container iteration (the names bear too
strong a resemblance! but this way you can see the commonality).

I'd have liked to support

   for D in Period loop
      ...

but "array type required in indexed component" (which I think means that
Date_Container needs the Constant_Indexing aspect?).

Running this test just now,

   with Date_Iteration;
   with Ada.Calendar.Formatting;
   with Ada.Text_IO; use Ada.Text_IO;
   procedure Date_Iteration_Test is
      use type Ada.Calendar.Time;
      Period : Date_Iteration.Date_Container;
      Day : constant Duration := 86_400.0;
   begin
      Period.Initialize (Start_Time => Ada.Calendar.Clock,
                         End_Time   => Ada.Calendar.Clock + Day * 10,
                         Interval   => Day / 2);
      for C in Period.Iterate loop
         Put_Line (Ada.Calendar.Formatting.Image (Date_Iteration.Element (C)));
      end loop;
   end Date_Iteration_Test;

I got

   $ ./date_iteration_test
   2015-11-02 17:35:03
   2015-11-03 05:35:03
   2015-11-03 17:35:03
   2015-11-04 05:35:03
   2015-11-04 17:35:03
   2015-11-05 05:35:03
   2015-11-05 17:35:03
   2015-11-06 05:35:03
   2015-11-06 17:35:03
   2015-11-07 05:35:03
   2015-11-07 17:35:03
   2015-11-08 05:35:03
   2015-11-08 17:35:03
   2015-11-09 05:35:03
   2015-11-09 17:35:03
   2015-11-10 05:35:03
   2015-11-10 17:35:03
   2015-11-11 05:35:03
   2015-11-11 17:35:03
   2015-11-12 05:35:03
   2015-11-12 17:35:03

========================================================================
with Ada.Calendar;
with Ada.Iterator_Interfaces;
package Date_Iteration is
   type Date_Container is tagged private
   with
     Default_Iterator => Iterate,
     Iterator_Element => Ada.Calendar.Time;
   procedure Initialize (D          : out Date_Container;
                         Start_Time :     Ada.Calendar.Time;
                         End_Time   :     Ada.Calendar.Time;
                         Interval   :     Duration := 86_400.0);

   type Cursor is private;
   No_Element : constant Cursor;

   function First (Container : Date_Container) return Cursor;
   function Next (Position : Cursor) return Cursor;
   function Element (Position : Cursor) return Ada.Calendar.Time;

   function Has_Element (C : Cursor) return Boolean;
   package Iterator_Interfaces
     is new Ada.Iterator_Interfaces (Cursor, Has_Element);
   function Iterate (Container : Date_Container)
                    return Iterator_Interfaces.Forward_Iterator'Class;
private
   type Date_Container is tagged record
      Start_Time : Ada.Calendar.Time;
      End_Time   : Ada.Calendar.Time;
      Interval   : Duration;
   end record;
   type Date_Container_Access is access all Date_Container;
   for Date_Container_Access'Storage_Size use 0;

   type Cursor is record
      Container : Date_Container_Access;
      Index     : Ada.Calendar.Time;
   end record;
   No_Element : constant Cursor
     := (Container => null, Index => Ada.Calendar.Clock);

   type Iterator is new Iterator_Interfaces.Forward_Iterator
      with record
         Container : Date_Container_Access;
      end record;
   overriding
   function First (Object : Iterator) return Cursor;
   overriding
   function Next (Object : Iterator; Position : Cursor) return Cursor;
end Date_Iteration;

package body Date_Iteration is
   procedure Initialize (D          : out Date_Container;
                         Start_Time :     Ada.Calendar.Time;
                         End_Time   :     Ada.Calendar.Time;
                         Interval   :     Duration := 86_400.0) is
   begin
      D := (Start_Time => Start_Time,
            End_Time   => End_Time,
            Interval   => Interval);
   end;

   function First (Container : Date_Container) return Cursor is
   begin
      return C : Cursor do
         C.Container := Container'Unrestricted_Access;
         C.Index := Container.Start_Time;
      end return;
   end First;

   function Next (Position : Cursor) return Cursor is
      use type Ada.Calendar.Time;
   begin
      if Position.Container = null then
         return No_Element;
      end if;
      return C : Cursor do
         C.Container := Position.Container;
         C.Index := Position.Index + Position.Container.Interval;
      end return;
   end Next;

   function Element (Position : Cursor) return Ada.Calendar.Time is
     (Position.Index);

   function Has_Element (C : Cursor) return Boolean is
      use type Ada.Calendar.Time;
   begin
      return C.Index <= C.Container.End_Time;
   end Has_Element;

   function Iterate (Container : Date_Container)
                    return Iterator_Interfaces.Forward_Iterator'Class is
   begin
      return It : Iterator do
         It.Container := Container'Unrestricted_Access;
      end return;
   end Iterate;

   function First (Object : Iterator) return Cursor is
   begin
      return Object.Container.First;
   end First;

   function Next (Object : Iterator; Position : Cursor) return Cursor is
   begin
      if Position.Container = null then
         return No_Element;
      end if;
      if Position.Container /= Object.Container then
         raise Program_Error with "container/cursor mismatch";
      end if;
      return Next (Position);
   end Next;
end Date_Iteration;

  parent reply	other threads:[~2015-11-02 17:45 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-31 20:29 A few questions Laurent
2015-10-31 20:49 ` Dmitry A. Kazakov
2015-11-01 13:16   ` Laurent
2015-11-01  0:25 ` Jeffrey R. Carter
2015-11-01 13:30   ` Laurent
2015-11-03  6:25   ` Randy Brukardt
2015-11-01  9:05 ` Jacob Sparre Andersen
2015-11-01 13:40   ` Laurent
2015-11-01 18:14     ` Jacob Sparre Andersen
2015-11-01 18:40       ` Laurent
2015-11-01 13:42 ` brbarkstrom
2015-11-01 13:52   ` Laurent
2015-11-01 17:59     ` Jeffrey R. Carter
2015-11-01 18:35       ` Laurent
2015-11-02 13:25     ` brbarkstrom
2015-11-01 15:15   ` Dennis Lee Bieber
2015-11-01 16:33 ` gautier_niouzes
2015-11-01 16:36   ` gautier_niouzes
2015-11-01 18:17 ` Stephen Leake
2015-11-01 18:53   ` Laurent
2015-11-02  0:41     ` Dennis Lee Bieber
2015-11-02 16:42     ` Stephen Leake
2015-11-02 17:45 ` Simon Wright [this message]
2015-11-02 18:48   ` Simon Wright
2015-11-03  6:33     ` Randy Brukardt
2015-11-03  8:26       ` Simon Wright
2015-11-03  6:40   ` Randy Brukardt
2015-11-03  8:34     ` Simon Wright
2015-11-04 16:19       ` Simon Wright
2015-11-05  1:20         ` Randy Brukardt
2015-11-05  8:34           ` briot.emmanuel
2015-11-12 18:28             ` Randy Brukardt
2015-11-12 20:19               ` Simon Wright
2015-11-12 20:56               ` Dmitry A. Kazakov
2015-11-12 21:15                 ` Randy Brukardt
2015-11-13  8:40                   ` Dmitry A. Kazakov
2015-11-13 17:52                     ` Randy Brukardt
2015-11-13 20:37                       ` Dmitry A. Kazakov
2015-11-13 22:15                         ` Randy Brukardt
2015-11-14 11:42                           ` Dmitry A. Kazakov
2015-11-14 12:37                           ` Simon Wright
2015-11-14 17:24                             ` Shark8
2015-11-14 20:09                               ` Simon Wright
2015-11-15 18:54                             ` Brad Moore
2015-11-13  8:45               ` briot.emmanuel
2015-11-13 17:41                 ` Randy Brukardt
2015-11-14 19:57                   ` briot.emmanuel
2015-11-16 19:13                     ` Randy Brukardt
2015-11-16 20:47                       ` Dmitry A. Kazakov
2015-11-17 21:30                         ` Randy Brukardt
2015-11-18  9:53                           ` Dmitry A. Kazakov
2015-11-18 22:27                             ` Randy Brukardt
2015-11-19  8:52                               ` Dmitry A. Kazakov
2015-11-19 21:15                                 ` Randy Brukardt
2015-11-16 21:50                       ` Simon Wright
2015-11-17 21:33                         ` Randy Brukardt
2015-11-17 23:14                           ` Simon Wright
2015-11-17  8:49                       ` briot.emmanuel
2015-11-17 22:09                         ` Randy Brukardt
2015-11-05  8:45           ` Simon Wright
2015-11-05  8:52             ` Simon Wright
2015-11-12 18:29               ` Randy Brukardt
2015-11-12 18:32               ` Randy Brukardt
2015-11-12 20:02                 ` Simon Wright
2015-11-12 21:08                   ` Randy Brukardt
2015-11-15 17:56                     ` Brad Moore
2015-11-15 21:42                       ` Simon Wright
2015-11-16 19:16                       ` Randy Brukardt
  -- strict thread matches above, loose matches on Subject: below --
2015-02-07 17:43 Laurent
2015-02-07 22:15 ` Brad Moore
2015-02-08 22:37   ` Laurent
2015-02-09 13:56     ` Brad Moore
2015-02-09 18:36       ` Laurent
replies disabled

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