7f26fab392981531afc346e7828cc134c6d954e3b7156d9920e2db801cc7156c64516016d6bac67a8b786ba9ef5dcfecc829e5a4880666d86b9c6106c33d83 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import type LinkedNode from './linked-node.js';
  2. class LinkedList<T extends LinkedNode> {
  3. public head: T | null;
  4. public tail: T | null;
  5. public length: number;
  6. constructor() {
  7. this.head = null;
  8. this.tail = null;
  9. this.length = 0;
  10. }
  11. public append(...nodes: T[]): void {
  12. this.insertBefore(nodes[0], null);
  13. if (nodes.length > 1) {
  14. const rest = nodes.slice(1);
  15. this.append(...rest);
  16. }
  17. }
  18. public at(index: number): T | null {
  19. const next = this.iterator();
  20. let cur = next();
  21. while (cur && index > 0) {
  22. index -= 1;
  23. cur = next();
  24. }
  25. return cur;
  26. }
  27. public contains(node: T): boolean {
  28. const next = this.iterator();
  29. let cur = next();
  30. while (cur) {
  31. if (cur === node) {
  32. return true;
  33. }
  34. cur = next();
  35. }
  36. return false;
  37. }
  38. public indexOf(node: T): number {
  39. const next = this.iterator();
  40. let cur = next();
  41. let index = 0;
  42. while (cur) {
  43. if (cur === node) {
  44. return index;
  45. }
  46. index += 1;
  47. cur = next();
  48. }
  49. return -1;
  50. }
  51. public insertBefore(node: T | null, refNode: T | null): void {
  52. if (node == null) {
  53. return;
  54. }
  55. this.remove(node);
  56. node.next = refNode;
  57. if (refNode != null) {
  58. node.prev = refNode.prev;
  59. if (refNode.prev != null) {
  60. refNode.prev.next = node;
  61. }
  62. refNode.prev = node;
  63. if (refNode === this.head) {
  64. this.head = node;
  65. }
  66. } else if (this.tail != null) {
  67. this.tail.next = node;
  68. node.prev = this.tail;
  69. this.tail = node;
  70. } else {
  71. node.prev = null;
  72. this.head = this.tail = node;
  73. }
  74. this.length += 1;
  75. }
  76. public offset(target: T): number {
  77. let index = 0;
  78. let cur = this.head;
  79. while (cur != null) {
  80. if (cur === target) {
  81. return index;
  82. }
  83. index += cur.length();
  84. cur = cur.next as T;
  85. }
  86. return -1;
  87. }
  88. public remove(node: T): void {
  89. if (!this.contains(node)) {
  90. return;
  91. }
  92. if (node.prev != null) {
  93. node.prev.next = node.next;
  94. }
  95. if (node.next != null) {
  96. node.next.prev = node.prev;
  97. }
  98. if (node === this.head) {
  99. this.head = node.next as T;
  100. }
  101. if (node === this.tail) {
  102. this.tail = node.prev as T;
  103. }
  104. this.length -= 1;
  105. }
  106. public iterator(curNode: T | null = this.head): () => T | null {
  107. // TODO use yield when we can
  108. return (): T | null => {
  109. const ret = curNode;
  110. if (curNode != null) {
  111. curNode = curNode.next as T;
  112. }
  113. return ret;
  114. };
  115. }
  116. public find(index: number, inclusive = false): [T | null, number] {
  117. const next = this.iterator();
  118. let cur = next();
  119. while (cur) {
  120. const length = cur.length();
  121. if (
  122. index < length ||
  123. (inclusive &&
  124. index === length &&
  125. (cur.next == null || cur.next.length() !== 0))
  126. ) {
  127. return [cur, index];
  128. }
  129. index -= length;
  130. cur = next();
  131. }
  132. return [null, 0];
  133. }
  134. public forEach(callback: (cur: T) => void): void {
  135. const next = this.iterator();
  136. let cur = next();
  137. while (cur) {
  138. callback(cur);
  139. cur = next();
  140. }
  141. }
  142. public forEachAt(
  143. index: number,
  144. length: number,
  145. callback: (cur: T, offset: number, length: number) => void,
  146. ): void {
  147. if (length <= 0) {
  148. return;
  149. }
  150. const [startNode, offset] = this.find(index);
  151. let curIndex = index - offset;
  152. const next = this.iterator(startNode);
  153. let cur = next();
  154. while (cur && curIndex < index + length) {
  155. const curLength = cur.length();
  156. if (index > curIndex) {
  157. callback(
  158. cur,
  159. index - curIndex,
  160. Math.min(length, curIndex + curLength - index),
  161. );
  162. } else {
  163. callback(cur, 0, Math.min(curLength, index + length - curIndex));
  164. }
  165. curIndex += curLength;
  166. cur = next();
  167. }
  168. }
  169. public map(callback: (cur: T) => any): any[] {
  170. return this.reduce((memo: T[], cur: T) => {
  171. memo.push(callback(cur));
  172. return memo;
  173. }, []);
  174. }
  175. public reduce<M>(callback: (memo: M, cur: T) => M, memo: M): M {
  176. const next = this.iterator();
  177. let cur = next();
  178. while (cur) {
  179. memo = callback(memo, cur);
  180. cur = next();
  181. }
  182. return memo;
  183. }
  184. }
  185. export default LinkedList;