Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alphabetical string array sorting #468

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading