* Array-like object indexing
@ 2002-08-18 0:22 Ryan
2002-08-19 3:55 ` R. Tim Coslet
2002-08-19 5:36 ` Simon Wright
0 siblings, 2 replies; 5+ messages in thread
From: Ryan @ 2002-08-18 0:22 UTC (permalink / raw)
I've recently been playing with making all the standard data structures
in Ada to prepare myself for writing my first major project. Currently
I'm writing a resizable-array package. I am trying to make it as
similar to built-in arrays as possible, with functions for First, Length
and other useful operations and attributes. It is a generic package
instantiated with a type for the elements to be stored and a type for
indexing. I've run into a small problem with respect to the indexing type.
I'm not sure how to represent the boundaries of an empty array without
raising a constraint error in some cases. If an array is being indexed
by Natural, for example, and the first index should be 0 (as makes sense
in many situations), then what should the First and Last functions
(intended to work like their analogous attributes) return? I cannot
return 0 and -1 because -1 would raise a constraint error.
Should the responsibility for this rest on the package or the user?
Would it be a better to return 1 and 0, or to force a user of the
package to instantiate it with an indexing type that starts at -1
instead even though -1 should never be used to actually look into the
array? What are your opinions on this?
Thank you,
Ryan Tarpine
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Array-like object indexing
2002-08-18 0:22 Array-like object indexing Ryan
@ 2002-08-19 3:55 ` R. Tim Coslet
2002-08-19 5:36 ` Simon Wright
1 sibling, 0 replies; 5+ messages in thread
From: R. Tim Coslet @ 2002-08-19 3:55 UTC (permalink / raw)
Have First and Last return Index_Type'Base. At least for numeric types this
will eliminate most cases of the Constraint_Error that you don't want.
(Natural'Base does include -1 in its 'Range even though Natural doesn't)
Unfortunately you can't declare your own functions to return "universal"
types, like the attributes return.
--
R. Tim Coslet
r_tim_coslet@pacbell.net
Technology, n. Domesticated natural phenomena.
> From: Ryan <rtarpine@hotmail.com>
> Newsgroups: comp.lang.ada
> Date: Sat, 17 Aug 2002 17:22:33 -0700
> Subject: Array-like object indexing
>
> I've recently been playing with making all the standard data structures
> in Ada to prepare myself for writing my first major project. Currently
> I'm writing a resizable-array package. I am trying to make it as
> similar to built-in arrays as possible, with functions for First, Length
> and other useful operations and attributes. It is a generic package
> instantiated with a type for the elements to be stored and a type for
> indexing. I've run into a small problem with respect to the indexing type.
>
> I'm not sure how to represent the boundaries of an empty array without
> raising a constraint error in some cases. If an array is being indexed
> by Natural, for example, and the first index should be 0 (as makes sense
> in many situations), then what should the First and Last functions
> (intended to work like their analogous attributes) return? I cannot
> return 0 and -1 because -1 would raise a constraint error.
>
> Should the responsibility for this rest on the package or the user?
> Would it be a better to return 1 and 0, or to force a user of the
> package to instantiate it with an indexing type that starts at -1
> instead even though -1 should never be used to actually look into the
> array? What are your opinions on this?
>
> Thank you,
> Ryan Tarpine
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Array-like object indexing
2002-08-18 0:22 Array-like object indexing Ryan
2002-08-19 3:55 ` R. Tim Coslet
@ 2002-08-19 5:36 ` Simon Wright
2002-08-19 19:33 ` Ryan
1 sibling, 1 reply; 5+ messages in thread
From: Simon Wright @ 2002-08-19 5:36 UTC (permalink / raw)
Ryan <rtarpine@hotmail.com> writes:
> I've recently been playing with making all the standard data
> structures in Ada to prepare myself for writing my first major
> project. Currently I'm writing a resizable-array package. I am
> trying to make it as similar to built-in arrays as possible, with
> functions for First, Length and other useful operations and
> attributes. It is a generic package instantiated with a type for
> the elements to be stored and a type for indexing. I've run into a
> small problem with respect to the indexing type.
>
> I'm not sure how to represent the boundaries of an empty array
> without raising a constraint error in some cases. If an array is
> being indexed by Natural, for example, and the first index should be
> 0 (as makes sense in many situations), then what should the First
> and Last functions (intended to work like their analogous
> attributes) return? I cannot return 0 and -1 because -1 would raise
> a constraint error.
The non-Ada C++ STL approach is to denote the end of the range by a
value notionally one past the last valid element. It's not obvious
that Last is the right name for an operation returning such a value,
cf My_Array'Last.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Array-like object indexing
2002-08-19 5:36 ` Simon Wright
@ 2002-08-19 19:33 ` Ryan
2002-08-19 19:49 ` Darren New
0 siblings, 1 reply; 5+ messages in thread
From: Ryan @ 2002-08-19 19:33 UTC (permalink / raw)
Simon Wright wrote:
> Ryan <rtarpine@hotmail.com> writes:
>
> The non-Ada C++ STL approach is to denote the end of the range by a
> value notionally one past the last valid element. It's not obvious
> that Last is the right name for an operation returning such a value,
> cf My_Array'Last.
Of course, that's just for iterators (which I am wrestling with
separately :). The STL name did always confuse me (Why is
My_String.end() actually *past* the last character of My_String?). I
like First and Last being the exact indicies because it lets me ask "if
Index not in First(My_Array)..Last(My_Array)". I've (since originally
asking) decided to put the responsibility on the user, but now I'm going
to try the Index_Type'Base suggestion I received.
Thanks,
Ryan
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Array-like object indexing
2002-08-19 19:33 ` Ryan
@ 2002-08-19 19:49 ` Darren New
0 siblings, 0 replies; 5+ messages in thread
From: Darren New @ 2002-08-19 19:49 UTC (permalink / raw)
Ryan wrote:
> separately :). The STL name did always confuse me (Why is
> My_String.end() actually *past* the last character of My_String?).
Because C starts counting at zero, and once you make the convention that
ranges are half-open, including the first index and excluding the last, then
a bunch of stuff works a lot better.
--
Darren New
San Diego, CA, USA (PST). Cryptokeys on demand.
** http://images.fbrtech.com/dnew/ **
Try our EbolaBurgers...
So tender they melt in your mouth.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2002-08-19 19:49 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-18 0:22 Array-like object indexing Ryan
2002-08-19 3:55 ` R. Tim Coslet
2002-08-19 5:36 ` Simon Wright
2002-08-19 19:33 ` Ryan
2002-08-19 19:49 ` Darren New
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox