Skip to content

Commit d7926e9

Browse files
♻️ refactor: Use @data-structure-algebra/doubly-linked-list.
1 parent 91b17a2 commit d7926e9

File tree

7 files changed

+38
-38
lines changed

7 files changed

+38
-38
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@
7575
"test:module": "IMPORT_MAP_PATH=test/import-maps/dist/index.module.json npm run test-cmd",
7676
"test:src": "IMPORT_MAP_PATH=test/import-maps/src/index.json npm run test-cmd"
7777
},
78-
"dependencies": {},
78+
"dependencies": {
79+
"@data-structure-algebra/doubly-linked-list": "^1.0.0"
80+
},
7981
"devDependencies": {
8082
"@babel/core": "7.20.2",
8183
"@babel/plugin-transform-destructuring": "7.20.2",

src/DoublyLinkedList.js

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import Node from './Node.js';
1+
import {
2+
Node,
3+
_concat,
4+
_erase,
5+
_extend,
6+
_remove,
7+
_insertAfter,
8+
_insertBefore,
9+
} from '@data-structure-algebra/doubly-linked-list';
10+
211
import Iterator from './Iterator.js';
312
import ReverseIterator from './ReverseIterator.js';
413

@@ -9,17 +18,15 @@ import ReverseIterator from './ReverseIterator.js';
918
*/
1019
export default function DoublyLinkedList() {
1120
this.front = new Node(null, null, null);
12-
this.back = new Node(this.front, null, null);
21+
this.back = new Node(null, this.front, null);
1322
this.front.next = this.back;
1423
this.length = 0;
1524
}
1625

1726
DoublyLinkedList.prototype.insertAfter = function (iterator, value) {
1827
const prev = iterator.current;
1928

20-
const node = new Node(prev, prev.next, value);
21-
prev.next.prev = node;
22-
prev.next = node;
29+
const node = _insertAfter(prev, value);
2330

2431
++this.length;
2532
return this.iterator(node);
@@ -28,9 +35,7 @@ DoublyLinkedList.prototype.insertAfter = function (iterator, value) {
2835
DoublyLinkedList.prototype.insertBefore = function (iterator, value) {
2936
const next = iterator.current;
3037

31-
const node = new Node(next.prev, next, value);
32-
next.prev.next = node;
33-
next.prev = node;
38+
const node = _insertBefore(next, value);
3439

3540
++this.length;
3641
return this.iterator(node);
@@ -47,8 +52,7 @@ DoublyLinkedList.prototype.push = function (value) {
4752
DoublyLinkedList.prototype.erase = function (iterator) {
4853
const node = iterator.current;
4954

50-
node.prev.next = node.next;
51-
node.next.prev = node.prev;
55+
_remove(node);
5256

5357
--this.length;
5458
return this.iterator(node.next);
@@ -57,8 +61,7 @@ DoublyLinkedList.prototype.erase = function (iterator) {
5761
DoublyLinkedList.prototype.rerase = function (iterator) {
5862
const node = iterator.current;
5963

60-
node.next.prev = node.prev;
61-
node.prev.next = node.next;
64+
_remove(node);
6265

6366
--this.length;
6467
return this.iterator(node.prev);
@@ -68,14 +71,13 @@ DoublyLinkedList.prototype.eraserange = function (first, last) {
6871
const firstnode = first.current;
6972
const lastnode = last.current;
7073

71-
lastnode.prev = firstnode.prev;
72-
firstnode.prev.next = lastnode;
74+
_erase(firstnode, lastnode);
7375

74-
const it = first.copy();
76+
let x = first.current;
7577

76-
while (it.current !== lastnode) {
78+
while (x !== lastnode) {
7779
--this.length;
78-
it.next();
80+
x = x.next;
7981
}
8082

8183
return last.copy();
@@ -85,14 +87,13 @@ DoublyLinkedList.prototype.reraserange = function (first, last) {
8587
const firstnode = first.current;
8688
const lastnode = last.current;
8789

88-
lastnode.next = firstnode.next;
89-
firstnode.next.prev = lastnode;
90+
_concat(lastnode, firstnode.next);
9091

91-
const it = first.copy();
92+
let x = first.current;
9293

93-
while (it.current !== lastnode) {
94+
while (x !== lastnode) {
9495
--this.length;
95-
it.next();
96+
x = x.prev;
9697
}
9798

9899
return last.copy();
@@ -102,9 +103,7 @@ DoublyLinkedList.prototype.shift = function () {
102103
if (this.length === 0) return null;
103104

104105
const node = this.front.next;
105-
106-
this.front.next = node.next;
107-
node.next.prev = this.front;
106+
_remove(node);
108107

109108
--this.length;
110109

@@ -115,18 +114,15 @@ DoublyLinkedList.prototype.pop = function () {
115114
if (this.length === 0) return null;
116115

117116
const node = this.back.prev;
118-
119-
this.back.prev = node.prev;
120-
node.prev.next = this.back;
117+
_remove(node);
121118

122119
--this.length;
123120

124121
return node.value;
125122
};
126123

127124
DoublyLinkedList.prototype.clear = function () {
128-
this.front.next = this.back;
129-
this.back.prev = this.front;
125+
_concat(this.front, this.back);
130126
this.length = 0;
131127
return this;
132128
};
@@ -157,7 +153,8 @@ DoublyLinkedList.prototype.rend = function () {
157153

158154
const from = (iterable) => {
159155
const list = new DoublyLinkedList();
160-
for (const value of iterable) list.push(value);
156+
const last = _extend(list.front, iterable);
157+
_concat(last, list.back);
161158
return list;
162159
};
163160

src/Node.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import DoublyLinkedList from './DoublyLinkedList.js';
22

33
export {default as DoublyLinkedList} from './DoublyLinkedList.js';
44
export {default as Iterator} from './Iterator.js';
5-
export {default as Node} from './Node.js';
65
export {default as ReverseIterator} from './ReverseIterator.js';
6+
77
export const from = DoublyLinkedList.from;
8+
export const Node = DoublyLinkedList.Node;
File renamed without changes.
File renamed without changes.

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,11 @@
11511151
dependencies:
11521152
"@jridgewell/trace-mapping" "0.3.9"
11531153

1154+
"@data-structure-algebra/doubly-linked-list@^1.0.0":
1155+
version "1.0.0"
1156+
resolved "https://registry.yarnpkg.com/@data-structure-algebra/doubly-linked-list/-/doubly-linked-list-1.0.0.tgz#1bfbb25136fd364ba6c101833fdf199a67cdec51"
1157+
integrity sha512-ZC+tPhRcwsSUX/FcO4mBmSzMgFL4yLBYrTSOuHyAyTEcmLv7GVX1rt5EJAFXzMsKp941zGiWqHi5amlZhnYlNQ==
1158+
11541159
"@eslint/eslintrc@^1.3.0", "@eslint/eslintrc@^1.3.3":
11551160
version "1.3.3"
11561161
resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-1.3.3.tgz#2b044ab39fdfa75b4688184f9e573ce3c5b0ff95"

0 commit comments

Comments
 (0)