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!feeder.eternal-september.org!news.stack.nl!feeder.erje.net!2.us.feeder.erje.net!weretis.net!feeder6.news.weretis.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!newsswitch.lcs.mit.edu!nntp.TheWorld.com!.POSTED!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: How to access an array using two different indexing schemes Date: Fri, 24 Nov 2017 17:12:04 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: NNTP-Posting-Host: shell02.theworld.com Mime-Version: 1.0 Content-Type: text/plain X-Trace: pcls7.std.com 1511561523 14324 192.74.137.72 (24 Nov 2017 22:12:03 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Fri, 24 Nov 2017 22:12:03 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:rZWEKsW1ccjK16aJqKm5uj7OpyI= Xref: feeder.eternal-september.org comp.lang.ada:48182 Date: 2017-11-24T17:12:04-05:00 List-Id: Jerry writes: > I could declare two arrays such as > > x : Real_Vector(0 .. 4); > y : Real_Vector(1 .. 5); > > Ada then allows the assignments > > y := x; > and > x := y; This is called "sliding". So long as X'Length = Y'Length, the bounds can change in this way on assignment statements. I don't think the RM defines the term "sliding", but you can find it in the AARM, as part of array subtype conversions. > but this has two problems. First, I have wasted memory and cycles by > declaring y and copying x into it. ... Sliding is also allowed in various other contexts, including parameter passing, which is typically done by reference for the types you're interested in. Here's an example. P expects a String starting at 5. Q is a wrapper, that takes a String with any lower bound, and slides it as appropriate, and then passes it to P. The program prints " 5 5 5 5". Strings with various lower bounds are passed to Q, but they all have 'First = 5 inside P. And there's no copying of the strings (on any sensible compiler). This is admittedly kind of convoluted. with Text_IO; use Text_IO; package Sliding is subtype String_1 is String with Predicate => String_1'First = 1; subtype String_5 is String with Predicate => String_5'First = 5; procedure P(X: String_5); procedure Q(X: String); end Sliding; package body Sliding is procedure P(X: String_5) is begin Put(X'First'Image); end P; procedure Q(X: String) is subtype Slide_To_5 is String_5(5 .. 5 + X'Length - 1); procedure Call_P (X: Slide_To_5) is begin P(X); -- Predicate check (that X'First = 5) occurs here. end Call_P; begin Call_P(X); -- Sliding to 5..something occurs here. end Q; end Sliding; procedure Sliding.Main is S1: constant String := ""; S2: constant String(-100..-1_000_000) := ""; S3: constant String := "Hello"; S4: constant String(101..105) := "Hello"; begin Q(S1); Q(S2); Q(S3); Q(S4); end Sliding.Main; - Bob