comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: how to delete from Ada.Containers.Doubly_Linked_Lists
Date: Mon, 18 Aug 2014 12:57:15 -0700
Date: 2014-08-18T12:57:15-07:00	[thread overview]
Message-ID: <lstlqr$ks3$1@dont-email.me> (raw)
In-Reply-To: <lsthum$n6e$1@dont-email.me>

On 08/18/2014 11:51 AM, Björn Lundin wrote:
> 
> 
> I've got a sample below,
> but I fell it is clumpsy.
> How should a nice solution look like ?

You probably ought to read about "tampering with cursors" in the ARM (A.18.3).
In your case, iterating over the list needs the list to remain unchanged until
iteration finishes. Deleting during iteration would violate this.

This restriction is intended for safety, and sometimes seems onerous. I've had
to iterate over one data structure, storing cursors in another data structure,
then iterate over the 2nd data structure to delete using the stored cursors.

You can delete during iteration if you don't invoke an Iterate subprogram:

   declare
      C    : Example_Pkg.Cursor := List.First;
      Next : Example_Pkg.Cursor;

      use type Example_Pkg.Cursor;
   begin
      loop
         exit when C = Example_Pkg.No_Element;

         declare
            I : constant Example_Type := Example_Pkg.Element (C);
         begin
            if I.A /= 15 then
               Example_Pkg.Next (Position => C);
            else
               Next := Example_Pkg.Next (C);
               List.Delete (Position => C);
               C := Next;
            end if;
         end ;
      end loop;
   end;

-- 
Jeff Carter
"You a big nose have it."
Never Give a Sucker an Even Break
107

  reply	other threads:[~2014-08-18 19:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-18 18:51 how to delete from Ada.Containers.Doubly_Linked_Lists Björn Lundin
2014-08-18 19:57 ` Jeffrey Carter [this message]
2014-08-19  8:56   ` Björn Lundin
replies disabled

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