deque

Java equivalent of std::deque

I'm a relatively new Java programmer coming from C++/STL, and am looking for a class with these characteristics (which the C++ std::deque has, as I understand it): O(1) performance for insertion/removal at the beginning/end O(1) performance for lookup by index are growable collections (don't need fixed size bounds) Is there a Java eq...

STL-like vector with arbitrary index range

What I want is something similar to STL vector when it comes to access complexity, reallocation on resize, etc. I want it to support arbitrary index range, for example there could be elements indexed from -2 to +7 or from +5 to +10. I want to be able to push_front efficiently. Also I want two-way resize... I know I could write something...

Why does push_back or push_front invalidate a deque's iterators?

As the title asks. My understanding of a deque was that it allocated "blocks". I don't see how allocating more space invalidates iterators, and if anything, one would think that a deque's iterators would have more guarantees than a vector's, not less. ...

Confusion on iterators invalidation in deque

I'm bit confused regarding iterator invalidation in deque. (In the context of this question) Following is the excerpts from -- The C++ Standard Library: A Tutorial and Reference, By Nicolai M. Josuttis Any insertion or deletion of elements other than at the beginning or end invalidates all pointers, references, and iterator...

What is a data structure that has O(1) for append, prepend, and retrieve element at any location?

I'm looking for Java solution but any general answer is also OK. Vector/ArrayList is O(1) for append and retrieve, but O(n) for prepend. LinkedList (in Java implemented as doubly-linked-list) is O(1) for append and prepend, but O(n) for retrieval. Deque (ArrayDeque) is O(1) for everything above but cannot retrieve element at arbitrary...

How to release memory from std::deque?

I'm using a std::deque to store a fairly large number of objects. If I remove a bunch of those objects, it appears to me that its memory usage does not decrease, in a similar fashion to std::vector. Is there a way to reduce it? I know that in a vector you have to use the 'swap trick', which I assume would work here too, but I'd rather a...

Java Deque without using any of the existing classes like LinkedList?

Hi, I've got to write a very short bit of code on a deque, however I'm not sure how to write the code for the methods, if someone could help me with one of the methods, (eg. a method to add an object to the from of the deque) then that would get me started. I'm sure I could manage the rest of the methods, just at the moment I'm pretty st...

Best way to obtain indexed access to a Python queue, thread-safe

I have a queue (from the Queue module), and I want to get indexed access into it. (i.e., being able to ask for item number four in the queue, without removing it from the queue.) I saw that a queue uses a deque internally, and deque has indexed access. The question is, how can I use the deque without (1) messing up the queue, (2) breaki...

Why is it so slow iterating over a big std::list ?

As title suggests, I had problems with a program of mine where I used a std::list as a stack and also to iterate over all elements of the list. The program was taking way too long when the lists became very big. Does anyone have a good explanation for this? Is it some stack/cache behavior? (Solved the problem by changing the lists to s...

C++ STL containers: what's the difference between deque and list?

What is the difference between the two? I mean the methods are all the same. So, for a user, they work identically. Is that correct?? ...

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

Hello all! 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 elem...

Why not resize and clear works in GotW 54?

Referring to article Gotw 54 by HerbSutter, he explain about 1.Tthe Right Way To "Shrink-To-Fit" a vector or deque and 2.The Right Way to Completely Clear a vector or deque Can we just use container.resize() and container.clear() for above task or Am I missing something. ...

Lock Free Deque that supports removing an arbitrary node

This needs to be lock free as it has to run in the interrupt handler of an SMP system. I cannot take locks. I have a contiguous array holding some values. Some of the entries in this array are "free", they are not occupied. I want to make a list of these entries so that I can quickly allocate one. However, I occasionally have to allocat...

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

Hey all, 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 perform some work. I thought the problem could be easily solved by a blocking q...

C# - Java's Deque

Hello, in Java, there's a class called Deque, and i would like to find something similar to this in .NET (C#). The reason i need this, is because i need to peek the last item in the collection, and then dequeue the first one in the collection. Thanks, AJ Ravindiran. ...

STL with a custom data type

What am I doing wrong? #include <iostream> #include <deque> using namespace std; struct mystruct { int number1; int number2; }; int main() { std::deque<mystruct> mydeque; mydeque.number1.push_front(77); return 0; } ...

stl::deque's insert(loc, val) - inconsistent behavior at end of deque vs other locations?

Using http://www.cppreference.com/wiki/stl/deque/insert as a reference, I was inserting values into a deque at certain locations. For example, if deque A was: a, b, d, e, g with an iterator pointing to d, i can: A.insert(iter, c); // insert val c before loc iter //deque is now a, b, c, d, e, g and the iter still points to d....

Adding block of elements to the end of std::deque

I've got a wrapper around a std::deque that I'm using to queue up audio data (coming in blocks via libavcodec, if that matters). This is the function that takes a buffer of 16-bit data and adds it to the deque void AVAudioBuffer::enqueue(int16_t* src, size_t num, double pts) { // Save current size of buffer size_t size = data_buffe...

Python deque performance for small iterables

Hey there, I was playing around with Python's collection.deque and wrote the following benchmark: #!/usr/bin/python import timeit if __name__=='__main__': number = 1000000 for r in (1,10,100,1000,5000,10000,100000): print r print timeit.timeit("while x: x.pop(0);", "x = list(range("...

How to store a sequence of timestamped data?

I have an application that need to store a sequence of voltage data, each entry is something like a pair {time, voltage} the time is not necessarily continuous, if the voltage doesn't move, I will not have any reading. The problem is that i also need to have a function that lookup timestamp, like, getVoltageOfTimestamp(float2second(922...