From: Ike Naar on 6 May 2010 04:59 In article <ln4oilxyd9.fsf(a)nuthaus.mib.org>, Keith Thompson <kst-u(a)mib.org> wrote: >A style point: comparisons to true and false are almost always >superfluous. Rather than > if (( xInd < data.size() ) != true ) >just write > if ( xInd <= data.size() ) It's not the same thing (``<='' should be ``>=''). > >As for the overall structure of your proposed replacement, you've >replaced three nested loops with one loop and three if statements. >Furthermore, the original version only tests zInd on most iterations; >your version tests zInd, yInd, and xInd on every iteration. > >The original problem is to traverse a 3-dimensional array. A triple >nested loop is the most obvious way to do that. There might be >some advantages in converting it to a single loop, but clarity >isn't one of them, at least in this case. > >-- >Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> >Nokia >"We must do something. This is something. Therefore, we must do this." > -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: bart.c on 6 May 2010 06:49 "Nathan Baker" <nathancbaker(a)gmail.com> wrote in message news:NamdnYqxi6pkH3_WnZ2dnUVZ_oydnZ2d(a)giganews.com... > "Keith Thompson" <kst-u(a)mib.org> wrote in message > news:ln4oilxyd9.fsf(a)nuthaus.mib.org... >> >> A style point: comparisons to true and false are almost always >> superfluous. Rather than >> if (( xInd < data.size() ) != true ) >> just write >> if ( xInd <= data.size() ) >> > > A true point. But I guess, for comparison sake, it is superfluous. :) > >> The original problem is to traverse a 3-dimensional array. A triple >> nested loop is the most obvious way to do that. There might be >> some advantages in converting it to a single loop, but clarity >> isn't one of them, at least in this case. >> > > I guess, the dragon that I am pointing at, is that CircuitCity does not > yet > sell 3-dinensional RAM. From the point of a view of program code, memory *is* 3-dimensional. That's why C's gone to the trouble of providing 3-dimensional arrays. > xyzMax = data[xInd][yInd].size() + data[xInd].size() + data.size(); > result = 0; > i = -1; > while ( i < xyzMax ) > { > ++i; > if ( data[i] = value ); > { > result = &data[i]; > i = xyzMax; > } > } > return result; > `--- > > ...a linear solution to a linear problem??? Except the memory layout might not be linear: each inner row might be allocated in a different place. -- Bartc
From: Keith Thompson on 6 May 2010 11:26 "bart.c" <bartc(a)freeuk.com> writes: > "Nathan Baker" <nathancbaker(a)gmail.com> wrote in message > news:NamdnYqxi6pkH3_WnZ2dnUVZ_oydnZ2d(a)giganews.com... >> "Keith Thompson" <kst-u(a)mib.org> wrote in message >> news:ln4oilxyd9.fsf(a)nuthaus.mib.org... [...] >>> The original problem is to traverse a 3-dimensional array. A triple >>> nested loop is the most obvious way to do that. There might be >>> some advantages in converting it to a single loop, but clarity >>> isn't one of them, at least in this case. >> >> I guess, the dragon that I am pointing at, is that CircuitCity does >> not yet sell 3-dinensional RAM. > > From the point of a view of program code, memory *is* 3-dimensional. That's > why C's gone to the trouble of providing 3-dimensional arrays. Exactly. Your programming isn't, and shouldn't be, limited by RAM layout. That's why we have programming languages. [code snipped] >> >> ...a linear solution to a linear problem??? It's a 3-dimensional problem. > Except the memory layout might not be linear: each inner row might be > allocated in a different place. No, C arrays are contiguous. -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Keith Thompson on 6 May 2010 11:27 ike(a)localhost.claranet.nl (Ike Naar) writes: > In article <ln4oilxyd9.fsf(a)nuthaus.mib.org>, > Keith Thompson <kst-u(a)mib.org> wrote: >>A style point: comparisons to true and false are almost always >>superfluous. Rather than >> if (( xInd < data.size() ) != true ) >>just write >> if ( xInd <= data.size() ) > > It's not the same thing (``<='' should be ``>=''). Argh! It was correct in my head. -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: bart.c on 6 May 2010 11:55
"Keith Thompson" <kst-u(a)mib.org> wrote in message news:lnr5lpvyp8.fsf(a)nuthaus.mib.org... > "bart.c" <bartc(a)freeuk.com> writes: >> "Nathan Baker" <nathancbaker(a)gmail.com> wrote in message >> news:NamdnYqxi6pkH3_WnZ2dnUVZ_oydnZ2d(a)giganews.com... >> Except the memory layout might not be linear: each inner row might be >> allocated in a different place. > > No, C arrays are contiguous. Flat arrays, yes. But it sounded like this 3D array consisted of pointers to pointers. -- Bartc |