Skip to main content
Suggest different verbs more consistent with the Java Collections Framework
Source Link
mfulton26
  • 701
  • 3
  • 8
  1. private val items: MutableList<T?> = mutableListOf() can be written more succinctly as private val items = mutableListOf<T?>() (the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).

  2. size can be backed by items.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
         ...
     }
    
  3. siftDown throws an AssertionError assertion when size == 0.

  4. push and pop have different meanings in the Java Collections Framework associated with using a java.util.Deque as a stack. You might instead use the same verbs as java.util.PriorityQueue (namely add and remove) which can be used as a max heap:

     fun <T : Comparable<T>> maxHeap() = PriorityQueue<T>(Comparator.reverseOrder<T>())
    
  1. private val items: MutableList<T?> = mutableListOf() can be written more succinctly as private val items = mutableListOf<T?>() (the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).

  2. size can be backed by items.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
         ...
     }
    
  3. siftDown throws an AssertionError assertion when size == 0.

  1. private val items: MutableList<T?> = mutableListOf() can be written more succinctly as private val items = mutableListOf<T?>() (the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).

  2. size can be backed by items.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
         ...
     }
    
  3. siftDown throws an AssertionError assertion when size == 0.

  4. push and pop have different meanings in the Java Collections Framework associated with using a java.util.Deque as a stack. You might instead use the same verbs as java.util.PriorityQueue (namely add and remove) which can be used as a max heap:

     fun <T : Comparable<T>> maxHeap() = PriorityQueue<T>(Comparator.reverseOrder<T>())
    
Updated #3 to not suggest a fix
Source Link
mfulton26
  • 701
  • 3
  • 8
  1. private val items: MutableList<T?> = mutableListOf() can be written more succinctly as private val items = mutableListOf<T?>() (the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).

  2. size can be backed by items.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
         ...
     }
    
  3. The siftDown assertion onthrows an iAssertionError should beassertion when assert(i >= 0 && i <= size) and not assert(i >=== 0 && i < size).

  1. private val items: MutableList<T?> = mutableListOf() can be written more succinctly as private val items = mutableListOf<T?>() (the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).

  2. size can be backed by items.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
         ...
     }
    
  3. The siftDown assertion on i should be assert(i >= 0 && i <= size) and not assert(i >= 0 && i < size).

  1. private val items: MutableList<T?> = mutableListOf() can be written more succinctly as private val items = mutableListOf<T?>() (the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).

  2. size can be backed by items.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
         ...
     }
    
  3. siftDown throws an AssertionError assertion when size == 0.

Source Link
mfulton26
  • 701
  • 3
  • 8

  1. private val items: MutableList<T?> = mutableListOf() can be written more succinctly as private val items = mutableListOf<T?>() (the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).

  2. size can be backed by items.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
         ...
     }
    
  3. The siftDown assertion on i should be assert(i >= 0 && i <= size) and not assert(i >= 0 && i < size).