-
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.
MultiSet consistent methods, Array setAll, MutableCollection popRandom (
#27)
- Loading branch information
1 parent
f50a48c
commit 741dac1
Showing
57 changed files
with
2,280 additions
and
315 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ on: | |
push: | ||
branches-ignore: | ||
- "main" | ||
pull_request: | ||
|
||
jobs: | ||
build: | ||
|
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ plugins { | |
} | ||
|
||
group = "xyz.lbres" | ||
version = "0.4.0" | ||
version = "1.0.0" | ||
|
||
repositories { | ||
mavenCentral() | ||
|
10 changes: 10 additions & 0 deletions
10
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/array/ext/ArrayExt.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,10 @@ | ||
package xyz.lbres.kotlinutils.array.ext | ||
|
||
/** | ||
* Assign all elements to have a given value | ||
* | ||
* @param value [T]: value to assign | ||
*/ | ||
fun <T> Array<T>.setAllValues(value: T) { | ||
indices.forEach { set(it, value) } | ||
} |
10 changes: 10 additions & 0 deletions
10
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/booleanarray/ext/BooleanArrayExt.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,10 @@ | ||
package xyz.lbres.kotlinutils.booleanarray.ext | ||
|
||
/** | ||
* Assign all elements to have a given value | ||
* | ||
* @param value [Boolean]: value to assign | ||
*/ | ||
fun BooleanArray.setAllValues(value: Boolean) { | ||
indices.forEach { set(it, value) } | ||
} |
10 changes: 10 additions & 0 deletions
10
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/bytearray/ext/ByteArrayExt.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,10 @@ | ||
package xyz.lbres.kotlinutils.bytearray.ext | ||
|
||
/** | ||
* Assign all elements to have a given value | ||
* | ||
* @param value [Byte]: value to assign | ||
*/ | ||
fun ByteArray.setAllValues(value: Byte) { | ||
indices.forEach { set(it, value) } | ||
} |
10 changes: 10 additions & 0 deletions
10
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/chararray/ext/CharArrayExt.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,10 @@ | ||
package xyz.lbres.kotlinutils.chararray.ext | ||
|
||
/** | ||
* Assign all elements to have a given value | ||
* | ||
* @param value [Char]: value to assign | ||
*/ | ||
fun CharArray.setAllValues(value: Char) { | ||
indices.forEach { set(it, value) } | ||
} |
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
16 changes: 16 additions & 0 deletions
16
...tils/src/main/kotlin/xyz/lbres/kotlinutils/collection/mutable/ext/MutableCollectionExt.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,16 @@ | ||
package xyz.lbres.kotlinutils.collection.mutable.ext | ||
|
||
/** | ||
* Remove a random element from the collection and return it | ||
* | ||
* @return [T]?: an element from the collection, or `null` if the collection is empty | ||
*/ | ||
fun <T> MutableCollection<T>.popRandom(): T? { | ||
if (isEmpty()) { | ||
return null | ||
} | ||
|
||
val element = random() | ||
remove(element) | ||
return element | ||
} |
10 changes: 10 additions & 0 deletions
10
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/doublearray/ext/DoubleArrayExt.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,10 @@ | ||
package xyz.lbres.kotlinutils.doublearray.ext | ||
|
||
/** | ||
* Assign all elements to have a given value | ||
* | ||
* @param value [Double]: value to assign | ||
*/ | ||
fun DoubleArray.setAllValues(value: Double) { | ||
indices.forEach { set(it, value) } | ||
} |
10 changes: 10 additions & 0 deletions
10
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/floatarray/ext/FloatArrayExt.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,10 @@ | ||
package xyz.lbres.kotlinutils.floatarray.ext | ||
|
||
/** | ||
* Assign all elements to have a given value | ||
* | ||
* @param value [Float]: value to assign | ||
*/ | ||
fun FloatArray.setAllValues(value: Float) { | ||
indices.forEach { set(it, value) } | ||
} |
10 changes: 10 additions & 0 deletions
10
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/intarray/ext/IntArrayExt.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,10 @@ | ||
package xyz.lbres.kotlinutils.intarray.ext | ||
|
||
/** | ||
* Assign all elements to have a given value | ||
* | ||
* @param value [Int]: value to assign | ||
*/ | ||
fun IntArray.setAllValues(value: Int) { | ||
indices.forEach { set(it, value) } | ||
} |
17 changes: 5 additions & 12 deletions
17
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/list/mutablelist/ext/MutableListExt.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,17 +1,10 @@ | ||
package xyz.lbres.kotlinutils.list.mutablelist.ext | ||
|
||
import xyz.lbres.kotlinutils.collection.mutable.ext.popRandom | ||
|
||
/** | ||
* Remove a random element from list and return it | ||
* Remove a random element from list and return it. | ||
* | ||
* @return [T]: an element from the list, or null if the list is empty | ||
* @return [T]?: an element from the list, or `null` if the list is empty | ||
*/ | ||
fun <T> MutableList<T>.popRandom(): T? { | ||
if (isEmpty()) { | ||
return null | ||
} | ||
|
||
val index = indices.random() | ||
val element = get(index) | ||
removeAt(index) | ||
return element | ||
} | ||
fun <T> MutableList<T>.popRandom(): T? = popRandom() |
10 changes: 10 additions & 0 deletions
10
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/longarray/ext/LongArrayExt.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,10 @@ | ||
package xyz.lbres.kotlinutils.longarray.ext | ||
|
||
/** | ||
* Assign all elements to have a given value | ||
* | ||
* @param value [Long]: value to assign | ||
*/ | ||
fun LongArray.setAllValues(value: Long) { | ||
indices.forEach { set(it, value) } | ||
} |
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
61 changes: 61 additions & 0 deletions
61
kotlin-utils/src/main/kotlin/xyz/lbres/kotlinutils/set/multiset/MultiSetGenerics.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,61 @@ | ||
package xyz.lbres.kotlinutils.set.multiset | ||
|
||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
/** | ||
* Generic [MultiSet.plus] implementation that can be applied to any two MultiSets. | ||
* | ||
* @param multiSet1 [MultiSet]<E>: first MultiSet in addition | ||
* @param multiSet2 [MultiSet]<E>: second MultiSet in addition | ||
* @return [MultiSet]<E>: MultiSet containing all values from both MultiSets | ||
*/ | ||
internal fun <E> genericMultiSetPlus(multiSet1: MultiSet<E>, multiSet2: MultiSet<E>): MultiSet<E> { | ||
val newSet = mutableMultiSetOf<E>() | ||
val distinctValues = multiSet1.distinctValues + multiSet2.distinctValues | ||
|
||
distinctValues.forEach { element -> | ||
val totalCount = multiSet1.getCountOf(element) + multiSet2.getCountOf(element) | ||
repeat(totalCount) { newSet.add(element) } | ||
} | ||
|
||
return newSet | ||
} | ||
|
||
/** | ||
* Generic [MultiSet.minus] implementation that can be applied to any two MultiSets. | ||
* | ||
* @param multiSet1 [MultiSet]<E>: first MultiSet in subtraction | ||
* @param multiSet2 [MultiSet]<E>: second MultiSet in subtraction | ||
* @return [MultiSet]<E>: MultiSet containing the items in the first MultiSet but not the second | ||
*/ | ||
internal fun <E> genericMultiSetMinus(multiSet1: MultiSet<E>, multiSet2: MultiSet<E>): MultiSet<E> { | ||
val newSet = mutableMultiSetOf<E>() | ||
val distinctValues = multiSet1.distinctValues + multiSet2.distinctValues | ||
|
||
distinctValues.forEach { element -> | ||
val totalCount = max(0, multiSet1.getCountOf(element) - multiSet2.getCountOf(element)) | ||
repeat(totalCount) { newSet.add(element) } | ||
} | ||
|
||
return newSet | ||
} | ||
|
||
/** | ||
* Generic [MultiSet.intersect] implementation that can be applied to any two MultiSets. | ||
* | ||
* @param multiSet1 [MultiSet]<E>: first MultiSet in intersection | ||
* @param multiSet2 [MultiSet]<E>: second MultiSet in intersection | ||
* @return [MultiSet]<E>: MultiSet containing only values that are in both MultiSets | ||
*/ | ||
internal fun <E> genericMultiSetIntersect(multiSet1: MultiSet<E>, multiSet2: MultiSet<E>): MultiSet<E> { | ||
val newSet = mutableMultiSetOf<E>() | ||
val distinctValues = multiSet1.distinctValues intersect multiSet2.distinctValues | ||
|
||
distinctValues.forEach { element -> | ||
val totalCount = min(multiSet1.getCountOf(element), multiSet2.getCountOf(element)) | ||
repeat(totalCount) { newSet.add(element) } | ||
} | ||
|
||
return newSet | ||
} |
Oops, something went wrong.