Skip to content

Commit

Permalink
alphabetical string array sorting (#468)
Browse files Browse the repository at this point in the history
* alphabetical string array sorting

* modify string array type detection

---------

Co-authored-by: Ge Wang <[email protected]>
  • Loading branch information
AndrewAday and gewang authored Oct 26, 2024
1 parent 9f4d1d6 commit d6b766b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
2 changes: 2 additions & 0 deletions VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ ChucK VERSIONS log
are distinguished from windows drive letters (e.g., "A:\")
- (fixed) Type.of() now reports more specific array type (e.g., `string[]`) instead of
generic `@array`
- (updated) calling .sort() on an array of strings will sort by string
(instead of Object pointers)
- (updated, internal) dynamic array types are now cached and reused, preventing potential
memory build-up across compilations

Expand Down
47 changes: 43 additions & 4 deletions src/core/chuck_oo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,16 +924,55 @@ static bool ck_compare_sint( t_CKUINT lhs, t_CKUINT rhs )
// sort by 2-norm / magnitude
return (t_CKINT)lhs < (t_CKINT)rhs;
}




//-----------------------------------------------------------------------------
// name: ck_compare_string()
// desc: compare function for sorting uints as chuck strings
//-----------------------------------------------------------------------------
static bool ck_compare_string( t_CKUINT lhs, t_CKUINT rhs )
{
const std::string& lhs_str = ((Chuck_String*)lhs)->str();
const std::string& rhs_str = ((Chuck_String*)rhs)->str();

return lhs_str.compare(rhs_str) < 0;
}




//-----------------------------------------------------------------------------
// name: sort()
// desc: sort the array in ascending order
//-----------------------------------------------------------------------------
void Chuck_ArrayInt::sort()
{
// if object references sort as unsigned
if( m_is_obj ) std::sort( m_vector.begin(), m_vector.end() );
// if not object references, sort as signed ints
else std::sort( m_vector.begin(), m_vector.end(), ck_compare_sint );
// check size
if( size() == 0 ) return;

// if object references | 1.5.3.5 (azaday) added
if( m_is_obj )
{
// if this is a string[]
if( this->type_ref->array_depth == 1 && this->type_ref->base_name == "string" )
{
// sort as string array
std::sort( m_vector.begin(), m_vector.end(), ck_compare_string );
}
else // not string object array
{
// sort object pointers as unsigned ints
std::sort( m_vector.begin(), m_vector.end() );
}
}
// if not object references
else
{
// sort as signed ints
std::sort( m_vector.begin(), m_vector.end(), ck_compare_sint );
}
}


Expand Down
9 changes: 9 additions & 0 deletions src/test/01-Basic/175-array-sort.ck
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ for( auto x : arrayVec3 ) { cherr <= x <= " "; } cherr <= IO.nl();
arrayVec4.sort();
// print
for( auto x : arrayVec4 ) { cherr <= x <= " "; } cherr <= IO.nl();

//-----------------------------------------------------------------------------
// string array: sort alphabetically
//-----------------------------------------------------------------------------
[ "ge", "andrew", "nick", ] @=> string arrString[];
// sort by ascending value
arrString.sort();
// print
for( auto x : arrString ) { cherr <= x <= " "; } cherr <= IO.nl();
15 changes: 8 additions & 7 deletions src/test/01-Basic/175-array-sort.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
-2 -1 0 1 2 3 4 5
5 4 3 2 1 0 -1 -2
-3.14159 1 2 3.14159
#(0,0) #(1,0) #(-1,-1) #(1,1)
%(1,3.14159) %(2,1.5708) %(2,3.14159)
@(0,0,0) @(-1,-1,-1) @(1,2,1)
@(0,0,0,0) @(-1,-1,-1,-1) @(1,3,1,-2)
-2 -1 0 1 2 3 4 5
5 4 3 2 1 0 -1 -2
-3.14159 1 2 3.14159
#(0,0) #(1,0) #(-1,-1) #(1,1)
%(1,3.14159) %(2,1.5708) %(2,3.14159)
@(0,0,0) @(-1,-1,-1) @(1,2,1)
@(0,0,0,0) @(-1,-1,-1,-1) @(1,3,1,-2)
andrew ge nick

0 comments on commit d6b766b

Please sign in to comment.