-
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.
Support unlimited buffer for channels (#86)
- Loading branch information
1 parent
e30ce4b
commit dc2ce52
Showing
5 changed files
with
48 additions
and
19 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,32 +1,31 @@ | ||
import { isNonNegativeSafeInteger } from "./utils.ts"; | ||
import { isSafeInteger } from "./utils.ts"; | ||
|
||
/** @internal */ | ||
export class Queue<T> { | ||
protected queue: T[] = []; | ||
#queue: T[] = []; | ||
|
||
constructor(readonly capacity: number) { | ||
if (!isNonNegativeSafeInteger(capacity)) { | ||
throw new RangeError( | ||
"queue capacity must be a non-negative safe integer", | ||
); | ||
if (!isSafeInteger(capacity)) { | ||
throw new RangeError("queue capacity must be a safe integer"); | ||
} | ||
} | ||
|
||
enqueue(val: T) { | ||
if (this.isFull) throw new RangeError("queue is full"); | ||
this.queue.push(val); | ||
this.#queue.push(val); | ||
} | ||
|
||
dequeue(): T { | ||
if (this.isEmpty) throw new RangeError("queue is empty"); | ||
return this.queue.shift() as T; | ||
return this.#queue.shift() as T; | ||
} | ||
|
||
get isFull(): boolean { | ||
return this.queue.length === this.capacity; | ||
if (this.capacity < 0) return false; | ||
return this.#queue.length === this.capacity; | ||
} | ||
|
||
get isEmpty(): boolean { | ||
return this.queue.length === 0; | ||
return this.#queue.length === 0; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,35 @@ | ||
import { Queue } from "./queue.ts"; | ||
import { assertThrows } from "deno/testing/asserts.ts"; | ||
import { assert, assertEquals, assertThrows } from "deno/testing/asserts.ts"; | ||
|
||
Deno.test("invalid capacity", () => { | ||
assertThrows(() => new Queue(-1), RangeError); | ||
assertThrows(() => new Queue(0.1), RangeError); | ||
}); | ||
|
||
Deno.test("Queue(1)", () => { | ||
const q = new Queue(1); | ||
assert(q.isEmpty && !q.isFull); | ||
q.enqueue(0); | ||
assert(!q.isEmpty && q.isFull); | ||
assertThrows(() => q.enqueue(1), RangeError); | ||
assertEquals(q.dequeue(), 0); | ||
assertThrows(() => q.dequeue(), RangeError); | ||
assert(q.isEmpty && !q.isFull); | ||
}); | ||
|
||
Deno.test("Queue(0)", () => { | ||
const q = new Queue(0); | ||
assert(q.isEmpty && q.isFull); | ||
assertThrows(() => q.enqueue(0), RangeError); | ||
assertThrows(() => q.dequeue(), RangeError); | ||
}); | ||
|
||
Deno.test("Queue(-1)", () => { | ||
const q = new Queue(-1); | ||
assert(q.isEmpty && !q.isFull); | ||
q.enqueue(0); | ||
q.enqueue(1); | ||
assert(!q.isEmpty && !q.isFull); | ||
assertEquals(q.dequeue(), 0); | ||
assertEquals(q.dequeue(), 1); | ||
assert(q.isEmpty && !q.isFull); | ||
}); |
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