Container with fast inserts and index? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am looking for a C++ container class that is indexed like an std::vector, but has fast insertions, deletions and indexing. For example, a vector interface implemented with an underlying balancing tree would have O(logN) insertions/deletions and O(logN) indexing as well.
To be clear: I am not looking for std::map<int, T>. Inserting an element at index N should increment indices of all subsequent elements in the array which would not be the case with std::map<int, T>.
I have found AVL Array which does exactly what I am looking for. It has the right interface, but I would like to see if there are other options.
Do you know any other (production-quality) implementations? Maybe something more popular (does boost have something of the sort?). Something with a smaller memory footprint? (A node holding a pointer in the AVL Array is 64 bytes on my machine.)

Thought about using SkipLists yet? Basically it is a linked list, with multiple levels of shortcuts added on top that are organised as a tree. No shuffling of nodes, just a few pointer updates. The shortcuts allow you to iterate much faster across your list. One of my favorites.
http://openmymind.net/Building-A-Skiplist/
http://en.wikipedia.org/wiki/Skip_list

Related

i want a C++ priority-queue implement, and when the element is popped, the corresponding queue space is released? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 10 months ago.
Improve this question
Which C++ third-party library implements the queue, and when the element is popped, the corresponding queue space is released?
c++ stl library support queue.but when i pop() some elements,queue use memory does not be samll.So i want to find a third-party library which can solve this problem.
The C++ standard library has a class that suits your needs: It's std::list. Just use its push_front and pop_back member functions. The memory of any element you pop will immediately be freed.
Note that this is only worth the trouble if your elements are large. Otherwise, the allocation overhead of the linked list nodes will outweigh the advantage of freeing nodes immediately.
Also keep in mind that your malloc implementation won't give the memory back to the operating system immediately. (Or at all, really. Looking at you, glibc.) So you may not see the program's memory usage decrease immediately, anyway. However, your program can immediately re-use the memory of any freed nodes.

C++ implementation of 2-3-4 Trees [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I was looking for C++ implementation of 2-3-4 Trees online and was surprised that
There is no code available for it. I couldn't find anything. I have studies this Trees
But writing code is difficult for me as of now so I wanted to look at some already
implemented code. Is there an easy way to implement it using a 2-3 Tree or some other
existing Data Structure or one has to start from scratch to implement it ?
Any links/references or ideas will help
You're unlikely to find a production-quality implementation. A red-black tree is an isomorphic structure to a 2-3-4 tree, and is more efficient and easier to work with. So you'll find plenty of RB trees, and they're basically the same thing. (You could rework a RB tree into a 2-3-4 tree but that would just make it worse.)

Good C++ data structure implementation of *min-heap* augmented with *hash table* [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I am looking an efficient C++ data structure implementation of min-heap augmented with hash table.
There is a counter-part in python which called pqdict.
Priority Queue Dictionary — pqdict 1.0.0 documentation
https://pqdict.readthedocs.io/
To be more specific, I want to use this data structure as the open list for an efficient a* search implementation.
I hope there already exists one so I do not need to re-implement.
I assume you want this kind of data structure to support the decrease_key operation...
When I implement A* or Dijkstra's algorithm, I just don't do it that way.
In C++, I would:
put (node *,priority) records in a std::priority_queue, and also store the priorities in the nodes.
When decreasing the priority in a node, just insert another record into the priority queue and leave the old one where it is.
When popping a record off the priority queue, check to see if the priority is accurate. If it isn't then discard it and pop again.
Keep track of the number of invalid records in the priority queue. When/if the number of invalid records grows to half the size of the priority queue, then clear and rebuild the priority queue with only valid records.
This sort of system is easy to implement, doesn't affect the complexity of Dijkstra's algorithm or A*, and uses less memory than most of the kinds of data structure you're asking for.

c++ stl containers:where can we use them [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
So we know that the basic structures which form the backbone of C++ algorithms are:
trees set
queue
linkedlist
array
vector map
unordered_map and pair.
My question is which data structure is suitable for which application.For instance I know that for Database indexing and searching preferred choices are B+ tree and Hash table.Can anyone shed some more light on this,
This is not only a C++ problem, but also an algorithm question. It maybe too broad, but I can give you some advice.
set and map: They are ordered container, it is used for a both manytimes-insert-and-read structure. It can finish insert delete read in O(logn) time.
vector: used for something like dynamic array or a structure you will frequently push_back at it, and if no other reason, you should use it.
deque: much like vector, but it can also finish push_front in O(1) time
list: used for a structure you need to frequently insert, but less random access
unordered_map and unordered_set: look for hash table
array: used for a structure whose size is fixed.
pair and tuple: bind many object into one struct. Nothing special
Beside all of this, there are also some container meeting other requirement, you can serach them.
e.g. any and optional

iterator++ complexity for stl map [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What's the complexity of iterator++ operation for stl RB-Tree(set or map)?
I always thought they would use indices thus the answer should be O(1), but recently I read the vc10 implementation and shockly found that they did not.
To find the next element in an ordered RB-Tree, it would take time to search the smallest element in the right subtree, or if the node is a left child and has no right child, the smallest element in the right sibling. This introduce a recursive process and I believe the ++ operator takes O(lgn) time.
Am I right? And is this the case for all stl implementations or just visual C++?
Is it really difficult to maintain indices for an RB-Tree? As long as I see, by holding two extra pointers in the node structure we can maintain a doubly linked list as long as the RB-Tree. Why don't they do that?
The amortized complexity when incrementing the iterator over the whole container is O(1) per increment, which is all that's required by the standard. You're right that a single increment is only O(log n), since the depth of the tree has that complexity class.
It seems likely to me that other RB-tree implementations of map will be similar. As you've said, the worst-case complexity for operator++ could be improved, but the cost isn't trivial.
It quite possible that the total time to iterate the whole container would be improved by the linked list, but it's not certain since bigger node structures tend to result in more cache misses.