All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.stress.WrapperGenerator.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2019 JetBrains s.r.o.
 * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
 */

package tests.stress

import kotlin.random.Random


class WrapperGenerator>(private val hashCodeUpperBound: Int) {
    private val elementMap = hashMapOf>()
    private val hashCodeMap = hashMapOf>>()

    fun wrapper(element: K): ObjectWrapper {
        val existing = elementMap[element]
        if (existing != null) {
            return existing
        }
        val hashCode = Random.nextInt(hashCodeUpperBound)
        val wrapper = ObjectWrapper(element, hashCode)
        elementMap[element] = wrapper

        val wrappers = hashCodeMap[hashCode] ?: mutableListOf()
        wrappers.add(wrapper)
        hashCodeMap[hashCode] = wrappers

        return wrapper
    }

    fun wrappersByHashCode(hashCode: Int): List> {
        return hashCodeMap[hashCode] ?: mutableListOf()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy