digitalmars.D.learn - Array Lower Bounds
- Mike Marquard (2/2) Dec 17 2007 I know the default starting index for arrays is zero in D. But is there ...
- Bill Baxter (7/10) Dec 17 2007 That's a Fortran feature, right?
- Mike Marquard (2/4) Dec 17 2007 I've never used fortran but I think it does have that feature and I thin...
- novice2 (2/3) Dec 17 2007 as for me (and some other peoples too i hope), this is bad feature.
- Jarrett Billingsley (5/11) Dec 18 2007 What things are better with arbitrary-lower-bound arrays? I honestly ca...
- Mike Marquard (2/5) Dec 18 2007 Well one reason is because some people prefer starting arrays at 1 (like...
- Jarrett Billingsley (3/7) Dec 18 2007 Wouldn't slices be perfect for this?
- 0ffh (7/17) Dec 18 2007 Sorry, IMHO zero-starting arrays are the logically most consistent choic...
-
Daniel Keep
(13/27)
Dec 18 2007
- Jarrett Billingsley (5/8) Dec 18 2007 For fun and profit.
- 0ffh (3/6) Dec 18 2007 That's a quite... daring and improbable claim!
- Mike Marquard (1/3) Dec 18 2007 Well I was only born in 1978 so I don't have any first hand experience a...
- bearophile (39/40) Dec 18 2007 Delphi, TurboPascal, and probably FreePascal have such feature, and once...
- Bill Baxter (14/22) Dec 18 2007 I have wished on occasion that I could make a slice that started at its
- Jarrett Billingsley (6/16) Dec 18 2007 Thankfully D has slices and value types with overloadable indexing opera...
- Bill Baxter (6/23) Dec 18 2007 It's not so niche I don't think. It's pretty widely used in numerical
- 0ffh (8/12) Dec 18 2007 Yes, I also think it would, therefore growing the array record by 50%.
- Mike Marquard (1/1) Dec 18 2007 Why would there be a need for an extra memory in normall zero based arra...
- Jarrett Billingsley (7/12) Dec 18 2007 You're right, this could be made to work by introducing another array ty...
- 0ffh (12/17) Dec 18 2007 Indeed it would only concern those who use the non-standard arrays /if/
- Mike Marquard (2/2) Dec 18 2007 Thank you bearophile.
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?
Dec 17 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
Dec 17 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.
Dec 17 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.
Dec 17 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.
Dec 18 2007
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.Well one reason is because some people prefer starting arrays at 1 (like myself). As for numbers other than 0 or 1, there are situations where that makes sense. Right now I am experimenting with an artificial intelligence concept of my own in another base zero only language and sometimes I'm doing things like copying and processing a portion of an image. There have been alot of situations were it would have been nice if I could simply have those arrays have starting and ending bounds that conform to the portion of the image they represent. Obviously this isn't a necesity but it would be a helpfull feature for something like that if I decide to rewrite it in d some day.
Dec 18 2007
"Mike Marquard" <mike_marquard hotmail.com> wrote in message news:fk9g3a$73v$1 digitalmars.com...I'm doing things like copying and processing a portion of an image. There have been alot of situations were it would have been nice if I could simply have those arrays have starting and ending bounds that conform to the portion of the image they represent.Wouldn't slices be perfect for this?
Dec 18 2007
Mike Marquard wrote:Well one reason is because some people prefer starting arrays at 1 (like myself). As for numbers other than 0 or 1, there are situations where that makes sense. Right now I am experimenting with an artificial intelligence concept of my own in another base zero only language and sometimes I'm doing things like copying and processing a portion of an image. There have been alot of situations were it would have been nice if I could simply have those arrays have starting and ending bounds that conform to the portion of the image they represent.Sorry, IMHO zero-starting arrays are the logically most consistent choice in a language that also offers pointers, starting at 1 is awkward.Obviously this isn't a necesity but it would be a helpfull feature for something like that if I decide to rewrite it in d some day.You could just scrap the extra element and fill your array starting at 1, if you absolutely must. Check the 0th element for change with asserts or contracts, to catch errors. regards, frank
Dec 18 2007
Jarrett Billingsley wrote:"Mike Marquard" <mike_marquard hotmail.com> wrote in message news:fk7r0c$n3q$1 digitalmars.com...<joke type="obligatory"> Why would you want to start an array at 355,687,428,096,000?! </joke> Seriously, though, it can be a bit of a pain translating math stuff (which all uses 1-based indices) into C or D which is all 0-based. That said, I've used this feature in Visual Basic, and it left a very bad taste in my mouth. It really does make it easier to write, but an immense pain in the arse to work out if the code's actually working when you read it again. When they designed VB.NET, I believe the consensus was "this is a really stupid idea" and they excised it. -- DanielThanks 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.
Dec 18 2007
"Daniel Keep" <daniel.keep.lists gmail.com> wrote in message news:fk9qof$u4s$2 digitalmars.com...Why would you want to start an array at 355,687,428,096,000?!For fun and profit.When they designed VB.NET, I believe the consensus was "this is a really stupid idea" and they excised it.I think when they designed VB.Net, they basically said "how can we translate
Dec 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
Dec 18 2007
That's a quite... daring and improbable claim!Well I was only born in 1978 so I don't have any first hand experience and could be wrong but from what I've read Fortran, PL/1, Algol, Simula, and Pascal all had this feature. I don't know what Cobol and Lisp used but I'm guessing it wasn't base zero.
Dec 18 2007
Mike Marquard wrote:Well I was only born in 1978 so I don't have any first hand experience and could be wrong but from what I've read Fortran, PL/1, Algol, Simula, and Pascal all had this feature. I don't know what Cobol and Lisp used but I'm guessing it wasn't base zero.Well, I know about Pascal, and can't be bothered to look up the other languages you mentioned. But let's get this straight: a) You have not enumerated "just about every language that came before c became popular" here, even remotely. Although a few popular ones. b) Pascal (and probably all other languages you actually have enumerated) knows only static arrays! That means it was easy for the compiler writer to magically vanish the funny offsets away at compile time, which Walter won't be able to do with D's dynamic arrays. regards, frank
Dec 18 2007
0ffh wrote:That means it was easy for the compiler writer to magically vanish the funny offsets away at compile time, which Walter won't be able to do with D's dynamic arrays.Actually, come to think of it, you /could/ manipulate the pointer in the array structure (ugly hack, but... =), if it wasn't for the GC. regards, frank
Dec 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
Dec 18 2007
bearophile wrote:Mike Marquard: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. :-) --bbI 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):
Dec 18 2007
"bearophile" <bearophileHUGS lycos.com> wrote in message news:fk8ips$1jt$1 digitalmars.com...Mike Marquard: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.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.
Dec 18 2007
Jarrett Billingsley wrote:"bearophile" <bearophileHUGS lycos.com> wrote in message news:fk8ips$1jt$1 digitalmars.com...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? --bbMike Marquard: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.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.
Dec 18 2007
Bill Baxter wrote: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?Yes, I also think it would, therefore growing the array record by 50%. That doesn't sound very wholesome to me; I'd rather waive the feature than suffer the added memory and runtime overhead. We may hope that those who would compromise on their ressource usage will at some future point of time be able to do so by using a dedicated library array class or struct which implements this feature. regards, frank
Dec 18 2007
Why would there be a need for an extra memory in normall zero based arrays? I would think this would only affect people who decide to use the non-standard arrays to hold the lower bounds. And if bounds checking is turned off I would think it wouldn't make any difference. Then again I've never written a compiler so maybe I'm missing something.
Dec 18 2007
"Mike Marquard" <mike_marquard hotmail.com> wrote in message news:fk9hid$adm$1 digitalmars.com...Why would there be a need for an extra memory in normall zero based arrays? I would think this would only affect people who decide to use the non-standard arrays to hold the lower bounds. And if bounds checking is turned off I would think it wouldn't make any difference. Then again I've never written a compiler so maybe I'm missing something.You're right, this could be made to work by introducing another array type, a "bounded array" with explicit lower and upper bounds. But the thing is with that, and with the T[new] array type that Walter wants to introduce, we'd have static arrays, dynamic arrays, bounded arrays, T[new] arrays, and associative arrays :O That's an awfully large number of array types!
Dec 18 2007
Mike Marquard wrote:Why would there be a need for an extra memory in normall zero based arrays? I would think this would only affect people who decide to use the non-standard arrays to hold the lower bounds. And if bounds checking is turned off I would think it wouldn't make any difference. Then again I've never written a compiler so maybe I'm missing something.Indeed it would only concern those who use the non-standard arrays /if/ you are actually proposing to add a second array type which is inherently incompatible with the current native array type (yes, of course it would be possible to cast between them, if you are prepared to loose the extra information when casting). But that situation seems... ugly, sorry. I propose leaving the current array type as it is; you can have your own (albeit imperfect) array type with any kind if indexing you like right now - just write it. And sooner or later, you can probably even add the last bit of perfection you could want (just wait for D to evolve - Rome wasn't built in a day, or even a decade). regards, frank
Dec 18 2007