Skip to content

Commit 4cf058c

Browse files
committed
fix .zero() for multi-dimensional arrays
1 parent b48257a commit 4cf058c

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

VERSIONS

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
ChucK VERSIONS log
33
------------------
44

5+
1.5.5.1
6+
=======
7+
(fixed) array.zero() now works properly with multi-dimensional arrays; previously
8+
it would zero out the top-level array elements (which are actually the next
9+
level of inner arrays, since multi-dimensional array in chuck are arrays of
10+
arrays of arrays...)
11+
(updated) examples/array/array_zero.ck now includes example of using .zero() with
12+
multi-dimensional arrays (thanks to @terryzfeng for reporting this issue)
13+
14+
515
1.5.5.0 (March 2025)
616
=======
717
(added) ChuMP: ChucK Manager of Packages! v0.0.1 (alpha)

examples/array/array_zero.ck

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// .zero() zeros out the contents AND keeps size unchanged
22
// version: need chuck-1.5.0.0 or higher
3+
//
4+
// NOTE: using .zero() with multidimensional arrays
5+
// requires chuck-1.5.5.1 or higher
36

47
// an array
58
[1,3,4] @=> int array[];
@@ -12,3 +15,23 @@ array.zero();
1215

1316
// now should print 0 3
1417
<<< array[1], array.size() >>>;
18+
19+
// a multi-dimensional array
20+
[ [1,2], [3,4] ] @=> int array2[][];
21+
22+
// NOTE: using .zero() with multidimensional arrays
23+
// requires chuck-1.5.5.1 or higher
24+
array2.zero();
25+
26+
// iterate over array2
27+
for( int inner[] : array2 )
28+
{
29+
// iterate over each inner array
30+
for( int x: inner )
31+
{
32+
// print each element within
33+
cherr <= x <= " ";
34+
}
35+
}
36+
// print newline
37+
cherr <= IO.nl();

src/core/chuck_oo.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,48 @@ void Chuck_ArrayInt::clear( )
10271027

10281028

10291029

1030+
//-----------------------------------------------------------------------------
1031+
// name: zero()
1032+
// desc: zero out an int/object array
1033+
// 1.5.5.1 (ge) handle multidimensional arrays
1034+
//-----------------------------------------------------------------------------
1035+
void Chuck_ArrayInt::zero()
1036+
{
1037+
// if primitive int
1038+
if( this->m_is_obj == FALSE )
1039+
{
1040+
// zero out
1041+
this->zero( 0, m_vector.size() );
1042+
// done
1043+
return;
1044+
}
1045+
1046+
// array contains object;
1047+
// check if the objects are themselves arrays | 1.5.5.1 (ge)
1048+
if( this->type_ref->array_depth > 0 )
1049+
{
1050+
// iterate over elements
1051+
for( t_CKUINT i = 0; i < m_vector.size(); i++ )
1052+
{
1053+
// if NULL, move to next
1054+
if( !m_vector[i] ) continue;
1055+
1056+
// cast to base array pointer
1057+
Chuck_Array * array = (Chuck_Array *)m_vector[i];
1058+
// call the array's zero()
1059+
array->zero();
1060+
}
1061+
}
1062+
else // array contains non-array object references
1063+
{
1064+
// zero out
1065+
this->zero( 0, m_vector.size() );
1066+
}
1067+
}
1068+
1069+
1070+
1071+
10301072
//-----------------------------------------------------------------------------
10311073
// name: set_capacity()
10321074
// desc: ...

src/core/chuck_oo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ struct Chuck_ArrayInt : public Chuck_Array
315315
// zero out the array by range [start,end)
316316
virtual void zero( t_CKUINT start, t_CKUINT end );
317317
// zero out all elements
318-
virtual void zero() { this->zero(0, m_vector.size()); }
318+
virtual void zero();
319319

320320
// pop the back of the array
321321
virtual t_CKINT pop_back();

src/test/01-Basic/119.ck

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ array[1] => int three;
77
// zero out the array, leaving size as is
88
array.zero();
99

10-
// check: size should be 3 but array[1] should now be zero
11-
if( three == 3 && array.size() == 3 && array[1] == 0 )
12-
<<< "success" >>>;
10+
// print (should be three 3s)
11+
<<< three, array.size(), array[1] >>>;
12+
13+
// using .zero() with multidimensional arrays
14+
// NOTE: requires chuck-1.5.5.1 or higher
15+
[ [1,2], [3,4] ] @=> int array2[][];
16+
17+
// zero out the multidimensional array, leaving size as is
18+
array2.zero();
19+
20+
// iterate over array2
21+
for( int inner[] : array2 )
22+
{
23+
// iterate over each inner array
24+
for( int x: inner )
25+
{
26+
// print each element within
27+
cherr <= x <= " ";
28+
}
29+
}
30+
// print newline
31+
cherr <= IO.nl();
32+
33+

0 commit comments

Comments
 (0)