Skip to content

Instantly share code, notes, and snippets.

@jamshark70
Last active September 5, 2021 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamshark70/f5baf35794c72955034fb136e7be7f00 to your computer and use it in GitHub Desktop.
Save jamshark70/f5baf35794c72955034fb136e7be7f00 to your computer and use it in GitHub Desktop.
Sketch of an implementation of a two-row array, optimizing simple operations
ArrayPair : SequenceableCollection {
var <array1, <array2;
*new { |size = 0|
^super.newCopyArgs(Array(size), Array(size))
}
*newClear { |size = 0|
^super.newCopyArgs(Array.newClear(size), Array.newClear(size))
}
*newFromPair { |array1, array2|
if(array1.size == array2.size) {
^super.newCopyArgs(array1, array2)
} {
Error("ArrayPair: Arrays must be the same size").throw;
}
}
do { |function|
array1.do { |item1, i|
function.value(item1, array2[i], i)
}
}
collect { |function|
var new1 = Array(this.size), new2 = Array(this.size);
array1.do { |item1, i|
var result = function.value(item1, array2[i], i); // a pair
new1.add(result[0]);
new2.add(result[1]);
};
^this.class.newFromPair(new1, new2)
}
size { ^array1.size }
at { |i| ^[array1[i], array2[i]] }
at1 { |i| ^array1[i] }
at2 { |i| ^array2[i] }
put { |i, pair|
array1[i] = pair[0];
array2[i] = pair[1];
}
moveTo { |fromIndex, toIndex|
array1[toIndex] = array1[fromIndex];
array2[toIndex] = array2[fromIndex];
}
swap { |i, j|
array1.swap(i, j);
array2.swap(i, j);
}
// missing printOn and streamOn
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment