Skip to content

Commit

Permalink
feat: _ -> # for private modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
upupming authored and harttle committed Jun 14, 2021
1 parent 9a45f17 commit 4e0e9b0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
18 changes: 9 additions & 9 deletions deque.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ class CircularDeque<T> {
export class Deque<T> {
head: CircularDeque<T>
tail: CircularDeque<T>
_size: number
#size: number
constructor (collection: T[] = []) {
this.head = new CircularDeque<T>(128)
this.tail = new CircularDeque<T>(128)
this.tail.empty = this.head.empty = false
this.tail.prev = this.head
this.head.next = this.tail
this._size = 0
this.#size = 0
for (const item of collection) this.push(item)
}

size (): number {
return this._size
return this.#size
}

push (val: T): void {
Expand All @@ -103,11 +103,11 @@ export class Deque<T> {
last = inserted
}
last.push(val)
this._size++
this.#size++
}

back (): T | undefined {
if (this._size === 0) return
if (this.#size === 0) return
return this.tail.prev!.back()
}

Expand All @@ -119,7 +119,7 @@ export class Deque<T> {
this.tail.prev = last.prev
last.prev!.next = this.tail
}
this._size--
this.#size--
return value
}

Expand All @@ -137,7 +137,7 @@ export class Deque<T> {
first = inserted
}
first.unshift(val)
this._size++
this.#size++
}

shift (): T | undefined {
Expand All @@ -148,12 +148,12 @@ export class Deque<T> {
this.head.next = first.next
first.next!.prev = this.head
}
this._size--
this.#size--
return value
}

front (): T | undefined {
if (this._size === 0) return undefined
if (this.#size === 0) return undefined
return this.head.next!.front()
}

Expand Down
24 changes: 12 additions & 12 deletions heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Heap<T=number> {
export class RemovableHeap<T> {
heap: Heap<T>
counts: Map<T, number>
_invalidCount: number
#invalidCount: number
constructor ()
constructor (data: T[])
constructor (cmp: (lhs: T, rhs: T) => boolean)
Expand All @@ -64,47 +64,47 @@ export class RemovableHeap<T> {
constructor (data: (T[] | ((lhs: T, rhs: T) => boolean)) = [], cmp = (lhs: T, rhs: T) => lhs < rhs) {
this.heap = new Heap<T>(data, cmp)
this.counts = new Map()
this._invalidCount = 0
this.#invalidCount = 0
}

size (): number {
return this.heap.size() - this._invalidCount
return this.heap.size() - this.#invalidCount
}

top (): T {
this._normalize()
this.#normalize()
return this.heap.top()
}

pop (): T | undefined {
this._normalize()
this.#normalize()
if (this.heap.size() < 1) return undefined
const top = this.heap.pop()
this._count(top, -1)
this.#count(top, -1)
return top
}

push (num: T): void {
this._count(num, 1)
this.#count(num, 1)
this.heap.push(num)
}

remove (num: T): void {
if (Number(this.counts.get(num)) > 0) {
this._count(num, -1)
this._invalidCount++
this.#count(num, -1)
this.#invalidCount++
}
}

_count (num: T, diff: number): void {
#count (num: T, diff: number): void {
const count = this.counts.get(num) ?? 0
this.counts.set(num, count + diff)
}

_normalize (): void {
#normalize (): void {
while (this.heap.size() && !this.counts.get(this.heap.top())) {
this.heap.pop()
this._invalidCount--
this.#invalidCount--
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ export class ListNode<T = number> {
export class Queue<T=number> {
lead: ListNode<T>
tail: ListNode<T>
_size: number
#size: number
constructor (collection: T[] = []) {
this.lead = new ListNode<T>()
this.tail = this.lead
this._size = 0
this.#size = 0
for (const item of collection) this.push(item)
}

size (): number {
return this._size
return this.#size
}

push (value: T): void {
this.tail = this.tail.next = new ListNode(value)
this._size++
this.#size++
}

back (): T {
return this.tail.value!
}

shift (): T | undefined {
if (!this._size) return undefined
if (!this.#size) return undefined
const first = this.lead.next!
this.lead.next = first.next
this._size--
if (this._size === 0) this.tail = this.lead
this.#size--
if (this.#size === 0) this.tail = this.lead
return first.value
}

front (): T | undefined {
if (!this._size) return undefined
if (!this.#size) return undefined
return this.lead.next!.value
}

Expand Down
20 changes: 10 additions & 10 deletions treeset.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { RBTree } from './rbtree'

export class TreeSet<T = number> {
_size: number
#size: number
tree: RBTree<T>
compare: (l: T, r: T) => boolean
constructor (collection: T[] = [], compare = (l: T, r: T) => l < r) {
this._size = 0
this.#size = 0
this.tree = new RBTree(compare)
this.compare = compare
for (const val of collection) this.add(val)
}

size (): number {
return this._size
return this.#size
}

has (val: T): boolean {
Expand All @@ -21,13 +21,13 @@ export class TreeSet<T = number> {

add (val: T): boolean {
const added = this.tree.insert(val)
this._size += added ? 1 : 0
this.#size += added ? 1 : 0
return added
}

delete (val: T): boolean {
const deleted = this.tree.deleteByValue(val)
this._size -= deleted ? 1 : 0
this.#size -= deleted ? 1 : 0
return deleted
}

Expand Down Expand Up @@ -101,20 +101,20 @@ export class TreeSet<T = number> {
}

export class TreeMultiSet<T = number> {
_size: number
#size: number
tree: RBTree<T>
counts: Map<T, number>
compare: (l: T, r: T) => boolean
constructor (collection: T[] = [], compare = (l: T, r: T) => l < r) {
this._size = 0
this.#size = 0
this.tree = new RBTree(compare)
this.counts = new Map()
this.compare = compare
for (const val of collection) this.add(val)
}

size (): number {
return this._size
return this.#size
}

has (val: T): boolean {
Expand All @@ -124,13 +124,13 @@ export class TreeMultiSet<T = number> {
add (val: T): void {
this.tree.insert(val)
this.increase(val)
this._size++
this.#size++
}

delete (val: T): void {
this.tree.deleteByValue(val)
this.decrease(val)
this._size--
this.#size--
}

count (val: T): number {
Expand Down

0 comments on commit 4e0e9b0

Please sign in to comment.