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=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,55f6e230b02eff2f X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!19g2000hsx.googlegroups.com!not-for-mail From: Matthew Heaney Newsgroups: comp.lang.ada Subject: Re: Containers - nontrivial element access Date: Tue, 02 Oct 2007 09:37:51 -0700 Organization: http://groups.google.com Message-ID: <1191343071.685478.146710@19g2000hsx.googlegroups.com> References: <1191275759.184463.238350@n39g2000hsh.googlegroups.com> NNTP-Posting-Host: 66.162.65.129 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1191343073 7582 127.0.0.1 (2 Oct 2007 16:37:53 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 2 Oct 2007 16:37:53 +0000 (UTC) In-Reply-To: <1191275759.184463.238350@n39g2000hsh.googlegroups.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: 19g2000hsx.googlegroups.com; posting-host=66.162.65.129; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Xref: g2news2.google.com comp.lang.ada:2247 Date: 2007-10-02T09:37:51-07:00 List-Id: On Oct 1, 5:55 pm, Maciej Sobczak wrote: > > There is a record type (for example Person) with a couple of fields > (for example Salary) that together make the whole a bit heavy, so that > unnecessary copying of the whole is to be avoided. Objects of this > type are stored in the container. Right, so you probably want to avoid operations Element or Replace_Element, and use Query_Element or Update_Element instead. > I would like to swap salaries of two guys which I can refer with the > index/iterator/cursor/etc. The reference method is not really > important - what is important is the problem of modifying more than > one element in the container. > > C++ example is easy: > > vector people; > // ... > swap(people[x].salary, people[y].salary); V : Person_Vectors.Vector; declare procedure Process_X (PX : in out Person) is procedure Process_Y (PY : in out Person) is SX : constant Salary_Type := PX.Salary; begin PX.Salary := PY.Salary; PY.Salary := SX; end; begin V.Update_Element (Y, Process_Y'Access); end; begin V.Update_Element (X, Process_X'Access); end; > I hope it is obvious what it does (suppose x and y are some indices > into the vector). Just in case it isn't - all the components of the > two records stay intact except the salary, which is swapped between > the two. C++ makes it possible by explicitly returning a reference > from the method that accesses the element. Right, but Ada doesn't have explicit reference types, so you have to use Update_Element, which allows in-place editing. > What would you suggest as the Ada solution for this problem? See above. > The Update_Element procedure with its access to the user-provided > modifying procedure requires to pass data "under the table" (like with > a separate variable declared aside the modifying procedure) - and > seems to be just clunky. Is this the only possibility? I don't understand what you think the problem is. There is no need for a separate variable or anything else.