Earlier quoted context omitted.
It really does depend on what you need to be doing in order to make the assumption of what data structure is needed. A vector is all well and good for certain tasks, but sometimes, a list is what you need. I will cover vectors too, but for now I am focused on C. not C++. Thanks though.
You need a list when you have routine insertions somewhere besides the front or the back of the container, and you don't have routine random-access reads. The vector covers more cases and is generally more performant. Use whatever you like, though.
Linked lists in C, with open source ADT
31–40 of 43 posts
Re: Linked lists in C, with open source ADT
#32Earlier quoted context omitted.
see my post below your second highest voted comment on this thread.
http://yaserzt.com/blog/archives/615 "This result should not surprise any experienced programmers, but unfortunately it does many of us." whistles
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.
Re: Linked lists in C, with open source ADT
#33Earlier quoted context omitted.
"insertions to the end of a variable-length array are very fast" 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.
You have a magic allocator for your list nodes that does better than O(1) amortized?
Re: Linked lists in C, with open source ADT
#34Earlier quoted context omitted.
see my post below your second highest voted comment on this thread.
http://yaserzt.com/blog/archives/615 "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.
Re: Linked lists in C, with open source ADT
#35Earlier quoted context omitted.
"insertions to the end of a variable-length array are very fast" 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.
You have a magic allocator for your list nodes that does better than O(1) amortized?
Re: Linked lists in C, with open source ADT
#36Earlier quoted context omitted.
http://yaserzt.com/blog/archives/615 "This result should not surprise any experienced programmers, but unfortunately it does many of us." whistles
That blog post doesn't address the worst-case running time argument, and doesn't measure worst-case running time costs, since it measures batches of 5000 operations at a time. And hey, it's about a workload that totally obviates worst-case running time considerations by requiring a linear scan for every operation. If you responded by pointing out that even when the vector consumes half of physical memory, the worst c…
Re: Linked lists in C, with open source ADT
#37Earlier quoted context omitted.
You have a magic allocator for your list nodes that does better than O(1) amortized?
Do you need magic to create an O(1) allocator for a pool of fixed size objects?