* Compute unique value information for an array * * IMPORTANT: Note that values are considered unique * if their string representations are unique. * * @param {Array} values * @param {Array|undefined} uniqueValues * Array of expected unique values. The uniqueValues property of the resulting
(values, uniqueValues)
| 389 | * @return {UniqueInfo} |
| 390 | */ |
| 391 | function getUniqueInfo(values, uniqueValues) { |
| 392 | // Initialize uniqueValues if not specified |
| 393 | if(uniqueValues === undefined || uniqueValues === null) { |
| 394 | uniqueValues = []; |
| 395 | } else { |
| 396 | // Shallow copy so append below doesn't alter input array |
| 397 | uniqueValues = uniqueValues.map(function(e) {return e;}); |
| 398 | } |
| 399 | |
| 400 | // Initialize Variables |
| 401 | var uniqueValueCounts = {}; |
| 402 | var uniqueValueInds = {}; |
| 403 | var inds = []; |
| 404 | |
| 405 | // Initialize uniqueValueCounts and |
| 406 | uniqueValues.forEach(function(uniqueVal, valInd) { |
| 407 | uniqueValueCounts[uniqueVal] = 0; |
| 408 | uniqueValueInds[uniqueVal] = valInd; |
| 409 | }); |
| 410 | |
| 411 | // Compute the necessary unique info in a single pass |
| 412 | for(var i = 0; i < values.length; i++) { |
| 413 | var item = values[i]; |
| 414 | var itemInd; |
| 415 | |
| 416 | if(uniqueValueCounts[item] === undefined) { |
| 417 | // This item has a previously unseen value |
| 418 | uniqueValueCounts[item] = 1; |
| 419 | itemInd = uniqueValues.push(item) - 1; |
| 420 | uniqueValueInds[item] = itemInd; |
| 421 | } else { |
| 422 | // Increment count for this item |
| 423 | uniqueValueCounts[item]++; |
| 424 | itemInd = uniqueValueInds[item]; |
| 425 | } |
| 426 | inds.push(itemInd); |
| 427 | } |
| 428 | |
| 429 | // Build UniqueInfo |
| 430 | var uniqueCounts = uniqueValues.map(function(v) { return uniqueValueCounts[v]; }); |
| 431 | |
| 432 | return { |
| 433 | uniqueValues: uniqueValues, |
| 434 | uniqueCounts: uniqueCounts, |
| 435 | inds: inds |
| 436 | }; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /** |
no outgoing calls
no test coverage detected
searching dependent graphs…