unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* expose XHASH [patch]
@ 2016-03-31 19:29 Paul Pogonyshev
  2016-03-31 21:52 ` Stefan Monnier
  2016-03-31 22:43 ` Paul Eggert
  0 siblings, 2 replies; 12+ messages in thread
From: Paul Pogonyshev @ 2016-03-31 19:29 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1066 bytes --]

Suppose you need a hash table with the following Lisp structures
as keys:

    (NAME . [VECTOR-OF-ATTRIBUTES])

where NAME is a string.  The structures are supposed to be
compared in a "mixed" way: NAME with 'equal' (or 'string='), but
VECTOR-OF-ATTRIBUTES should be compared with 'eq', i.e. by
identity.

Now, while defining test function for 'define-hash-table-test' is
not a problem, hash is problematic.  While 'sxhash' _will_ work,
it is wasteful, since it computes needless information about
vector contents.  Excluding vector from the hash completely is
also legal, but might considerably worsen hash table performance.

Attached trivial patch just exposes internally used XHASH(),
which fully suits this usecase.  E.g. with it you could

    (defun my-funny-hash (structure)
      (+ (sxhash (car structure)) (* 13 (xhash (cdr structure)))))

Please don't write that the example is stupid, it is just an
example after all.

Paul



* src/fns.c (Fxhash): New function.

* doc/lispref/hash.texi (Defining Hash): Document 'xhash'.

* etc/NEWS: Mention 'xhash'.

[-- Attachment #2: xhash.diff --]
[-- Type: text/plain, Size: 2153 bytes --]

diff --git a/doc/lispref/hash.texi b/doc/lispref/hash.texi
index 8389c21..6ee18d6 100644
--- a/doc/lispref/hash.texi
+++ b/doc/lispref/hash.texi
@@ -307,6 +307,16 @@ and equal-looking objects are considered the same key.
 (make-hash-table :test 'contents-hash)
 @end example
 
+@defun xhash obj
+This function returns a hash code for Lisp object @var{obj}.  Its
+result reflects identity of @var{obj}, but not its contents.
+
+If two objects @var{obj1} and @var{obj2} are @var{eq}, then
+@code{(xhash @var{obj1})} and @code{(xhash @var{obj2})} are the same
+integer.
+@end defun
+
+
 @node Other Hash
 @section Other Hash Table Functions
 
diff --git a/etc/NEWS b/etc/NEWS
index 66777e9..146c756 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -208,6 +208,12 @@ permanent and documented, and may be used by Lisp programs.  Its value
 is a list of currently open parenthesis positions, starting with the
 outermost parenthesis.
 
++++
+** New function 'xhash' returns identity hash code of a Lisp object.
+If two objects are 'eq', then the result of 'xhash' on them will be
+the same.  In other words, this function to 'eq' is the same as
+'sxhash' is to 'equal'.
+
 \f
 * Changes in Emacs 25.2 on Non-Free Operating Systems
 
diff --git a/src/fns.c b/src/fns.c
index 114a556..a1cf2b1 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4449,6 +4449,16 @@ sxhash (Lisp_Object obj, int depth)
  ***********************************************************************/
 
 
+DEFUN ("xhash", Fxhash, Sxhash, 1, 1, 0,
+       doc: /* Compute identity hash code for OBJ and return it as integer.
+In other words, hash codes of two non-`eq' lists will be (most likely)
+different, even if the lists contain the same elements. */)
+  (Lisp_Object obj)
+{
+  return make_number (XHASH (obj));
+}
+
+
 DEFUN ("sxhash", Fsxhash, Ssxhash, 1, 1, 0,
        doc: /* Compute a hash code for OBJ and return it as integer.  */)
   (Lisp_Object obj)
@@ -5067,6 +5077,7 @@ syms_of_fns (void)
   DEFSYM (Qkey_or_value, "key-or-value");
   DEFSYM (Qkey_and_value, "key-and-value");
 
+  defsubr (&Sxhash);
   defsubr (&Ssxhash);
   defsubr (&Smake_hash_table);
   defsubr (&Scopy_hash_table);

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2016-04-08 22:37 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-31 19:29 expose XHASH [patch] Paul Pogonyshev
2016-03-31 21:52 ` Stefan Monnier
2016-03-31 22:43 ` Paul Eggert
2016-04-01  9:44   ` Paul Pogonyshev
2016-04-02 11:52     ` Paul Pogonyshev
2016-04-08 16:08       ` Paul Pogonyshev
2016-04-08 18:10         ` Stefan Monnier
2016-04-08 18:37           ` Paul Pogonyshev
2016-04-08 18:44             ` Stefan Monnier
2016-04-08 19:24               ` Paul Pogonyshev
2016-04-08 20:51                 ` Stefan Monnier
2016-04-08 22:37                 ` Paul Eggert

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).