bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#45539: 26.3; `add-to-ordered-list': Add optional arg for :test predi


From: Lars Ingebrigtsen
Subject: bug#45539: 26.3; `add-to-ordered-list': Add optional arg for :test predicate for hash table
Date: Thu, 31 Dec 2020 04:55:05 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Drew Adams <drew.adams@oracle.com> writes:

> What this function is really about, I think, is this: a list with
> unique elements - no duplicates.
>
> With your fix of this bug, "unique" will be defined by the new TEST
> arg (predicate).  Until then, "unique" is defined by `eq'.

My confusion here was due to not reading the code closely enough,
and because it's implemented how you'd think, it's just ... confusing.

> (defun add-to-ordered-list (list-var element &optional order)
>   "Add ELEMENT to the value of LIST-VAR if it isn't there yet.
> The test for presence of ELEMENT is done with `eq'.

> The resulting list is reordered so that the elements are in the
> order given by each element's numeric list order.  Elements
> without a numeric list order are placed at the end of the list.

What on earth is a "numeric list order" of an object?  This seems to
imply that ELEMENT should be a number.

> If the third optional argument ORDER is a number (integer or
> float), set the element's list order to the given value.  If
> ORDER is nil or omitted, do not change the numeric order of
> ELEMENT.  If ORDER has any other value, remove the numeric order
> of ELEMENT if it has one.

The actual ORDER bit is optional, which makes no sense for a function
that's allegedly about an ordered list.

    > (when order
    >   (puthash element (and (numberp order) order) ordering))

The hash table is only used to store this ORDER, and ORDER is silently
discarded unless it's a number.

    > (unless (memq element (symbol-value list-var))
    >   (set list-var (cons element (symbol-value list-var))))

This is the actual member check -- the hash table isn't used to keep
track of what's already in the list or not.

    > (set list-var (sort (symbol-value list-var)

The list is re-sorted upon every call, which is maximally inefficient.

                        > (lambda (a b)
                        >   (let ((oa (gethash a ordering))
                        >       (ob (gethash b ordering)))
                        >     (if (and oa ob)
                        >       (< oa ob)
                        >       oa)))))))

Finally, we sort based on the values stashed in the hash table, and the
ones with a (numerical) order gets sorted first.

It's a strange, maximally inefficient function.  I'll fix it up a bit
and add the test-function.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





reply via email to

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