-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shift ConstMultiSet code to impl classes (#63)
- Loading branch information
1 parent
cf7e15e
commit 0f796e6
Showing
6 changed files
with
272 additions
and
352 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 37 additions & 1 deletion
38
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/set/multiset/const/ConstMultiSetImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,40 @@ | ||
package xyz.lbres.kotlinutils.set.multiset.const | ||
|
||
import xyz.lbres.kotlinutils.generic.ext.ifNull | ||
import xyz.lbres.kotlinutils.set.multiset.MultiSet | ||
import xyz.lbres.kotlinutils.set.multiset.utils.countsToString | ||
import xyz.lbres.kotlinutils.set.multiset.utils.createCountsMap | ||
|
||
// final implementation of ConstMultiSet | ||
internal class ConstMultiSetImpl<E>(initialElements: Collection<E>, initialCounts: Map<E, Int>? = null) : ConstMultiSet<E>(initialElements, initialCounts) | ||
internal class ConstMultiSetImpl<E>(private val elements: Collection<E>, initialCounts: Map<E, Int>? = null) : ConstMultiSet<E>(elements, initialCounts) { | ||
private val manager: ConstMultiSetManager<E> | ||
override val size: Int = elements.size | ||
override val distinctValues: Set<E> | ||
private val string: String | ||
val counts: Map<E, Int> | ||
|
||
init { | ||
counts = initialCounts.ifNull { createCountsMap(elements) } | ||
distinctValues = counts.keys | ||
manager = ConstMultiSetManager(counts) | ||
string = countsToString(counts) | ||
} | ||
|
||
override fun getCountOf(element: E): Int = manager.getCountOf(element) | ||
override fun contains(element: E): Boolean = manager.contains(element) | ||
override fun containsAll(elements: Collection<E>): Boolean = manager.containsAll(elements) | ||
|
||
override fun plus(other: MultiSet<E>): MultiSet<E> = manager.plus(other) | ||
override fun minus(other: MultiSet<E>): MultiSet<E> = manager.minus(other) | ||
override fun intersect(other: MultiSet<E>): MultiSet<E> = manager.intersect(other) | ||
|
||
override fun plusC(other: ConstMultiSet<E>): ConstMultiSet<E> = manager.plusC(other) | ||
override fun minusC(other: ConstMultiSet<E>): ConstMultiSet<E> = manager.minusC(other) | ||
override fun intersectC(other: ConstMultiSet<E>): ConstMultiSet<E> = manager.intersectC(other) | ||
|
||
override fun isEmpty(): Boolean = manager.isEmpty() | ||
override fun iterator(): Iterator<E> = elements.iterator() | ||
override fun hashCode(): Int = manager.getHashCode() | ||
override fun equals(other: Any?): Boolean = manager.equalsSet(other) | ||
override fun toString(): String = string | ||
} |
95 changes: 95 additions & 0 deletions
95
...in-utils/src/main/kotlin/xyz/lbres/kotlinutils/set/multiset/const/ConstMultiSetManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package xyz.lbres.kotlinutils.set.multiset.const | ||
|
||
import xyz.lbres.kotlinutils.general.simpleIf | ||
import xyz.lbres.kotlinutils.set.multiset.MultiSet | ||
import xyz.lbres.kotlinutils.set.multiset.impl.MultiSetImpl | ||
import xyz.lbres.kotlinutils.set.multiset.utils.createCountsMap | ||
import kotlin.math.min | ||
|
||
/** | ||
* Manager class for common ConstMultiSet code | ||
* | ||
* @param counts [Map]<E, Int>: counts map for set | ||
*/ | ||
internal class ConstMultiSetManager<E>(private val counts: Map<E, Int>) { | ||
fun getCountOf(element: E): Int = counts.getOrDefault(element, 0) | ||
fun isEmpty(): Boolean = counts.isEmpty() | ||
|
||
fun contains(element: E): Boolean = counts.contains(element) | ||
fun containsAll(elements: Collection<E>): Boolean { | ||
val otherCounts = createCountsMap(elements) | ||
|
||
return otherCounts.all { (element, otherCount) -> | ||
otherCount <= getCountOf(element) | ||
} | ||
} | ||
|
||
fun plus(other: MultiSet<E>): MultiSet<E> { | ||
return combineCounts(other, Int::plus, true, const = false) | ||
} | ||
fun minus(other: MultiSet<E>): MultiSet<E> { | ||
return combineCounts(other, Int::minus, false, const = false) | ||
} | ||
fun intersect(other: MultiSet<E>): MultiSet<E> { | ||
return combineCounts(other, ::min, false, const = false) | ||
} | ||
fun plusC(other: ConstMultiSet<E>): ConstMultiSet<E> { | ||
return combineCounts(other, Int::plus, true, const = true) as ConstMultiSet<E> | ||
} | ||
fun minusC(other: ConstMultiSet<E>): ConstMultiSet<E> { | ||
return combineCounts(other, Int::minus, false, const = true) as ConstMultiSet<E> | ||
} | ||
fun intersectC(other: ConstMultiSet<E>): ConstMultiSet<E> { | ||
return combineCounts(other, ::min, false, const = true) as ConstMultiSet<E> | ||
} | ||
|
||
/** | ||
* Combine counts with another MultiSet, using the given operation | ||
* | ||
* @param other [MultiSet]<E>: MultiSet to combine with | ||
* @param operation (Int, Int) -> Int: combination function | ||
* @param useAllValues [Boolean]: if all values from both sets should be used to generate the new set. If `false`, only the values from this set will be used. | ||
* @param const [Boolean]: if the returned MultiSet should be a ConstMultiSet | ||
* @return [MultiSet]<E>: new set where each element has the number of occurrences specified by the operation. If [const] is `true`, the set will be a const multi set | ||
*/ | ||
private fun combineCounts(other: MultiSet<E>, operation: (count: Int, otherCount: Int) -> Int, useAllValues: Boolean, const: Boolean): MultiSet<E> { | ||
val values: Set<E> = simpleIf(useAllValues, { counts.keys + other.distinctValues }, { counts.keys }) | ||
val newCounts: MutableMap<E, Int> = mutableMapOf() | ||
val newElements: MutableList<E> = mutableListOf() | ||
|
||
values.forEach { value -> | ||
val count = operation(getCountOf(value), other.getCountOf(value)) | ||
if (count > 0) { | ||
newCounts[value] = count | ||
repeat(count) { newElements.add(value) } | ||
} | ||
} | ||
|
||
return simpleIf(const, { ConstMultiSetImpl(newElements, newCounts) }, { MultiSetImpl(newElements) }) | ||
} | ||
|
||
/** | ||
* Check equality of the managed set to another MultiSet | ||
* | ||
* @param other [Any]? | ||
* @return [Boolean] `true` if [other] is a MultiSet containing the same elements as the counts map, `false` otherwise | ||
*/ | ||
fun equalsSet(other: Any?): Boolean { | ||
return when (other) { | ||
is ConstMultiSetImpl<*> -> counts == other.counts | ||
is ConstMutableMultiSetImpl<*> -> counts == other.counts | ||
is MultiSet<*> -> counts == createCountsMap(other) | ||
else -> false | ||
} | ||
} | ||
|
||
/** | ||
* Get hash code for set. | ||
* | ||
* @return [Int] | ||
*/ | ||
fun getHashCode(): Int { | ||
val hashCode = counts.hashCode() | ||
return 31 * hashCode + MultiSet::class.java.name.hashCode() | ||
} | ||
} |
Oops, something went wrong.