-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayUtil.ck
105 lines (97 loc) · 2.61 KB
/
ArrayUtil.ck
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class ArrayUtil {
// octaveShift (int notes[], int octaves)
// choose (int n, int octaves)
// sum (int list[])
// range (int a, int b)
// sort (int list[])
// max (int list[])
fun int[] octaveShift(int notes[], int oct){
notes.cap() => int N;
int r[N];
for (0 => int i; i < N; i++){
notes[i] + (12 * oct) => r[i];
}
return r;
}
fun int max(int a[]){
a[0] => int rv;
int tmp;
for (1 => int i; i < a.cap(); i++){
if (a[i] > tmp){
a[i] => tmp;
}
}
return rv;
}
// choose n elements randomly from a list
fun int[] choose(int scale[], int n, int oct){
int r[n];
int choice;
int octave;
for (0 => int i; i < n; i++){
Math.random2(0, scale.cap() - 1) => choice;
if (oct > 0){
Math.random2(0, oct) => octave;
}
scale[choice] + 12*octave => r[i];
}
return r;
}
fun int sum(int array[]){
0 => int acc;
for (0 => int i; i < array.cap(); i++){
array[i] +=> acc;
}
return acc;
}
// generate a range from a to b (inc, exc)
fun int[] range(int a, int b){
b - a => int M;
int r[M];
for (0 => int i; i < M; i++){
a + i => r[i];
}
r;
}
fun int[] sort(int l[]){
l.cap() => int N;
int r[N];
-1 => int smallest;
int temp;
int k;
int found_ind;
for (0 => int i; i < N; i++){
l[i] => temp;
i => found_ind;
for (i => int j; j < N; j++){
l[j] => k;
if ((k >= smallest) && (k <= temp)){
k => temp;
j => found_ind;
}
}
// swap out the smallest
l[found_ind] => smallest;
l[i] => l[found_ind];
smallest => l[i];
}
return l;
}
fun int[] morph(int a[], int step_choices[], int max){
step_choices.cap() => int Nsc;
a.cap() => int Ni;
int rv[Ni];
int choice;
for (0 => int i; i < Ni; i++){
a[i] + step_choices[Math.random2(0, Nsc-1)] => choice;
if (choice > max){
Math.random2(0, max) => rv[i];
} else if (choice < 0) {
Math.random2(0, max) => rv[i];
} else {
choice => rv[i];
}
}
return rv;
}
}