1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/18_aa.htm
2024-04-01 10:24:07 +02:00

51 lines
6.2 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Section 18.1.1</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="18_a.htm">
<LINK REL=UP HREF="18_a.htm">
<LINK REL=NEXT HREF="18_ab.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="18_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="18_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="18_ab.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
18.1.1 Hash-Table Operations</H2> <P>
The next figure lists some <A REL=DEFINITION HREF="26_glo_d.htm#defined_name"><I>defined names</I></A> that are applicable to <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>hash tables</I></A>. The following rules apply to <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>hash tables</I></A>. <P>
<P><DL><DT>-- A <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>hash table</I></A> can only associate one value with a given key. If an attempt is made to add a second value for a given key, the second value will replace the first. Thus, adding a value to a <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>hash table</I></A> is a destructive operation; the <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>hash table</I></A> is modified. <P><DD>
<DT>-- There are four kinds of <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>hash tables</I></A>: those whose keys are compared with <A REL=DEFINITION HREF="f_eq.htm#eq"><B>eq</B></A>, those whose keys are compared with <A REL=DEFINITION HREF="f_eql.htm#eql"><B>eql</B></A>, those whose keys are compared with <A REL=DEFINITION HREF="f_equal.htm#equal"><B>equal</B></A>, and those whose keys are compared with <A REL=DEFINITION HREF="f_equalp.htm#equalp"><B>equalp</B></A>. <P><DD>
<DT>-- <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>Hash tables</I></A> are created by <A REL=DEFINITION HREF="f_mk_has.htm#make-hash-table"><B>make-hash-table</B></A>. <A REL=DEFINITION HREF="f_gethas.htm#gethash"><B>gethash</B></A> is used to look up a key and find the associated value. New entries are added to <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>hash tables</I></A> using <A REL=DEFINITION HREF="m_setf_.htm#setf"><B>setf</B></A> with <A REL=DEFINITION HREF="f_gethas.htm#gethash"><B>gethash</B></A>. <A REL=DEFINITION HREF="f_remhas.htm#remhash"><B>remhash</B></A> is used to remove an entry. For example: <P><DD>
<PRE>
(setq a (make-hash-table)) =&gt; #&lt;HASH-TABLE EQL 0/120 32536573&gt;
(setf (gethash 'color a) 'brown) =&gt; BROWN
(setf (gethash 'name a) 'fred) =&gt; FRED
(gethash 'color a) =&gt; BROWN, <A REL=DEFINITION HREF="26_glo_t.htm#true">true</A>
(gethash 'name a) =&gt; FRED, <A REL=DEFINITION HREF="26_glo_t.htm#true">true</A>
(gethash 'pointy a) =&gt; NIL, <A REL=DEFINITION HREF="26_glo_f.htm#false">false</A>
</PRE>
</TT> <P>
In this example, the symbols <TT>color</TT> and <TT>name</TT> are being used as keys, and the symbols <TT>brown</TT> and <TT>fred</TT> are being used as the associated values. The <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>hash table</I></A> has two items in it, one of which associates from <TT>color</TT> to <TT>brown</TT>, and the other of which associates from <TT>name</TT> to <TT>fred</TT>. <P>
<DT>-- A key or a value may be any <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A>. <P><DD>
<P>
<DT>-- The existence of an entry in the <A REL=DEFINITION HREF="26_glo_h.htm#hash_table"><I>hash table</I></A> can be determined from the <A REL=DEFINITION HREF="26_glo_s.htm#secondary_value"><I>secondary value</I></A> returned by <A REL=DEFINITION HREF="f_gethas.htm#gethash"><B>gethash</B></A>. <P><DD></DL><P>
<PRE>
<A REL=DEFINITION HREF="f_clrhas.htm#clrhash">clrhash</A> <A REL=DEFINITION HREF="f_hash_t.htm#hash-table-p">hash-table-p</A> <A REL=DEFINITION HREF="f_remhas.htm#remhash">remhash</A>
<A REL=DEFINITION HREF="f_gethas.htm#gethash">gethash</A> <A REL=DEFINITION HREF="f_mk_has.htm#make-hash-table">make-hash-table</A> <A REL=DEFINITION HREF="f_sxhash.htm#sxhash">sxhash</A>
<A REL=DEFINITION HREF="f_hash_1.htm#hash-table-count">hash-table-count</A> <A REL=DEFINITION HREF="f_maphas.htm#maphash">maphash</A>
</PRE>
<P><B>Figure 18-1. Hash-table defined names</B> <P>
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss190.htm">HASH-TABLE-SIZE:INTENDED-ENTRIES</A><LI> <A REL=CHILD HREF="../Issues/iss191.htm">HASH-TABLE-TESTS:ADD-EQUALP</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>