www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Which type it better to use for array's indices?

reply ref2401 <refactor24 gmail.com> writes:
Which type it better to use for array's indices?

float[] arr = new float[10];
int i;
long j;		
size_t k;
// which one is better arr[i], a[j]or arr[k] ?

It seem like `size_t` suites well because 'is large enough to 
represent an offset into all addressible memory.' 
(http://dlang.org/spec/type.html#size_t)
However sometimes I want index to be less the 0 to represent a 
particular case.
For instance `find(float[] arr, float v)` may return -1 if `v` 
has not been found.

Again which type is better or put it into another words which 
type should I pick as default?

Thank you.
Dec 04 2015
next sibling parent ghjk <yukyk ksdjf.ht> writes:
On Friday, 4 December 2015 at 13:24:16 UTC, ref2401 wrote:
 Which type it better to use for array's indices?

 float[] arr = new float[10];
 int i;
 long j;		
 size_t k;
 // which one is better arr[i], a[j]or arr[k] ?

 It seem like `size_t` suites well because 'is large enough to 
 represent an offset into all addressible memory.' 
 (http://dlang.org/spec/type.html#size_t)
 However sometimes I want index to be less the 0 to represent a 
 particular case.
 For instance `find(float[] arr, float v)` may return -1 if `v` 
 has not been found.

 Again which type is better or put it into another words which 
 type should I pick as default?

 Thank you.
size_t because it's the same type as .length.
Dec 04 2015
prev sibling next sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Friday, 4 December 2015 at 13:24:16 UTC, ref2401 wrote:
 Which type it better to use for array's indices?

 float[] arr = new float[10];
 int i;
 long j;		
 size_t k;
 // which one is better arr[i], a[j]or arr[k] ?

 It seem like `size_t` suites well because 'is large enough to 
 represent an offset into all addressible memory.' 
 (http://dlang.org/spec/type.html#size_t)
 However sometimes I want index to be less the 0 to represent a 
 particular case.
 For instance `find(float[] arr, float v)` may return -1 if `v` 
 has not been found.

 Again which type is better or put it into another words which 
 type should I pick as default?

 Thank you.
size_t. it is inferred if you use auto. foreach(i,e; arr) typeof(i)== size_t Nic
Dec 04 2015
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2015-12-04 14:24, ref2401 wrote:

 For instance `find(float[] arr, float v)` may return -1 if `v` has not
 been found.
If "find" is returning an index, it could return arr.length to indicate it was not found. -- /Jacob Carlborg
Dec 04 2015
prev sibling next sibling parent Chris Wright <dhasenan gmail.com> writes:
On Fri, 04 Dec 2015 13:24:16 +0000, ref2401 wrote:

 It seem like `size_t` suites well because 'is large enough to represent
 an offset into all addressible memory.'
 (http://dlang.org/spec/type.html#size_t)
 However sometimes I want index to be less the 0 to represent a
 particular case.
ptrdiff_t is the signed equivalent of size_t. You could use that -- unless you anticipate having an array longer than two billion elements and are compiling to a 32-bit program.
Dec 04 2015
prev sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
On Friday, December 04, 2015 13:24:16 ref2401 via Digitalmars-d-learn wrote:
 Which type it better to use for array's indices?

 float[] arr = new float[10];
 int i;
 long j;
 size_t k;
 // which one is better arr[i], a[j]or arr[k] ?

 It seem like `size_t` suites well because 'is large enough to
 represent an offset into all addressible memory.'
 (http://dlang.org/spec/type.html#size_t)
 However sometimes I want index to be less the 0 to represent a
 particular case.
 For instance `find(float[] arr, float v)` may return -1 if `v`
 has not been found.

 Again which type is better or put it into another words which
 type should I pick as default?
In general, you should always use size_t for length unless you have a very good reason otherwise. Arrays, ranges, and containers all do it. Using anything else is asking for trouble. So, while you _could_ use something else for specific use cases, I'd strongly suggest that you not do so. As for find, std.algorithm already has it. If the range that you give it does not contain the value you're looking for, then the range it returns is empty. Otherwise, it returns a range starting at the element you're searching for. e.g. auto arr = [1, 7, 42, 99, 2, 5]; auto found1 = arr.find(42); assert(found1 == [42, 99, 2, 5]); auto found2 = arr.find(500); assert(found2.empty); Alternatively, if you want the index, then you can use std.algorithm.count, in which case, you'd effectively get the 1-based index of what you're looking for and thus can subtract one from it to get the proper 0-based index. If you really do want to return -1, then you can do like std.string.indexOf does and return ptrdiff_t instead of size_t, but that halves the maximum array size that you can operate on, which can be a problem in 32-bit programs which operate on a lot of data but wouldn't be a problem in 64-bit programs. But in general, size_t is what's used for indices and for length, and using anything else is potentially asking for trouble - especially if you then mix that with other code that does use size_t. For instance, some folks have used int instead of size_t and then gotten bitten when their code was compiled on 64-bit platforms, because there size_t is ulong instead of uint, because then all of a sudden code like int len = arr.length; won't compile even though it did in 32-bit land. Using size_t as much as possible for indices and length avoids that sort of problem. - Jonathan M Davis
Dec 04 2015