简体   繁体   English

Dart 中的 DoubleLinkedQueue 和 ListQueue 有什么区别?

[英]What's the difference between a DoubleLinkedQueue and a ListQueue in Dart?

The Dart core API has two classes that implement the Queue<E> interface, DoubleLinkedQueue<E> and ListQueue<E> . Dart 核心 API 有两个实现Queue<E>接口的类, DoubleLinkedQueue<E>ListQueue<E>

The documentation of both classes is almost identical, the only difference that is explicitly mentioned is the following note in the ListQueue<E> documentation:这两个类的文档几乎相同,唯一明确提到的区别是ListQueue<E>文档中的以下注释:

Operations like removeAll and removeWhere are very inefficient.removeAllremoveWhere这样的操作效率很低。 If those are needed, use a DoubleLinkedQueue instead.如果需要这些,请改用DoubleLinkedQueue

What is the actual difference between them implementation-wise and when should which implementation be used?它们在实现方面的实际区别是什么,什么时候应该使用哪个实现?

The DoubleLinkedQueue is basically a Queue implemented on top of a double-linked list. DoubleLinkedQueue基本上是一个在双向链表之上实现的队列。 This means that removing elements at arbitrary positions in it is fast, since it only requires the adjustment of pointers.这意味着删除其中任意位置的元素很快,因为它只需要调整指针。

The ListQueue is implemented on top of a List. ListQueue是在 List 之上实现的。 First and last are indices into the list.第一个和最后一个是列表中的索引。 In general this is the more efficient implementation, since it has less memory-overhead than the double-linked list.一般来说,这是更有效的实现,因为它比双链表具有更少的内存开销。

You can see both implementations here你可以在这里看到这两种实现

Most of the time you want to use the ListQueue .大多数时候你想使用ListQueue For that reason, the Queue interface defaults to the ListQueue (ie Queue() returns a ListQueue ).出于这个原因, Queue接口默认为 ListQueue (即Queue()返回一个ListQueue )。

The DoubleLinkedQueue implementation is mostly useful if you need to selectively remove elements inside the queue.如果您需要有选择地删除队列内的元素,则DoubleLinkedQueue实现最有用。 It's a relatively rare scenario, and the main-reason the class is in the dart-libraries, is that the DoubleLinkedQueue existed before the ListQueue .这是一种相对罕见的情况,该类在 dart 库中的DoubleLinkedQueueDoubleLinkedQueue存在于ListQueue之前。 Since we already had the DoubleLinkedQueue we kept it.因为我们已经有了DoubleLinkedQueue我们保留了它。 If we had started out with the ListQueue we probably wouldn't have added the DoubleLinkedQueue .如果我们从 ListQueue 开始,我们可能不会添加DoubleLinkedQueue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM