tagan.base.1.5.1.source-code.sets.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of base Show documentation
Show all versions of base Show documentation
Yatagan is a Dependency Injection framework, specializing on runtime performance and build speed. Supports code generation (apt/kapt/ksp) or reflection.
The newest version!
/*
* Copyright 2022 Yandex LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("NOTHING_TO_INLINE")
package com.yandex.yatagan.base
import java.util.EnumMap
import java.util.EnumSet
private class PairSet(
private val one: T,
private val two: T,
) : AbstractSet() {
override val size get() = 2
override fun isEmpty() = false
override fun contains(element: @UnsafeVariance T) = one == element || two == element
override fun containsAll(elements: Collection<@UnsafeVariance T>) = elements.all { it == one || it == two }
override fun iterator(): Iterator = object : Iterator {
private var next = 1
override fun hasNext() = next < 3
override fun next(): T = when (next++) {
1 -> one; 2 -> two; else -> throw NoSuchElementException()
}
}
}
fun setOf(one: T, two: T): Set {
return if (one == two) setOf(one) else PairSet(one, two)
}
infix fun Set.intersects(another: Set): Boolean {
if (isEmpty() || another.isEmpty()) {
return false
}
if (size < another.size) {
for (element in this) if (element in another)
return true
} else {
for (element in another) if (element in this)
return true
}
return false
}
inline infix fun Set.notIntersects(another: Set) = !(this intersects another)
// region enumSetOf
inline fun > enumSetOf(): EnumSet = EnumSet.noneOf(E::class.java)
fun > enumSetOf(value: E): EnumSet = EnumSet.of(value)
fun > enumSetOf(v1: E, v2: E): EnumSet = EnumSet.of(v1, v2)
fun > enumSetOf(v1: E, v2: E, v3: E): EnumSet = EnumSet.of(v1, v2, v3)
fun > enumSetOf(v1: E, v2: E, v3: E, v4: E): EnumSet = EnumSet.of(v1, v2, v3, v4)
fun > enumSetOf(v1: E, vararg rest: E): EnumSet = EnumSet.of(v1, *rest)
// endregion
// region enumMapOf
inline fun , V> enumMapOf(): EnumMap = EnumMap(K::class.java)
inline fun , V> enumMapOf(key: K, value: V): EnumMap =
enumMapOf().apply { put(key, value) }
inline fun , V> enumMapOf(kv1: Pair, kv2: Pair): EnumMap =
enumMapOf().apply {
put(kv1.first, kv1.second)
put(kv2.first, kv2.second)
}
inline fun , V> enumMapOf(kv1: Pair, kv2: Pair, kv3: Pair): EnumMap =
enumMapOf().apply {
put(kv1.first, kv1.second)
put(kv2.first, kv2.second)
put(kv3.first, kv3.second)
}
inline fun , V> enumMapOf(kv1: Pair, kv2: Pair, kv3: Pair, kv4: Pair): EnumMap =
enumMapOf().apply {
put(kv1.first, kv1.second)
put(kv2.first, kv2.second)
put(kv3.first, kv3.second)
put(kv4.first, kv4.second)
}
inline fun , V> enumMapOf(vararg pairs: Pair): EnumMap =
enumMapOf().apply { pairs.forEachBi(::put) }
// endregion