Skip to content

Commit

Permalink
Backend: Queue drain Functions (#1182)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
Thunderblade73 and hannibal002 authored Apr 25, 2024
1 parent 64c502e commit 0eed4b0
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/main/java/at/hannibal2/skyhanni/utils/CollectionUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down

0 comments on commit 0eed4b0

Please sign in to comment.