5

I need something that's cache efficient, but where I can also insert quickly at both front and back. C# has Queue, but the documentation doesn't say anything about the underlying implementation. For example, it could be implemented as some kind of linked list.

So is there an equivalent to C++ std::deque in C#?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Noone
  • 51
  • 2
  • 2
    You can check Microsoft Reference Source to know more about the implementation and see if it fits your requirements [**Queue on MS Reference Source**](http://referencesource.microsoft.com/#mscorlib/system/collections/queue.cs) – mishamosher Jun 26 '16 at 23:30
  • 1
    According to the above, Queue is implemented as a ring buffer. – Etienne de Martel Jun 26 '16 at 23:33
  • Just to have a direct link, the generic implementation is on here: [**Queue on MS Reference Source**](http://referencesource.microsoft.com/#System/compmod/system/collections/generic/queue.cs) – mishamosher Jun 26 '16 at 23:36
  • 2
    I think LinkedList is what you're looking for. – lexx9999 Jun 26 '16 at 23:45
  • @mishamosher Thanks! – Noone Jun 27 '16 at 00:00
  • 1
    @lexx9999: `LinkedList` is .NET's equivalent to C++ `std::list`. `deque` is quite a different arrangement. – Ben Voigt Jun 27 '16 at 00:01
  • 2
    @lexx9999 A linked list is what I am trying to avoid. It has poor cache performance. – Noone Jun 27 '16 at 00:01
  • @Noone you're welcome (: – mishamosher Jun 27 '16 at 00:02
  • @mishamosher Looking at Queue source, it seems that it's implemented as a contiguous ring buffer, so cache performance should be good. I would answer this question myself, but a user does not have a name ;) – Noone Jun 27 '16 at 00:24
  • @Noone However, its not a bad idea to answer it, Q&A style (or self-answered) questions are encouraged by the FAQ of Stack Overflow, they allow for anyone that searches for your question to rapidly see that it has a working solution and not ignore it just because the answered check was not present. – mishamosher Jun 27 '16 at 00:27
  • Isn't this a duplicate of http://stackoverflow.com/questions/15599127/c-sharp-equivalent-for-c-vector-or-deque ? – Jim Mischel Jun 27 '16 at 00:32
  • 1
    @JimMischel The user that asks this question specifically requires an equivalent to `std::deque`, and not an equivalent to c++ `vector` _or_ `deque`. Users without much experience reaching this question and seeing it as duplicate can think that `vector` is the same as `deque`. I do not think that this is a duplicate. – mishamosher Jun 27 '16 at 00:36
  • Ever find a solution for this? The main issue I have with Queue is that it is a ring buffer, which means it does not work well when one with many items needs to be resized. With std::deque, or my implementation of “bigdeque”, which assumes that it will be relatively big, and creates larger “pages” – bpeikes Feb 23 '23 at 02:56

0 Answers0