arrayRemoveDuplicates-87160c89.js 4.8 KB

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