lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Binary Search Tree in Scheme


From: Jan-Peter Voigt
Subject: Re: Binary Search Tree in Scheme
Date: Mon, 18 Jun 2018 10:57:46 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0

Hi Urs,

there are self balancing trees like AVL or Red-Black, but I'd say implementation cost is quite for this purpose to sort n<1000 elements. I might be wrong, but I'd prefer the sort method. The EE uses a tree that sorts by measure on the first level, by moment on second and then by the edition-id-path. So there is a tree structure, but on each level the child-list for each node is sorted by the guile method sort, when it is displayed in order. To access the elements it uses a hashtable. In your case annotations should be at least partially sorted. The question is, where/when do you need the sorted list? While typesetting music, you need to insert access the elements by a path moment/context or the like. To export a summary the order might be given by a path piece/movement/measure/moment/context. To give a different view on the data I'd build another tree with the desired scheme like type/mvmt/measure. If you have the primary tre in insertion order you would have to visit some nodes more than twice so a copy should be cheaper. (That is not a proof but an assumption!)

HTH
Jan-Peter


Am 18.06.2018 um 10:18 schrieb Urs Liska:
Hi all,

as you know I'm currently reviewing the scholarLY package, not only for new features but also for its coding - which blatantly shows how it was initially done directly from within my learning curve ;-)

One thing I'd like to change is the storage/handling of annotations. The current implementation does the following:

  * an engraver creates an annotation (an alist) and appends it to a
    global "annotations" variable (OK, simple improvement: don't append
    but cons to the beginning)
  * in a later stage sort this "annotations" list and export the
    annotations.

This looks very inefficient, and I think it should be much better to store the annotations in a binary search tree and have them sorted upon insertion already. (Or am I misunderstanding this? One thing I'm unaware of is how efficient Guile's "stable-sort" works on a simple (linked) list).

On https://gist.github.com/tushar-sharma/c476003598c03373e52c I found a clean and simple implementation of a BST which looks like I can very easily make if work in LilyPond (I can't test because I'm not at my PC at the moment, but I'm talking about the general algorithms anyway).

This should let me insert the annotations in sorted order (replacing the <, >, and = with appropriate functions to sort annotations according to the configured requests), and it's not hard to add a function to walk the tree from left to right.

However - and now the question starts - if I'm not mistaken the annotations encountered by the engraver will be somewhat (or completely?) sorted by musical moment, which will be the requested sort order most of the time but not always. And if elements that are inserted in that BST are already sorted the tree will be totally unbalanced, and the insertion time is as bad (or worse) as simply appending to a list.

So if annotations would always be sorted by moment I would probably go back to the simple linked list, but they may be sorted differently and also by multiple keys (e.g. first by part, then by annotation type and only then by moment).

I could make a switch and choose the storage data structure based on the requested sorting (moment => list, everything else => BST), or I could use a BST that realigns itself after each insertion.

I would be glad about any opinions/recommendations:

  * Is it worth the effort looking into this (often there will be only a
    couple of dozens of annotations per score, but three-digit numbers
    shouldn't be unusual, and I have already encountered scores with
    four-digit numbers of annotations)?
  * Is my idea of a self-balancing BST the right direction?
  * If so, which type of BST would be good to investigate (both in terms
    of performance and implementation complexity)?

Thanks
Urs



_______________________________________________
lilypond-user mailing list
address@hidden
https://lists.gnu.org/mailman/listinfo/lilypond-user





reply via email to

[Prev in Thread] Current Thread [Next in Thread]