-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve and. simplify syncnodes introducing DoubleLinkedList
- Loading branch information
1 parent
bdcac65
commit 7c7f355
Showing
17 changed files
with
695 additions
and
281 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { DoubleLinkedList } from './DoubleLinkedList' | ||
|
||
describe('DoubleLinkedList', () => { | ||
it('should push, remove, and clear values correctly', () => { | ||
let ll= new DoubleLinkedList(); | ||
|
||
ll.push(1); | ||
ll.push(2); | ||
ll.push(3); | ||
|
||
expect([...ll]).toEqual([1, 2, 3]) | ||
expect(ll.head).toBe(1) | ||
expect(ll.getNextValueOf(2)).toBe(3) | ||
expect(ll.getPreviousValueOf(2)).toBe(1) | ||
expect(ll.tail).toBe(3) | ||
expect(ll.size).toBe(3) | ||
|
||
ll.remove(2) | ||
|
||
expect([...ll]).toEqual([1, 3]) | ||
expect(ll.size).toBe(2) | ||
expect(ll.has(2)).toBeFalsy() | ||
|
||
ll.remove(3) | ||
|
||
expect([...ll]).toEqual([1]) | ||
expect(ll.size).toBe(1) | ||
expect(ll.has(3)).toBeFalsy() | ||
|
||
ll.remove(1) | ||
|
||
expect([...ll]).toEqual([]) | ||
expect(ll.size).toBe(0) | ||
|
||
ll = DoubleLinkedList.fromArray([1, 2, 3]) | ||
|
||
expect(ll.size).toBe(3) | ||
|
||
ll.clear() | ||
|
||
expect([...ll]).toEqual([]) | ||
expect(ll.size).toBe(0) | ||
}) | ||
|
||
it('should insert value before', () => { | ||
const ll= new DoubleLinkedList(); | ||
|
||
ll.push(5); | ||
|
||
expect(ll.head).toBe(ll.tail) | ||
expect([...ll]).toEqual([5]) | ||
|
||
ll.insertValueBefore(4, 5) | ||
|
||
expect([...ll]).toEqual([4, 5]) | ||
|
||
expect(ll.head).toBe(4) | ||
expect(ll.getNextValueOf(4)).toBe(5) | ||
expect(ll.getPreviousValueOf(5)).toBe(4) | ||
expect(ll.tail).toBe(5) | ||
expect(ll.size).toBe(2) | ||
}) | ||
|
||
it('should insert value after', () => { | ||
const ll= new DoubleLinkedList(); | ||
|
||
ll.push(5); | ||
|
||
expect(ll.head).toBe(ll.tail) | ||
expect([...ll]).toEqual([5]) | ||
|
||
ll.insertValueAfter(6, 5) | ||
|
||
expect([...ll]).toEqual([5, 6]) | ||
|
||
expect(ll.head).toBe(5) | ||
expect(ll.getNextValueOf(5)).toBe(6) | ||
expect(ll.getPreviousValueOf(6)).toBe(5) | ||
expect(ll.tail).toBe(6) | ||
expect(ll.size).toBe(2) | ||
}) | ||
|
||
it('should reverse list', () => { | ||
const ll= DoubleLinkedList.fromArray([1, 2, 3, 4, 5]); | ||
|
||
expect([...ll]).toEqual([1,2,3,4,5]) | ||
|
||
let c = ll.head; | ||
|
||
[5, 4, 3, 2, 1].forEach(n => { | ||
c && n !== c && ll.insertValueBefore(n, c); | ||
}) | ||
|
||
expect([...ll]).toEqual([ | ||
5, | ||
4, | ||
3, | ||
2, | ||
1 | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
interface DLLElement<T> { | ||
value: T | ||
next: DLLElement<T> | null | ||
prev: DLLElement<T> | null | ||
} | ||
|
||
export class DoubleLinkedList<T> { | ||
#head: DLLElement<T> | null = null | ||
#tail: DLLElement<T> | null = null | ||
#map = new Map(); | ||
|
||
*[Symbol.iterator]() { | ||
let current = this.#head | ||
|
||
while (current) { | ||
yield current.value | ||
current = current.next | ||
} | ||
} | ||
|
||
static fromArray<T>(arr: Array<T>) { | ||
const list = new DoubleLinkedList<T>() | ||
|
||
for (const value of arr) { | ||
list.push(value) | ||
} | ||
|
||
return list | ||
} | ||
|
||
get size() { | ||
return this.#map.size | ||
} | ||
|
||
get head(): T | null { | ||
return this.#head?.value ?? null | ||
} | ||
|
||
get tail(): T | null { | ||
return this.#tail?.value ?? null | ||
} | ||
|
||
push(value: T) { | ||
if (!this.has(value)) { | ||
const element = { value, next: null, prev: null } as DLLElement<T> | ||
|
||
if (this.#map.size === 0) { | ||
this.#head = element | ||
} else { | ||
;(this.#tail as DLLElement<T>).next = element | ||
element.prev = this.#tail | ||
} | ||
|
||
this.#tail = element | ||
|
||
this.#map.set(value, element) | ||
} | ||
} | ||
|
||
remove(value: T) { | ||
if (this.has(value)) { | ||
const element = this.#map.get(value) | ||
|
||
if (this.#map.size === 1) { | ||
this.#head = null | ||
this.#tail = null | ||
} else if (element === this.#head) { | ||
this.#head = element.next | ||
element.next.prev = null | ||
} else if (element === this.#tail) { | ||
this.#tail = element.prev | ||
element.prev.next = null | ||
} else { | ||
element.prev.next = element.next | ||
element.next.prev = element.prev | ||
} | ||
|
||
this.#map.delete(value) | ||
} | ||
} | ||
|
||
insertValueAfter(newValue: T, value: T) { | ||
if (this.has(value)) { | ||
const element = this.#map.get(value) | ||
const existingValue = this.#map.has(newValue) | ||
const newElement = this.#map.get(newValue) || { | ||
value: newValue, | ||
next: null, | ||
prev: null, | ||
} | ||
|
||
if (element.next !== newElement) { | ||
if (existingValue) { | ||
this.remove(newValue) | ||
} | ||
|
||
newElement.prev = element | ||
newElement.next = element.next | ||
|
||
if (element === this.#tail) { | ||
this.#tail = newElement | ||
} else { | ||
element.next.prev = newElement | ||
} | ||
|
||
element.next = newElement | ||
|
||
this.#map.set(newValue, newElement) | ||
} | ||
} | ||
} | ||
|
||
insertValueBefore(newValue: T, value: T) { | ||
if (this.has(value)) { | ||
const element = this.#map.get(value) | ||
const existingValue = this.#map.has(newValue) | ||
const newElement = this.#map.get(newValue) || { | ||
value: newValue, | ||
next: null, | ||
prev: null, | ||
} | ||
|
||
if (element.prev !== newElement) { | ||
if (existingValue) { | ||
this.remove(newValue) | ||
} | ||
|
||
newElement.next = element | ||
newElement.prev = element.prev | ||
|
||
if (element === this.#head) { | ||
this.#head = newElement | ||
} else { | ||
element.prev.next = newElement | ||
} | ||
|
||
element.prev = newElement | ||
|
||
this.#map.set(newValue, newElement) | ||
} | ||
} | ||
} | ||
|
||
clear() { | ||
this.#head = null | ||
this.#tail = null | ||
this.#map.clear() | ||
} | ||
|
||
has(value: T) { | ||
return this.#map.has(value) | ||
} | ||
|
||
getNextValueOf(value: T | null): T | null { | ||
return this.#map.get(value)?.next?.value ?? null | ||
} | ||
|
||
getPreviousValueOf(value: T | null): T | null { | ||
return this.#map.get(value)?.prev?.value ?? null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.