-
Notifications
You must be signed in to change notification settings - Fork 71
scatter
myArray.scatter(indices, defaultValue, conflictFunction, length)
##Arguments
indices array of indices in the resulting array
defaultValue optional argument indicating the value of elements not set by scatter
When not present, the default value is undefined
conflictFunction optional function to resolve conflicts, details below.
length optional argument indicating the length of the resulting array. If absent, the length is the same as the length of the original ParallelArray.
##Returns
A freshly minted ParallelArray A where each element A[i] is defined as
-
A[indices[i]] = this[i], when indices[i] is unique
-
A[indices[i]] = conflictFunction(valA, valB) when multiple elements are scattered to the same location. see below
-
defaultValue, when
iis not present inindicesarray
A conflict occurs when multiple elements are scattered to the same location. It results in a call to conflictFunction, which is an optional third argument to scatter. If conflictFunction is undefined, conflicts are non-deterministically resolved between the two values.
###Synopsis conflictFunction(valA, valB) ###Arguments
this the entire ParallelArray
valA, valB the two values that conflict
To ensure determinism it is the programmer’s responsibility to provide a conflictFunction that is associative and commutative since there is no guarantee in what order the conflicts will be resolved.
###Returns Value to place in result[indices[index]]
##Examples: // Do the identity function var source = new ParallelArray([1,2,3,4,5]); var indices = [0, 1, 2, 3, 4]; var identity = source.scatter(indices); // <1, 2, 3, 4, 5>
var reorder = source.scatter([4,0,3,1,2]); // <2, 4, 5, 3, 1>
// if there is a conflict use the max. use 33 as a default value.
var reorder = source.scatter([4,0,3,4,2], 33, function max(a, b) {return a>b?a:b; }); // <2, 33, 5, 3, 4>
// One way to do histogram
var source = new ParallelArray([1,2,2,4,2,4,5]);
var ones = source.map(function one(v) { return 1; });
var histogram = ones.scatter(source, 0, function plus(a,b) { return a+b;}, 6); // <0, 1, 3, 0, 2 1>