From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Deleting elements from a Vector Date: Sun, 19 Apr 2015 21:27:50 +0100 Organization: A noiseless patient Spider Message-ID: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="fa9c30c20a5c60fcbf7b9fa05d266b35"; logging-data="24562"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18IrRHwAg6WYavJH/ld5RX5SD1JAeCWzcY=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (darwin) Cancel-Lock: sha1:kMwY2+y+3i9h55J1fciSp1NBGmI= sha1:Fw8uc7pIcB0XTGxdgHoQX0txX94= Xref: news.eternal-september.org comp.lang.ada:25549 Date: 2015-04-19T21:27:50+01:00 List-Id: 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;