Recent

Author Topic: [SOLVED] low() and high() on multi-dimensional arrays  (Read 6413 times)

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
[SOLVED] low() and high() on multi-dimensional arrays
« on: July 29, 2016, 10:07:34 pm »
I'm trying to be really good and avoid any 'magic numbers' in my code  ;) so I've got this ugly construct for 2D array access:

Code: [Select]
for y := low(kMap) to high(kMap) do
  for x := low(kMap[y]) to high(kMap[y]) do

Will these expressions get evaluated over and over or can/will the compiler figure out the right values just once? I could stuff the values into variables beforehand but it seems like a lot of work

Also, unless I'm imagining things (and I have had a lot of screen-time today!), array access via
Code: [Select]
a[x][y] seems to work in the same way as
Code: [Select]
a[x, y] but I don't see it mentioned anywhere - is it syntactic sugar?

TIA
« Last Edit: July 30, 2016, 08:23:39 am by mrguzgog »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: low() and high() on multi-dimensional arrays
« Reply #1 on: July 30, 2016, 12:10:05 am »
The docs include this in describing static arrays:

"Type
APoints = array[1..100] of Array[1..3] of Real;
is equivalent to the declaration:
Type
APoints = array[1..100,1..3] of Real;

The functions High and Low return the high and low bounds of the leftmost index type of the array.
In the above case, this would be 100 and 1. You should use them whenever possible, since it improves
maintainability of your code. The use of both functions is just as efficient as using constants, because
they are evaluated at compile time."

(Clearly for dynamic arrays the compiler must insert code which evaluates High and Low at runtime - well Low() will always be zero in this case).

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: low() and high() on multi-dimensional arrays
« Reply #2 on: July 30, 2016, 01:12:54 am »
Will these expressions get evaluated over and over or can/will the compiler figure out the right values just once?
It already does. First, Low and High complexity is all O(1), the values are embedded in the array itself. Second, for loop bounds are calculated once on every start of the loop (i.e.: for loop in Pascal is not a syntactic sugar of while loop, they have different semantics).
Also, unless I'm imagining things (and I have had a lot of screen-time today!), array access via
Code: [Select]
a[x][y] seems to work in the same way as
Code: [Select]
a[x, y] but I don't see it mentioned anywhere - is it syntactic sugar?
Yes, since 1970.
« Last Edit: July 30, 2016, 01:15:14 am by Leledumbo »

Thaddy

  • Hero Member
  • *****
  • Posts: 14363
  • Sensorship about opinions does not belong here.
Re: low() and high() on multi-dimensional arrays
« Reply #3 on: July 30, 2016, 07:06:35 am »
(Clearly for dynamic arrays the compiler must insert code which evaluates High and Low at runtime - well Low() will always be zero in this case).

That has no/little effect on low and high itself. Dynamic arrays adapt the array descriptor - at a negative offset of the start of the array - when resized and Low() and high() take their information from the descriptor. That's basically a single read to set the variable for the compare operation or a compare to memory instead of compare to const where applicable (depends on processor family and optimization level) and technically O(1) too. Dynamic arrays are for all intend and purpose just as efficient as normal arrays. And indeed Low() is probably replaced by const zero anyway by the compiler. There is no code inserted for normal array operations.

« Last Edit: July 30, 2016, 07:22:12 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mrguzgog

  • Jr. Member
  • **
  • Posts: 71
Re: low() and high() on multi-dimensional arrays
« Reply #4 on: July 30, 2016, 08:23:14 am »
Thanks for the replies, that's answered my question.
Yes, since 1970.
New fangled stuff, I can't keep up with it all :D

 

TinyPortal © 2005-2018