mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: Ed Schouten <ed@nuxi.nl>
Cc: musl@lists.openwall.com
Subject: Re: AVL tree: storing balances instead of heights
Date: Mon, 7 Dec 2015 14:03:44 +0100	[thread overview]
Message-ID: <20151207130344.GZ23362@port70.net> (raw)
In-Reply-To: <CABh_MKm1Ow5_dc4ENJM-fAqWc+uPPZgftRdSDxPJiPXZAfZ+FQ@mail.gmail.com>

* Ed Schouten <ed@nuxi.nl> [2015-12-07 09:46:54 +0100]:
> Hi Szabolcs,
> 
> Thanks again for your quick response to the bug in tdelete() that I
> reported! I ran into this issue because I was comparing
> implementations of tsearch() and tdelete() across different operating
> systems. I was doing this in preparation for adding these functions to
> CloudABI's C library.
> 
> I noticed that musl's implementation explicitly stores the height of
> the elements to compute the balance factor. What I read the other day
> is that this is not strictly necessary. It turns out that just storing
> the balances is sufficient. Though this required me to spend some time
> making notes to distinguish all of the states individually, it looks
> like the implementation itself becomes a lot simpler. This approach
> also makes it possible to more easily stop rebalancing as soon as a
> subtree has become balanced again.
> 
> Below are links to my implementation of these functions:
> 
> https://github.com/NuxiNL/cloudlibc/blob/master/src/libc/search/tsearch.c
> https://github.com/NuxiNL/cloudlibc/blob/master/src/libc/search/tdelete.c
> https://github.com/NuxiNL/cloudlibc/blob/master/src/libc/search/search_impl.h
> 
> If you like them, feel free to include them in musl as well. They are
> currently 2-clause BSD licensed, but I don't mind making them
> available under the MIT license as well if this is more practical for
> you folks.
> 

adding musl@ as it should be discussed there.

yes it is enough to only store the height difference.

note that this code was supposed to be size optimized in musl:
i think the tsearch api is not well designed and thus rarely
useful.

your balancing approach is better (i wouldn't call it a lot
simpler, but it is more efficient).

musl should probably move tfind and twalk into separate tu
(but tsearch and tdelete need the same balancing logic so
i'd keep those together).

i think the macro definitions for inlining twalk and tfind
are not justified.

you got the rootp==0 case wrong too (posix requires that to
return 0).

and the (T**) cast is invalid in

	void *tdelete(const void *restrict key, void **restrict rootp,
	              int (*compar)(const void *, const void *)) {
	  void *result = (void *)1;
	  tdelete_recurse(key, (struct __tnode **)rootp, compar, &result);
	  return result;
	}

posix specifies to return a pointer to a node, not to an element
pointer, i think that's a bug in posix (otherwise the api would
be useless).


       reply	other threads:[~2015-12-07 13:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CABh_MKm1Ow5_dc4ENJM-fAqWc+uPPZgftRdSDxPJiPXZAfZ+FQ@mail.gmail.com>
2015-12-07 13:03 ` Szabolcs Nagy [this message]
2015-12-07 13:19   ` Ed Schouten
2015-12-07 13:22     ` Ed Schouten
2015-12-07 14:46     ` Szabolcs Nagy
2015-12-10  9:21       ` Ed Schouten
2015-12-10 12:14         ` Szabolcs Nagy
2015-12-10 13:12           ` Ed Schouten
2015-12-10 13:43             ` Szabolcs Nagy
2015-12-20 21:43               ` Szabolcs Nagy
2015-12-21  1:28                 ` Szabolcs Nagy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151207130344.GZ23362@port70.net \
    --to=nsz@port70.net \
    --cc=ed@nuxi.nl \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).