@tsevimli/collections
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -76,8 +76,14 @@ 'use strict'; | ||
unshift(data) { | ||
this.bottom = (this.bottom - 1 + kSize) & kMask; | ||
this.list[this.bottom] = data; | ||
} | ||
shift() { | ||
const nextItem = this.list[this.bottom]; | ||
if (nextItem === undefined) return null; | ||
this.list[this.bottom] = undefined; | ||
this.bottom = (this.bottom + 1) & kMask; | ||
return nextItem; | ||
if (nextItem !== undefined) { | ||
this.list[this.bottom] = undefined; | ||
this.bottom = (this.bottom + 1) & kMask; | ||
} | ||
return nextItem ?? null; | ||
} | ||
@@ -118,2 +124,12 @@ } | ||
unshift(data) { | ||
this.length++; | ||
if (this.tail.isFull()) { | ||
const newTail = new FixedCircularBuffer(); | ||
newTail.next = this.tail; | ||
this.tail = newTail; | ||
} | ||
this.tail.unshift(data); | ||
} | ||
sort(compareFn = (a, b) => a - b) { | ||
@@ -120,0 +136,0 @@ const size = this.length; |
{ | ||
"name": "@tsevimli/collections", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Data structures in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "collections.js", |
@@ -6,3 +6,3 @@ 'use strict'; | ||
plan(11); | ||
plan(13); | ||
@@ -18,3 +18,3 @@ test('FixedQueue initialization', (t) => { | ||
test('Push method (small queue)', (t) => { | ||
test('push method (small queue)', (t) => { | ||
t.plan(1); | ||
@@ -31,7 +31,8 @@ | ||
test('Push method (large queue)', (t) => { | ||
t.plan(1); | ||
test('push method (large queue)', (t) => { | ||
t.plan(2); | ||
const MAX = 3_000; | ||
const expected = new Array(MAX).fill(undefined); | ||
const queue = new FixedQueue(); | ||
@@ -41,8 +42,10 @@ | ||
queue.push(i); | ||
expected[i] = i; | ||
} | ||
t.equal(queue.length, MAX); | ||
t.same([...queue], expected); | ||
}); | ||
test('Shift method (small queue)', (t) => { | ||
test('unshift method (small queue)', (t) => { | ||
t.plan(6); | ||
@@ -52,2 +55,36 @@ | ||
queue.unshift(1); | ||
queue.unshift(2); | ||
queue.unshift(3); | ||
t.equal(queue.shift(), 3); | ||
t.equal(queue.length, 2); | ||
t.equal(queue.shift(), 2); | ||
t.equal(queue.length, 1); | ||
t.equal(queue.shift(), 1); | ||
t.equal(queue.length, 0); | ||
}); | ||
test('unshift method (large queue)', (t) => { | ||
t.plan(2); | ||
const MAX = 3_000; | ||
const expected = new Array(MAX).fill(undefined); | ||
const queue = new FixedQueue(); | ||
for (let i = 0; i < MAX; i++) { | ||
queue.unshift(i); | ||
expected[MAX - i - 1] = i; | ||
} | ||
t.equal(queue.length, MAX); | ||
t.same([...queue], expected); | ||
}); | ||
test('shift method (small queue)', (t) => { | ||
t.plan(6); | ||
const queue = new FixedQueue(); | ||
queue.push(1); | ||
@@ -65,5 +102,4 @@ queue.push(2); | ||
test('Shift method (large queue)', (t) => { | ||
test('shift method (large queue)', (t) => { | ||
const MAX = 3_000; | ||
t.plan(MAX); | ||
@@ -77,7 +113,11 @@ const queue = new FixedQueue(); | ||
for (let i = 0; i < MAX; i++) { | ||
t.equal(queue.shift(), i); | ||
if (queue.shift() !== i) { | ||
t.fail(); | ||
break; | ||
} | ||
} | ||
t.end(); | ||
}); | ||
test('Sort method (small queue)', (t) => { | ||
test('sort method (small queue)', (t) => { | ||
t.plan(4); | ||
@@ -99,3 +139,3 @@ | ||
test('Sort method (large queue)', (t) => { | ||
test('sort method (large queue)', (t) => { | ||
t.plan(1); | ||
@@ -146,3 +186,3 @@ | ||
test('Every method', (t) => { | ||
test('every method', (t) => { | ||
t.plan(2); | ||
@@ -165,3 +205,3 @@ | ||
test('Find method', (t) => { | ||
test('find method', (t) => { | ||
t.plan(2); | ||
@@ -168,0 +208,0 @@ |
@@ -12,9 +12,10 @@ 'use strict'; | ||
for (let i = 0; i < 3_000; i++) { | ||
list.push(i); | ||
for (let i = 0; i < 3500; i++) { | ||
list.unshift(i); | ||
} | ||
list.sort((a, b) => b - a); | ||
// list.sort((a, b) => b - a); | ||
for (const item of list) { | ||
if (item === null) throw new Error('item is null'); | ||
console.log(item); | ||
@@ -21,0 +22,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
26961
23
788