SequencedPriorityIterator.java
net.minecraft.util.SequencedPriorityIterator
信息
- 全限定名:net.minecraft.util.SequencedPriorityIterator
- 类型:public final class
- 包:net.minecraft.util
- 源码路径:src/main/java/net/minecraft/util/SequencedPriorityIterator.java
- 起始行号:L12
- 继承:AbstractIterator
- 职责:
TODO
字段/常量
-
MIN_PRIO- 类型:
int - 修饰符:
private static final - 源码定位:
L13 - 说明:
TODO
- 类型:
-
highestPrioQueue- 类型:
Deque<T> - 修饰符:
private - 源码定位:
L14 - 说明:
TODO
- 类型:
-
highestPrio- 类型:
int - 修饰符:
private - 源码定位:
L15 - 说明:
TODO
- 类型:
-
queuesByPriority- 类型:
Int2ObjectMap<Deque<T>> - 修饰符:
private final - 源码定位:
L16 - 说明:
TODO
- 类型:
内部类/嵌套类型
- 无
构造器
- 无
方法
下面的方法块按源码顺序生成。
public void add(T data, int priority) @ L18
- 方法名:add
- 源码定位:L18
- 返回类型:void
- 修饰符:public
参数:
- data: T
- priority: int
说明:
TODO
protected T computeNext() @ L31
- 方法名:computeNext
- 源码定位:L31
- 返回类型:T
- 修饰符:protected
参数:
- 无
说明:
TODO
private void switchCacheToNextHighestPrioQueue() @ L49
- 方法名:switchCacheToNextHighestPrioQueue
- 源码定位:L49
- 返回类型:void
- 修饰符:private
参数:
- 无
说明:
TODO
代码
public final class SequencedPriorityIterator<T> extends AbstractIterator<T> {
private static final int MIN_PRIO = Integer.MIN_VALUE;
private @Nullable Deque<T> highestPrioQueue = null;
private int highestPrio = Integer.MIN_VALUE;
private final Int2ObjectMap<Deque<T>> queuesByPriority = new Int2ObjectOpenHashMap<>();
public void add(T data, int priority) {
if (priority == this.highestPrio && this.highestPrioQueue != null) {
this.highestPrioQueue.addLast(data);
} else {
Deque<T> queue = this.queuesByPriority.computeIfAbsent(priority, order -> Queues.newArrayDeque());
queue.addLast(data);
if (priority >= this.highestPrio) {
this.highestPrioQueue = queue;
this.highestPrio = priority;
}
}
}
@Override
protected @Nullable T computeNext() {
if (this.highestPrioQueue == null) {
return this.endOfData();
} else {
T result = this.highestPrioQueue.removeFirst();
if (result == null) {
return this.endOfData();
} else {
if (this.highestPrioQueue.isEmpty()) {
this.switchCacheToNextHighestPrioQueue();
}
return result;
}
}
}
private void switchCacheToNextHighestPrioQueue() {
int foundHighestPrio = Integer.MIN_VALUE;
Deque<T> foundHighestPrioQueue = null;
for (Entry<Deque<T>> entry : Int2ObjectMaps.fastIterable(this.queuesByPriority)) {
Deque<T> queue = entry.getValue();
int prio = entry.getIntKey();
if (prio > foundHighestPrio && !queue.isEmpty()) {
foundHighestPrio = prio;
foundHighestPrioQueue = queue;
if (prio == this.highestPrio - 1) {
break;
}
}
}
this.highestPrio = foundHighestPrio;
this.highestPrioQueue = foundHighestPrioQueue;
}
}引用的其他类
- 无