algorithm

Dynamic Data Structure Switching

Submitted by Jeremy
on September 9, 2007 - 11:18am
Linux news

Nick Piggin posted an efficient algorithm for converting a data structure, "this is my 'data structure switch' algorithm that can convert one data structure into another, with just a single unlikely branch in fastpaths and no locking or atomic operations (the branch is only triggered when the data structure is in the process of being converted). A pointer indirection is generally also needed when converting a global data structure." He provided an example of using it to resize a hash then noted:

"the algorithm need not only convert between two different sized hashes, but that's just the most obvious and useful thing to do. It _could_ convert between a hash and a tree or something crazy like that too :)"

Nick added that while the conversion algorithm was efficiently implemented, converting it into an API was proving difficult, "the algorithm implementation is basically optimal, but the API means that it requires and extra function call and indirect function call to work, which probably makes it unusable for important implementations. (C preprocessor magic to avoid the indirect function calls and allow some level of templating while retaining the types might be the way to do it)."