File: array_sort.ck

package info (click to toggle)
chuck 1.5.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,056 kB
  • sloc: cpp: 123,473; ansic: 35,893; javascript: 2,111; yacc: 609; makefile: 457; python: 174; perl: 86
file content (80 lines) | stat: -rw-r--r-- 3,000 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//-----------------------------------------------------------------------------
// name: array_sort.ck
// desc: sort the contents of arrays (various types)
// requires: chuck-1.5.0.9 or higher


//-----------------------------------------------------------------------------
// integer array
//-----------------------------------------------------------------------------
[ 5,2,-1,4,3,1,0,-2 ] @=> int arrayInt[];
// sort by ascending value
arrayInt.sort();
// print
for( auto x : arrayInt ) { cherr <= x <= " "; } cherr <= IO.nl();
// reverse for descending
arrayInt.reverse();
// print
for( auto x : arrayInt ) { cherr <= x <= " "; } cherr <= IO.nl();


//-----------------------------------------------------------------------------
// float array
//-----------------------------------------------------------------------------
[ 2.0, -pi, 1, pi ] @=> float arrayFloat[];
// sort by ascending value
arrayFloat.sort();
// print
for( auto x : arrayFloat ) { cherr <= x <= " "; } cherr <= IO.nl();


//-----------------------------------------------------------------------------
// complex array: sort by 2-norm/mag with phase as tie-breaker
//-----------------------------------------------------------------------------
[ #(-1,-1), #(1,1), #(0,0), #(1,0) ] @=> complex arrayComplex[];
// sort by ascending value
arrayComplex.sort();
// print
for( auto x : arrayComplex ) { cherr <= x <= " "; } cherr <= IO.nl();


//-----------------------------------------------------------------------------
// polar array: sort by magnitude with phase as tie-breaker
//-----------------------------------------------------------------------------
[ %(2,pi/2), %(1,pi), %(2,pi) ] @=> polar arrayPolar[];
// sort by ascending value
arrayPolar.sort();
// print
for( auto x : arrayPolar ) { cherr <= x <= " "; } cherr <= IO.nl();


//-----------------------------------------------------------------------------
// vec3 array: sort by magnitude with x then y then z as tie breakers
//-----------------------------------------------------------------------------
[ @(-1,-1,-1), @(1,2,1), @(0,0,0) ] @=> vec3 arrayVec3[];
// sort by ascending value
arrayVec3.sort();
// print
for( auto x : arrayVec3 ) { cherr <= x <= " "; } cherr <= IO.nl();


//-----------------------------------------------------------------------------
// vec4 array: sort by magnitude with x then y then z then w as tie breakers
//-----------------------------------------------------------------------------
[ @(-1,-1,-1,-1), @(1,3,1,-2), @(0,0,0,0) ] @=> vec4 arrayVec4[];
// sort by ascending value
arrayVec4.sort();
// print
for( auto x : arrayVec4 ) { cherr <= x <= " "; } cherr <= IO.nl();


//-----------------------------------------------------------------------------
// Object array: sort by reference/pointer value for now
//-----------------------------------------------------------------------------
SinOsc foos[3];
// shuffle
foos.shuffle();
// sort by ascending value
foos.sort();
// print
for( auto x : foos ) { <<< x >>>; }