-
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.
Move combineCounts to util file (#64)
- Loading branch information
1 parent
0f796e6
commit 1838fdb
Showing
10 changed files
with
217 additions
and
78 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
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
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
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
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
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: 38 additions & 0 deletions
38
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/set/multiset/utils/CountsMap.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,38 @@ | ||
package xyz.lbres.kotlinutils.set.multiset.utils | ||
|
||
/** | ||
* Mapping of occurrences to the number of times that they occur | ||
*/ | ||
@JvmInline | ||
internal value class CountsMap<E>(private val counts: Map<E, Int>) { | ||
/** | ||
* Distinct values in map | ||
*/ | ||
val distinct: Set<E> | ||
get() = counts.keys | ||
|
||
/** | ||
* Get number of occurrences of a single element | ||
* | ||
* @param element E: element to get count of | ||
* @return [Int]: number of occurrences | ||
*/ | ||
fun getCountOf(element: E): Int = counts.getOrDefault(element, 0) | ||
|
||
companion object { | ||
/** | ||
* Create a CountsMap from a collection of elements | ||
* | ||
* @param elements [Collection]<E>: elements to include in the map | ||
* @return [CountsMap]<E>: map containing exactly the elements in the given collection | ||
*/ | ||
fun <E> from(elements: Collection<E>): CountsMap<E> { | ||
val counts: MutableMap<E, Int> = mutableMapOf() | ||
elements.forEach { | ||
counts[it] = counts.getOrDefault(it, 0) + 1 | ||
} | ||
|
||
return CountsMap(counts) | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.