[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Sc-devel] maxIndex and minIndex?



Just wondering whether there is a method already for returning the index of the maximum or minimum element (rather than the value)? This is something I've often been recoding in algorithms and added my own extentions to SequenceableCollection as below; but perhaps there is some existing method I don't know the name of? Else can I add these? I have an interpolation constructor method as well that could go in as a convenience method rather than .series...

best,
Nick


+ SequenceableCollection {
*interp { arg size, start=0.0, end=1.0;
		var obj = this.new(size);
		
		if(size!=1,{
		size.do {|i|
		var t;
		
			t= i/(size-1);
			
			obj.add(start + (t*(end-start)));
		};
		},{obj.add(start);});
		
		^obj
}
maxIndex {

	var maxValue, maxElement, maxIndex;
			this.do { | elem,index|
				if (maxElement.isNil) {
					maxElement = elem;
					maxIndex = index;
				}{
					if (elem > maxElement) {
						maxElement = elem;
						maxIndex = index;
					}
				}		
			}
			^maxIndex;
}



minIndex {

	var minValue, minElement, minIndex;
			this.do { | elem,index|
				if (minElement.isNil) {
					minElement = elem;
					minIndex = index;
				}{
					if (elem < minElement) {
						minElement = elem;
						minIndex = index;
					}
				}		
			}
			^minIndex;
}


}