Package cn.nukkit.utils
Class SortedList<T>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<T>
cn.nukkit.utils.SortedList<T>
- Type Parameters:
T
- 列表类型.
- All Implemented Interfaces:
Serializable
,Iterable<T>
,Collection<T>
,List<T>
@PowerNukkitXOnly
@Since("1.6.0.0-PNX")
public class SortedList<T>
extends AbstractList<T>
implements Serializable
SortedList是
List
的一个有序实现,内部是用平衡二叉树实现的。
请注意,你不能对这个列表做指定项操作,除了remove(int)
,remove(Object)
,clear
和add()
。
此列表操作的时间复杂度如下:contains
,add
,remove
以及get
的时间复杂度为O(log(n))。
此列表不保证线程安全,若有必要,请使用Collections.synchronizedList(List)
包装以确保线程安全。
这个列表提供的迭代器是快速失效的,所以除了通过迭代器本身之外的任何结构修改都会导致它抛出ConcurrentModificationException
。
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
用于表示树中位置的内部类。每个节点存储一个相等值的列表,包含它们的子节点和父节点、在该点扎根的子树的高度以及它们拥有的子元素的总数。 -
Field Summary
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorDescriptionSortedList
(Comparator<? super T> comparator) 构造一个新的空SortedList,根据给定的比较器对元素进行排序。 -
Method Summary
Modifier and TypeMethodDescriptionprotected void
add
(SortedList<T>.Node toAdd) 将给定节点添加到此SortedList。boolean
将给定对象插入SortedList的适当位置,以确保列表中的元素按给定比较器指定的顺序保存。void
clear()
boolean
返回给定对象是否在此SortedList中。protected SortedList<T>.Node
findFirstNodeWithValue
(T value) 返回表示树中给定值的节点,如果不存在此类节点,则该节点可以为null。protected SortedList<T>.Node
findNodeAtIndex
(int index) get
(int index) 返回此SortedList中给定索引处的元素。由于列表已排序,因此这是从0-n-1开始计算的第四个最小元素的索引。protected SortedList<T>.Node
getRoot()
boolean
isEmpty()
iterator()
提供一个迭代器,该迭代器按照给定比较器确定的顺序提供此SortedList的元素。remove
(int index) 移除并返回此SortedList中给定索引处的元素。由于列表已排序,这是从0-n-1开始计数的第四个最小元素。protected void
remove
(SortedList<T>.Node toRemove) 从这个SortedList中删除给定的节点,如果需要重新平衡,也会添加modCount。 时间复杂度O(log(n))。boolean
删除列表中具有给定值的第一个元素(如果存在这样的节点),否则不执行任何操作。使用给定的比较器对元素进行比较。int
size()
Object[]
toArray()
<E> E[]
toArray
(E[] holder) Methods inherited from class java.util.AbstractList
add, addAll, equals, hashCode, indexOf, lastIndexOf, listIterator, listIterator, removeRange, set, subList
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, removeAll, retainAll, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
addAll, containsAll, removeAll, replaceAll, retainAll, sort, spliterator
-
Constructor Details
-
SortedList
构造一个新的空SortedList,根据给定的比较器对元素进行排序。- Parameters:
comparator
- 给元素排序的比较器
-
-
Method Details
-
add
将给定对象插入SortedList的适当位置,以确保列表中的元素按给定比较器指定的顺序保存。此方法仅允许添加非null值,如果给定对象为null,则列表保持不变并返回false。
- Specified by:
add
in interfaceCollection<T>
- Specified by:
add
in interfaceList<T>
- Overrides:
add
in classAbstractList<T>
- Parameters:
object
- 要添加的元素- Returns:
- 当给定对象为null时为false,否则为true
-
add
将给定节点添加到此SortedList。此方法可以被子类重写,以便更改此列表将存储的节点的定义。
此实现使用
SortedList.Node.compareTo(Node)
方法来确定给定节点应该存储在哪里。它还会增加此列表的modCount。- Parameters:
toAdd
- 要新增的节点
-
iterator
提供一个迭代器,该迭代器按照给定比较器确定的顺序提供此SortedList的元素。此迭代器实现允许以O(n)时间复杂度迭代整个列表,其中n是列表中的元素数。
-
size
public int size()- Specified by:
size
in interfaceCollection<T>
- Specified by:
size
in interfaceList<T>
- Specified by:
size
in classAbstractCollection<T>
- Returns:
- 存储在此SortedList中的元素数量。
-
getRoot
- Returns:
- 此SortedList的根节点,如果此列表为空,则为空。
-
contains
返回给定对象是否在此SortedList中。元素比较使用
Object.equals(Object)
方法,并假设给定的obj必须具有与此SortedList中的元素相等的T类型。 时间复杂度为O(log(n)),其中n是列表中的元素数。- Specified by:
contains
in interfaceCollection<T>
- Specified by:
contains
in interfaceList<T>
- Overrides:
contains
in classAbstractCollection<T>
- Parameters:
obj
- 要检查存在性的对象- Returns:
- 是否存在于此SortedList中
-
findFirstNodeWithValue
返回表示树中给定值的节点,如果不存在此类节点,则该节点可以为null。该方法使用给定的比较器执行二进制搜索,时间复杂度为O(log(n))。
- Parameters:
value
- 要搜索的值- Returns:
- 此列表中具有给定值的第一个节点
-
remove
移除并返回此SortedList中给定索引处的元素。由于列表已排序,这是从0-n-1开始计数的第四个最小元素。例如,调用remove(0)将删除列表中最小的元素。
- Specified by:
remove
in interfaceList<T>
- Overrides:
remove
in classAbstractList<T>
- Parameters:
index
- 要删除的元素的索引- Returns:
- 被删除的元素
- Throws:
IllegalArgumentException
- 如果索引不是有效的索引则抛出此异常
-
remove
删除列表中具有给定值的第一个元素(如果存在这样的节点),否则不执行任何操作。使用给定的比较器对元素进行比较。返回是否找到并删除了匹配的元素。
- Specified by:
remove
in interfaceCollection<T>
- Specified by:
remove
in interfaceList<T>
- Overrides:
remove
in classAbstractCollection<T>
- Parameters:
value
- 要移除的元素- Returns:
- 是否找到并删除了匹配的元素。
-
remove
从这个SortedList中删除给定的节点,如果需要重新平衡,也会添加modCount。 时间复杂度O(log(n))。- Parameters:
toRemove
- 此SortedList中的节点
-
get
返回此SortedList中给定索引处的元素。由于列表已排序,因此这是从0-n-1开始计算的第四个最小元素的索引。例如,调用get(0)将返回列表中最小的元素。
- Specified by:
get
in interfaceList<T>
- Specified by:
get
in classAbstractList<T>
- Parameters:
index
- 要获取的元素的索引- Returns:
- 此SortedList中给定索引处的元素
- Throws:
IllegalArgumentException
- 如果索引不是有效的索引则抛出此异常
-
findNodeAtIndex
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<T>
- Specified by:
isEmpty
in interfaceList<T>
- Overrides:
isEmpty
in classAbstractCollection<T>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<T>
- Specified by:
clear
in interfaceList<T>
- Overrides:
clear
in classAbstractList<T>
-
toArray
- Specified by:
toArray
in interfaceCollection<T>
- Specified by:
toArray
in interfaceList<T>
- Overrides:
toArray
in classAbstractCollection<T>
-
toArray
public <E> E[] toArray(E[] holder) - Specified by:
toArray
in interfaceCollection<T>
- Specified by:
toArray
in interfaceList<T>
- Overrides:
toArray
in classAbstractCollection<T>
-