From: Mike H <postmaster@ada-augusta.demon.co.uk>
Subject: Re: Pretty printing the pretty print.
Date: Mon, 6 Jan 2014 16:37:48 +0000
Date: 2014-01-06T16:37:48+00:00 [thread overview]
Message-ID: <Cq5U8ICcvtySFwRj@ada-augusta.demon.co.uk> (raw)
In-Reply-To: 20140104121332.22310cd2@vostro
In message <20140104121332.22310cd2@vostro>, Oliver Kleinke
<oliver.kleinke@c-01a.de> writes
>Do you by chance remember the task?
As far as I remember the original "specification" was a requirement to
split an array at an arbitrary point and re-join the two parts (first
and last) in the reverse order (last then first). Spotting that what was
actually required was a circular shift changed the game somewhat. By
some odd coincidence, before Christmas I stumbled across the following
(see below). It seems to fit the bill. I also seem to remember that
turning it into a generic was not as sweet and clean as I expected.
function Shift_right_circular
(A : in Array_type;
Places : in Natural) return Array_type is
P : Natural := Places rem A'LENGTH;
-- Array will be split into two slices (I1 .. I2) & (I3 .. I4) and
-- re-made as (I3 .. I4) & (I1 .. I2). The values of I2 and I3 are
-- derived from their position numbers, P2 and P3.
P1 : Natural := Index_type'POS (A'FIRST); -- position number of
I1
P2 : Natural := P1 + P - 1; -- position
number of I2
P3 : Natural := P1 + P; -- position
number of I3
--
-- I1 is A'FIRST
I2 : Index_type := Index_type'VAL(P2);
I3 : Index_type := Index_type'VAL(P3);
-- I4 is A'LAST
begin
if Places = 0 then
-- no shifting to be done
return A;
else
return A (I3 .. A'LAST) & A (A'FIRST .. I2);
end if;
end Shift_right_circular;
--
Mike
Swim? Naturally at Severn Vale
<http://www.severnvalesc.org/>
next prev parent reply other threads:[~2014-01-06 16:37 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-25 10:36 Pretty printing the pretty print Mike H
2013-09-25 11:14 ` Georg Bauhaus
2013-09-25 14:27 ` Britt
2013-09-25 14:48 ` Mike H
2013-09-25 15:16 ` gautier_niouzes
2013-09-26 4:30 ` Per Sandberg
2013-09-27 13:25 ` Mike H
2014-01-04 11:13 ` Oliver Kleinke
2014-01-06 16:37 ` Mike H [this message]
2013-09-29 21:38 ` Stephen Leake
2013-09-25 14:47 ` Felix Krause
2013-09-26 18:38 ` Manuel Gomez
2013-09-30 12:20 ` Stefan.Lucks
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox