discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Are ranges implemented iterators or vectors?

FH
Father Horton
Mon, Oct 11, 2021 2:13 AM

The manual says that they aren't vectors. If I do a for-loop over
[1:10000000], does it generate each index as needed or does it generate the
whole thing?

The manual says that they aren't vectors. If I do a for-loop over [1:10000000], does it generate each index as needed or does it generate the whole thing?
JB
Jordan Brown
Mon, Oct 11, 2021 3:00 PM

On 10/10/2021 7:13 PM, Father Horton wrote:

The manual says that they aren't vectors. If I do a for-loop over
[1:10000000], does it generate each index as needed or does it
generate the whole thing?

From a black-box perspective, it's clear that they are not immediately
converted into vectors:

echo([1:10]);

yields

ECHO: [1 : 1 : 10]

Looking at the gut agrees:  sure looks like an iterator.

https://github.com/openscad/openscad/blob/c4ecd2b1b73d08b8fffa5fec53669f85a3fc48f3/src/value.cc#L1277

RangeType::iterator& RangeType::iterator::operator++()
{
  val = range.begin_val + range.step_val * ++i_step;
  update_type();
  return *this;
}
On 10/10/2021 7:13 PM, Father Horton wrote: > The manual says that they aren't vectors. If I do a for-loop over > [1:10000000], does it generate each index as needed or does it > generate the whole thing? From a black-box perspective, it's clear that they are not immediately converted into vectors: echo([1:10]); yields ECHO: [1 : 1 : 10] Looking at the gut agrees:  sure looks like an iterator. https://github.com/openscad/openscad/blob/c4ecd2b1b73d08b8fffa5fec53669f85a3fc48f3/src/value.cc#L1277 RangeType::iterator& RangeType::iterator::operator++() { val = range.begin_val + range.step_val * ++i_step; update_type(); return *this; }
FH
Father Horton
Mon, Oct 11, 2021 3:06 PM

Thanks!

On Mon, Oct 11, 2021 at 10:00 AM Jordan Brown openscad@jordan.maileater.net
wrote:

On 10/10/2021 7:13 PM, Father Horton wrote:

The manual says that they aren't vectors. If I do a for-loop over
[1:10000000], does it generate each index as needed or does it generate the
whole thing?

From a black-box perspective, it's clear that they are not immediately
converted into vectors:

echo([1:10]);

yields

ECHO: [1 : 1 : 10]

Looking at the gut agrees:  sure looks like an iterator.

https://github.com/openscad/openscad/blob/c4ecd2b1b73d08b8fffa5fec53669f85a3fc48f3/src/value.cc#L1277

RangeType::iterator& RangeType::iterator::operator++()
{
val = range.begin_val + range.step_val * ++i_step;
update_type();
return *this;
}

Thanks! On Mon, Oct 11, 2021 at 10:00 AM Jordan Brown <openscad@jordan.maileater.net> wrote: > On 10/10/2021 7:13 PM, Father Horton wrote: > > The manual says that they aren't vectors. If I do a for-loop over > [1:10000000], does it generate each index as needed or does it generate the > whole thing? > > > From a black-box perspective, it's clear that they are not immediately > converted into vectors: > > echo([1:10]); > > yields > > ECHO: [1 : 1 : 10] > > Looking at the gut agrees: sure looks like an iterator. > > > https://github.com/openscad/openscad/blob/c4ecd2b1b73d08b8fffa5fec53669f85a3fc48f3/src/value.cc#L1277 > > RangeType::iterator& RangeType::iterator::operator++() > { > val = range.begin_val + range.step_val * ++i_step; > update_type(); > return *this; > } > > >