Errata [entries|reading|network|archive]
simont

[ userinfo | dreamwidth userinfo ]
[ archive | journal archive ]

Fri 2011-03-18 13:33
Errata
LinkReply
[personal profile] gerald_duckFri 2011-03-18 14:49
Mmm. The doubly-linked cyclic list model is almost canonical for implementing a list that supports bidirectional traversal with arbitrary insertions and removals (there's a sneakier version where one instead stores in each node the difference between the addresses of its previous and next nodes — this saves a word in each node, but complicates updates). There, treating the head/tail as the next element after the last one and the previous element before the first one gives considerable orthogonality and symmetry benefits, making the code simpler and more efficient. A nicety particularly relevant is that the empty list ceases to be a special case for anything. The objective is emphatically not to allow one to iterate through the list's head/tail in some kind of wrap-around.

The data structure you're looking at seems much more specialised and peculiar; I'm left wondering what its purpose is, and what operations it's expected to permit, with what complexity order. Particularly, any kind of insertion or removal is fiddly, but if the structure is essentially static, why not just use contiguous memory with modular arithmetic?
Link Reply to this | Parent | Thread
[personal profile] simontFri 2011-03-18 14:57
Oh, I don't think we're talking about complexity order. We're right down in the realm of cycle-shaving here, making the constant-time operations happen in smaller or larger constant amounts of time.
Link Reply to this | Parent | Thread
[personal profile] gerald_duckFri 2011-03-18 19:55
If you're down to cycle-shaving, surely your for loop spells disaster? That the list is cyclic is not so intrinsic that the optimiser can spot it and the null check in elephant ? elephant != head : (elephant = head) != NULL will have to be performed each time round the loop.
Link Reply to this | Parent | Thread
[identity profile] cartesiandaemon.livejournal.comSat 2011-03-19 15:42
I know what you mean, but once I got my head round the conversation, Simon's description made sense to me: if you want to traverse the circle more often than you want to do anything which involves treating the head=null case specially, then the circular implementation without distinguished elements is conceptually simpler, and gives you the freedom to write extremely optimised assembly later, at any point it's needed, even if in the meantime you only have a simple loop.
Link Reply to this | Parent
navigation
[ go | Previous Entry | Next Entry ]
[ add | to Memories ]