* Array Help? @ 2012-02-28 20:15 Will 2012-02-28 20:47 ` Ludovic Brenta ` (3 more replies) 0 siblings, 4 replies; 44+ messages in thread From: Will @ 2012-02-28 20:15 UTC (permalink / raw) Hello all, I am looking for your help again. I know how to refer to a certain index in an array. for example if some array A is (53,83,3,62,3,13) then A(4) = 62 but what if I wanted to go through an array and identify certain indexes by what they contain? In my particular case I need to identify the indexes that have numbers in them that do not end in 1 or 2. These numbers range from 1 to 4 digits. Any ideas on how I can do this? Thanks a bunch ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-28 20:15 Array Help? Will @ 2012-02-28 20:47 ` Ludovic Brenta 2012-02-28 22:11 ` Simon Wright 2012-02-29 0:24 ` Adam Beneschan 2012-02-28 20:50 ` Gautier write-only ` (2 subsequent siblings) 3 siblings, 2 replies; 44+ messages in thread From: Ludovic Brenta @ 2012-02-28 20:47 UTC (permalink / raw) Will writes on comp.lang.ada: > Hello all, > > I am looking for your help again. I know how to refer to a certain > index in an array. for example if some array A is (53,83,3,62,3,13) > then A(4) = 62 but what if I wanted to go through an array and > identify certain indexes by what they contain? In my particular case > I need to identify the indexes that have numbers in them that do not > end in 1 or 2. These numbers range from 1 to 4 digits. Any ideas on > how I can do this? > > Thanks a bunch Since this looks very much like a school assignment, my answer will be in English rather than Ada :) Traverse the array. Each element in the array is an integer; take that integer modulo 10. If the integer modulo 10 is neither 1 nor 2 then output the index of the element (or add that index to a vector of indexes for further processing). HTH -- Ludovic Brenta. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-28 20:47 ` Ludovic Brenta @ 2012-02-28 22:11 ` Simon Wright 2012-02-28 23:09 ` Will 2012-02-29 0:24 ` Adam Beneschan 1 sibling, 1 reply; 44+ messages in thread From: Simon Wright @ 2012-02-28 22:11 UTC (permalink / raw) Ludovic Brenta <ludovic@ludovic-brenta.org> writes: > Since this looks very much like a school assignment, my answer will be > in English rather than Ada :) I think you're probably right. Damn. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-28 22:11 ` Simon Wright @ 2012-02-28 23:09 ` Will 0 siblings, 0 replies; 44+ messages in thread From: Will @ 2012-02-28 23:09 UTC (permalink / raw) Thanks guys... I appreciate your replies. For future reference all you had to say was something along these lines. "Here's a hint, if you divide each number by 10 what remainder do you get?" I am a student and working on a more complex problem than just returning numbers, I just needed to know how to do this in order to continue the problem. I do not want to be spoon fed, I want to learn and yes I probably could have phrased my question in a better fashion. But I do understand basic Ada. I am just not a great problem solver yet. Thanks again. Will ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-28 20:47 ` Ludovic Brenta 2012-02-28 22:11 ` Simon Wright @ 2012-02-29 0:24 ` Adam Beneschan 1 sibling, 0 replies; 44+ messages in thread From: Adam Beneschan @ 2012-02-29 0:24 UTC (permalink / raw) On Feb 28, 12:47 pm, Ludovic Brenta <ludo...@ludovic-brenta.org> wrote: > Will writes on comp.lang.ada: > > > Hello all, > > > I am looking for your help again. I know how to refer to a certain > > index in an array. for example if some array A is (53,83,3,62,3,13) > > then A(4) = 62 but what if I wanted to go through an array and > > identify certain indexes by what they contain? In my particular case > > I need to identify the indexes that have numbers in them that do not > > end in 1 or 2. These numbers range from 1 to 4 digits. Any ideas on > > how I can do this? > > > Thanks a bunch > > Since this looks very much like a school assignment, my answer will be > in English rather than Ada :) > > Traverse the array. Each element in the array is an integer; take that > integer modulo 10. That'll work if each element of the array is a NATURAL or a POSITIVE. If it's possible for A to contain any negative integers, then a more careful statement of the problem is needed (does "ends in 1 or 2" mean what it looks like?), and some extra thought is needed to get that case right. -- Adam ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-28 20:15 Array Help? Will 2012-02-28 20:47 ` Ludovic Brenta @ 2012-02-28 20:50 ` Gautier write-only 2012-02-28 21:33 ` Simon Wright 2012-02-29 0:22 ` Adam Beneschan 3 siblings, 0 replies; 44+ messages in thread From: Gautier write-only @ 2012-02-28 20:50 UTC (permalink / raw) Will <willmann...@gmail.com> wrote: >... A(4) = 62 Just to avoid confusion, 4 is the index, 62 is the element. >... if I wanted to go through an array easy man: for index in A'range loop [your nice code] end loop; >... numbers in them that do not end in 1 or 2. no need to worry about how many digits number N has: (N mod 10) has guaranteed one :-) HTH _________________________ Gautier's Ada programming http://freecode.com/users/gdemont ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-28 20:15 Array Help? Will 2012-02-28 20:47 ` Ludovic Brenta 2012-02-28 20:50 ` Gautier write-only @ 2012-02-28 21:33 ` Simon Wright 2012-02-29 0:27 ` Adam Beneschan 2012-02-29 0:22 ` Adam Beneschan 3 siblings, 1 reply; 44+ messages in thread From: Simon Wright @ 2012-02-28 21:33 UTC (permalink / raw) Will <willmann817@gmail.com> writes: > Hello all, > > I am looking for your help again. I know how to refer to a certain > index in an array. for example if some array A is (53,83,3,62,3,13) > then A(4) = 62 but what if I wanted to go through an array and > identify certain indexes by what they contain? In my particular case > I need to identify the indexes that have numbers in them that do not > end in 1 or 2. These numbers range from 1 to 4 digits. Any ideas on > how I can do this? I presume A is an array of Integer, and that you're interested in the decimal representation (remember, modern computers run in binary so in a very real sense numbers have to "end in" 0 or 1!) for J in A'Range loop declare Decimal_Units : constant Natural := abs (A (J) mod 10); begin if Decimal_Units /= 1 and Decimal_Units /= 2 then -- A (J) doesn't end in 1 or 2. end if; end; end loop; ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-28 21:33 ` Simon Wright @ 2012-02-29 0:27 ` Adam Beneschan 2012-02-29 8:00 ` Simon Wright 0 siblings, 1 reply; 44+ messages in thread From: Adam Beneschan @ 2012-02-29 0:27 UTC (permalink / raw) On Feb 28, 1:33 pm, Simon Wright <si...@pushface.org> wrote: > Will <willmann...@gmail.com> writes: > > Hello all, > > > I am looking for your help again. I know how to refer to a certain > > index in an array. for example if some array A is (53,83,3,62,3,13) > > then A(4) = 62 but what if I wanted to go through an array and > > identify certain indexes by what they contain? In my particular case > > I need to identify the indexes that have numbers in them that do not > > end in 1 or 2. These numbers range from 1 to 4 digits. Any ideas on > > how I can do this? > > I presume A is an array of Integer, and that you're interested in the > decimal representation (remember, modern computers run in binary so > in a very real sense numbers have to "end in" 0 or 1!) > > for J in A'Range loop > declare > Decimal_Units : constant Natural := abs (A (J) mod 10); If this is a school assignment, then F for you. :) OK, B-, since you almost got it right, so I'll give you part credit. > begin > if Decimal_Units /= 1 and Decimal_Units /= 2 then > -- A (J) doesn't end in 1 or 2. > end if; > end; > end loop; -- Adam ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 0:27 ` Adam Beneschan @ 2012-02-29 8:00 ` Simon Wright 2012-02-29 8:48 ` Simon Wright 0 siblings, 1 reply; 44+ messages in thread From: Simon Wright @ 2012-02-29 8:00 UTC (permalink / raw) Adam Beneschan <adam@irvine.com> writes: > On Feb 28, 1:33 pm, Simon Wright <si...@pushface.org> wrote: >> for J in A'Range loop >> declare >> Decimal_Units : constant Natural := abs (A (J) mod 10); > > If this is a school assignment, then F for you. :) OK, B-, since you > almost got it right, so I'll give you part credit. :blush: All these years and I still only know enough to realise there's a problem and go off to the ARM to check. I certainly haven't had to solve a problem like this IRL often enough for it to sink in. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 8:00 ` Simon Wright @ 2012-02-29 8:48 ` Simon Wright 0 siblings, 0 replies; 44+ messages in thread From: Simon Wright @ 2012-02-29 8:48 UTC (permalink / raw) Simon Wright <simon@pushface.org> writes: > All these years and I still only know enough to realise there's a > problem and go off to the ARM to check. ... but not enough to write a test case to make sure I've actually understood it. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-28 20:15 Array Help? Will ` (2 preceding siblings ...) 2012-02-28 21:33 ` Simon Wright @ 2012-02-29 0:22 ` Adam Beneschan 2012-02-29 16:09 ` Robert A Duff 3 siblings, 1 reply; 44+ messages in thread From: Adam Beneschan @ 2012-02-29 0:22 UTC (permalink / raw) On Feb 28, 12:15 pm, Will <willmann...@gmail.com> wrote: > Hello all, > > I am looking for your help again. I know how to refer to a certain > index in an array. for example if some array A is (53,83,3,62,3,13) > then A(4) = 62 FYI, this is not necessarily true. In Ada, the starting and ending indexes of an array can be anything you want. You can declare your array like this: A : array (2 .. 7) of Integer; or, since I don't like anonymous array types, type Integer_Array is array (Integer range <>) of Integer; A : Integer_Array (2 .. 7); Now, A(4) = 3, not 62. You may be aware of this already, but I wanted to point it out in case you weren't. -- Adam ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 0:22 ` Adam Beneschan @ 2012-02-29 16:09 ` Robert A Duff 2012-02-29 16:50 ` Ludovic Brenta ` (2 more replies) 0 siblings, 3 replies; 44+ messages in thread From: Robert A Duff @ 2012-02-29 16:09 UTC (permalink / raw) Adam Beneschan <adam@irvine.com> writes: > type Integer_Array is array (Integer range <>) of Integer; > A : Integer_Array (2 .. 7); Right, and this is a rich source of bugs. You usually want arrays to start at 1, or sometimes 0 (assuming the index type is a signed integer type, which is almost always the case for unconstrained arrays). In Ada 2012, you can say: type Integer_Array is array (Positive range <>) of Integer with Dynamic_Predicate => Integer_Array'First = 1; or: type Integer_Array is array (Natural range <>) of Integer with Dynamic_Predicate => Integer_Array'First = 0; Now 'Last can be anything you like, but 'First is fixed. In GNAT, you can write "Predicate" instead of "Dynamic_Predicate". - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 16:09 ` Robert A Duff @ 2012-02-29 16:50 ` Ludovic Brenta 2012-02-29 18:24 ` Robert A Duff 2012-02-29 18:35 ` Jeffrey Carter 2012-02-29 20:40 ` Adam Beneschan 2 siblings, 1 reply; 44+ messages in thread From: Ludovic Brenta @ 2012-02-29 16:50 UTC (permalink / raw) Robert A Duff wrote on comp.lang.ada: > You usually want arrays to start at 1, or sometimes 0 (assuming > the index type is a signed integer type, which is almost always > the case for unconstrained arrays). In Ada 2012, you can say: > > type Integer_Array is array (Positive range <>) of Integer > with Dynamic_Predicate => Integer_Array'First = 1; > > or: > > type Integer_Array is array (Natural range <>) of Integer > with Dynamic_Predicate => Integer_Array'First = 0; > > Now 'Last can be anything you like, but 'First is fixed. Doesn't that preclude slices that don't start at 'First? Supposing your declarations, can you call procedure Foo (Param : in out Integer_Array); like this: declare A : Integer_Array (1 .. 10) := (others => 0); begin Foo (A (3 .. 8)); end; ? -- Ludovic Brenta. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 16:50 ` Ludovic Brenta @ 2012-02-29 18:24 ` Robert A Duff 2012-02-29 19:45 ` stefan-lucks ` (2 more replies) 0 siblings, 3 replies; 44+ messages in thread From: Robert A Duff @ 2012-02-29 18:24 UTC (permalink / raw) Ludovic Brenta <ludovic@ludovic-brenta.org> writes: > Doesn't that preclude slices that don't start at 'First? Yes. > Supposing your declarations, can you call > > procedure Foo (Param : in out Integer_Array); > > like this: > > declare > A : Integer_Array (1 .. 10) := (others => 0); > begin > Foo (A (3 .. 8)); > end; > > ? That will raise C_E. IMHO, that's a language design flaw -- inside Foo, Param'First ought to be 1. The fact that Foo can see that it came from a slice is a leak of abstraction. The fact that arrays slide in many situations is proof that people don't really care too much about the bounds -- they care about the length. Anyway, slices aren't really all that useful -- for many array types, you don't need them at all. And slices as l-values, as in your example, are quite rare, because you really want to be able to change the length of the slice, if you want to change it at all. The following won't work: X : String := "Hello, world."; X(1..5) := "Goodbye"; -- raises C_E - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 18:24 ` Robert A Duff @ 2012-02-29 19:45 ` stefan-lucks 2012-02-29 20:45 ` Jeffrey Carter 2012-02-29 23:06 ` Robert A Duff 2012-02-29 20:07 ` Dmitry A. Kazakov 2012-02-29 20:47 ` Simon Wright 2 siblings, 2 replies; 44+ messages in thread From: stefan-lucks @ 2012-02-29 19:45 UTC (permalink / raw) On Wed, 29 Feb 2012, Robert A Duff wrote: > Anyway, slices aren't really all that useful -- for many array types, > you don't need them at all. And slices as l-values, as in your > example, are quite rare, because you really want to be able to > change the length of the slice, if you want to change it at all. Not quite. I quite frequently use something like X := X(X'Last) & (X'First .. X'Last-1) and First := X'First; while First < X'Last and then X(First) = ' ' loop First := First + 1; end loop; return X(First .. X'Last); and similar constructs, where array slicing is a highly useful language feature. On the other hand, I agree that the fact that array slices carry their original indices with them is a misfeature of Ada. I would appreciate way to convert ingoing parameters into some standard indexing. For example (inventing a new syntax variant on the fly): procedure Sort(Items: in out array(generic Positive range <>) of T) is -- inside Sort, Items'First=1 and Item'Last = Items'Length -- regardless of the index type of the actual parameter begin ... end Sort; and later call the same Sort procedure Sort(X); Sort(Y); Sort(Z); for any one-dimensional array of type T, e.g., X: array (Positive range 3 .. 13) of T; Y: array (range 31 .. 3343) of T; type Month_Names is (January, [...] December); Z: array (Month_Names) of T; -- ---- Stefan.Lucks (at) uni-weimar.de, University of Weimar, Germany ---- <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html> ------ I love the taste of Cryptanalysis in the morning! ------ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 19:45 ` stefan-lucks @ 2012-02-29 20:45 ` Jeffrey Carter 2012-02-29 21:27 ` stefan-lucks 2012-02-29 23:06 ` Robert A Duff 1 sibling, 1 reply; 44+ messages in thread From: Jeffrey Carter @ 2012-02-29 20:45 UTC (permalink / raw) On 02/29/2012 12:45 PM, stefan-lucks@see-the.signature wrote: > > First := X'First; > while First< X'Last and then X(First) = ' ' loop > First := First + 1; > end loop; > return X(First .. X'Last); Ada.Strings.Fixed.Index_Non_Blank? -- Jeff Carter "Sir Robin the not-quite-so-brave-as-Sir-Lancelot, who had nearly fought the Dragon of Angnor, who nearly stood up to the vicious Chicken of Bristol, and who had personally wet himself at the Battle of Badon Hill." Monty Python & the Holy Grail 68 --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net --- ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 20:45 ` Jeffrey Carter @ 2012-02-29 21:27 ` stefan-lucks 0 siblings, 0 replies; 44+ messages in thread From: stefan-lucks @ 2012-02-29 21:27 UTC (permalink / raw) On Wed, 29 Feb 2012, Jeffrey Carter wrote: > On 02/29/2012 12:45 PM, stefan-lucks@see-the.signature wrote: > > > > First := X'First; > > while First< X'Last and then X(First) = ' ' loop > > First := First + 1; > > end loop; > > return X(First .. X'Last); > > Ada.Strings.Fixed.Index_Non_Blank? That was just a simplified example for a common pattern. In that specific case, I would probably use Ada.Strings.Fixed.Trim(..., Left). -- ---- Stefan.Lucks (at) uni-weimar.de, University of Weimar, Germany ---- <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html> ------ I love the taste of Cryptanalysis in the morning! ------ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 19:45 ` stefan-lucks 2012-02-29 20:45 ` Jeffrey Carter @ 2012-02-29 23:06 ` Robert A Duff 2012-03-02 13:45 ` stefan-lucks 2012-03-07 23:58 ` Randy Brukardt 1 sibling, 2 replies; 44+ messages in thread From: Robert A Duff @ 2012-02-29 23:06 UTC (permalink / raw) stefan-lucks@see-the.signature writes: > On Wed, 29 Feb 2012, Robert A Duff wrote: > >> Anyway, slices aren't really all that useful -- for many array types, >> you don't need them at all. And slices as l-values, as in your >> example, are quite rare, because you really want to be able to >> change the length of the slice, if you want to change it at all. > > Not quite. I quite frequently use something like > > X := X(X'Last) & (X'First .. X'Last-1) > > and > > First := X'First; > while First < X'Last and then X(First) = ' ' loop > First := First + 1; > end loop; > return X(First .. X'Last); > > and similar constructs, where array slicing is a highly useful language > feature. Mildly useful, I'd say. If you didn't have slices, you'd have to write a few lines of code instead of the above -- an extra loop. That's a purely local change. No big deal. Contrast that with features like packages, private types, user-defined integer types, etc, the lack of which would damage the entire structure of your program. Sure, slices are useful, and I use them. But I wouldn't mind too much if they didn't exist. It's trivial to write a Slice function that does what "A(F..L)" does. You didn't address my point about l-values. None of your slices above are l-values. L-value slices cause a lot of implementation trouble, and are nearly useless -- the language would be better off without them. > On the other hand, I agree that the fact that array slices carry their > original indices with them is a misfeature of Ada. > > I would appreciate way to convert ingoing parameters into some standard > indexing. For example (inventing a new syntax variant on the fly): > > procedure Sort(Items: in out array(generic Positive range <>) of T) is Interesting idea. - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 23:06 ` Robert A Duff @ 2012-03-02 13:45 ` stefan-lucks 2012-03-07 23:58 ` Randy Brukardt 1 sibling, 0 replies; 44+ messages in thread From: stefan-lucks @ 2012-03-02 13:45 UTC (permalink / raw) On Wed, 29 Feb 2012, Robert A Duff wrote: > Sure, slices are useful, and I use them. But I wouldn't mind too > much if they didn't exist. It's trivial to write a Slice function > that does what "A(F..L)" does. A predefined function Slice for all (at least for all one-dimensional) array types would be OK. Having to instantiate a generic or to write a slice function on ones own each time one needs a slice would clutter the code ... > You didn't address my point about l-values. There is a simple reason why I didn't address that part of your posting: I don't have a strong point in favor of slices as l-values. > None of your slices above are l-values. L-value slices cause a lot of > implementation trouble, and are nearly useless -- the language would be > better off without them. I haven't been aware that l-value slices are so difficult to implement, and I wouldn't mind to put them away. > > procedure Sort(Items: in out array(generic Positive range <>) of T) is > > Interesting idea. Thank you! Stefan -- ---- Stefan.Lucks (at) uni-weimar.de, University of Weimar, Germany ---- <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html> ------ I love the taste of Cryptanalysis in the morning! ------ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 23:06 ` Robert A Duff 2012-03-02 13:45 ` stefan-lucks @ 2012-03-07 23:58 ` Randy Brukardt 2012-03-08 11:20 ` stefan-lucks 1 sibling, 1 reply; 44+ messages in thread From: Randy Brukardt @ 2012-03-07 23:58 UTC (permalink / raw) "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message news:wccy5rls0hl.fsf@shell01.TheWorld.com... > stefan-lucks@see-the.signature writes: ... > Sure, slices are useful, and I use them. But I wouldn't mind too > much if they didn't exist. It's trivial to write a Slice function > that does what "A(F..L)" does. In Ada 2012, it's not that hard to write a Slice_LValue function that would let you assign into A(F..L). It's in fact the key to my half-formed Root_String'Class proposal (plus the indexing features already in Ada 2012 - the only thing missing is string literals). > You didn't address my point about l-values. None of your slices > above are l-values. L-value slices cause a lot of implementation > trouble, and are nearly useless -- the language would be better > off without them. I don't buy this at all. I've tried using loops instead of slices to do string manipulation (early Janus/Ada didn't have slices), and the results had horrific readability and performance. Even today, the optimization of slices is much better than the optimization of the supposedly equivalent loop (mainly because no optimization is needed to optimize the slice; the code is already nearly optimal when created by the Janus/Ada front end). Obviously, this could be different on a different Ada compiler, but I'm at least a bit skeptical. >> On the other hand, I agree that the fact that array slices carry their >> original indices with them is a misfeature of Ada. >> >> I would appreciate way to convert ingoing parameters into some standard >> indexing. For example (inventing a new syntax variant on the fly): >> >> procedure Sort(Items: in out array(generic Positive range <>) of T) is > > Interesting idea. Can't you do this with a type conversion to an appropriate constrained array subtype? (Such conversions slide.) I suppose it could be a pain to declare such a subtype in front of every call, but it is a trick I've used a some cases (usually in wrappers) to force sliding on arguments. I suppose a more convinient way to force that would be helpful. Randy. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-03-07 23:58 ` Randy Brukardt @ 2012-03-08 11:20 ` stefan-lucks 2012-03-09 2:02 ` Randy Brukardt 0 siblings, 1 reply; 44+ messages in thread From: stefan-lucks @ 2012-03-08 11:20 UTC (permalink / raw) On Wed, 7 Mar 2012, Randy Brukardt wrote: > "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message > news:wccy5rls0hl.fsf@shell01.TheWorld.com... > > stefan-lucks@see-the.signature writes: > >> procedure Sort(Items: in out array(generic Positive range <>) of T) is > > > > Interesting idea. > > Can't you do this with a type conversion to an appropriate constrained array > subtype? (Such conversions slide.) I suppose it could be a pain to declare > such a subtype in front of every call, but it is a trick I've used a some > cases (usually in wrappers) to force sliding on arguments. Can you explain a bit more? > I suppose a more convinient way to force that would be helpful. Beyond the inconvenience, there are two further issues: 1. Testing When you declare procedure Sort(Items: in out array(Positive range <>) of T) in a package specification, and you try to write a proper black-box test, you need *more* test cases then when you do the same with another popular language. One of the additional test cases could look like declare X: array (Positive range Positive'last-2 .. Positive'Last) := ...; Y: array (Positive range Positive'last-2 .. Positive'Last) := ...; begin Sort(X); Assert(X=Y); end; Some time ago, I have actually been bitten by forgetting such a test case. So the sad reality is that there is a property where Ada makes testing harder than, say, C or C++. 2. Generality Given the (Ada 2020?) specification procedure Sort(Items: in out array(generic Positive range <>) of T); Sort(X) should be callable if the index type of X is any discrete type, except when the index type is too large (i.e., X has more than Positive'Last elements). -- ---- Stefan.Lucks (at) uni-weimar.de, University of Weimar, Germany ---- <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html> ------ I love the taste of Cryptanalysis in the morning! ------ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-03-08 11:20 ` stefan-lucks @ 2012-03-09 2:02 ` Randy Brukardt 2012-03-09 8:48 ` stefan-lucks 0 siblings, 1 reply; 44+ messages in thread From: Randy Brukardt @ 2012-03-09 2:02 UTC (permalink / raw) <stefan-lucks@see-the.signature> wrote in message news:Pine.LNX.4.64.1203081150460.6571@medsec1.medien.uni-weimar.de... > On Wed, 7 Mar 2012, Randy Brukardt wrote: > >> "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message >> news:wccy5rls0hl.fsf@shell01.TheWorld.com... >> > stefan-lucks@see-the.signature writes: >> >> procedure Sort(Items: in out array(generic Positive range <>) of T) >> >> is >> > >> > Interesting idea. >> >> Can't you do this with a type conversion to an appropriate constrained >> array >> subtype? (Such conversions slide.) I suppose it could be a pain to >> declare >> such a subtype in front of every call, but it is a trick I've used a some >> cases (usually in wrappers) to force sliding on arguments. > > Can you explain a bit more? Bob and I both gave examples of it in other messages. >> I suppose a more convinient way to force that would be helpful. > > Beyond the inconvenience, there are two further issues: > > 1. Testing > > When you declare > > procedure Sort(Items: in out array(Positive range <>) of T) Let's hope you never do that, because anonymous types are the bane of Ada. And you surely can't do it today! > in a package specification, and you try to write a proper black-box test, > you need *more* test cases then when you do the same with another popular > language. Right. Bob's suggestion for some semi-constrained array subtype would fix that. But note that it was killed in Ada 95 in part because the implementation needed was essentially a record type with some sort of magic indexing. Which was a mess. Not sure if those issues can really go away, and I doubt there is much interest in new first-class kinds of constraints. ... > 2. Generality > > Given the (Ada 2020?) specification > > procedure Sort(Items: in out array(generic Positive range <>) of T); Again, anonymous types are a disaster in Ada, and there is no way that we should have more of them. I would strongly hope that this never appears even in Ada 2525. ;-) > Sort(X) should be callable if the index type of X is any discrete type, > except when the index type is too large (i.e., X has more than > Positive'Last elements). If you want that, you need to declare that. Positive is a numeric type, and thus has "+" and "-" available; it is never going to be allowed to match "any discrete type" which includes enumeration types. It's plenty easy to write a generic like this (see the predefined ones for examples), so I fail to see what possible need you are addressing here. Randy. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-03-09 2:02 ` Randy Brukardt @ 2012-03-09 8:48 ` stefan-lucks 2012-03-09 21:10 ` Randy Brukardt 2012-03-15 2:55 ` BrianG 0 siblings, 2 replies; 44+ messages in thread From: stefan-lucks @ 2012-03-09 8:48 UTC (permalink / raw) On Thu, 8 Mar 2012, Randy Brukardt wrote: [...] > <stefan-lucks@see-the.signature> wrote: > > 1. Testing > > > > When you declare > > > > procedure Sort(Items: in out array(Positive range <>) of T) > > Let's hope you never do that, because anonymous types are the bane of Ada. > And you surely can't do it today! OK, the real thing would look about like that: generic type Element_Type is (<>); type Index_Type is (Positive range <>); type Collection_Type is array(Index_Type range <>) of Element_Type; with function "<"(Left, Right : Element_Type) return Boolean is <>; function Sort(Items : Collection_Type) return Collection_Type; > > in a package specification, and you try to write a proper black-box test, > > you need *more* test cases then when you do the same with another popular > > language. > > Right. Bob's suggestion for some semi-constrained array subtype would fix > that. But note that it was killed in Ada 95 in part because the > implementation needed was essentially a record type with some sort of magic > indexing. Which was a mess. Would it ease the implementation if you constrain the start index to some fixed value, such as Index_Type'First? [...] > > procedure Sort(Items: in out array(generic Positive range <>) of T); > Again, anonymous types are a disaster in Ada, and there is no way that we > should have more of them. I would strongly hope that this never appears even > in Ada 2525. ;-) Sorry for using anonymous arrays. I hardly ever do this in real code. Please change the above code to generic type Element_Type is (<>); type Collection_Type is array(generic Positive range <>) of Element_Type; with function "<"(Left, Right : Element_Type) return Boolean is <>; function Sort(Items : Collection_Type) return Collection_Type; The issue is the same: > > Sort(X) should be callable if the index type of X is any discrete type, > > except when the index type is too large (i.e., X has more than > > Positive'Last elements). > > If you want that, you need to declare that. Positive is a numeric type, and > thus has "+" and "-" available; it is never going to be allowed to match > "any discrete type" which includes enumeration types. > > It's plenty easy to write a generic like this (see the predefined ones for > examples), so I fail to see what possible need you are addressing here. It is a readability issue. I frequently use arrays with different index types, that I need to sort, or to search for the same item being stored more than once ... Having to instantiate, say, the same Sort procedure several times makes the program less readable. But I agree with you that that is a minor issue, and that Bob's suggestion seems to solve the testing thing quite well. -- ---- Stefan.Lucks (at) uni-weimar.de, University of Weimar, Germany ---- <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html> ------ I love the taste of Cryptanalysis in the morning! ------ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-03-09 8:48 ` stefan-lucks @ 2012-03-09 21:10 ` Randy Brukardt 2012-03-15 2:55 ` BrianG 1 sibling, 0 replies; 44+ messages in thread From: Randy Brukardt @ 2012-03-09 21:10 UTC (permalink / raw) <stefan-lucks@see-the.signature> wrote in message news:Pine.LNX.4.64.1203090923440.19986@medsec1.medien.uni-weimar.de... > On Thu, 8 Mar 2012, Randy Brukardt wrote: ... > Sorry for using anonymous arrays. I hardly ever do this in real > code. Please change the above code to > > generic > type Element_Type is (<>); > type Collection_Type is array(generic Positive range <>) of > Element_Type; > with function "<"(Left, Right : Element_Type) return Boolean is <>; > function Sort(Items : Collection_Type) return Collection_Type; > > The issue is the same: > >> > Sort(X) should be callable if the index type of X is any discrete type, >> > except when the index type is too large (i.e., X has more than >> > Positive'Last elements). Which as I said doesn't make sense: Positive is numeric; if you want any discrete type for the index you have to say so: generic type Element_Type is (<>); -- Really ought to be "is private". type Index_Type is (<>); type Collection_Type is array(Index_Type range <>) of Element_Type; with function "<"(Left, Right : Element_Type) return Boolean is <>; function Sort(Items : Collection_Type) return Collection_Type; This works now (and has since Ada 83!), so I fail to see exactly what your proposal gains (other than a bit less typing, and Ada have never been about saving typing). Randy. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-03-09 8:48 ` stefan-lucks 2012-03-09 21:10 ` Randy Brukardt @ 2012-03-15 2:55 ` BrianG 2012-03-15 7:46 ` stefan-lucks 1 sibling, 1 reply; 44+ messages in thread From: BrianG @ 2012-03-15 2:55 UTC (permalink / raw) On 03/09/2012 03:48 AM, stefan-lucks@see-the.signature wrote: > On Thu, 8 Mar 2012, Randy Brukardt wrote: > [...] >> <stefan-lucks@see-the.signature> wrote: > >>> 1. Testing >>> >>> When you declare >>> >>> procedure Sort(Items: in out array(Positive range<>) of T) >> >> Let's hope you never do that, because anonymous types are the bane of Ada. >> And you surely can't do it today! > > OK, the real thing would look about like that: > > generic > type Element_Type is (<>); > type Index_Type is (Positive range<>); > type Collection_Type is array(Index_Type range<>) of Element_Type; > with function "<"(Left, Right : Element_Type) return Boolean is<>; > > function Sort(Items : Collection_Type) return Collection_Type; > >>> in a package specification, and you try to write a proper black-box test, >>> you need *more* test cases then when you do the same with another popular >>> language. >> >> Right. Bob's suggestion for some semi-constrained array subtype would fix >> that. But note that it was killed in Ada 95 in part because the >> implementation needed was essentially a record type with some sort of magic >> indexing. Which was a mess. > > Would it ease the implementation if you constrain the start index to some > fixed value, such as Index_Type'First? If that's what you want, why can't you (in this case) define that in your generic parameters, rather than an index range (which already has a defined type)? -- --- BrianG 000 @[Google's email domain] .com ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-03-15 2:55 ` BrianG @ 2012-03-15 7:46 ` stefan-lucks 0 siblings, 0 replies; 44+ messages in thread From: stefan-lucks @ 2012-03-15 7:46 UTC (permalink / raw) On Wed, 14 Mar 2012, BrianG wrote: > On 03/09/2012 03:48 AM, stefan-lucks@see-the.signature wrote: > > On Thu, 8 Mar 2012, Randy Brukardt wrote: > > [...] > > > <stefan-lucks@see-the.signature> wrote: > > > > > > 1. Testing > > > > > > > > When you declare > > > > > > > > procedure Sort(Items: in out array(Positive range<>) of T) > > > > > > Let's hope you never do that, because anonymous types are the bane of Ada. > > > And you surely can't do it today! > > > > OK, the real thing would look about like that: > > > > generic > > type Element_Type is (<>); > > type Index_Type is (Positive range<>); > > type Collection_Type is array(Index_Type range<>) of Element_Type; > > with function "<"(Left, Right : Element_Type) return Boolean is<>; > > > > function Sort(Items : Collection_Type) return Collection_Type; > > > > > > in a package specification, and you try to write a proper black-box > > > > test, > > > > you need *more* test cases then when you do the same with another > > > > popular > > > > language. > > > > > > Right. Bob's suggestion for some semi-constrained array subtype would fix > > > that. But note that it was killed in Ada 95 in part because the > > > implementation needed was essentially a record type with some sort of > > > magic > > > indexing. Which was a mess. > > > > Would it ease the implementation if you constrain the start index to some > > fixed value, such as Index_Type'First? > > If that's what you want, why can't you (in this case) define that in your > generic parameters, rather than an index range (which already has a defined > type)? Good catch! I actually meant Positive'First. -- ---- Stefan.Lucks (at) uni-weimar.de, University of Weimar, Germany ---- <http://www.uni-weimar.de/cms/medien/mediensicherheit/home.html> ------ I love the taste of Cryptanalysis in the morning! ------ ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 18:24 ` Robert A Duff 2012-02-29 19:45 ` stefan-lucks @ 2012-02-29 20:07 ` Dmitry A. Kazakov 2012-02-29 23:15 ` Robert A Duff 2012-02-29 20:47 ` Simon Wright 2 siblings, 1 reply; 44+ messages in thread From: Dmitry A. Kazakov @ 2012-02-29 20:07 UTC (permalink / raw) On Wed, 29 Feb 2012 13:24:10 -0500, Robert A Duff wrote: > Ludovic Brenta <ludovic@ludovic-brenta.org> writes: > >> Doesn't that preclude slices that don't start at 'First? > > Yes. Too bad, yet another source of silly exceptions. >> Supposing your declarations, can you call >> >> procedure Foo (Param : in out Integer_Array); >> >> like this: >> >> declare >> A : Integer_Array (1 .. 10) := (others => 0); >> begin >> Foo (A (3 .. 8)); >> end; >> >> ? > > That will raise C_E. IMHO, that's a language design flaw -- inside > Foo, Param'First ought to be 1. Nope. Consider index of an enumeration type. > The fact that Foo can see that > it came from a slice is a leak of abstraction. There is no leak, but a misuse of a cardinal index as if it were ordinal number. This is typical for broken languages like C, but Ada was always better. What you want is something like: A (Index_Type'Val (1)) -- The first element of A rather than A (1) -- The element of A at the position corresponding to 1 Probably, the language should have a special syntax for that. Beloved [] brackets could suffice for the job: A [<universal-integer>] could denote the array element by its position rather than by the index. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 20:07 ` Dmitry A. Kazakov @ 2012-02-29 23:15 ` Robert A Duff 2012-03-01 8:54 ` Dmitry A. Kazakov 0 siblings, 1 reply; 44+ messages in thread From: Robert A Duff @ 2012-02-29 23:15 UTC (permalink / raw) "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes: > On Wed, 29 Feb 2012 13:24:10 -0500, Robert A Duff wrote: >> That will raise C_E. No, Assert_Failure. >...IMHO, that's a language design flaw -- inside >> Foo, Param'First ought to be 1. > > Nope. Consider index of an enumeration type. I'm talking about the example where the array is (1) unconstrained, (2) the index type is Positive, and (3) the programmer wants all array objects A to have A'First = Positive'First. Nothing to do with enumeration types. Ada allows you to fix 'First and 'Last, and it allows you to let both vary. What's missing is a way to fix 'First, but allow 'Last to vary. I sort of implied that a predicate should do that, but that's wrong. There should be SOME way to do it. And for completeness, I'd allow fixing 'Last, but allowing 'First to vary, although that's much less useful. Ada 9X proposed a solution based on discriminated arrays. When an array index is an enum, it's almost always constrained (i.e. 'First and 'Last are both fixed), slices are usually meaningless, and of course 1 isn't a value of any enum type. I'm certainly not saying "all arrays should start at 1". I'm saying "there should a way to declare an array type such that all objects of that type start at 1 (or any other value) while allowing 'Last to vary". - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 23:15 ` Robert A Duff @ 2012-03-01 8:54 ` Dmitry A. Kazakov 2012-03-01 14:06 ` Robert A Duff 0 siblings, 1 reply; 44+ messages in thread From: Dmitry A. Kazakov @ 2012-03-01 8:54 UTC (permalink / raw) On Wed, 29 Feb 2012 18:15:52 -0500, Robert A Duff wrote: > Ada allows you to fix 'First and 'Last, and it allows you to let > both vary. What's missing is a way to fix 'First, but allow > 'Last to vary. I sort of implied that a predicate should do that, > but that's wrong. There should be SOME way to do it. And for > completeness, I'd allow fixing 'Last, but allowing 'First to vary, > although that's much less useful. That sort of thing has nothing to do with constraining, so the idea to get it using a predicate is straight wrong. > I'm certainly not saying "all arrays should start at 1". I'm saying > "there should a way to declare an array type such that all objects of > that type start at 1 (or any other value) while allowing 'Last to > vary". The array abstraction is that the given index denotes the *same* array element. Sliding indices is another abstraction (ordinal, sequence) and it does not require arrays having separate index types. Ordinals are same for all sequences. The point is that all cases where you wanted arrays to start from some element are actually views on an array as a sequence of elements. Provide that view and the problem would vanish. This would also eliminate the problem of constructing empty arrays and simplify generics dealing with arrays. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-03-01 8:54 ` Dmitry A. Kazakov @ 2012-03-01 14:06 ` Robert A Duff 0 siblings, 0 replies; 44+ messages in thread From: Robert A Duff @ 2012-03-01 14:06 UTC (permalink / raw) "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes: > That sort of thing has nothing to do with constraining, so the idea to get > it using a predicate is straight wrong. Yes, I agree. That's what I meant when I said, "I sort of implied that a predicate should do that, but that's wrong." - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 18:24 ` Robert A Duff 2012-02-29 19:45 ` stefan-lucks 2012-02-29 20:07 ` Dmitry A. Kazakov @ 2012-02-29 20:47 ` Simon Wright 2012-02-29 22:23 ` Robert A Duff 2 siblings, 1 reply; 44+ messages in thread From: Simon Wright @ 2012-02-29 20:47 UTC (permalink / raw) Robert A Duff <bobduff@shell01.TheWorld.com> writes: > Ludovic Brenta <ludovic@ludovic-brenta.org> writes: > >> Doesn't that preclude slices that don't start at 'First? > > Yes. > >> Supposing your declarations, can you call >> >> procedure Foo (Param : in out Integer_Array); >> >> like this: >> >> declare >> A : Integer_Array (1 .. 10) := (others => 0); >> begin >> Foo (A (3 .. 8)); >> end; >> >> ? > > That will raise C_E. IMHO, that's a language design flaw -- inside > Foo, Param'First ought to be 1. The fact that Foo can see that > it came from a slice is a leak of abstraction. I know that GNAT is a work-in-progress, but it doesn't raise C_E with GNAT GPL 2011. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 20:47 ` Simon Wright @ 2012-02-29 22:23 ` Robert A Duff 2012-02-29 23:27 ` Robert A Duff 2012-02-29 23:30 ` Simon Wright 0 siblings, 2 replies; 44+ messages in thread From: Robert A Duff @ 2012-02-29 22:23 UTC (permalink / raw) Simon Wright <simon@pushface.org> writes: > I know that GNAT is a work-in-progress, but it doesn't raise C_E with > GNAT GPL 2011. Did you turn on assertions? They're off by default in GNAT. You need to use a switch or a pragma. It works with the latest GNAT Pro. But this shows I was wrong when I said it raises Constraint_Error. I momentarily forgot that it raises Assert_Failure. Some folks think it should raise Constraint_Error, and Ada 2012 isn't quite finished, so... % gnatmake -f -gnata -gnat2012 array_test.adb gcc -c -gnata -gnat2012 array_test.adb gnatbind -x array_test.ali gnatlink array_test.ali % ./array_test raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : predicate failed at array_test.adb:14 % cat array_test.adb procedure Array_Test is type Integer_Array is array (Positive range <>) of Integer with Dynamic_Predicate => Integer_Array'First = 1; procedure Foo (Param : in out Integer_Array) is begin null; end Foo; A : Integer_Array (1 .. 10) := (others => 0); begin Foo (A (3 .. 8)); end Array_Test; % - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 22:23 ` Robert A Duff @ 2012-02-29 23:27 ` Robert A Duff 2012-03-01 0:53 ` Adam Beneschan ` (2 more replies) 2012-02-29 23:30 ` Simon Wright 1 sibling, 3 replies; 44+ messages in thread From: Robert A Duff @ 2012-02-29 23:27 UTC (permalink / raw) Robert A Duff <bobduff@shell01.TheWorld.com> writes: > % ./array_test > > raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : predicate failed at array_test.adb:14 Actually, I just realized there's a workaround for the slice problem. The following does not raise any exception. procedure Array_Test is type Integer_Array is array (Positive range <>) of Integer with Dynamic_Predicate => Integer_Array'First = 1; procedure Foo (Param : in out Integer_Array) is begin null; end Foo; A : Integer_Array (1 .. 10) := (others => 0); subtype Slide is Integer_Array(1..6); begin Foo (Slide(A (3 .. 8))); end Array_Test; But that's kind of tricky; I wouldn't do that without copious comments. And some constants or something to avoid violating the DRY principle. - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 23:27 ` Robert A Duff @ 2012-03-01 0:53 ` Adam Beneschan 2012-03-01 7:16 ` Ludovic Brenta 2012-03-08 0:08 ` Randy Brukardt 2 siblings, 0 replies; 44+ messages in thread From: Adam Beneschan @ 2012-03-01 0:53 UTC (permalink / raw) On Feb 29, 3:27 pm, Robert A Duff <bobd...@shell01.TheWorld.com> wrote: > > But that's kind of tricky; I wouldn't do that without copious > comments. And some constants or something to avoid violating > the DRY principle. What's the DRY principle? Does it have something to do with not writing code that will drive your readers to drink? -- Adam ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 23:27 ` Robert A Duff 2012-03-01 0:53 ` Adam Beneschan @ 2012-03-01 7:16 ` Ludovic Brenta 2012-03-01 14:03 ` Robert A Duff 2012-03-08 0:08 ` Randy Brukardt 2 siblings, 1 reply; 44+ messages in thread From: Ludovic Brenta @ 2012-03-01 7:16 UTC (permalink / raw) Robert A Duff writes on comp.lang.ada: > Robert A Duff <bobduff@shell01.TheWorld.com> writes: > >> % ./array_test >> >> raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : predicate failed at array_test.adb:14 > > Actually, I just realized there's a workaround for the slice problem. > The following does not raise any exception. > > procedure Array_Test is > > type Integer_Array is array (Positive range <>) of Integer > with Dynamic_Predicate => Integer_Array'First = 1; > > procedure Foo (Param : in out Integer_Array) is > begin > null; > end Foo; > > A : Integer_Array (1 .. 10) := (others => 0); > > subtype Slide is Integer_Array(1..6); > begin > Foo (Slide(A (3 .. 8))); Doesn't that involve copy-in and copy-out between the slice and a temporary object of type Slide? Grossly inefficient, methinks. > end Array_Test; > > But that's kind of tricky; I wouldn't do that without copious > comments. And some constants or something to avoid violating the DRY > principle. Yes. For Adam: DRY=Don't Repeat Yourself. -- Ludovic Brenta. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-03-01 7:16 ` Ludovic Brenta @ 2012-03-01 14:03 ` Robert A Duff 2012-03-08 0:11 ` Randy Brukardt 0 siblings, 1 reply; 44+ messages in thread From: Robert A Duff @ 2012-03-01 14:03 UTC (permalink / raw) Ludovic Brenta <ludovic@ludovic-brenta.org> writes: > Robert A Duff writes on comp.lang.ada: >> Foo (Slide(A (3 .. 8))); > > Doesn't that involve copy-in and copy-out between the slice and a > temporary object of type Slide? Grossly inefficient, methinks. No, I don't see any need for the compiler to make a copy here. It just needs to pass the 1..6 bounds, plus a pointer to the array elements. I'm too lazy to check the machine code to see whether GNAT makes a copy, but I don't see why it would. Nor any other Ada compiler. - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-03-01 14:03 ` Robert A Duff @ 2012-03-08 0:11 ` Randy Brukardt 0 siblings, 0 replies; 44+ messages in thread From: Randy Brukardt @ 2012-03-08 0:11 UTC (permalink / raw) "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message news:wcclinkxvth.fsf@shell01.TheWorld.com... > Ludovic Brenta <ludovic@ludovic-brenta.org> writes: > >> Robert A Duff writes on comp.lang.ada: >>> Foo (Slide(A (3 .. 8))); >> >> Doesn't that involve copy-in and copy-out between the slice and a >> temporary object of type Slide? Grossly inefficient, methinks. > > No, I don't see any need for the compiler to make a copy here. > It just needs to pass the 1..6 bounds, plus a pointer to > the array elements. > > I'm too lazy to check the machine code to see whether GNAT > makes a copy, but I don't see why it would. Nor any other > Ada compiler. Surely Janus/Ada does not. It just makes a new array descriptor and passes that -- exactly the same as it would do for the original slice. The only interesting question is whether Janus/Ada would be smart enough to get rid of the (unused) original slice's array descriptor. I didn't check, but I'm sure there wouldn't be one in this case (static bounds). I have no idea what would happen if the bounds were dynamic (as in my better example). Randy. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 23:27 ` Robert A Duff 2012-03-01 0:53 ` Adam Beneschan 2012-03-01 7:16 ` Ludovic Brenta @ 2012-03-08 0:08 ` Randy Brukardt 2 siblings, 0 replies; 44+ messages in thread From: Randy Brukardt @ 2012-03-08 0:08 UTC (permalink / raw) "Robert A Duff" <bobduff@shell01.TheWorld.com> wrote in message news:wccpqcxrzj2.fsf@shell01.TheWorld.com... ... > Actually, I just realized there's a workaround for the slice problem. Yes, as I just posted. I guess I should have read further before answering. > The following does not raise any exception. > > procedure Array_Test is > > type Integer_Array is array (Positive range <>) of Integer > with Dynamic_Predicate => Integer_Array'First = 1; > > procedure Foo (Param : in out Integer_Array) is > begin > null; > end Foo; > > A : Integer_Array (1 .. 10) := (others => 0); > > subtype Slide is Integer_Array(1..6); > begin > Foo (Slide(A (3 .. 8))); > end Array_Test; > > But that's kind of tricky; I wouldn't do that without copious > comments. And some constants or something to avoid violating > the DRY principle. Maybe it's tricky, buit I've used it a lot, especially inside of things like Ada.Strings.Fixed (and in my spam filter, which tends to do direct string manipulations because it's usually easier to do that than to figure out what Ada.Strings.Fixed function does 80% of the job [none of them ever seem to do the whole job] and then figure out how to do the rest afterwards). But typically, "Slide" uses bounds derived from whatever you're going to pass (usually, "3" and "8" above are local variables anyway): declare L : Natural := 3; R : Natural := 8; -- These are usually calculated from the other code nearby. subtype Slide is Integer_Array(1..R-L+1); begin Foo (Slide(A (L .. R))); end; Feel free to turn this into a "gem" if you're so inclined. :-) Randy. ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 22:23 ` Robert A Duff 2012-02-29 23:27 ` Robert A Duff @ 2012-02-29 23:30 ` Simon Wright 1 sibling, 0 replies; 44+ messages in thread From: Simon Wright @ 2012-02-29 23:30 UTC (permalink / raw) Robert A Duff <bobduff@shell01.TheWorld.com> writes: > Simon Wright <simon@pushface.org> writes: > >> I know that GNAT is a work-in-progress, but it doesn't raise C_E with >> GNAT GPL 2011. > > Did you turn on assertions? They're off by default in GNAT. You need > to use a switch or a pragma. > > It works with the latest GNAT Pro. But this shows I was wrong when I > said it raises Constraint_Error. I momentarily forgot that it raises > Assert_Failure. Some folks think it should raise Constraint_Error, > and Ada 2012 isn't quite finished, so... With -gnata, GNAT GPL 2011 behaves as you say. So does FSF GCC 4.7 (as at r183368). ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 16:09 ` Robert A Duff 2012-02-29 16:50 ` Ludovic Brenta @ 2012-02-29 18:35 ` Jeffrey Carter 2012-02-29 22:59 ` Robert A Duff 2012-02-29 20:40 ` Adam Beneschan 2 siblings, 1 reply; 44+ messages in thread From: Jeffrey Carter @ 2012-02-29 18:35 UTC (permalink / raw) On 02/29/2012 09:09 AM, Robert A Duff wrote: > > Right, and this is a rich source of bugs. You usually want > arrays to start at 1, or sometimes 0 (assuming the index type > is a signed integer type, which is almost always the case > for unconstrained arrays). My 1st programming job, using FORTRAN 66, dealt with tree-ring data. We had data from 1600 to 1970. Calculating the correct array index for a year was a rich source of bugs for us; if we'd been able to have the lower bounds be 1600 rather than 1 life would have been a lot easier. -- Jeff Carter "Sir Robin the not-quite-so-brave-as-Sir-Lancelot, who had nearly fought the Dragon of Angnor, who nearly stood up to the vicious Chicken of Bristol, and who had personally wet himself at the Battle of Badon Hill." Monty Python & the Holy Grail 68 --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net --- ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 18:35 ` Jeffrey Carter @ 2012-02-29 22:59 ` Robert A Duff 2012-02-29 23:51 ` Jeffrey Carter 0 siblings, 1 reply; 44+ messages in thread From: Robert A Duff @ 2012-02-29 22:59 UTC (permalink / raw) Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org> writes: > My 1st programming job, using FORTRAN 66, dealt with tree-ring data. We > had data from 1600 to 1970. Calculating the correct array index for a > year was a rich source of bugs for us; if we'd been able to have the > lower bounds be 1600 rather than 1 life would have been a lot easier. Indeed. In Ada that would be a constrained array. I was talking about unconstrained arrays. There are really two different kinds of arrays: sequences and mappings. A sequence represented as an array is usually an unconstrained type (but can be constrained if you know the length), and the index subtype is almost always a signed integer, and usually starts at 0 or 1. A mapping represented as an array is almost always constrained, and the index subtype might be an enumeration, or something like your 1600..1970 above. The index type should almost never be modular, especially when unconstrained. - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 22:59 ` Robert A Duff @ 2012-02-29 23:51 ` Jeffrey Carter 2012-03-01 3:03 ` Robert A Duff 0 siblings, 1 reply; 44+ messages in thread From: Jeffrey Carter @ 2012-02-29 23:51 UTC (permalink / raw) On 02/29/2012 03:59 PM, Robert A Duff wrote: > Jeffrey Carter<spam.jrcarter.not@spam.not.acm.org> writes: > >> My 1st programming job, using FORTRAN 66, dealt with tree-ring data. We >> had data from 1600 to 1970. Calculating the correct array index for a >> year was a rich source of bugs for us; if we'd been able to have the >> lower bounds be 1600 rather than 1 life would have been a lot easier. > > Indeed. In Ada that would be a constrained array. > I was talking about unconstrained arrays. I think if I were doing it in Ada today, I'd have an unconstrained array type, and then create a constrained subtype with bounds determined from the input. But remembering the monolithic, goto-filled crap I coded in those days, there are a lot of things I'd do differently today. -- Jeff Carter "Sir Robin the not-quite-so-brave-as-Sir-Lancelot, who had nearly fought the Dragon of Angnor, who nearly stood up to the vicious Chicken of Bristol, and who had personally wet himself at the Battle of Badon Hill." Monty Python & the Holy Grail 68 --- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net --- ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 23:51 ` Jeffrey Carter @ 2012-03-01 3:03 ` Robert A Duff 0 siblings, 0 replies; 44+ messages in thread From: Robert A Duff @ 2012-03-01 3:03 UTC (permalink / raw) Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org> writes: > I think if I were doing it in Ada today, I'd have an unconstrained array > type, and then create a constrained subtype with bounds determined from > the input. Fair enough. It's a mapping from years to tree-ring data, as opposed to a sequence of tree-ring data items, so it certainly makes sense to have 1600..1970. > But remembering the monolithic, goto-filled crap I coded in those days, > there are a lot of things I'd do differently today. ;-) - Bob ^ permalink raw reply [flat|nested] 44+ messages in thread
* Re: Array Help? 2012-02-29 16:09 ` Robert A Duff 2012-02-29 16:50 ` Ludovic Brenta 2012-02-29 18:35 ` Jeffrey Carter @ 2012-02-29 20:40 ` Adam Beneschan 2 siblings, 0 replies; 44+ messages in thread From: Adam Beneschan @ 2012-02-29 20:40 UTC (permalink / raw) On Feb 29, 8:09 am, Robert A Duff <bobd...@shell01.TheWorld.com> wrote: > Adam Beneschan <a...@irvine.com> writes: > > type Integer_Array is array (Integer range <>) of Integer; > > A : Integer_Array (2 .. 7); > > Right, and this is a rich source of bugs. You usually want > arrays to start at 1, or sometimes 0 (assuming the index type > is a signed integer type, which is almost always the case > for unconstrained arrays). I think most of my array subtypes start at 0 or 1. Occasionally I find a good reason to start at -1. The other thing is that if I write a procedure with an unconstrained array parameter P, I'll often declare a local variable like A : Some_Array (P'Range); where the array may or may not be the same array type as the parameter. It's very useful to be able to declare an array where the index values mean the same thing as the index values into a different array, rather than having to deal with some blasted +/- offset when trying to work with parallel elements in the two arrays. -- Adam ^ permalink raw reply [flat|nested] 44+ messages in thread
end of thread, other threads:[~2012-03-15 7:50 UTC | newest] Thread overview: 44+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2012-02-28 20:15 Array Help? Will 2012-02-28 20:47 ` Ludovic Brenta 2012-02-28 22:11 ` Simon Wright 2012-02-28 23:09 ` Will 2012-02-29 0:24 ` Adam Beneschan 2012-02-28 20:50 ` Gautier write-only 2012-02-28 21:33 ` Simon Wright 2012-02-29 0:27 ` Adam Beneschan 2012-02-29 8:00 ` Simon Wright 2012-02-29 8:48 ` Simon Wright 2012-02-29 0:22 ` Adam Beneschan 2012-02-29 16:09 ` Robert A Duff 2012-02-29 16:50 ` Ludovic Brenta 2012-02-29 18:24 ` Robert A Duff 2012-02-29 19:45 ` stefan-lucks 2012-02-29 20:45 ` Jeffrey Carter 2012-02-29 21:27 ` stefan-lucks 2012-02-29 23:06 ` Robert A Duff 2012-03-02 13:45 ` stefan-lucks 2012-03-07 23:58 ` Randy Brukardt 2012-03-08 11:20 ` stefan-lucks 2012-03-09 2:02 ` Randy Brukardt 2012-03-09 8:48 ` stefan-lucks 2012-03-09 21:10 ` Randy Brukardt 2012-03-15 2:55 ` BrianG 2012-03-15 7:46 ` stefan-lucks 2012-02-29 20:07 ` Dmitry A. Kazakov 2012-02-29 23:15 ` Robert A Duff 2012-03-01 8:54 ` Dmitry A. Kazakov 2012-03-01 14:06 ` Robert A Duff 2012-02-29 20:47 ` Simon Wright 2012-02-29 22:23 ` Robert A Duff 2012-02-29 23:27 ` Robert A Duff 2012-03-01 0:53 ` Adam Beneschan 2012-03-01 7:16 ` Ludovic Brenta 2012-03-01 14:03 ` Robert A Duff 2012-03-08 0:11 ` Randy Brukardt 2012-03-08 0:08 ` Randy Brukardt 2012-02-29 23:30 ` Simon Wright 2012-02-29 18:35 ` Jeffrey Carter 2012-02-29 22:59 ` Robert A Duff 2012-02-29 23:51 ` Jeffrey Carter 2012-03-01 3:03 ` Robert A Duff 2012-02-29 20:40 ` Adam Beneschan
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox