deque

STL deque accessing by index is O(1)?

别等时光非礼了梦想. 提交于 2019-11-27 02:30:25
问题 I've read that accessing elements by position index can be done in constant time in a STL deque. As far as I know, elements in a deque may be stored in several non-contiguous locations, eliminating safe access through pointer arithmetic. For example: abc->defghi->jkl->mnop The elements of the deque above consists of a single character. The set of characters in one group indicate it is allocated in contiguous memory (e.g. abc is in a single block of memory, defhi is located in another block of

deque.popleft() and list.pop(0). Is there performance difference?

淺唱寂寞╮ 提交于 2019-11-27 01:51:42
问题 deque.popleft() and list.pop(0) seem to return the same result. Is there any performance difference between them and why? 回答1: deque.popleft() is faster than list.pop(0), because the deque has been optimized to do popleft() approximately in O(1), while list.pop(0) takes O(n) (see deque objects). Comments and code in _collectionsmodule.c for deque and listobject.c for list provide implementation insights to explain the performance differences. Namely that a deque object "is composed of a

How are deques in Python implemented, and when are they worse than lists?

▼魔方 西西 提交于 2019-11-27 00:30:20
I've recently gotten into investigating how various data structures are implemented in Python in order to make my code more efficient. In investigating how lists and deques work, I found that I can get benefits when I want to shift and unshift reducing the time from O(n) in lists to O(1) in deques (lists being implemented as fixed-length arrays that have to be copied completely each time something is inserted at the front, etc...). What I can't seem to find are the specifics of how a deque is implemented, and the specifics of its downsides v.s. lists. Can someone enlighten me on these two

Implement an immutable deque as a balanced binary tree?

三世轮回 提交于 2019-11-27 00:10:09
问题 I've been thinking for a while about how to go about implementing a deque (that is, a double-ended queue) as an immutable data structure. There seem to be different ways of doing this. AFAIK, immutable data structures are generally hierarchical, so that major parts of it can be reused after modifying operations such as the insertion or removal of an item. Eric Lippert has two articles on his blog about this topic, along with sample implementations in C#. Both of his implementations strike me

C++ deque's iterator invalidated after push_front()

徘徊边缘 提交于 2019-11-26 20:47:46
问题 Just now, I'm reading Josuttis' STL book. As far as I know -- c++ vector is a c-array that can be reallocated. So, I understand, why after push_back() all iterators and references can become invalid. But my question is about std::deque. As I know it is array of large blocks (c-array of c-arrays). So push_front() inserts element at the beginning and if there is no space, deque allocates new block, and places the element at allocated block's end. After insert() in the middle all references and

Why would I prefer using vector to deque

醉酒当歌 提交于 2019-11-26 17:29:49
问题 Since they are both contiguous memory containers; feature wise, deque has almost everything vector has but more, since it is more efficient to insert in the front. Why whould anyone prefer std::vector to std::deque ? 回答1: Elements in a deque are not contiguous in memory; vector elements are guaranteed to be. So if you need to interact with a plain C library that needs contiguous arrays, or if you care (a lot) about spatial locality, then you might prefer vector . In addition, since there is

How are deques in Python implemented, and when are they worse than lists?

本小妞迷上赌 提交于 2019-11-26 09:24:02
问题 I\'ve recently gotten into investigating how various data structures are implemented in Python in order to make my code more efficient. In investigating how lists and deques work, I found that I can get benefits when I want to shift and unshift reducing the time from O(n) in lists to O(1) in deques (lists being implemented as fixed-length arrays that have to be copied completely each time something is inserted at the front, etc...). What I can\'t seem to find are the specifics of how a deque

What really is a deque in STL?

萝らか妹 提交于 2019-11-26 03:19:10
问题 I was looking at STL containers and trying to figure what they really are (i.e. the data structure used), and the deque stopped me: I thought at first that it was a double linked list, which would allow insertion and deletion from both ends in constant time, but I am troubled by the promise made by the operator [] to be done in constant time. In a linked list, arbitrary access should be O(n), right? And if it\'s a dynamic array, how can it add elements in constant time? It should be mentioned