diff options
author | Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> | 2024-04-25 11:53:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-25 11:53:59 +0200 |
commit | 0eed4b0d2121a206d4eba81aa4a1888125acbe3e (patch) | |
tree | 6673c7ed37ed4603902200bc30a29d64db99954d | |
parent | 64c502ef79a8fd309a1a4654285a77632e02068c (diff) | |
download | skyhanni-0eed4b0d2121a206d4eba81aa4a1888125acbe3e.tar.gz skyhanni-0eed4b0d2121a206d4eba81aa4a1888125acbe3e.tar.bz2 skyhanni-0eed4b0d2121a206d4eba81aa4a1888125acbe3e.zip |
Backend: Queue drain Functions (#1182)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt index 3082112ce..d33729ae3 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt @@ -4,14 +4,35 @@ import at.hannibal2.skyhanni.utils.NEUItems.getItemStack import at.hannibal2.skyhanni.utils.renderables.Renderable import net.minecraft.item.ItemStack import java.util.Collections +import java.util.Queue import java.util.WeakHashMap -import java.util.concurrent.ConcurrentLinkedQueue object CollectionUtils { - fun <E> ConcurrentLinkedQueue<E>.drainTo(list: MutableCollection<E>) { + inline fun <reified T : Queue<E>, reified E> T.drainForEach(action: (E) -> Unit): T { + while (true) + action(this.poll() ?: break) + return this + } + + inline fun <reified T : Queue<E>, reified E> T.drain(amount: Int): T { + for (i in 1..amount) + this.poll() ?: break + return this + } + + inline fun <reified E, reified K, reified L : MutableCollection<K>> + Queue<E>.drainTo(list: L, action: (E) -> K): L { + while (true) + list.add(action(this.poll() ?: break)) + return list + } + + inline fun <reified E, reified L : MutableCollection<E>> + Queue<E>.drainTo(list: L): L { while (true) list.add(this.poll() ?: break) + return list } // Let garbage collector handle the removal of entries in this list @@ -143,13 +164,6 @@ object CollectionUtils { return toList().sorted().reversed().toMap() } - inline fun <reified T> ConcurrentLinkedQueue<T>.drainForEach(action: (T) -> Unit) { - while (true) { - val value = this.poll() ?: break - action(value) - } - } - fun <T> Sequence<T>.takeWhileInclusive(predicate: (T) -> Boolean) = sequence { with(iterator()) { while (hasNext()) { |