|
| 1 | +package org.jetbrains.kotlinx.dataframe.impl |
| 2 | + |
| 3 | +import kotlin.collections.plusAssign |
| 4 | + |
| 5 | +/** |
| 6 | + * Map that resolves values lazily. |
| 7 | + */ |
| 8 | +internal class LazyMap<K, out V>(val lazyMap: Map<K, Lazy<V>>) : Map<K, V> { |
| 9 | + |
| 10 | + // Operations that traverse all values will resolve all values anyway |
| 11 | + private val resolvedMap = lazy { |
| 12 | + lazyMap.mapValues { it.value.value } |
| 13 | + } |
| 14 | + |
| 15 | + override val size: Int get() = lazyMap.size |
| 16 | + override val keys: Set<K> get() = lazyMap.keys |
| 17 | + |
| 18 | + // resolves all values! |
| 19 | + override val values: Collection<V> get() = resolvedMap.value.values |
| 20 | + |
| 21 | + // resolves all values! |
| 22 | + override val entries: Set<Map.Entry<K, V>> |
| 23 | + get() = resolvedMap.value.entries |
| 24 | + |
| 25 | + override fun isEmpty(): Boolean = lazyMap.isEmpty() |
| 26 | + |
| 27 | + override fun containsKey(key: K): Boolean = lazyMap.containsKey(key) |
| 28 | + |
| 29 | + // resolves as little values as possible |
| 30 | + override fun containsValue(value: @UnsafeVariance V): Boolean { |
| 31 | + if (resolvedMap.isInitialized()) { |
| 32 | + return resolvedMap.value.containsValue(value) |
| 33 | + } |
| 34 | + for (key in keys) { |
| 35 | + if (get(key) == value) return true |
| 36 | + } |
| 37 | + return false |
| 38 | + } |
| 39 | + |
| 40 | + override fun get(key: K): V? = |
| 41 | + if (resolvedMap.isInitialized()) { |
| 42 | + resolvedMap.value[key] |
| 43 | + } else { |
| 44 | + lazyMap[key]?.value |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Creates a [Map] that resolves values lazily. |
| 50 | + */ |
| 51 | +internal fun <K, V> lazyMapOf(vararg entries: Pair<K, () -> V>): LazyMap<K, V> = |
| 52 | + LazyMap(mapOf(*entries).mapValues { lazy(it.value) }) |
| 53 | + |
| 54 | +///** |
| 55 | +// * Merges two maps of Iterables, appending the values from the lists in the other map |
| 56 | +// * to the lists in the first map. |
| 57 | +// */ |
| 58 | +//internal infix fun <K, V> Map<K, Iterable<V>>.merge(other: Map<K, Iterable<V>>): Map<K, List<V>> = |
| 59 | +// buildMap<K, MutableList<V>> { |
| 60 | +// this@merge.forEach { (key, values) -> |
| 61 | +// this[key] = values.toMutableList() |
| 62 | +// } |
| 63 | +// other.forEach { (key, values) -> |
| 64 | +// this.getOrPut(key) { mutableListOf() } += values |
| 65 | +// } |
| 66 | +// } |
| 67 | +// |
| 68 | +///** |
| 69 | +// * Same as [Map.merge], however, it retains the laziness of the values in the first map. |
| 70 | +// */ |
| 71 | +//internal infix fun <K, V> LazyMap<K, Iterable<V>>.merge(other: Map<K, Iterable<V>>): LazyMap<K, List<V>> = |
| 72 | +// LazyMap( |
| 73 | +// buildMap<K, Lazy<List<V>>> { |
| 74 | +// this@merge.lazyMap.forEach { (key, values) -> |
| 75 | +// this[key] = lazy { values.value.toList() } |
| 76 | +// } |
| 77 | +// other.forEach { (key, values) -> |
| 78 | +// if (key in this) { |
| 79 | +// this[key] = this[key]!! + values |
| 80 | +// } else { |
| 81 | +// this[key] = lazy { values.toList() } |
| 82 | +// } |
| 83 | +// } |
| 84 | +// }, |
| 85 | +// ) |
| 86 | +// |
| 87 | +///** |
| 88 | +// * Same as [Map.merge], however, it retains the laziness of the values in the second map. |
| 89 | +// */ |
| 90 | +//internal infix fun <K, V> Map<K, Iterable<V>>.merge(other: LazyMap<K, Iterable<V>>): LazyMap<K, List<V>> = |
| 91 | +// LazyMap( |
| 92 | +// buildMap<K, Lazy<List<V>>> { |
| 93 | +// this@merge.forEach { (key, values) -> |
| 94 | +// this[key] = lazy { values.toList() } |
| 95 | +// } |
| 96 | +// other.lazyMap.forEach { (key, values) -> |
| 97 | +// if (key in this) { |
| 98 | +// this[key] = this[key]!! + values |
| 99 | +// } else { |
| 100 | +// this[key] = lazy { values.value.toList() } |
| 101 | +// } |
| 102 | +// } |
| 103 | +// }, |
| 104 | +// ) |
| 105 | +// |
| 106 | +///** |
| 107 | +// * Same as [Map.merge], however, it retains the laziness of the values in both maps. |
| 108 | +// */ |
| 109 | +//internal infix fun <K, V> LazyMap<K, Iterable<V>>.merge(other: LazyMap<K, Iterable<V>>): LazyMap<K, List<V>> = |
| 110 | +// LazyMap( |
| 111 | +// buildMap<K, Lazy<List<V>>> { |
| 112 | +// this@merge.lazyMap.forEach { (key, values) -> |
| 113 | +// this[key] = lazy { values.value.toList() } |
| 114 | +// } |
| 115 | +// other.lazyMap.forEach { (key, values) -> |
| 116 | +// if (key in this) { |
| 117 | +// this[key] = this[key]!! + values |
| 118 | +// } else { |
| 119 | +// this[key] = lazy { values.value.toList() } |
| 120 | +// } |
| 121 | +// } |
| 122 | +// }, |
| 123 | +// ) |
| 124 | +// |
| 125 | +//internal operator fun <T> Lazy<Iterable<T>>.plus(other: Lazy<Iterable<T>>): Lazy<List<T>> = |
| 126 | +// lazy { this.value + other.value } |
| 127 | +// |
| 128 | +//internal operator fun <T> Lazy<Iterable<T>>.plus(other: Iterable<T>): Lazy<List<T>> = lazy { this.value + other } |
| 129 | +// |
| 130 | +//internal operator fun <T> Iterable<T>.plus(other: Lazy<Iterable<T>>): Lazy<List<T>> = lazy { this + other.value } |
0 commit comments