Questions tagged [deque]

A double-ended queue. Container datatype which is typically supporting efficient insertion and removal from two ends.

993 questions
7
votes
1 answer

How can I see the source code of deque module from Python's collections library?

I tried using inspect.getsource(deque) and while it works on other modules from collections, running it on deque throws an error "could not find class definition". Why does this happen only to deque and how can I see it's source code?
7
votes
1 answer

Are there any benchmarks showing good performance of `collections.deque`?

I was always intrigued by Python's collections.deque object. It seems to be like a list, except that adding/deleting items in the beginning is faster than in a list. This makes me want to replace list with deque in various places in my code where I…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
7
votes
1 answer

Fastest way to merge two deques

Exist a faster way to merge two deques than this? # a, b are two deques. The maximum length # of a is greater than the current length # of a plus the current length of b while len(b): a.append(b.popleft()) Note that I'm not interested in…
gvgramazio
  • 1,115
  • 3
  • 13
  • 30
7
votes
2 answers

Can I use std::max_element() on std::deque in c++11?

Can I code like std::max_element(std::begin(my_deque), std::end(my_deque))? I am asking because I know deque is not guaranteed to store continuously, so I want to know if it will behave correctly when using functions involving iterator like…
user5025141
  • 399
  • 1
  • 3
  • 15
7
votes
3 answers

Anyone know this Python data structure?

The Python class has six requirements as listed below. Only bold terms are to be read as requirements. Close to O(1) performance for as many of the following four operations. Maintaining sorted order while inserting an object into the…
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
7
votes
1 answer

Python Deque appendleft with list

I am currently creating my deque object using the following, self.CommandList = deque((['S', False, 60],['c'],['g16'],['i50'],['r30', True],['u320'],['o5000'],['b1'],['B4500'],['W1'],['l5154'],['!10'],['p2', True,…
Schodemeiss
  • 1,144
  • 3
  • 16
  • 37
7
votes
5 answers

What's the Big-O of a stack, queue, set, and deque?

What is the Big-O efficiency of a stack, queue, set, and deque as it pertains to insertion, search, indexing, space, and deletion complexities?
cereallarceny
  • 4,913
  • 4
  • 39
  • 74
7
votes
3 answers

Vector vs Deque operator[]

Deques are to be chosen over vectors if we are constantly adding in front and in the back of the container. But what about offseting? Do vector's and deque's operator[] work the same, or not? If not, which one is faster?
user2653125
  • 185
  • 2
  • 9
7
votes
6 answers

Building a multithreaded work-queue (consumer/producer) in C++

I have the following scenario: I have a single thread that is supposed to fill a container with pairs of integers (in essence, task descriptions), and I have a large number of worker threads (8-16) that should take elements from this container and…
Thomas Dullien
7
votes
2 answers

std::deque or std::list

I am using push_front() and push_back() only. Thus, I do not incur any other cost of using insert() or remove(). I know both containers offer O(1) complexity for each of these functions, deques having amortized constant time compared to lists having…
Intern87
  • 469
  • 1
  • 6
  • 18
7
votes
2 answers

std deque is surprisingly slow

I just find out that standard std deque is really slow when comparing with my "home-made" version of stack that use pre-allocated array. This is code of my stack: template class FastStack { public: T* st; int allocationSize; …
icl7126
  • 5,740
  • 4
  • 53
  • 51
6
votes
3 answers

About deque's extra indirection

Wondering why my memory accesses were somewhat slower than I expected, I finally figured out that the Visual C++ implementation of deque indeed has an extra layer of indirection built-in, destroying my memory locality. i.e. it seems to hold an array…
user541686
  • 205,094
  • 128
  • 528
  • 886
6
votes
4 answers

Why typical Array List implementations aren't double-ended?

Why aren't ArrayLists generally implemented to be double-ended, which would support fast amortized insertion in the front as well as the back? Is there ever a disadvantage to using the latter over the former? (I'm not talking just about Java -- I…
user541686
  • 205,094
  • 128
  • 528
  • 886
6
votes
1 answer

Why is deque faster than queue?

I have the following working codes (g++ 8.2 , the C++17 standard.) queue q{}; q.push(root); q.push(nullptr); int sum = root -> val; while (!q.empty()) { TreeNode *n = q.front(); q.pop(); if (n…
Edamame
  • 23,718
  • 73
  • 186
  • 320
6
votes
2 answers

Std::deque does not release memory until program exits

On linux, std::deque does not release memory until program exits. The complete code is below. Any help will be greatly appreciated! #include #include #include #include #include #include…
wu3mei
  • 61
  • 1
  • 2