Jump to page: 1 2 3
Thread overview
Array Lower Bounds
Dec 18, 2007
Mike Marquard
Dec 18, 2007
Bill Baxter
Dec 18, 2007
Mike Marquard
Dec 18, 2007
novice2
Dec 18, 2007
Mike Marquard
Dec 18, 2007
0ffh
Dec 19, 2007
Daniel Keep
Dec 18, 2007
0ffh
Dec 18, 2007
Mike Marquard
Dec 18, 2007
0ffh
Dec 18, 2007
0ffh
Dec 18, 2007
bearophile
Dec 18, 2007
Bill Baxter
Dec 18, 2007
Bill Baxter
Dec 18, 2007
0ffh
Dec 18, 2007
Mike Marquard
Dec 18, 2007
0ffh
Dec 18, 2007
Mike Marquard
December 18, 2007
I know the default starting index for arrays is zero in D. But is there any way you can change the lower bounds to something other zero? For instance having the index go from 1 to 10 or say 100 to 200.

If not are there any plans to add this feature in the future?
December 18, 2007
Mike Marquard wrote:
> I know the default starting index for arrays is zero in D. But is there any way you can change the lower bounds to something other zero? For instance having the index go from 1 to 10 or say 100 to 200.
> 
> If not are there any plans to add this feature in the future?

That's a Fortran feature, right?
D doesn't have it and I pretty much doubt it ever will.

You could try to create your own user type with this feature.  But lack of ability to return lvalues in D makes it impossible to create an array type that works just like the built-in arrays.

--bb
December 18, 2007
Thanks for the answer Bill. That's too bad, I would think that something like that would be fairly easy to implement in the language and would certainly be usefull.

> That's a Fortran feature, right?
> D doesn't have it and I pretty much doubt it ever will.

I've never used fortran but I think it does have that feature and I think just about every language that came before c became popular had that feature.
December 18, 2007
> I've never used fortran but I think it does have that feature and I think just about every language that came before c became popular had that feature.

as for me (and some other peoples too i hope), this is bad feature.
uniform lower bound for all arrays make code more readable and more errorless.

December 18, 2007
"Mike Marquard" <mike_marquard@hotmail.com> wrote in message news:fk7r0c$n3q$1@digitalmars.com...
> Thanks for the answer Bill. That's too bad, I would think that something like that would be fairly easy to implement in the language and would certainly be usefull.
>
>
> I've never used fortran but I think it does have that feature and I think just about every language that came before c became popular had that feature.

What things are better with arbitrary-lower-bound arrays?  I honestly can't think of a time where I was like "damn!  I wish I could start this array at 17!" or something.


December 18, 2007
Mike Marquard:
> I know the default starting index for arrays is zero in D. But is there any way you can change the lower bounds to something other zero? For instance having the index go from 1 to 10 or say 100 to 200.

Delphi, TurboPascal, and probably FreePascal have such feature, and once in a while it's useful, see here for example:
http://blogs.warwick.ac.uk/mtcharemza/entry/fortran_9095_versus_1
but it's not difficult to adapt your mind to the fixed 0 starting point, and you don't need to go looking at definition of the array type every time to see what's the actual starting point of the array you want to use.
If you want to simulate something like that in C (and D) you may do something like the following, but I don't like this solution much, it's probably better to just use the 0 starting point (note that with -release the asserts go away, and -inline does its work, removing the call to bounds() too):

import std.stdio: writef, writefln;
import std.string: format;

long bounds(long i, long start, long len) {
  // assert doesn't string-ify all successive arguments yet
  assert(i >= start, format("%d < %d", i, start));
  assert(i < start + len, format("%d >= %d + %d", i, start, len));
  return i;
}

