arrayRemoveDuplicates-1a15bd09.js 5.6 KB

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