arrayRemoveDuplicates-1af79ba4.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.95
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['exports', './RuntimeError-4f8ec8a2', './defaultValue-97284df2', './ComponentDatatype-4eeb6d9b'], (function (exports, RuntimeError, defaultValue, ComponentDatatype) { 'use strict';
  26. const removeDuplicatesEpsilon = ComponentDatatype.CesiumMath.EPSILON10;
  27. /**
  28. * Removes adjacent duplicate values in an array of values.
  29. *
  30. * @param {Array.<*>} [values] The array of values.
  31. * @param {Function} equalsEpsilon Function to compare values with an epsilon. Boolean equalsEpsilon(left, right, epsilon).
  32. * @param {Boolean} [wrapAround=false] Compare the last value in the array against the first value. If they are equal, the last value is removed.
  33. * @param {Array.<Number>} [removedIndices=undefined] Store the indices that correspond to the duplicate items removed from the array, if there were any.
  34. * @returns {Array.<*>|undefined} A new array of values with no adjacent duplicate values or the input array if no duplicates were found.
  35. *
  36. * @example
  37. * // 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)]
  38. * const values = [
  39. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  40. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  41. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  42. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  43. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  44. * const nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon);
  45. *
  46. * @example
  47. * // Returns [(1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0)]
  48. * const values = [
  49. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  50. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  51. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  52. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  53. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  54. * const nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon, true);
  55. *
  56. * @example
  57. * // Returns [(1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0)]
  58. * // removedIndices will be equal to [1, 3, 5]
  59. * const values = [
  60. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  61. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  62. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  63. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  64. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  65. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  66. * const nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon, true);
  67. * @private
  68. */
  69. function arrayRemoveDuplicates(
  70. values,
  71. equalsEpsilon,
  72. wrapAround,
  73. removedIndices
  74. ) {
  75. //>>includeStart('debug', pragmas.debug);
  76. RuntimeError.Check.defined("equalsEpsilon", equalsEpsilon);
  77. //>>includeEnd('debug');
  78. if (!defaultValue.defined(values)) {
  79. return undefined;
  80. }
  81. wrapAround = defaultValue.defaultValue(wrapAround, false);
  82. const storeRemovedIndices = defaultValue.defined(removedIndices);
  83. const length = values.length;
  84. if (length < 2) {
  85. return values;
  86. }
  87. let i;
  88. let v0 = values[0];
  89. let v1;
  90. // We only want to create a new array if there are duplicates in the array.
  91. // As such, cleanedValues is undefined until it encounters the first duplicate, if it exists.
  92. let cleanedValues;
  93. let lastCleanIndex = 0;
  94. // removedIndexLCI keeps track of where lastCleanIndex would be if it were sorted into the removedIndices array.
  95. // In case of arrays such as [A, B, C, ..., A, A, A], removedIndices will not be sorted properly without this.
  96. let removedIndexLCI = -1;
  97. for (i = 1; i < length; ++i) {
  98. v1 = values[i];
  99. if (equalsEpsilon(v0, v1, removeDuplicatesEpsilon)) {
  100. if (!defaultValue.defined(cleanedValues)) {
  101. cleanedValues = values.slice(0, i);
  102. lastCleanIndex = i - 1;
  103. removedIndexLCI = 0;
  104. }
  105. if (storeRemovedIndices) {
  106. removedIndices.push(i);
  107. }
  108. } else {
  109. if (defaultValue.defined(cleanedValues)) {
  110. cleanedValues.push(v1);
  111. lastCleanIndex = i;
  112. if (storeRemovedIndices) {
  113. removedIndexLCI = removedIndices.length;
  114. }
  115. }
  116. v0 = v1;
  117. }
  118. }
  119. if (
  120. wrapAround &&
  121. equalsEpsilon(values[0], values[length - 1], removeDuplicatesEpsilon)
  122. ) {
  123. if (storeRemovedIndices) {
  124. if (defaultValue.defined(cleanedValues)) {
  125. removedIndices.splice(removedIndexLCI, 0, lastCleanIndex);
  126. } else {
  127. removedIndices.push(length - 1);
  128. }
  129. }
  130. if (defaultValue.defined(cleanedValues)) {
  131. cleanedValues.length -= 1;
  132. } else {
  133. cleanedValues = values.slice(0, -1);
  134. }
  135. }
  136. return defaultValue.defined(cleanedValues) ? cleanedValues : values;
  137. }
  138. exports.arrayRemoveDuplicates = arrayRemoveDuplicates;
  139. }));
  140. //# sourceMappingURL=arrayRemoveDuplicates-1af79ba4.js.map