void main() {
  const N = 10;
  const SHIFT = 5;

  int[N] a;
  foreach(i, ref el; a)
    el = i;

  typeof(a[0])* b = a.ptr - SHIFT;
  for(int i = 0; i < N; i++) {
    writef(b[bounds(i+SHIFT, SHIFT, a.length)], " ");
    b[bounds(i+SHIFT, SHIFT, a.length)] = i * 10;
  }
  writefln();

  writefln(a);

  // speed benckmark ----------------------
  uint arr[1_000];
  for(uint j; j < 30_000; ++j)
    for(uint i; i < arr.length; ++i) {
      // With -release -O -inline they run in equal time
      static if (true) // change this
        arr[i] = i;
      else
        arr[bounds(i, 0, arr.length)] = i;
    }
}

Bye,
bearophile
December 18, 2007
Mike Marquard wrote:
> I've never used fortran but I think it does have that feature and I
> think just about every language that came before c became popular had
> that feature.

That's a quite... daring and improbable claim!

regards, frank
December 18, 2007
bearophile wrote:
> Mike Marquard:
>> I know the default starting index for arrays is zero in D. But is there any way you can change the lower bounds to something other zero? For instance having the index go from 1 to 10 or say 100 to 200.
> 
> Delphi, TurboPascal, and probably FreePascal have such feature, and once in a while it's useful, see here for example:
> http://blogs.warwick.ac.uk/mtcharemza/entry/fortran_9095_versus_1
> but it's not difficult to adapt your mind to the fixed 0 starting point, and you don't need to go looking at definition of the array type every time to see what's the actual starting point of the array you want to use.
> If you want to simulate something like that in C (and D) you may do something like the following, but I don't like this solution much, it's probably better to just use the 0 starting point (note that with -release the asserts go away, and -inline does its work, removing the call to bounds() too):
> 

I have wished on occasion that I could make a slice that started at its actual real index.  For instance when doing foreach on a slice:

foreach(i,v; things[p..$-q]) {
   // i counts from zero rather than p :-(
}

But as Daniel pointed out, that could be remedied by calling a separate function to get the delegate:

foreach(i,v; enumerate(things, p, things.length-q)) {
   // i from p to $-q here
}

It's too bad that writing a function like enumerate() makes my head hurt or I would undoubtedly use that kind of solution more often. :-)

--bb
December 18, 2007
"bearophile" <bearophileHUGS@lycos.com> wrote in message news:fk8ips$1jt$1@digitalmars.com...
> Mike Marquard:
>> I know the default starting index for arrays is zero in D. But is there any way you can change the lower bounds to something other zero? For instance having the index go from 1 to 10 or say 100 to 200.
>
> Delphi, TurboPascal, and probably FreePascal have such feature, and once
> in a while it's useful, see here for example:
> http://blogs.warwick.ac.uk/mtcharemza/entry/fortran_9095_versus_1
> but it's not difficult to adapt your mind to the fixed 0 starting point,
> and you don't need to go looking at definition of the array type every
> time to see what's the actual starting point of the array you want to use.

Thankfully D has slices and value types with overloadable indexing operators (though, as noted, without ref returns you can't *perfectly* emulate built-in arrays, though that will change), making it possible to emulate this rather .. niche feature.


December 18, 2007
Jarrett Billingsley wrote:
> "bearophile" <bearophileHUGS@lycos.com> wrote in message news:fk8ips$1jt$1@digitalmars.com...
>> Mike Marquard:
>>> I know the default starting index for arrays is zero in D. But is there any way you can change the lower bounds to something other zero? For instance having the index go from 1 to 10 or say 100 to 200.
>> Delphi, TurboPascal, and probably FreePascal have such feature, and once in a while it's useful, see here for example:
>> http://blogs.warwick.ac.uk/mtcharemza/entry/fortran_9095_versus_1
>> but it's not difficult to adapt your mind to the fixed 0 starting point, and you don't need to go looking at definition of the array type every time to see what's the actual starting point of the array you want to use.
> 
> Thankfully D has slices and value types with overloadable indexing operators (though, as noted, without ref returns you can't *perfectly* emulate built-in arrays, though that will change), making it possible to emulate this rather .. niche feature. 

It's not so niche I don't think.  It's pretty widely used in numerical computing.  It's just a question of whether it's useful enough to warrant the extra syntactical and memory baggage it would require.  I think it would require all arrays to carry around a third value, no?

--bb
« First   ‹ Prev
1 2 3