private val items: MutableList<T?> = mutableListOf()can be written more succinctly asprivate val items = mutableListOf<T?>()(the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).sizecan be backed byitems.size:val size: Int get() = items.size fun push(item: T) { set(size + 1, item) siftUp(size - 1) } ... fun pop(): T? { ... set(0, get(size - 1)) items.removeAt(items.lastIndex) // Remove original reference to the last item ... }siftDownthrows anAssertionErrorassertion whensize == 0.
mfulton26
- 701
- 3
- 8