unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: 30458@debbugs.gnu.org
Subject: bug#30458: 26.0; `ucs-names': No reverse lookup function now
Date: Wed, 14 Feb 2018 09:41:10 -0800 (PST)	[thread overview]
Message-ID: <35ab0734-9032-4331-90e8-825f58fba059@default> (raw)

Before Emacs 26, `ucs-names' was an alist.  That meant that you could
not only look up a character, given its name or code, but you could also
easily look up a character name, given the character:

(car (rassq CHARACTER (ucs-names)))

How is this done now, with (ucs-names) returning a hash table?

There is now a function `char-from-name', to replace the former forward
alist lookup (car (assoc CHAR-NAME (ucs-names))).  But there doesn't
seem to be any reverse lookup now for `ucs-names' (e.g. `char-name' or
`char-name-from-char').

Seems like it should be possible to have such a reverse-lookup function
for any hash table (perhaps coded in C).

Sure, just like an alist, a hash table can have multiple keys that have
the same value.  And the order in a hash table is undefined, so when
there are multiple keys for the same value, a function that gives you
the key when you pass it the value can only give you one of those keys.
But that might be better than nothing.

Another possibility would be to have a function that returns all keys
that have the same value.

For example, naively (with lexical-binding):

(defun get-hash-keys (value hash-table &optional value-test-function)
  "Return a list of keys associated with VALUE in HASH-TABLE.
Optional arg VALUE-TEST-FUNCTION (default `equal') is the equality
predicate used to compare values."
  (setq value-test-function  (or value-test-function  #'equal))
  (let ((keys  ()))
    (maphash (lambda (key val)
               (when (funcall value-test-function val value)
                 (push key keys)))
             hash-table)
    keys))

Then:

(get-hash-keys (char-from-name "GREEK SMALL LETTER LAMBDA")
               (ucs-names))

returns ("GREEK SMALL LETTER LAMBDA" "GREEK SMALL LETTER LAMDA").

And for example:

(defun get-a-hash-key (value hash-table &optional value-test-function)
  "Return a hash key associated with VALUE in HASH-TABLE.
If there is more than one such key then it is undefined which is
returned.
Optional arg VALUE-TEST-FUNCTION (default `equal') is the equality
predicate used to compare values."
  (setq value-test-function  (or value-test-function  #'equal))
  (catch 'get-a-hash-key
    (maphash (lambda (key val)
               (when (funcall value-test-function val value)
                 (throw 'get-a-hash-key key)))
             hash-table)
    nil))

Then:

(get-a-hash-key (char-from-name "GREEK SMALL LETTER LAMBDA")
                (ucs-names))

returns "GREEK SMALL LETTER LAMDA" (though one would probably prefer
"GREEK SMALL LETTER LAMBDA").

For the reverse-lookup functions (reverse of `char-from-name') for
a character we would have:

(defun char-names (character)
  "Return a list of the names for CHARACTER."
  (get-hash-keys character (ucs-names)))

(defun char-name (character)
  "Return a name for CHARACTER, from `ucs-names'."
  (get-a-hash-key character (ucs-names)))

Please consider adding such reverse-lookup functions for hash
tables (whether using such an implementation or something else),
and adding specific such functions for `ucs-names', to accompany
the forward-lookup function `char-from-name'.

In GNU Emacs 26.0.91 (build 1, x86_64-w64-mingw32)
 of 2018-01-22
Repository revision: 752fba992b793a74d202c9cfc3e1a92fd458e748
Windowing system distributor `Microsoft Corp.', version 6.1.7601
Configured using:
 `configure --without-dbus --host=x86_64-w64-mingw32
 --without-compress-install 'CFLAGS=-O2 -static -g3''





             reply	other threads:[~2018-02-14 17:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-14 17:41 Drew Adams [this message]
2018-02-14 19:04 ` bug#30458: 26.0; `ucs-names': No reverse lookup function now Andy Moreton
2018-02-14 21:09   ` Drew Adams
2018-02-15  2:01     ` Noam Postavsky
2021-08-12 14:40     ` bug#30458: hash table reverse-lookup (get-keys VALUE TABLE) => KEYS Lars Ingebrigtsen
2021-08-12 15:08       ` bug#30458: [External] : " Drew Adams

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=35ab0734-9032-4331-90e8-825f58fba059@default \
    --to=drew.adams@oracle.com \
    --cc=30458@debbugs.gnu.org \
    /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.savannah.gnu.org/cgit/emacs.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).