-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved performance and reduced stack complexity
- Loading branch information
Showing
14 changed files
with
325 additions
and
195 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
76 changes: 76 additions & 0 deletions
76
common/src/main/scala/hexacraft/util/UniqueLongQueue.scala
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,76 @@ | ||
package hexacraft.util | ||
|
||
import scala.collection.mutable | ||
|
||
class LongSet { | ||
private val sets: mutable.LongMap[mutable.BitSet] = mutable.LongMap.empty | ||
|
||
def add(elem: Long): Boolean = { | ||
val chunk = elem >>> 12 | ||
val block = (elem & ((1 << 12) - 1)).toInt | ||
val set = sets.getOrElseUpdate(chunk, new mutable.BitSet(16 * 16 * 16)) | ||
|
||
val isNew = !set.contains(block) | ||
if isNew then { | ||
set.addOne(block) | ||
} | ||
isNew | ||
} | ||
|
||
def remove(elem: Long): Unit = { | ||
val chunk = elem >>> 12 | ||
val block = (elem & ((1 << 12) - 1)).toInt | ||
val set = sets.getOrElseUpdate(chunk, new mutable.BitSet(16 * 16 * 16)) | ||
|
||
set.subtractOne(block) | ||
} | ||
|
||
def clear(): Unit = { | ||
sets.clear() | ||
} | ||
} | ||
|
||
class UniqueLongQueue { | ||
private val q: mutable.Queue[Long] = mutable.Queue.empty | ||
private val set: LongSet = new LongSet | ||
|
||
def enqueue(elem: Long): Unit = { | ||
if set.add(elem) then { | ||
q.enqueue(elem) | ||
} | ||
} | ||
|
||
def enqueueMany(elems: Iterable[Long]): Unit = { | ||
q.ensureSize(q.size + elems.size) | ||
|
||
val it = elems.iterator | ||
while it.hasNext do { | ||
val elem = it.next | ||
if set.add(elem) then { | ||
q.enqueue(elem) | ||
} | ||
} | ||
} | ||
|
||
def dequeue(): Long = { | ||
val elem = q.dequeue() | ||
set.remove(elem) | ||
elem | ||
} | ||
|
||
inline def drainInto(inline f: Long => Unit): Unit = { | ||
set.clear() | ||
while q.nonEmpty do { | ||
f(q.dequeue()) | ||
} | ||
} | ||
|
||
def size: Int = q.length | ||
|
||
def isEmpty: Boolean = q.isEmpty | ||
|
||
def clear(): Unit = { | ||
q.clear() | ||
set.clear() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.