Int2IntMap

class Int2IntMap(initialCapacity: Int)

Simple map for Int to Int relations. Designed for small to medium maps with a low amount of entries (<1000).

As there is no way to specify invalid values, a return value of 0 is given when the mapping does not exist. While it does mostly comply to the MutableMap interface, it is not implemented for performance reasons and for simplicity. It is designed to be used as a drop in replacement for a Map when the key is an Int and the value is an Int.

Implemented as a dual sorted array with binary search for fast lookups. Here is a summary of the performance characteristics:

get(key: Int):

  • Best/Average Case: 𝑂(log 𝑛). Searching is performed using a binary algorithm.

set(key: Int, value: Int):

  • Best/Average Case: 𝑂(log 𝑛) for the binary search and 𝑂(1) for the insertion without resizing.

  • Worst Case: 𝑂(log 𝑛) for the binary search and 𝑂(𝑛) for the insertion with array shifting and resizing.

Constructors

Link copied to clipboard
constructor(initialCapacity: Int)

Types

Link copied to clipboard
value class Entry(it: Long)
Link copied to clipboard
inner class Iter

Properties

Link copied to clipboard
var size: Int

Functions

Link copied to clipboard
fun clear()

Clear the map. This does not actually discard any data for performance reasons.

Link copied to clipboard
operator fun contains(key: Int): Boolean
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
inline fun forEach(action: (Int, Int) -> Unit)
Link copied to clipboard
operator fun get(key: Int): Int

Get the value associated with the given key.

Link copied to clipboard
inline fun getOrPut(key: Int, defaultValue: () -> Int): Int
Link copied to clipboard
Link copied to clipboard
operator fun iterator(): Int2IntMap.Iter
Link copied to clipboard
fun put(key: Int, value: Int)
Link copied to clipboard
fun remove(key: Int): Int

Remove the mapping for the given key.

Link copied to clipboard
operator fun set(key: Int, value: Int)
Link copied to clipboard
fun trim()

Trim the internal arrays to the current size. This is useful when the map will not be modified anymore.