4 ms·
Yes, that word is extremely important. With that, intrusive data-structures really are great in a lot of cases, it's unfortunate that most languages can't reall
by DSMan195276 9y ago
Yes, that word is extremely important. With that, intrusive data-structures really are great in a lot of cases, it's unfortunate that most languages can't really handle them. That said, for languages with a GC they aren't really as useful from a programming standpoint (They may still be useful from a speed standpoint). Still, they allow you avoid tons of small allocations, as well as avoid the extra 'hop' necessary when you store the node information separate form the data.
Another great part about intrusive data structures is that you can place an object on multiple structures without having to allocate any separate data for them. So to give an example, `std::list<person>` will actually create an 'intrusive' list - it will store the node information along with `person`. However, that `person` is completely tied to the list. If you want to take a `person` off of that list and place it into another one, you can't, you would have to either copy it into a node on the other list, or make the all the lists `std::list<person *>` and then store a pointer to the same person in both lists. With intrusive structures, you can instead use the same set of node information for every list, so you can detach a node from the first list, and then attach it to the second one without having to do any allocations.
As a note, the Linux Kernel makes fairly extensive use of intrusive data-structures, which is an area they particularly shine in since you really want to avoid all those small allocations if you can.