This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCluster.sc
233 lines (165 loc) · 4.71 KB
/
Cluster.sc
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Cluster Library
Copyright 2009-2012 Miguel Negrão.
Cluster Library: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Cluster Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Cluster Library. If not, see <http://www.gnu.org/licenses/>.
*/
// class to apply methods to all elements of the items array
// all items should be of type oclass
//To Do merge Cluster and ClusterBasic into just one, and subclass as appropriate.
Cluster{
var <items, <oclass;
prCollect{ |selector,args|
var return = items.collect(_.perform(*([selector]++args)));
if(return[0].class == this.oclass){
^this
}{
^Cluster(return)
}
}
prCollectSimple{ |selector|
var return = items.collect(_.perform(selector));
"prcollectsimple";
if(return[0].class == this.oclass){
^this
}{
^Cluster(return)
}
}
prCollectArray{ |selector,argArray|
var return = items.collect{ |item,i| item.perform(*([selector]++argArray[i]))};
"!!!prCollectArray";
//if((return[0].class == this.oclass) || (return[0] == nil)){
// ^this
//}{
^Cluster(return)
//}
}
//expand a set of arguments into an array of size items.size
prExpand{ |args|
("prExpand: "++args);
^Cluster.expandArray(args,this);
}
prExpandCollect{ |selector,args|
if(args.isNil){
"args nil";
^this.prCollectSimple(selector)
}{
"args not nil";
^this.prCollectArray(selector,this.prExpand(args))
}
}
prCheckSize{ |clusterobject|
if(clusterobject.items.size != items.size){
Error("Cluster sizes mismatch. ClusterObject with size "++clusterobject.items.size++
" vs ClusterObject with size "++items.size++" \n "++[clusterobject.items,items]).throw
}
}
//default implementation of collected classes.
// will work, but it's not possible to pass arguments by name. e.g. busnum: 3
doesNotUnderstand{ arg selector...args;
//("does not understand- base class "++this.class++" selector "++selector);
if(this.respondsTo(selector)){
^this.prExpandCollect(selector,args)
}{
^super.doesNotUnderstand(*([selector]++args));
}
}
respondsTo{ |selector|
^items[0].respondsTo(selector)
}
dopost{ "items:".postln; items.do(_.postln) }
printOn { arg stream;
stream << "Cluster" << items;
}
clApply{ |func|
^Cluster(items.collect{ |item| func.(item) })
}
clApplyF{ |func|
^Cluster(items.collect{ |item| { func.(item) } })
}
*applyN{ arg func ...clusterObjects;
var n = clusterObjects.collect{ |obj|
if(obj.class == Cluster){obj.items.size}{1}
}.maxItem;
//if object is not cluster it is duplicated to the maximum number of items and made into a cluster.
clusterObjects = clusterObjects.collect{ |obj|
if(obj.class != Cluster){Cluster(Array.fill(n,{obj}))}{obj}
};
^Cluster(n.collect{ |i|
func.(*clusterObjects.collect{ |clusterObj|
if(clusterObj.items.size == n){
clusterObj.items[i]
}{
clusterObj.items[0]
}
})
})
}
// Class methods wrapping
*fromArray{ |array,oclass|
if(array.collect{ |x| x.class}.as(Set).size>1){
Error("Cluster: "++array++" - Items should be all of the same class").throw
};
^super.newCopyArgs(array,array[0].class);
}
*new{ |array,oclass|
^this.fromArray(array,array[0].class);
}
*expandArray{ |array,clusterObject|
var recursF1,recursF2,finalF;
recursF1 = { |array,clusterobject|
array.collect{ |item|
if(item.isArray && item.isString.not){
recursF1.(item,clusterobject)
}{
if(item.class == Cluster){
clusterObject.prCheckSize(item);
item
}{
Cluster(clusterobject.items.collect{ item })
}
}
}
};
recursF2 = { |array,i|
array.collect{ |item|
if(item.isArray && item.isString.not){
"is array";
recursF2.(item,i)
}{
"is Cluster";
item.items[i]
}
}
};
finalF = { |array,clusterobject|
clusterobject.items.collect{ |adas,i|
("line "++i);
recursF2.(array,i)
}};
^finalF.(recursF1.(array,clusterObject), clusterObject)
}
*searchArrayForCluster{ |array|
array.postln;
array.do{ |item|
item.postln;
if(item.class.superclasses.includes(ClusterBasic)){
^item
}{
if(item.isArray && item.isString.not){
^this.searchArrayForCluster(item)
}
}
};
Error("arguments must have at least one Cluster class instance").throw
}
}