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: Jeffrey Carter Newsgroups: comp.lang.ada Subject: Re: Deleting elements from a Vector Date: Sun, 19 Apr 2015 14:07:27 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Sun, 19 Apr 2015 21:06:31 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="a3855fbfe1a666be9aefba0563039ed5"; logging-data="371"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+NQHWytDVELEUkZ1qObGf/Aytw/AQQSTA=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 In-Reply-To: Cancel-Lock: sha1:euevV9Loirc8yrbfjsi4ROpqsiU= Xref: news.eternal-september.org comp.lang.ada:25550 Date: 2015-04-19T14:07:27-07:00 List-Id: On 04/19/2015 01:27 PM, Simon Wright wrote: > 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. > > 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; Seems to me that in the general case a cursor becomes invalid after the element it designates is deleted from the container through another cursor. For most such containers I'd think doing Next on the cursor before the Delete would work. However, since a Vector is an unbounded array, the cursor may just be an index, in which case that wouldn't work. I'd advise always using a Vector as if it were an array, and iterate over it using indices. -- Jeff Carter "He didn't get that nose from playing ping-pong." Never Give a Sucker an Even Break 110