smartArray.d.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Defines an array and its length.
  3. * It can be helpful to group result from both Arrays and smart arrays in one structure.
  4. */
  5. export interface ISmartArrayLike<T> {
  6. /**
  7. * The data of the array.
  8. */
  9. data: Array<T>;
  10. /**
  11. * The active length of the array.
  12. */
  13. length: number;
  14. }
  15. /**
  16. * Defines an GC Friendly array where the backfield array do not shrink to prevent over allocations.
  17. */
  18. export declare class SmartArray<T> implements ISmartArrayLike<T> {
  19. /**
  20. * The full set of data from the array.
  21. */
  22. data: Array<T>;
  23. /**
  24. * The active length of the array.
  25. */
  26. length: number;
  27. protected _id: number;
  28. /**
  29. * Instantiates a Smart Array.
  30. * @param capacity defines the default capacity of the array.
  31. */
  32. constructor(capacity: number);
  33. /**
  34. * Pushes a value at the end of the active data.
  35. * @param value defines the object to push in the array.
  36. */
  37. push(value: T): void;
  38. /**
  39. * Iterates over the active data and apply the lambda to them.
  40. * @param func defines the action to apply on each value.
  41. */
  42. forEach(func: (content: T) => void): void;
  43. /**
  44. * Sorts the full sets of data.
  45. * @param compareFn defines the comparison function to apply.
  46. */
  47. sort(compareFn: (a: T, b: T) => number): void;
  48. /**
  49. * Resets the active data to an empty array.
  50. */
  51. reset(): void;
  52. /**
  53. * Releases all the data from the array as well as the array.
  54. */
  55. dispose(): void;
  56. /**
  57. * Concats the active data with a given array.
  58. * @param array defines the data to concatenate with.
  59. */
  60. concat(array: any): void;
  61. /**
  62. * Returns the position of a value in the active data.
  63. * @param value defines the value to find the index for
  64. * @returns the index if found in the active data otherwise -1
  65. */
  66. indexOf(value: T): number;
  67. /**
  68. * Returns whether an element is part of the active data.
  69. * @param value defines the value to look for
  70. * @returns true if found in the active data otherwise false
  71. */
  72. contains(value: T): boolean;
  73. private static _GlobalId;
  74. }
  75. /**
  76. * Defines an GC Friendly array where the backfield array do not shrink to prevent over allocations.
  77. * The data in this array can only be present once
  78. */
  79. export declare class SmartArrayNoDuplicate<T> extends SmartArray<T> {
  80. private _duplicateId;
  81. /**
  82. * Pushes a value at the end of the active data.
  83. * THIS DOES NOT PREVENT DUPPLICATE DATA
  84. * @param value defines the object to push in the array.
  85. */
  86. push(value: T): void;
  87. /**
  88. * Pushes a value at the end of the active data.
  89. * If the data is already present, it won t be added again
  90. * @param value defines the object to push in the array.
  91. * @returns true if added false if it was already present
  92. */
  93. pushNoDuplicate(value: T): boolean;
  94. /**
  95. * Resets the active data to an empty array.
  96. */
  97. reset(): void;
  98. /**
  99. * Concats the active data with a given array.
  100. * This ensures no duplicate will be present in the result.
  101. * @param array defines the data to concatenate with.
  102. */
  103. concatWithNoDuplicate(array: any): void;
  104. }