comp.lang.ada
 help / color / mirror / Atom feed
* Deleting elements from a Vector
@ 2015-04-19 20:27 Simon Wright
  2015-04-19 21:07 ` Jeffrey Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Simon Wright @ 2015-04-19 20:27 UTC (permalink / raw)


I need to remove some of the elements from a vector, depending on the
element. I have the code below, which works for GNAT, but which seems
distinctly dodgy; in particular, it fails if the last Element is
negative and therefore gets deleted.

The Booch Components have

   procedure Delete_Item_At (It : in out Iterator) is abstract;

(an Iterator is very similar to a Cursor) which is defined to leave the
Iterator designating the 'next' Element (i.e., the one, is any, you
would have reached by applying Next to the original Iterator). But I
don't see any such promise for the Containers.

Is there a proper idiom for doing this job? Should I maybe go over the
Vector in reverse order and use the version of Delete that uses the
index?

with Ada.Containers.Vectors;
with Ada.Text_IO; use Ada.Text_IO;
procedure Deleting_From_Queue is
   package Vectors is new Ada.Containers.Vectors (Positive, Integer);
   V : Vectors.Vector;
begin
   V.Append (1);
   V.Append (-1);
   V.Append (2);
   V.Append (-2);
   V.Append (3);
   declare
      C : Vectors.Cursor := V.First;
      D : Vectors.Cursor;
      use type Vectors.Cursor;
   begin
      while C /= Vectors.No_Element loop
         if Vectors.Element (C) < 0 then
            Put_Line ("deleting element " & Vectors.Element (C)'Img);
            D := C;
            V.Delete (D);  -- D -> No_Element
         else
            Put_Line ("skipping element " & Vectors.Element (C)'Img);
            Vectors.Next (C);
         end if;
      end loop;
   end;
   Put_Line ("length now " & V.Length'Img);
end Deleting_From_Queue;

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

end of thread, other threads:[~2015-04-20 11:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-19 20:27 Deleting elements from a Vector Simon Wright
2015-04-19 21:07 ` Jeffrey Carter
2015-04-19 21:12 ` Niklas Holsti
2015-04-20  7:56   ` Simon Wright
2015-04-20  8:39     ` Georg Bauhaus
2015-04-20 11:13       ` Simon Wright
2015-04-20  7:36 ` Dmitry A. Kazakov

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