arrayRemoveDuplicates.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Check from "./Check.js";
  2. import defaultValue from "./defaultValue.js";
  3. import defined from "./defined.js";
  4. import CesiumMath from "./Math.js";
  5. var removeDuplicatesEpsilon = CesiumMath.EPSILON10;
  6. /**
  7. * Removes adjacent duplicate values in an array of values.
  8. *
  9. * @param {Array.<*>} [values] The array of values.
  10. * @param {Function} equalsEpsilon Function to compare values with an epsilon. Boolean equalsEpsilon(left, right, epsilon).
  11. * @param {Boolean} [wrapAround=false] Compare the last value in the array against the first value.
  12. * @returns {Array.<*>|undefined} A new array of values with no adjacent duplicate values or the input array if no duplicates were found.
  13. *
  14. * @example
  15. * // 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)]
  16. * var values = [
  17. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  18. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  19. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  20. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  21. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  22. * var nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon);
  23. *
  24. * @example
  25. * // Returns [(1.0, 1.0, 1.0), (2.0, 2.0, 2.0), (3.0, 3.0, 3.0)]
  26. * var values = [
  27. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  28. * new Cesium.Cartesian3(1.0, 1.0, 1.0),
  29. * new Cesium.Cartesian3(2.0, 2.0, 2.0),
  30. * new Cesium.Cartesian3(3.0, 3.0, 3.0),
  31. * new Cesium.Cartesian3(1.0, 1.0, 1.0)];
  32. * var nonDuplicatevalues = Cesium.PolylinePipeline.removeDuplicates(values, Cartesian3.equalsEpsilon, true);
  33. *
  34. * @private
  35. */
  36. function arrayRemoveDuplicates(values, equalsEpsilon, wrapAround) {
  37. //>>includeStart('debug', pragmas.debug);
  38. Check.defined("equalsEpsilon", equalsEpsilon);
  39. //>>includeEnd('debug');
  40. if (!defined(values)) {
  41. return undefined;
  42. }
  43. wrapAround = defaultValue(wrapAround, false);
  44. var length = values.length;
  45. if (length < 2) {
  46. return values;
  47. }
  48. var i;
  49. var v0;
  50. var v1;
  51. for (i = 1; i < length; ++i) {
  52. v0 = values[i - 1];
  53. v1 = values[i];
  54. if (equalsEpsilon(v0, v1, removeDuplicatesEpsilon)) {
  55. break;
  56. }
  57. }
  58. if (i === length) {
  59. if (
  60. wrapAround &&
  61. equalsEpsilon(
  62. values[0],
  63. values[values.length - 1],
  64. removeDuplicatesEpsilon
  65. )
  66. ) {
  67. return values.slice(1);
  68. }
  69. return values;
  70. }
  71. var cleanedvalues = values.slice(0, i);
  72. for (; i < length; ++i) {
  73. // v0 is set by either the previous loop, or the previous clean point.
  74. v1 = values[i];
  75. if (!equalsEpsilon(v0, v1, removeDuplicatesEpsilon)) {
  76. cleanedvalues.push(v1);
  77. v0 = v1;
  78. }
  79. }
  80. if (
  81. wrapAround &&
  82. cleanedvalues.length > 1 &&
  83. equalsEpsilon(
  84. cleanedvalues[0],
  85. cleanedvalues[cleanedvalues.length - 1],
  86. removeDuplicatesEpsilon
  87. )
  88. ) {
  89. cleanedvalues.shift();
  90. }
  91. return cleanedvalues;
  92. }
  93. export default arrayRemoveDuplicates;