Debian Codegraph @

examples/array/array_sort.ck

raw
1 //-----------------------------------------------------------------------------
2 // name: array_sort.ck
3 // desc: sort the contents of arrays (various types)
4 // requires: chuck-1.5.0.9 or higher
5
6
7 //-----------------------------------------------------------------------------
8 // integer array
9 //-----------------------------------------------------------------------------
10 [ 5,2,-1,4,3,1,0,-2 ] @=> int arrayInt[];
11 // sort by ascending value
12 arrayInt.sort();
13 // print
14 for( auto x : arrayInt ) { cherr <= x <= " "; } cherr <= IO.nl();
15 // reverse for descending
16 arrayInt.reverse();
17 // print
18 for( auto x : arrayInt ) { cherr <= x <= " "; } cherr <= IO.nl();
19
20
21 //-----------------------------------------------------------------------------
22 // float array
23 //-----------------------------------------------------------------------------
24 [ 2.0, -pi, 1, pi ] @=> float arrayFloat[];
25 // sort by ascending value
26 arrayFloat.sort();
27 // print
28 for( auto x : arrayFloat ) { cherr <= x <= " "; } cherr <= IO.nl();
29
30
31 //-----------------------------------------------------------------------------
32 // complex array: sort by 2-norm/mag with phase as tie-breaker
33 //-----------------------------------------------------------------------------
34 [ #(-1,-1), #(1,1), #(0,0), #(1,0) ] @=> complex arrayComplex[];
35 // sort by ascending value
36 arrayComplex.sort();
37 // print
38 for( auto x : arrayComplex ) { cherr <= x <= " "; } cherr <= IO.nl();
39
40
41 //-----------------------------------------------------------------------------
42 // polar array: sort by magnitude with phase as tie-breaker
43 //-----------------------------------------------------------------------------
44 [ %(2,pi/2), %(1,pi), %(2,pi) ] @=> polar arrayPolar[];
45 // sort by ascending value
46 arrayPolar.sort();
47 // print
48 for( auto x : arrayPolar ) { cherr <= x <= " "; } cherr <= IO.nl();
49
50
51 //-----------------------------------------------------------------------------
52 // vec3 array: sort by magnitude with x then y then z as tie breakers
53 //-----------------------------------------------------------------------------
54 [ @(-1,-1,-1), @(1,2,1), @(0,0,0) ] @=> vec3 arrayVec3[];
55 // sort by ascending value
56 arrayVec3.sort();
57 // print
58 for( auto x : arrayVec3 ) { cherr <= x <= " "; } cherr <= IO.nl();
59
60
61 //-----------------------------------------------------------------------------
62 // vec4 array: sort by magnitude with x then y then z then w as tie breakers
63 //-----------------------------------------------------------------------------
64 [ @(-1,-1,-1,-1), @(1,3,1,-2), @(0,0,0,0) ] @=> vec4 arrayVec4[];
65 // sort by ascending value
66 arrayVec4.sort();
67 // print
68 for( auto x : arrayVec4 ) { cherr <= x <= " "; } cherr <= IO.nl();
69
70
71 //-----------------------------------------------------------------------------
72 // Object array: sort by reference/pointer value for now
73 //-----------------------------------------------------------------------------
74 SinOsc foos[3];
75 // shuffle
76 foos.shuffle();
77 // sort by ascending value
78 foos.sort();
79 // print
80 for( auto x : foos ) { <<< x >>>; }