1
0
Fork 0
cl-sites/novaspec.org/cl/f_sxhash.html

477 lines
10 KiB
HTML
Raw Normal View History

2025-02-05 18:52:26 +01:00
<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>sxhash | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_sxhash.html"
><LINK REL="next" HREF="19_Filenames.html" TYPE="text/html" TITLE="19. Filenames"
><LINK REL="prev" HREF="f_clrhash.html" TYPE="text/html" TITLE="clrhash"
><LINK REL="up" HREF="18_2_Hash_Tables_Dictionary.html" TYPE="text/html" TITLE="18.2 Hash Tables Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="18_Hash_Tables.html"
>18. Hash Tables</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="18_2_Hash_Tables_Dictionary.html"
>18.2 Hash Tables Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_sxhash.html"
>sxhash</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_clrhash.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="19_Filenames.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="sxhash"
></SPAN
><B
>sxhash</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Function</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
><B
>sxhash</B
> <SPAN CLASS="cmssi"
>object</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>hash-code</SPAN
></P
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>object</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>hash-code</VAR
> &#8212; a non-negative <A HREF="26_1_Glossary.html#fixnum"
><EM CLASS="term"
>fixnum</EM
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_sxhash.html" CLASS="funref"
><B
>sxhash</B
></A
> returns a hash code for <VAR CLASS="param"
>object</VAR
>. </P
><P CLASS="j"
>The manner in which the hash code is computed is <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
>, but subject to certain constraints:</P
><OL
><LI
><P CLASS="j"
><CODE CLASS="f"
>(equal <VAR CLASS="param"
>x</VAR
> <VAR CLASS="param"
>y</VAR
>)</CODE
> implies <CODE CLASS="f"
>(= (sxhash <VAR CLASS="param"
>x</VAR
>) (sxhash <VAR CLASS="param"
>y</VAR
>))</CODE
>.</P
></LI
><LI
><P CLASS="j"
>For any two <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>objects</EM
></A
>, <VAR CLASS="param"
>x</VAR
> and <VAR CLASS="param"
>y</VAR
>, both of which are <A HREF="26_1_Glossary.html#bit_vector"
><EM CLASS="term"
>bit vectors</EM
></A
>, <A HREF="26_1_Glossary.html#character"
><EM CLASS="term"
>characters</EM
></A
>, <A HREF="26_1_Glossary.html#cons"
><EM CLASS="term"
>conses</EM
></A
>, <A HREF="26_1_Glossary.html#number"
><EM CLASS="term"
>numbers</EM
></A
>, <A HREF="26_1_Glossary.html#pathname"
><EM CLASS="term"
>pathnames</EM
></A
>, <A HREF="26_1_Glossary.html#string"
><EM CLASS="term"
>strings</EM
></A
>, or <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
>, and which are <A HREF="26_1_Glossary.html#similar"
><EM CLASS="term"
>similar</EM
></A
>, <CODE CLASS="f"
>(sxhash <VAR CLASS="param"
>x</VAR
>)</CODE
> and <CODE CLASS="f"
>(sxhash <VAR CLASS="param"
>y</VAR
>)</CODE
> <A HREF="26_1_Glossary.html#yield"
><EM CLASS="term"
>yield</EM
></A
> the same mathematical value even if <VAR CLASS="param"
>x</VAR
> and <VAR CLASS="param"
>y</VAR
> exist in different <A HREF="26_1_Glossary.html#Lisp_image"
><EM CLASS="term"
>Lisp images</EM
></A
> of the same <A HREF="26_1_Glossary.html#implementation"
><EM CLASS="term"
>implementation</EM
></A
>. See <A HREF="3_2_Compilation.html#sec_3_2_4" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.2.4</SPAN
> <SPAN CLASS="cmr"
>(Literal</SPAN
> <SPAN CLASS="cmr"
>Objects</SPAN
> <SPAN CLASS="cmr"
>in</SPAN
> <SPAN CLASS="cmr"
>Compiled</SPAN
> <SPAN CLASS="cmr"
>Files)</SPAN
></A
>.</P
></LI
><LI
><P CLASS="j"
>The <VAR CLASS="param"
>hash-code</VAR
> for an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> is always the <A HREF="26_1_Glossary.html#same"
><EM CLASS="term"
>same</EM
></A
> within a single <A HREF="26_1_Glossary.html#session"
><EM CLASS="term"
>session</EM
></A
> provided that the <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
> is not visibly modified with regard to the equivalence test <A HREF="f_equal.html" CLASS="funref"
><B
>equal</B
></A
>. See <A HREF="18_1_Hash_Table_Concepts.html#sec_18_1_2" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>18.1.2</SPAN
> <SPAN CLASS="cmr"
>(Modifying</SPAN
> <SPAN CLASS="cmr"
>Hash</SPAN
> <SPAN CLASS="cmr"
>Table</SPAN
> <SPAN CLASS="cmr"
>Keys)</SPAN
></A
>.</P
></LI
><LI
><P CLASS="j"
>The <VAR CLASS="param"
>hash-code</VAR
> is intended for hashing. This places no verifiable constraint on a <A HREF="26_1_Glossary.html#conforming_implementation"
><EM CLASS="term"
>conforming implementation</EM
></A
>, but the intent is that an <A HREF="26_1_Glossary.html#implementation"
><EM CLASS="term"
>implementation</EM
></A
> should make a good-faith effort to produce <VAR CLASS="param"
>hash-codes</VAR
> that are well distributed within the range of non-negative <A HREF="26_1_Glossary.html#fixnum"
><EM CLASS="term"
>fixnums</EM
></A
>.</P
></LI
><LI
><P CLASS="j"
>Computation of the <VAR CLASS="param"
>hash-code</VAR
> must terminate, even if the <VAR CLASS="param"
>object</VAR
> contains circularities.</P
></LI
></OL
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (= (sxhash (list 'list "ab")) (sxhash (list 'list "ab"))) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>
(= (sxhash "a") (sxhash (make-string 1 :initial-element #\a))) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#true"
><EM CLASS="term"
>true</EM
></A
>
(let ((r (make-random-state)))
(= (sxhash r) (sxhash (make-random-state r))))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
></PRE
></DD
><DT
><B
>Affected By</B
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#implementation"
><EM CLASS="term"
>implementation</EM
></A
>.</P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>Many common hashing needs are satisfied by <A HREF="f_make-hash-table.html" CLASS="funref"
><B
>make-hash-table</B
></A
> and the related functions on <A HREF="26_1_Glossary.html#hash_table"
><EM CLASS="term"
>hash tables</EM
></A
>. <A HREF="f_sxhash.html" CLASS="funref"
><B
>sxhash</B
></A
> is intended for use where the pre-defined abstractions are insufficient. Its main intent is to allow the user a convenient means of implementing more complicated hashing paradigms than are provided through <A HREF="26_1_Glossary.html#hash_table"
><EM CLASS="term"
>hash tables</EM
></A
>. </P
><P CLASS="j"
>The hash codes returned by <A HREF="f_sxhash.html" CLASS="funref"
><B
>sxhash</B
></A
> are not necessarily related to any hashing strategy used by any other <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> in Common Lisp. </P
><P CLASS="j"
>For <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>objects</EM
></A
> of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>types</EM
></A
> that <A HREF="f_equal.html" CLASS="funref"
><B
>equal</B
></A
> compares with <A HREF="f_eq.html" CLASS="funref"
><B
>eq</B
></A
>, item 3 requires that the <VAR CLASS="param"
>hash-code</VAR
> be based on some immutable quality of the identity of the object. Another legitimate implementation technique would be to have <A HREF="f_sxhash.html" CLASS="funref"
><B
>sxhash</B
></A
> assign (and cache) a random hash code for these <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>objects</EM
></A
>, since there is no requirement that <A HREF="26_1_Glossary.html#similar"
><EM CLASS="term"
>similar</EM
></A
> but non-<A HREF="f_eq.html" CLASS="funref"
><B
>eq</B
></A
> objects have the same hash code. </P
><P CLASS="j"
>Although <A HREF="26_1_Glossary.html#similarity"
><EM CLASS="term"
>similarity</EM
></A
> is defined for <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbols</EM
></A
> in terms of both the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
>&#8217;s <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>name</EM
></A
> and the <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>packages</EM
></A
> in which the <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> is <A HREF="26_1_Glossary.html#accessible"
><EM CLASS="term"
>accessible</EM
></A
>, item 3 disallows using <A HREF="26_1_Glossary.html#package"
><EM CLASS="term"
>package</EM
></A
> information to compute the hash code, since changes to the package status of a symbol are not visible to <VAR CLASS="param"
>equal</VAR
>.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_clrhash.html" CLASS="prev"
>&#8592;</A
><A HREF="19_Filenames.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>