chunked-array.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * from https://github.com/dsehnal/CIFTools.js
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. /**
  8. * A generic chunked array builder.
  9. *
  10. * When adding elements, the array growns by a specified number
  11. * of elements (either linear or exponential growth) and no copying
  12. * is done until ChunkedArray.compact is called.
  13. */
  14. interface ChunkedArray<T> {
  15. ctor: (size: number) => any,
  16. elementSize: number,
  17. linearGrowth: boolean,
  18. initialSize: number,
  19. allocatedSize: number,
  20. elementCount: number,
  21. currentSize: number,
  22. currentChunk: any,
  23. currentIndex: number,
  24. chunks: any[]
  25. }
  26. // TODO: better api, write tests
  27. namespace ChunkedArray {
  28. export function is(x: any): x is ChunkedArray<any> {
  29. return x.creator && x.chunkSize;
  30. }
  31. function allocateNext(array: ChunkedArray<any>) {
  32. let nextSize = !array.allocatedSize || array.linearGrowth
  33. ? array.initialSize * array.elementSize
  34. : Math.max(Math.ceil(0.61 * array.allocatedSize), 1);
  35. if (nextSize % array.elementSize !== 0) nextSize += nextSize % array.elementSize;
  36. array.currentSize = nextSize;
  37. array.currentIndex = 0;
  38. array.currentChunk = array.ctor(nextSize);
  39. array.allocatedSize += nextSize;
  40. array.chunks[array.chunks.length] = array.currentChunk;
  41. }
  42. export function add4<T>(array: ChunkedArray<T>, x: T, y: T, z: T, w: T) {
  43. if (array.currentIndex >= array.currentSize) allocateNext(array);
  44. const c = array.currentChunk;
  45. c[array.currentIndex++] = x;
  46. c[array.currentIndex++] = y;
  47. c[array.currentIndex++] = z;
  48. c[array.currentIndex++] = w;
  49. return array.elementCount++;
  50. }
  51. export function add3<T>(array: ChunkedArray<T>, x: T, y: T, z: T) {
  52. if (array.currentIndex >= array.currentSize) allocateNext(array);
  53. const c = array.currentChunk;
  54. c[array.currentIndex++] = x;
  55. c[array.currentIndex++] = y;
  56. c[array.currentIndex++] = z;
  57. return array.elementCount++;
  58. }
  59. export function add2<T>(array: ChunkedArray<T>, x: T, y: T) {
  60. if (array.currentIndex >= array.currentSize) allocateNext(array);
  61. const c = array.currentChunk;
  62. c[array.currentIndex++] = x;
  63. c[array.currentIndex++] = y;
  64. return array.elementCount++;
  65. }
  66. export function add<T>(array: ChunkedArray<T>, x: T) {
  67. if (array.currentIndex >= array.currentSize) allocateNext(array);
  68. array.currentChunk[array.currentIndex++] = x;
  69. return array.elementCount++;
  70. }
  71. export function compact<T>(array: ChunkedArray<T>): ArrayLike<T> {
  72. const { ctor, chunks, currentIndex } = array;
  73. if (!chunks.length) return ctor(0);
  74. if (chunks.length === 1 && currentIndex === array.allocatedSize) {
  75. return chunks[0];
  76. }
  77. const ret = ctor(array.elementSize * array.elementCount);
  78. let offset = 0;
  79. if (ret.buffer) {
  80. for (let i = 0, _i = chunks.length - 1; i < _i; i++) {
  81. ret.set(chunks[i], offset);
  82. offset += chunks[i].length;
  83. }
  84. } else {
  85. for (let i = 0, _i = chunks.length - 1; i < _i; i++) {
  86. const chunk = chunks[i];
  87. for (let j = 0, _j = chunk.length; j < _j; j++) ret[offset + j] = chunk[j];
  88. offset += chunk.length;
  89. }
  90. }
  91. const lastChunk = chunks[chunks.length - 1];
  92. if (ret.buffer && currentIndex >= array.currentSize) {
  93. ret.set(lastChunk, offset);
  94. } else {
  95. for (let j = 0, _j = lastChunk.length; j < _j; j++) ret[offset + j] = lastChunk[j];
  96. }
  97. return ret;
  98. }
  99. export function create<T>(ctor: (size: number) => any, elementSize: number, initialSize: number, linearGrowth: boolean): ChunkedArray<T> {
  100. return {
  101. ctor,
  102. elementSize,
  103. linearGrowth,
  104. initialSize,
  105. allocatedSize: 0,
  106. elementCount: 0,
  107. currentSize: 0,
  108. currentChunk: void 0,
  109. currentIndex: 0,
  110. chunks: []
  111. } as ChunkedArray<T>;
  112. }
  113. }
  114. export default ChunkedArray