To copy a LinkedHashSet from JAVA to C ++, I think you will need two vanilla std::map (note that instead you get a LinkedTreeSet instead of a real LinkedHashSet, which will get O (log n) for insertion and deletion) for this to work .
- The actual value as the key and the insertion order (usually int or long int) are used as the value.
- Others are inverse, the insertion order is used as the value and value as the value.
When you are going to embed, you use std::map::find in the first std::map to make sure that it does not have an identical object.
- If it already exists, ignore the new one.
- If this is not the case, you map this object to the incremental insertion order on both
std::map mentioned earlier.
When you are going to iterate over the insertion order, you repeat the second std::map , since it will be sorted by the insertion order (everything that falls into std::map or std::set will be sorted automatically).
When you are about to remove an element from it, use std::map::find to get the insertion order. Using this insertion order, remove the element from the second std::map and remove the object from the first.
Please note that this solution is not ideal, if you plan to use it on a long-term basis , you will need to βshrinkβ the insertion order after a certain number of paragraphs, since you will end the insertion order (indexes 2 ^ 32 for unsigned int or 2 ^ 64 indexes for unsigned long long int). To do this, you will need to put all the βvalueβ objects in the vector, clear all the values ββfrom both cards, and then reinsert the values ββfrom the vector back into both cards. This procedure takes O (nlogn) time.
If you use C ++ 11, you can replace the first std::map with std::unordered_map to increase efficiency, however you cannot replace it with the second. The reason is that the std::unordered map uses a hash code for indexing so that the index cannot be reliably sorted in this situation.
source share