
org.diffkt.SequentialIntegerAssigner.kt Maven / Gradle / Ivy
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package org.diffkt
import java.util.*
import shapeTyping.annotations.NoSType
/**
* A helper class to assign unique integers to each value in a list.
* You can also think of it like a list with an efficient [indexOf] operation.
* Uses reference equality, not [Object.equals] to identify the object in the list.
*/
internal class SequentialIntegerAssigner {
private var nextInteger: Int = 0
private val map: IdentityHashMap = IdentityHashMap()
private val keys: MutableList = mutableListOf()
val values get(): List = keys
operator fun get(i: Int) = keys[i]
val size get() = keys.size
fun add(key: T) {
val assignedValue = nextInteger
nextInteger++
map.put(key, assignedValue)
keys.add(key)
}
fun indexOf(key: Any?): Int {
return map.get(key) ?: -1
}
@NoSType // shapeTyping #111: Generics in same module as calls result in unsubstituted Kotlin types
inline fun map(crossinline f: (T) -> Q): List = keys.map(f)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy