arrayRemoveDuplicates.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 (wrapAround && equalsEpsilon(values[0], values[values.length - 1], removeDuplicatesEpsilon)) {
  60. return values.slice(1);
  61. }
  62. return values;
  63. }
  64. var cleanedvalues = values.slice(0, i);
  65. for (; i < length; ++i) {
  66. // v0 is set by either the previous loop, or the previous clean point.
  67. v1 = values[i];
  68. if (!equalsEpsilon(v0, v1, removeDuplicatesEpsilon)) {
  69. cleanedvalues.push(v1);
  70. v0 = v1;
  71. }
  72. }
  73. if (wrapAround && cleanedvalues.length > 1 && equalsEpsilon(cleanedvalues[0], cleanedvalues[cleanedvalues.length - 1], removeDuplicatesEpsilon)) {
  74. cleanedvalues.shift();
  75. }
  76. return cleanedvalues;
  77. }
  78. export default arrayRemoveDuplicates;