Linked lists in C, with open source ADT
xanthu.com
xanthu.com
Thanks.
I prefer to store next/prev pointers with the data itself, and to use offsetof/containerof to find a pointer to the data struct which contains a given set of next/prev pointers.
One example of such an scheme is the list used in the Linux kernel:
"This result should not surprise any experienced programmers, but unfortunately it does many of us."
whistles
# The value type (int in this case) is very small, in fact it's pointer-sized.
# This entire benchmark easily fits into L2 cache. The benchmark only goes up to 100000 elements, which for an int is only 390K. This can be an advantage of vector, but it's not necessarily such a slam-dunk in real-world programs where there are multiple data structures and other threads contending for the use of the cache.
# The value type has 'trivial' copy/move constructors and assignment operators. It's POD and outright memmove()-able.
# The benchmark code is only able to use list nodes one time. It doesn't take advantage of a list's ability to move items from one container to another without reallocation.
# The benchamark is thoroughly randomized. If it were more typical real-world data (such as supplied by another algorithm or even a malicious attacker) then you would want to consider the possibility of pathological access patterns arising.
So if you meet all of these conditions, definitely use a vector. If not, life is back to being complicated again.
If you responded by pointing out that even when the vector consumes half of physical memory, the worst case running time isn't going to be any worse than iterating through all of physical memory, which isn't really so bad, and that for the vast majority of vectors it's going to cost less than a page fault, maybe take a couple of microseconds, to copy those values, you might have an argument that actually addressed spinlocked's point.
Of course, std::vectors are only good for copyable types, and copyable types are the devil.
The C++11 move constructor and move assignment operator seem to me to be a wonderful middle ground.
1. for that workload the result should not really surprise anyone.
2. you are pointing to one pathological workload to make a point that vector performance is always better than lists?
You are, for whatever it's worth, better off defaulting to a variable-length array than to a linked list.
really tptacek? its O(1) ammortized over time, and when N grows it becomes really slow because it involves reallocation and a linear copy of all elements.
1. Please align the asterisks properly. It hurts eyes.
2. Access by index?
3. Iterator (callback on each element)
4. use 'init' instead of 'initialize'. Extra typing hurts fingers.