Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: LinkList implementation



On Sun, Feb 28, 2010 at 4:06 AM, Andrey Borzenkov <arvidjaar@xxxxxxxxx> wrote:
> On Sunday 28 of February 2010 01:33:24 Michael Hwang wrote:
>> In the process of writing a new builtin, I discovered an oddity of
>> zsh's implementation of LinkList, which is a doubly linked list.
>>
>> LinkList alist = newlinklist();
>> LinkNode node;
>> for (node = lastnode(alist); node; decnode(alist)) {
>>      ((SomeStructPointer) getdata(node))->someField;
>> }
>>
> BTW decnode applies to node, not list - it should be decnode(node)

Right, that's another typo on my part.

>> Since no nodes have been added to the LinkList, one would expect the
>> body of the loop to not run at all.
>
> Well, you have to accept that and use another termination condition,
> like e.g. in zle_hist.c:isearch_newpos():
>
>        for (node = lastnode(matchlist);
>             node != (LinkNode)matchlist; decnode(node)) {
>
> For the sake of purity this actually should be
>             node != &matchlist->node
>
> The games linklist.c plays are really daunting ... but to change it now
> you have to carefully go through all of code to adjust snippets like
> above.

I solved my problem by simply reversing the direction I added on to
the LinkList, and therefore the direction I walked through it. I'll
see if I can't make the LinkList implementation simpler and more
intuitive. These "gotcha!"'s are a bit annoying.

Michael Hwang



Messages sorted by: Reverse Date, Date, Thread, Author