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

Re: [sc-dev] [commit]? indexOfEqual, find



I'd find the name find better, since it matches String.find.


Hi,

some improvements and additions to indexOfEqual and find,
and I'd like to move them all to SequenceableCollection.

// added offset argument, so search can start anywhere:

	indexOfEqual { arg item, offset=0;
		(this.lastIndex - offset).do ({ arg i;
			i = i + offset;
			if ( item == this[i], { ^i })
		});
		^nil
	}
// added method to get all indices:

	indicesOfEqual { |item|
		var indices, i=0, offset=0;
		while {
			i = this.indexOfEqual(item, offset);
			i.notNil
		}{
			indices = indices.add(i);
			offset = i + 1;
		}
		^indices
	}

// should replace current ArrayedCollection:find (faster)
// and move up to SequenceableCollection.
/* (
n = 10000;
a = Array.rand(n, 0, 10);
b = (0..5).wrapExtend(100);

{ a.find(b) }.bench.postln; // b.size 5: 0.3 secs // b.size 100: 0.36 secs { a.findEqual(b) }.bench.postln; // b.size 5: 0.015 secs // b.size 100: 0.021 secs
) */

	find { |sublist, offset=0|
		var subSize_1 = sublist.size - 1, first = sublist.first, index;
		(this.size - offset).do { |i|
			index = i + offset;
			if (this[index] == first) {
if (this.copyRange(index, index + subSize_1) == sublist) { ^index }
			};
		};
		^nil
	}

// added method, like String:findAll

	findAll { arg arr, offset=0;
		var indices, i=0;
		while {
			i = this.findEqual(arr, offset);
			i.notNil
		}{
			indices = indices.add(i);
			offset = i + 1;
		}
		^indices
	}

OK to commit?

best, adc
--
--
Alberto de Campo
Bergstrasse 59/33
A-8020 Graz, Austria
e-mail : decampo@xxxxxx
--
_______________________________________________
sc-dev mailing list
sc-dev@xxxxxxxxxxxxxxx
http://www.create.ucsb.edu/mailman/listinfo/sc-dev


--





.