arrayRemoveDuplicates-d2061e85.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. define(['exports', './Check-6ede7e26', './defaultValue-fe22d8c0', './Math-0a2ac845'], (function (exports, Check, defaultValue, Math) { 'use strict';
  2. const removeDuplicatesEpsilon = Math.CesiumMath.EPSILON10;
  3. /**
  4. * Removes adjacent duplicate values in an array of values.
  5. *
  6. * @param {any[]} [values] The array of values.
  7. * @param {Function} equalsEpsilon Function to compare values with an epsilon. Boolean equalsEpsilon(left, right, epsilon).
  8. * @param {boolean} [wrapAround=false] Compare the last value in the array against the first value. If they are equal, the last value is removed.
  9. * @param {number[]} [removedIndices=undefined] Store the indices that correspond to the duplicate items removed from the array, if there were any.
  10. * @returns {any[]|undefined} A new array of values with no adjacent duplicate values or the input array if no duplicates were found.
  11. *
  12. * @example
  13. * // Returns [(1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0), (1.0, 1.0, 1.0)]
  14. * const values = [
  15. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  16. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  17. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  18. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  19. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  20. * const nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon);
  21. *
  22. * @example
  23. * // Returns [(1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0)]
  24. * const values = [
  25. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  26. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  27. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  28. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  29. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  30. * const nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon, true);
  31. *
  32. * @example
  33. * // Returns [(1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0)]
  34. * // removedIndices will be equal to [1, 3, 5]
  35. * const values = [
  36. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  37. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  38. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  39. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  40. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  41. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  42. * const nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon, true);
  43. * @private
  44. */
  45. function arrayRemoveDuplicates(
  46. values,
  47. equalsEpsilon,
  48. wrapAround,
  49. removedIndices
  50. ) {
  51. //>>includeStart('debug', pragmas.debug);
  52. Check.Check.defined("equalsEpsilon", equalsEpsilon);
  53. //>>includeEnd('debug');
  54. if (!defaultValue.defined(values)) {
  55. return undefined;
  56. }
  57. wrapAround = defaultValue.defaultValue(wrapAround, false);
  58. const storeRemovedIndices = defaultValue.defined(removedIndices);
  59. const length = values.length;
  60. if (length < 2) {
  61. return values;
  62. }
  63. let i;
  64. let v0 = values[0];
  65. let v1;
  66. // We only want to create a new array if there are duplicates in the array.
  67. // As such, cleanedValues is undefined until it encounters the first duplicate, if it exists.
  68. let cleanedValues;
  69. let lastCleanIndex = 0;
  70. // removedIndexLCI keeps track of where lastCleanIndex would be if it were sorted into the removedIndices array.
  71. // In case of arrays such as [A, B, C, ..., A, A, A], removedIndices will not be sorted properly without this.
  72. let removedIndexLCI = -1;
  73. for (i = 1; i < length; ++i) {
  74. v1 = values[i];
  75. if (equalsEpsilon(v0, v1, removeDuplicatesEpsilon)) {
  76. if (!defaultValue.defined(cleanedValues)) {
  77. cleanedValues = values.slice(0, i);
  78. lastCleanIndex = i - 1;
  79. removedIndexLCI = 0;
  80. }
  81. if (storeRemovedIndices) {
  82. removedIndices.push(i);
  83. }
  84. } else {
  85. if (defaultValue.defined(cleanedValues)) {
  86. cleanedValues.push(v1);
  87. lastCleanIndex = i;
  88. if (storeRemovedIndices) {
  89. removedIndexLCI = removedIndices.length;
  90. }
  91. }
  92. v0 = v1;
  93. }
  94. }
  95. if (
  96. wrapAround &&
  97. equalsEpsilon(values[0], values[length - 1], removeDuplicatesEpsilon)
  98. ) {
  99. if (storeRemovedIndices) {
  100. if (defaultValue.defined(cleanedValues)) {
  101. removedIndices.splice(removedIndexLCI, 0, lastCleanIndex);
  102. } else {
  103. removedIndices.push(length - 1);
  104. }
  105. }
  106. if (defaultValue.defined(cleanedValues)) {
  107. cleanedValues.length -= 1;
  108. } else {
  109. cleanedValues = values.slice(0, -1);
  110. }
  111. }
  112. return defaultValue.defined(cleanedValues) ? cleanedValues : values;
  113. }
  114. exports.arrayRemoveDuplicates = arrayRemoveDuplicates;
  115. }));