zhangnaisong
2023-08-05 24d66c8d82b628a06e93dbb1abfea2049b3d45ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import LinkedNode from './linked-node';
 
class LinkedList<T extends LinkedNode> {
  head: T | null;
  tail: T | null;
  length: number;
 
  constructor() {
    this.head = this.tail = null;
    this.length = 0;
  }
 
  append(...nodes: T[]): void {
    this.insertBefore(nodes[0], null);
    if (nodes.length > 1) {
      this.append.apply(this, nodes.slice(1));
    }
  }
 
  contains(node: T): boolean {
    let cur,
      next = this.iterator();
    while ((cur = next())) {
      if (cur === node) return true;
    }
    return false;
  }
 
  insertBefore(node: T | null, refNode: T | null): void {
    if (!node) return
    node.next = refNode;
    if (refNode != null) {
      node.prev = refNode.prev;
      if (refNode.prev != null) {
        refNode.prev.next = node;
      }
      refNode.prev = node;
      if (refNode === this.head) {
        this.head = node;
      }
    } else if (this.tail != null) {
      this.tail.next = node;
      node.prev = this.tail;
      this.tail = node;
    } else {
      node.prev = null;
      this.head = this.tail = node;
    }
    this.length += 1;
  }
 
  offset(target: T): number {
    let index = 0,
      cur = this.head;
    while (cur != null) {
      if (cur === target) return index;
      index += cur.length();
      cur = <T>cur.next;
    }
    return -1;
  }
 
  remove(node: T): void {
    if (!this.contains(node)) return;
    if (node.prev != null) node.prev.next = node.next;
    if (node.next != null) node.next.prev = node.prev;
    if (node === this.head) this.head = <T>node.next;
    if (node === this.tail) this.tail = <T>node.prev;
    this.length -= 1;
  }
 
  iterator(curNode: T | null = this.head): () => T | null {
    // TODO use yield when we can
    return function(): T | null {
      let ret = curNode;
      if (curNode != null) curNode = <T>curNode.next;
      return ret;
    };
  }
 
  find(index: number, inclusive: boolean = false): [T | null, number] {
    let cur,
      next = this.iterator();
    while ((cur = next())) {
      let length = cur.length();
      if (
        index < length ||
        (inclusive && index === length && (cur.next == null || cur.next.length() !== 0))
      ) {
        return [cur, index];
      }
      index -= length;
    }
    return [null, 0];
  }
 
  forEach(callback: (cur: T) => void): void {
    let cur,
      next = this.iterator();
    while ((cur = next())) {
      callback(cur);
    }
  }
 
  forEachAt(
    index: number,
    length: number,
    callback: (cur: T, offset: number, length: number) => void,
  ): void {
    if (length <= 0) return;
    let [startNode, offset] = this.find(index);
    let cur,
      curIndex = index - offset,
      next = this.iterator(startNode);
    while ((cur = next()) && curIndex < index + length) {
      let curLength = cur.length();
      if (index > curIndex) {
        callback(cur, index - curIndex, Math.min(length, curIndex + curLength - index));
      } else {
        callback(cur, 0, Math.min(curLength, index + length - curIndex));
      }
      curIndex += curLength;
    }
  }
 
  map(callback: (cur: T | null) => any): any[] {
    return this.reduce(function(memo: (T | null)[], cur: T | null) {
      memo.push(callback(cur));
      return memo;
    }, []);
  }
 
  reduce<M>(callback: (memo: M, cur: T) => M, memo: M): M {
    let cur,
      next = this.iterator();
    while ((cur = next())) {
      memo = callback(memo, cur);
    }
    return memo;
  }
}
 
export default LinkedList;