emacs-devel
[Top][All Lists]
Advanced

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

Re: Comparing hash table objects


From: Ihor Radchenko
Subject: Re: Comparing hash table objects
Date: Wed, 08 Jun 2022 17:17:31 +0800

Andreas Schwab <schwab@suse.de> writes:

> On Jun 08 2022, Ihor Radchenko wrote:
>
>> Is there any way to compare objects containing hash tables for equality?
>
> Hash tables are only equal if eq.

This is somewhat unexpected.

To clarify my problem, let me explain in more details what I am trying
to achieve.

Currently, I am using structs to store objects with properties:
(setq obj1 (type (:key1 val1 :key2 val2 ...)))
(setq obj2 (type (:key1 val1 :key2 val2 ...))) ;; the same properties

With this approach, I can easily compare objects with (equal obj1 obj2),
use them as hash table keys, etc.

However, when the number of properties increases, :key access becomes
slow because plist-get has O(N) complexity.

The natural solution is using hash tables:

(setq obj1 (type (make-hash-table)))
(setq obj2 (type (make-hash-table)))
;; fill the hash tables with :keyN valN pairs

Now, I can retrieve the :key in O(1).

However, all of a sudden, I am unable to compare two objects with this
new internal structure. (equal obj1 obj2) returns nil.

The described behavior potentially generates a lot of room for errors.
It is relatively easy to remember that different hash table objects are
never equal (only equal when also eq). However, when the hash tables are
hidden inside a struct, it is easy to overlook that the structs cannot
be compared with equal as usual.

Best,
Ihor




reply via email to

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