SequencedPriorityIterator .java

net.minecraft.util.SequencedPriorityIterator (class): members: highestPrioQueue, queuesByPriority, highestPrio, MIN_PRIO, computeNext, add. imports: Entry.

net.minecraft.utilclass69 linescommonextends AbstractIterator<T>2.0 KB

API Surface (1)

void add(final T data, final int priority)

Fields & Constants (4)

MIN_PRIO
highestPrioQueue
highestPrio
queuesByPriority

Full Source

SequencedPriorityIterator.java69 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util;
2
3
import com.google.common.collect.AbstractIterator;
4
import com.google.common.collect.Queues;
5
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
6
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
7
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
8
import it.unimi.dsi.fastutil.ints.Int2ObjectMap.Entry;
9
import java.util.Deque;
10
import org.jspecify.annotations.Nullable;
11
12
public final class SequencedPriorityIterator<T> extends AbstractIterator<T> {
13
	private static final int MIN_PRIO = Integer.MIN_VALUE;
14
	private @Nullable Deque<T> highestPrioQueue = null;
15
	private int highestPrio = Integer.MIN_VALUE;
16
	private final Int2ObjectMap<Deque<T>> queuesByPriority = new Int2ObjectOpenHashMap<>();
17
18
	public void add(final T data, final int priority) {
19
		if (priority == this.highestPrio && this.highestPrioQueue != null) {
20
			this.highestPrioQueue.addLast(data);
21
		} else {
22
			Deque<T> queue = this.queuesByPriority.computeIfAbsent(priority, order -> Queues.newArrayDeque());
23
			queue.addLast(data);
24
			if (priority >= this.highestPrio) {
25
				this.highestPrioQueue = queue;
26
				this.highestPrio = priority;
27
			}
28
		}
29
	}
30
31
	@Override
32
	protected @Nullable T computeNext() {
33
		if (this.highestPrioQueue == null) {
34
			return this.endOfData();
35
		}
36
37
		T result = this.highestPrioQueue.removeFirst();
38
		if (result == null) {
39
			return this.endOfData();
40
		}
41
42
		if (this.highestPrioQueue.isEmpty()) {
43
			this.switchCacheToNextHighestPrioQueue();
44
		}
45
46
		return result;
47
	}
48
49
	private void switchCacheToNextHighestPrioQueue() {
50
		int foundHighestPrio = Integer.MIN_VALUE;
51
		Deque<T> foundHighestPrioQueue = null;
52
53
		for (Entry<Deque<T>> entry : Int2ObjectMaps.fastIterable(this.queuesByPriority)) {
54
			Deque<T> queue = entry.getValue();
55
			int prio = entry.getIntKey();
56
			if (prio > foundHighestPrio && !queue.isEmpty()) {
57
				foundHighestPrio = prio;
58
				foundHighestPrioQueue = queue;
59
				if (prio == this.highestPrio - 1) {
60
					break;
61
				}
62
			}
63
		}
64
65
		this.highestPrio = foundHighestPrio;
66
		this.highestPrioQueue = foundHighestPrioQueue;
67
	}
68
}
69

Decompiled Minecraft 26.3 source of SequencedPriorityIterator (net/minecraft/util/SequencedPriorityIterator.java) with Mojang official mappings, stored verbatim on the site.