My various dotfiles

chap-18.texi 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. @node Hash Tables, Filenames, Sequences, Top
  2. @chapter Hash Tables
  3. @menu
  4. * Hash Table Concepts::
  5. * Hash Tables Dictionary::
  6. @end menu
  7. @node Hash Table Concepts, Hash Tables Dictionary, Hash Tables, Hash Tables
  8. @section Hash Table Concepts
  9. @c including concept-hash-tables
  10. @menu
  11. * Hash-Table Operations::
  12. * Modifying Hash Table Keys::
  13. @end menu
  14. @node Hash-Table Operations, Modifying Hash Table Keys, Hash Table Concepts, Hash Table Concepts
  15. @subsection Hash-Table Operations
  16. Figure 18--1 lists some @i{defined names} that are applicable
  17. to @i{hash tables}. The following rules apply to @i{hash tables}.
  18. @table @asis
  19. @item --
  20. A @i{hash table} can only associate one value with a given
  21. key. If an attempt is made to add a second value for a given key,
  22. the second value will replace the first.
  23. Thus, adding a value to a @i{hash table} is a destructive operation;
  24. the @i{hash table} is modified.
  25. @item --
  26. There are four kinds of @i{hash tables}:
  27. those whose keys are compared with @b{eq},
  28. those whose keys are compared with @b{eql},
  29. those whose keys are compared with @b{equal}, and
  30. those whose keys are compared with @b{equalp}.
  31. @item --
  32. @i{Hash tables} are created by @b{make-hash-table}.
  33. @b{gethash} is used to look up a key and find the associated value.
  34. New entries are added to @i{hash tables} using @b{setf} with @b{gethash}.
  35. @b{remhash} is used to remove an entry.
  36. For example:
  37. @example
  38. (setq a (make-hash-table)) @result{} #<HASH-TABLE EQL 0/120 32536573>
  39. (setf (gethash 'color a) 'brown) @result{} BROWN
  40. (setf (gethash 'name a) 'fred) @result{} FRED
  41. (gethash 'color a) @result{} BROWN, @i{true}
  42. (gethash 'name a) @result{} FRED, @i{true}
  43. (gethash 'pointy a) @result{} NIL, @i{false}
  44. @end example
  45. In this example, the symbols @t{color} and @t{name} are being used as
  46. keys, and the symbols @t{brown} and @t{fred} are being used as the
  47. associated values. The @i{hash table}
  48. has two items in it, one of which
  49. associates from @t{color} to @t{brown}, and the other of which
  50. associates from @t{name} to @t{fred}.
  51. @item --
  52. A key or a value may be any @i{object}.
  53. @item --
  54. The existence of an entry in the @i{hash table} can be determined
  55. from the @i{secondary value} returned by @b{gethash}.
  56. @end table
  57. @group
  58. @noindent
  59. @w{ clrhash hash-table-p remhash }
  60. @w{ gethash make-hash-table sxhash }
  61. @w{ hash-table-count maphash }
  62. @noindent
  63. @w{ Figure 18--1: Hash-table defined names }
  64. @end group
  65. @node Modifying Hash Table Keys, , Hash-Table Operations, Hash Table Concepts
  66. @subsection Modifying Hash Table Keys
  67. The function supplied as the @t{:test} argument to @b{make-hash-table}
  68. specifies the `equivalence test' for the @i{hash table} it creates.
  69. An @i{object} is `visibly modified' with regard to an equivalence test
  70. if there exists some set of @i{objects} (or potential @i{objects})
  71. which are equivalent to the @i{object} before the modification but are
  72. no longer equivalent afterwards.
  73. If an @i{object} O_1 is used as a key in a @i{hash table} H
  74. and is then visibly modified with regard to the equivalence test of H,
  75. then the consequences are unspecified if O_1, or any @i{object}
  76. O_2 equivalent to O_1 under the equivalence test (either before
  77. or after the modification), is used as a key in further operations on H.
  78. The consequences of using O_1 as a key are unspecified
  79. even if O_1 is visibly modified
  80. and then later modified again in such a way as
  81. to undo the visible modification.
  82. Following are specifications of the modifications which are visible to the
  83. equivalence tests which must be supported by @i{hash tables}. The modifications
  84. are described in terms of modification of components, and are defined
  85. recursively. Visible modifications of components of the @i{object} are
  86. visible modifications of the @i{object}.
  87. @menu
  88. * Visible Modification of Objects with respect to EQ and EQL::
  89. * Visible Modification of Objects with respect to EQUAL::
  90. * Visible Modification of Conses with respect to EQUAL::
  91. * Visible Modification of Bit Vectors and Strings with respect to EQUAL::
  92. * Visible Modification of Objects with respect to EQUALP::
  93. * Visible Modification of Structures with respect to EQUALP::
  94. * Visible Modification of Arrays with respect to EQUALP::
  95. * Visible Modification of Hash Tables with respect to EQUALP::
  96. * Visible Modifications by Language Extensions::
  97. @end menu
  98. @node Visible Modification of Objects with respect to EQ and EQL, Visible Modification of Objects with respect to EQUAL, Modifying Hash Table Keys, Modifying Hash Table Keys
  99. @subsubsection Visible Modification of Objects with respect to EQ and EQL
  100. No @i{standardized} @i{function} is provided that is capable of visibly
  101. modifying an @i{object} with regard to @b{eq} or @b{eql}.
  102. @node Visible Modification of Objects with respect to EQUAL, Visible Modification of Conses with respect to EQUAL, Visible Modification of Objects with respect to EQ and EQL, Modifying Hash Table Keys
  103. @subsubsection Visible Modification of Objects with respect to EQUAL
  104. As a consequence of the behavior for @b{equal},
  105. the rules for visible modification of @i{objects} not explicitly mentioned in this
  106. section are inherited from those in @ref{Visible Modification of Objects with respect to EQ and EQL}.
  107. @node Visible Modification of Conses with respect to EQUAL, Visible Modification of Bit Vectors and Strings with respect to EQUAL, Visible Modification of Objects with respect to EQUAL, Modifying Hash Table Keys
  108. @subsubsection Visible Modification of Conses with respect to EQUAL
  109. Any visible change to the @i{car} or the @i{cdr} of a @i{cons}
  110. is considered a visible modification with regard to @b{equal}.
  111. @node Visible Modification of Bit Vectors and Strings with respect to EQUAL, Visible Modification of Objects with respect to EQUALP, Visible Modification of Conses with respect to EQUAL, Modifying Hash Table Keys
  112. @subsubsection Visible Modification of Bit Vectors and Strings with respect to EQUAL
  113. For a @i{vector} of @i{type} @b{bit-vector} or of @i{type} @b{string}, any visible change
  114. to an @i{active} @i{element} of the @i{vector},
  115. or to the @i{length} of the @i{vector} (if it is @i{actually adjustable}
  116. or has a @i{fill pointer})
  117. is considered a visible modification with regard to @b{equal}.
  118. @node Visible Modification of Objects with respect to EQUALP, Visible Modification of Structures with respect to EQUALP, Visible Modification of Bit Vectors and Strings with respect to EQUAL, Modifying Hash Table Keys
  119. @subsubsection Visible Modification of Objects with respect to EQUALP
  120. As a consequence of the behavior for @b{equalp},
  121. the rules for visible modification of @i{objects} not explicitly mentioned in this
  122. section are inherited from those in @ref{Visible Modification of Objects with respect to EQUAL}.
  123. @node Visible Modification of Structures with respect to EQUALP, Visible Modification of Arrays with respect to EQUALP, Visible Modification of Objects with respect to EQUALP, Modifying Hash Table Keys
  124. @subsubsection Visible Modification of Structures with respect to EQUALP
  125. Any visible change to a @i{slot} of a @i{structure}
  126. is considered a visible modification with regard to @b{equalp}.
  127. @node Visible Modification of Arrays with respect to EQUALP, Visible Modification of Hash Tables with respect to EQUALP, Visible Modification of Structures with respect to EQUALP, Modifying Hash Table Keys
  128. @subsubsection Visible Modification of Arrays with respect to EQUALP
  129. In an @i{array}, any visible change
  130. to an @i{active} @i{element},
  131. to the @i{fill pointer} (if the @i{array} can and does have one),
  132. or to the @i{dimensions} (if the @i{array} is @i{actually adjustable})
  133. is considered a visible modification with regard to @b{equalp}.
  134. @node Visible Modification of Hash Tables with respect to EQUALP, Visible Modifications by Language Extensions, Visible Modification of Arrays with respect to EQUALP, Modifying Hash Table Keys
  135. @subsubsection Visible Modification of Hash Tables with respect to EQUALP
  136. In a @i{hash table}, any visible change
  137. to the count of entries in the @i{hash table},
  138. to the keys,
  139. or to the values associated with the keys
  140. is considered a visible modification with regard to @b{equalp}.
  141. Note that the visibility of modifications to the keys depends on the equivalence test
  142. of the @i{hash table}, not on the specification of @b{equalp}.
  143. @node Visible Modifications by Language Extensions, , Visible Modification of Hash Tables with respect to EQUALP, Modifying Hash Table Keys
  144. @subsubsection Visible Modifications by Language Extensions
  145. @i{Implementations} that extend the language by providing additional mutator
  146. functions (or additional behavior for existing mutator functions) must
  147. document how the use of these extensions interacts with equivalence tests and
  148. @i{hash table} searches.
  149. @i{Implementations} that extend the language by defining additional acceptable
  150. equivalence tests for @i{hash tables} (allowing additional values for the @t{:test}
  151. argument to @b{make-hash-table}) must document the visible components of these
  152. tests.
  153. @c end of including concept-hash-tables
  154. @node Hash Tables Dictionary, , Hash Table Concepts, Hash Tables
  155. @section Hash Tables Dictionary
  156. @c including dict-hash-tables
  157. @menu
  158. * hash-table::
  159. * make-hash-table::
  160. * hash-table-p::
  161. * hash-table-count::
  162. * hash-table-rehash-size::
  163. * hash-table-rehash-threshold::
  164. * hash-table-size::
  165. * hash-table-test::
  166. * gethash::
  167. * remhash::
  168. * maphash::
  169. * with-hash-table-iterator::
  170. * clrhash::
  171. * sxhash::
  172. @end menu
  173. @node hash-table, make-hash-table, Hash Tables Dictionary, Hash Tables Dictionary
  174. @subsection hash-table [System Class]
  175. @subsubheading Class Precedence List::
  176. @b{hash-table},
  177. @b{t}
  178. @subsubheading Description::
  179. @i{Hash tables} provide a way of mapping any @i{object} (a @i{key})
  180. to an associated @i{object} (a @i{value}).
  181. @subsubheading See Also::
  182. @ref{Hash Table Concepts},
  183. @ref{Printing Other Objects}
  184. @subsubheading Notes::
  185. The intent is that this mapping be implemented by a hashing mechanism,
  186. such as that described in Section 6.4 ``Hashing'' of {The Art of Computer Programming, Volume 3}
  187. (pp506-549). In spite of this intent, no @i{conforming implementation}
  188. is required to use any particular technique to implement the mapping.
  189. @node make-hash-table, hash-table-p, hash-table, Hash Tables Dictionary
  190. @subsection make-hash-table [Function]
  191. @code{make-hash-table} @i{{&key} test size rehash-size rehash-threshold} @result{} @i{hash-table}
  192. @subsubheading Arguments and Values::
  193. @i{test}---a @i{designator} for one of the @i{functions}
  194. @b{eq},
  195. @b{eql},
  196. @b{equal}, or
  197. @b{equalp}.
  198. The default is @b{eql}.
  199. @i{size}---a non-negative @i{integer}.
  200. The default is @i{implementation-dependent}.
  201. @i{rehash-size}---a @i{real} of @i{type} @t{(or (integer 1 *) (float (1.0) *))}.
  202. The default is @i{implementation-dependent}.
  203. @i{rehash-threshold}---a @i{real} of @i{type} @t{(real 0 1)}.
  204. The default is @i{implementation-dependent}.
  205. @i{hash-table}---a @i{hash table}.
  206. @subsubheading Description::
  207. Creates and returns a new @i{hash table}.
  208. @i{test} determines how @i{keys} are compared.
  209. An @i{object} is said to be present in the @i{hash-table}
  210. if that @i{object} is the @i{same} under the @i{test}
  211. as the @i{key} for some entry in the @i{hash-table}.
  212. @i{size} is a hint to the @i{implementation} about how much initial space
  213. to allocate in the @i{hash-table}.
  214. This information, taken together with the @i{rehash-threshold}, controls
  215. the approximate number of entries which it should be possible
  216. to insert before the table has to grow.
  217. The actual size might be rounded up from @i{size} to the next `good' size;
  218. for example, some @i{implementations} might round to the next prime number.
  219. @i{rehash-size} specifies a minimum amount to increase the size of the
  220. @i{hash-table} when it becomes full
  221. enough to require rehashing;
  222. see @i{rehash-theshold} below.
  223. If @i{rehash-size} is an @i{integer},
  224. the expected growth rate for the table is additive and
  225. the @i{integer} is the number of entries to add;
  226. if it is a @i{float},
  227. the expected growth rate for the table is multiplicative and
  228. the @i{float} is the ratio of the new size to the old size.
  229. As with @i{size}, the actual size of the increase might be rounded up.
  230. @i{rehash-threshold} specifies how full the @i{hash-table} can get
  231. before it must grow.
  232. It specifies the maximum desired hash-table occupancy level.
  233. The @i{values} of @i{rehash-size} and @i{rehash-threshold} do not constrain the
  234. @i{implementation} to use any particular method for computing when and by how much
  235. the size of @i{hash-table} should be enlarged. Such decisions are
  236. @i{implementation-dependent}, and these @i{values} only hints
  237. from the @i{programmer} to the @i{implementation}, and the @i{implementation}
  238. is permitted to ignore them.
  239. @subsubheading Examples::
  240. @example
  241. (setq table (make-hash-table)) @result{} #<HASH-TABLE EQL 0/120 46142754>
  242. (setf (gethash "one" table) 1) @result{} 1
  243. (gethash "one" table) @result{} NIL, @i{false}
  244. (setq table (make-hash-table :test 'equal)) @result{} #<HASH-TABLE EQUAL 0/139 46145547>
  245. (setf (gethash "one" table) 1) @result{} 1
  246. (gethash "one" table) @result{} 1, T
  247. (make-hash-table :rehash-size 1.5 :rehash-threshold 0.7)
  248. @result{} #<HASH-TABLE EQL 0/120 46156620>
  249. @end example
  250. @subsubheading See Also::
  251. @ref{gethash}
  252. ,
  253. @b{hash-table}
  254. @node hash-table-p, hash-table-count, make-hash-table, Hash Tables Dictionary
  255. @subsection hash-table-p [Function]
  256. @code{hash-table-p} @i{object} @result{} @i{generalized-boolean}
  257. @subsubheading Arguments and Values::
  258. @i{object}---an @i{object}.
  259. @i{generalized-boolean}---a @i{generalized boolean}.
  260. @subsubheading Description::
  261. Returns @i{true} if @i{object} is of @i{type} @b{hash-table};
  262. otherwise, returns @i{false}.
  263. @subsubheading Examples::
  264. @example
  265. (setq table (make-hash-table)) @result{} #<HASH-TABLE EQL 0/120 32511220>
  266. (hash-table-p table) @result{} @i{true}
  267. (hash-table-p 37) @result{} @i{false}
  268. (hash-table-p '((a . 1) (b . 2))) @result{} @i{false}
  269. @end example
  270. @subsubheading Notes::
  271. @example
  272. (hash-table-p @i{object}) @equiv{} (typep @i{object} 'hash-table)
  273. @end example
  274. @node hash-table-count, hash-table-rehash-size, hash-table-p, Hash Tables Dictionary
  275. @subsection hash-table-count [Function]
  276. @code{hash-table-count} @i{hash-table} @result{} @i{count}
  277. @subsubheading Arguments and Values::
  278. @i{hash-table}---a @i{hash table}.
  279. @i{count}---a non-negative @i{integer}.
  280. @subsubheading Description::
  281. Returns the number of entries in the @i{hash-table}.
  282. If @i{hash-table} has just been created
  283. or newly cleared (see @b{clrhash})
  284. the entry count is @t{0}.
  285. @subsubheading Examples::
  286. @example
  287. (setq table (make-hash-table)) @result{} #<HASH-TABLE EQL 0/120 32115135>
  288. (hash-table-count table) @result{} 0
  289. (setf (gethash 57 table) "fifty-seven") @result{} "fifty-seven"
  290. (hash-table-count table) @result{} 1
  291. (dotimes (i 100) (setf (gethash i table) i)) @result{} NIL
  292. (hash-table-count table) @result{} 100
  293. @end example
  294. @subsubheading Affected By::
  295. @b{clrhash},
  296. @b{remhash},
  297. @b{setf} of @b{gethash}
  298. @subsubheading See Also::
  299. @ref{hash-table-size}
  300. @subsubheading Notes::
  301. The following relationships are functionally correct, although in practice
  302. using @b{hash-table-count} is probably much faster:
  303. @example
  304. (hash-table-count @i{table}) @equiv{}
  305. (loop for value being the hash-values of @i{table} count t) @equiv{}
  306. (let ((total 0))
  307. (maphash #'(lambda (key value)
  308. (declare (ignore key value))
  309. (incf total))
  310. @i{table})
  311. total)
  312. @end example
  313. @node hash-table-rehash-size, hash-table-rehash-threshold, hash-table-count, Hash Tables Dictionary
  314. @subsection hash-table-rehash-size [Function]
  315. @code{hash-table-rehash-size} @i{hash-table} @result{} @i{rehash-size}
  316. @subsubheading Arguments and Values::
  317. @i{hash-table}---a @i{hash table}.
  318. @i{rehash-size}---a @i{real} of @i{type} @t{(or (integer 1 *) (float (1.0) *))}.
  319. @subsubheading Description::
  320. Returns the current rehash size of @i{hash-table},
  321. suitable for use in a call to @b{make-hash-table}
  322. in order to produce a @i{hash table}
  323. with state corresponding to the current state of the @i{hash-table}.
  324. @subsubheading Examples::
  325. @example
  326. (setq table (make-hash-table :size 100 :rehash-size 1.4))
  327. @result{} #<HASH-TABLE EQL 0/100 2556371>
  328. (hash-table-rehash-size table) @result{} 1.4
  329. @end example
  330. @subsubheading Exceptional Situations::
  331. Should signal an error of @i{type} @b{type-error}
  332. if @i{hash-table} is not a @i{hash table}.
  333. @subsubheading See Also::
  334. @ref{make-hash-table}
  335. ,
  336. @ref{hash-table-rehash-threshold}
  337. @subsubheading Notes::
  338. If the hash table was created with an @i{integer} rehash size,
  339. the result is an @i{integer},
  340. indicating that the rate of growth of the @i{hash-table} when rehashed
  341. is intended to be additive;
  342. otherwise,
  343. the result is a @i{float},
  344. indicating that the rate of growth of the @i{hash-table} when rehashed
  345. is intended to be multiplicative.
  346. However, this value is only advice to the @i{implementation};
  347. the actual amount by which the @i{hash-table} will grow upon rehash is
  348. @i{implementation-dependent}.
  349. @node hash-table-rehash-threshold, hash-table-size, hash-table-rehash-size, Hash Tables Dictionary
  350. @subsection hash-table-rehash-threshold [Function]
  351. @code{hash-table-rehash-threshold} @i{hash-table} @result{} @i{rehash-threshold}
  352. @subsubheading Arguments and Values::
  353. @i{hash-table}---a @i{hash table}.
  354. @i{rehash-threshold}---a @i{real} of @i{type} @t{(real 0 1)}.
  355. @subsubheading Description::
  356. Returns the current rehash threshold of @i{hash-table}, which is
  357. suitable for use in a call to @b{make-hash-table} in order to
  358. produce a @i{hash table} with state corresponding to the current
  359. state of the @i{hash-table}.
  360. @subsubheading Examples::
  361. @example
  362. (setq table (make-hash-table :size 100 :rehash-threshold 0.5))
  363. @result{} #<HASH-TABLE EQL 0/100 2562446>
  364. (hash-table-rehash-threshold table) @result{} 0.5
  365. @end example
  366. @subsubheading Exceptional Situations::
  367. Should signal an error of @i{type} @b{type-error}
  368. if @i{hash-table} is not a @i{hash table}.
  369. @subsubheading See Also::
  370. @ref{make-hash-table}
  371. ,
  372. @ref{hash-table-rehash-size}
  373. @node hash-table-size, hash-table-test, hash-table-rehash-threshold, Hash Tables Dictionary
  374. @subsection hash-table-size [Function]
  375. @code{hash-table-size} @i{hash-table} @result{} @i{size}
  376. @subsubheading Arguments and Values::
  377. @i{hash-table}---a @i{hash table}.
  378. @i{size}---a non-negative @i{integer}.
  379. @subsubheading Description::
  380. Returns the current size of @i{hash-table}, which is suitable for use in
  381. a call to @b{make-hash-table} in order to produce a @i{hash table}
  382. with state corresponding to the current state of the @i{hash-table}.
  383. @subsubheading Exceptional Situations::
  384. Should signal an error of @i{type} @b{type-error}
  385. if @i{hash-table} is not a @i{hash table}.
  386. @subsubheading See Also::
  387. @ref{hash-table-count}
  388. ,
  389. @ref{make-hash-table}
  390. @node hash-table-test, gethash, hash-table-size, Hash Tables Dictionary
  391. @subsection hash-table-test [Function]
  392. @code{hash-table-test} @i{hash-table} @result{} @i{test}
  393. @subsubheading Arguments and Values::
  394. @i{hash-table}---a @i{hash table}.
  395. @i{test}---a @i{function designator}.
  396. For the four @i{standardized} @i{hash table} test @i{functions}
  397. (see @b{make-hash-table}), the @i{test} value returned
  398. is always a @i{symbol}. If an @i{implementation} permits additional
  399. tests, it is @i{implementation-dependent} whether such tests are
  400. returned as @i{function} @i{objects} or @i{function names}.
  401. @subsubheading Description::
  402. Returns the test used for comparing @i{keys} in @i{hash-table}.
  403. @subsubheading Exceptional Situations::
  404. Should signal an error of @i{type} @b{type-error}
  405. if @i{hash-table} is not a @i{hash table}.
  406. @subsubheading See Also::
  407. @ref{make-hash-table}
  408. @node gethash, remhash, hash-table-test, Hash Tables Dictionary
  409. @subsection gethash [Accessor]
  410. @code{gethash} @i{key hash-table {&optional} default} @result{} @i{value, present-p}
  411. (setf (@code{ gethash} @i{key hash-table {&optional} default}) new-value)@*
  412. @subsubheading Arguments and Values::
  413. @i{key}---an @i{object}.
  414. @i{hash-table}---a @i{hash table}.
  415. @i{default}---an @i{object}.
  416. The default is @b{nil}.
  417. @i{value}---an @i{object}.
  418. @i{present-p}---a @i{generalized boolean}.
  419. @subsubheading Description::
  420. @i{Value} is the @i{object} in @i{hash-table} whose @i{key}
  421. is the @i{same} as @i{key} under the @i{hash-table}'s equivalence test.
  422. If there is no such entry, @i{value} is the @i{default}.
  423. @i{Present-p} is @i{true} if an entry is found; otherwise, it is @i{false}.
  424. @b{setf} may be used with @b{gethash} to modify the @i{value}
  425. associated with a given @i{key}, or to add a new entry.
  426. When a @b{gethash} @i{form} is used as a @b{setf} @i{place},
  427. any @i{default} which is supplied is evaluated according to normal
  428. left-to-right evaluation rules, but its @i{value} is ignored.
  429. @subsubheading Examples::
  430. @example
  431. (setq table (make-hash-table)) @result{} #<HASH-TABLE EQL 0/120 32206334>
  432. (gethash 1 table) @result{} NIL, @i{false}
  433. (gethash 1 table 2) @result{} 2, @i{false}
  434. (setf (gethash 1 table) "one") @result{} "one"
  435. (setf (gethash 2 table "two") "two") @result{} "two"
  436. (gethash 1 table) @result{} "one", @i{true}
  437. (gethash 2 table) @result{} "two", @i{true}
  438. (gethash nil table) @result{} NIL, @i{false}
  439. (setf (gethash nil table) nil) @result{} NIL
  440. (gethash nil table) @result{} NIL, @i{true}
  441. (defvar *counters* (make-hash-table)) @result{} *COUNTERS*
  442. (gethash 'foo *counters*) @result{} NIL, @i{false}
  443. (gethash 'foo *counters* 0) @result{} 0, @i{false}
  444. (defmacro how-many (obj) `(values (gethash ,obj *counters* 0))) @result{} HOW-MANY
  445. (defun count-it (obj) (incf (how-many obj))) @result{} COUNT-IT
  446. (dolist (x '(bar foo foo bar bar baz)) (count-it x))
  447. (how-many 'foo) @result{} 2
  448. (how-many 'bar) @result{} 3
  449. (how-many 'quux) @result{} 0
  450. @end example
  451. @subsubheading See Also::
  452. @ref{remhash}
  453. @subsubheading Notes::
  454. The @i{secondary value}, @i{present-p},
  455. can be used to distinguish the absence of an entry
  456. from the presence of an entry that has a value of @i{default}.
  457. @node remhash, maphash, gethash, Hash Tables Dictionary
  458. @subsection remhash [Function]
  459. @code{remhash} @i{key hash-table} @result{} @i{generalized-boolean}
  460. @subsubheading Arguments and Values::
  461. @i{key}---an @i{object}.
  462. @i{hash-table}---a @i{hash table}.
  463. @i{generalized-boolean}---a @i{generalized boolean}.
  464. @subsubheading Description::
  465. Removes the entry for @i{key} in @i{hash-table}, if any.
  466. Returns @i{true} if there was such an entry, or @i{false} otherwise.
  467. @subsubheading Examples::
  468. @example
  469. (setq table (make-hash-table)) @result{} #<HASH-TABLE EQL 0/120 32115666>
  470. (setf (gethash 100 table) "C") @result{} "C"
  471. (gethash 100 table) @result{} "C", @i{true}
  472. (remhash 100 table) @result{} @i{true}
  473. (gethash 100 table) @result{} NIL, @i{false}
  474. (remhash 100 table) @result{} @i{false}
  475. @end example
  476. @subsubheading Side Effects::
  477. The @i{hash-table} is modified.
  478. @node maphash, with-hash-table-iterator, remhash, Hash Tables Dictionary
  479. @subsection maphash [Function]
  480. @code{maphash} @i{function hash-table} @result{} @i{@b{nil}}
  481. @subsubheading Arguments and Values::
  482. @i{function}---a @i{designator} for a @i{function} of two @i{arguments},
  483. the @i{key} and the @i{value}.
  484. @i{hash-table}---a @i{hash table}.
  485. @subsubheading Description::
  486. Iterates over all entries in the @i{hash-table}. For each entry,
  487. the @i{function} is called with two @i{arguments}--the @i{key}
  488. and the @i{value} of that entry.
  489. The consequences are unspecified if any attempt is made to add or remove
  490. an entry from the @i{hash-table} while a @b{maphash} is in progress,
  491. with two exceptions:
  492. the @i{function} can use can use @b{setf} of @b{gethash}
  493. to change the @i{value} part of the entry currently being processed,
  494. or it can use @b{remhash} to remove that entry.
  495. @subsubheading Examples::
  496. @example
  497. (setq table (make-hash-table)) @result{} #<HASH-TABLE EQL 0/120 32304110>
  498. (dotimes (i 10) (setf (gethash i table) i)) @result{} NIL
  499. (let ((sum-of-squares 0))
  500. (maphash #'(lambda (key val)
  501. (let ((square (* val val)))
  502. (incf sum-of-squares square)
  503. (setf (gethash key table) square)))
  504. table)
  505. sum-of-squares) @result{} 285
  506. (hash-table-count table) @result{} 10
  507. (maphash #'(lambda (key val)
  508. (when (oddp val) (remhash key table)))
  509. table) @result{} NIL
  510. (hash-table-count table) @result{} 5
  511. (maphash #'(lambda (k v) (print (list k v))) table)
  512. (0 0)
  513. (8 64)
  514. (2 4)
  515. (6 36)
  516. (4 16)
  517. @result{} NIL
  518. @end example
  519. @subsubheading Side Effects::
  520. None, other than any which might be done by the @i{function}.
  521. @subsubheading See Also::
  522. @ref{loop}
  523. ,
  524. @ref{with-hash-table-iterator}
  525. ,
  526. @ref{Traversal Rules and Side Effects}
  527. @node with-hash-table-iterator, clrhash, maphash, Hash Tables Dictionary
  528. @subsection with-hash-table-iterator [Macro]
  529. @code{with-hash-table-iterator} @i{@r{(}name hash-table@r{)}
  530. @{@i{declaration}@}{*} @{@i{form}@}{*}} @result{} @i{@{@i{result}@}{*}}
  531. @subsubheading Arguments and Values::
  532. @i{name}---a name suitable for the first argument to @b{macrolet}.
  533. @i{hash-table}---a @i{form}, evaluated once, that should produce a @i{hash table}.
  534. @i{declaration}---a @b{declare} @i{expression}; not evaluated.
  535. @i{forms}---an @i{implicit progn}.
  536. @i{results}---the @i{values} returned by @i{forms}.
  537. @subsubheading Description::
  538. Within the lexical scope of the body, @i{name} is defined via @b{macrolet}
  539. such that successive invocations of @t{(@i{name})} return the items,
  540. one by one, from the @i{hash table} that is obtained by evaluating
  541. @i{hash-table} only once.
  542. An invocation @t{(@i{name})} returns three values as follows:
  543. @table @asis
  544. @item 1.
  545. A @i{generalized boolean} that is @i{true} if an entry is returned.
  546. @item 2.
  547. The key from the @i{hash-table} entry.
  548. @item 3.
  549. The value from the @i{hash-table} entry.
  550. @end table
  551. After all entries have been returned by successive invocations of
  552. @t{(@i{name})}, then only one value is returned, namely @b{nil}.
  553. It is unspecified what happens if any of the implicit interior state
  554. of an iteration is returned outside the dynamic extent of the
  555. @b{with-hash-table-iterator} @i{form}
  556. such as by returning some @i{closure} over the invocation @i{form}.
  557. Any number of invocations of @b{with-hash-table-iterator}
  558. can be nested, and the body of the innermost one can invoke all of the
  559. locally @i{established} @i{macros}, provided all of those @i{macros}
  560. have @i{distinct} names.
  561. @subsubheading Examples::
  562. The following function should return @b{t} on any
  563. @i{hash table}, and signal
  564. an error if the usage of @b{with-hash-table-iterator} does not agree
  565. with the corresponding usage of @b{maphash}.
  566. @example
  567. (defun test-hash-table-iterator (hash-table)
  568. (let ((all-entries '())
  569. (generated-entries '())
  570. (unique (list nil)))
  571. (maphash #'(lambda (key value) (push (list key value) all-entries))
  572. hash-table)
  573. (with-hash-table-iterator (generator-fn hash-table)
  574. (loop
  575. (multiple-value-bind (more? key value) (generator-fn)
  576. (unless more? (return))
  577. (unless (eql value (gethash key hash-table unique))
  578. (error "Key ~S not found for value ~S" key value))
  579. (push (list key value) generated-entries))))
  580. (unless (= (length all-entries)
  581. (length generated-entries)
  582. (length (union all-entries generated-entries
  583. :key #'car :test (hash-table-test hash-table))))
  584. (error "Generated entries and Maphash entries don't correspond"))
  585. t))
  586. @end example
  587. The following could be an acceptable definition of
  588. @b{maphash}, implemented by @b{with-hash-table-iterator}.
  589. @example
  590. (defun maphash (function hash-table)
  591. (with-hash-table-iterator (next-entry hash-table)
  592. (loop (multiple-value-bind (more key value) (next-entry)
  593. (unless more (return nil))
  594. (funcall function key value)))))
  595. @end example
  596. @subsubheading Exceptional Situations::
  597. The consequences are undefined if the local function named @i{name}
  598. @i{established} by @b{with-hash-table-iterator} is called after it has
  599. returned @i{false} as its @i{primary value}.
  600. @subsubheading See Also::
  601. @ref{Traversal Rules and Side Effects}
  602. @node clrhash, sxhash, with-hash-table-iterator, Hash Tables Dictionary
  603. @subsection clrhash [Function]
  604. @code{clrhash} @i{hash-table} @result{} @i{hash-table}
  605. @subsubheading Arguments and Values::
  606. @i{hash-table}---a @i{hash table}.
  607. @subsubheading Description::
  608. Removes all entries from @i{hash-table},
  609. and then returns that empty @i{hash table}.
  610. @subsubheading Examples::
  611. @example
  612. (setq table (make-hash-table)) @result{} #<HASH-TABLE EQL 0/120 32004073>
  613. (dotimes (i 100) (setf (gethash i table) (format nil "~R" i))) @result{} NIL
  614. (hash-table-count table) @result{} 100
  615. (gethash 57 table) @result{} "fifty-seven", @i{true}
  616. (clrhash table) @result{} #<HASH-TABLE EQL 0/120 32004073>
  617. (hash-table-count table) @result{} 0
  618. (gethash 57 table) @result{} NIL, @i{false}
  619. @end example
  620. @subsubheading Side Effects::
  621. The @i{hash-table} is modified.
  622. @node sxhash, , clrhash, Hash Tables Dictionary
  623. @subsection sxhash [Function]
  624. @code{sxhash} @i{object} @result{} @i{hash-code}
  625. @subsubheading Arguments and Values::
  626. @i{object}---an @i{object}.
  627. @i{hash-code}---a non-negative @i{fixnum}.
  628. @subsubheading Description::
  629. @b{sxhash} returns a hash code for @i{object}.
  630. The manner in which the hash code is computed is @i{implementation-dependent},
  631. but subject to certain constraints:
  632. @table @asis
  633. @item 1.
  634. @t{(equal @i{x} @i{y})} implies @t{(= (sxhash @i{x}) (sxhash @i{y}))}.
  635. @item 2.
  636. For any two @i{objects}, @i{x} and @i{y},
  637. both of which are
  638. @i{bit vectors},
  639. @i{characters},
  640. @i{conses},
  641. @i{numbers},
  642. @i{pathnames},
  643. @i{strings},
  644. or @i{symbols},
  645. and which are @i{similar},
  646. @t{(sxhash @i{x})} and @t{(sxhash @i{y})}
  647. @i{yield} the same mathematical value
  648. even if @i{x} and @i{y} exist in different @i{Lisp images} of
  649. the same @i{implementation}.
  650. See @ref{Literal Objects in Compiled Files}.
  651. @item 3.
  652. The @i{hash-code} for an @i{object} is always the @i{same}
  653. within a single @i{session} provided that the @i{object} is not
  654. visibly modified with regard to the equivalence test @b{equal}.
  655. See @ref{Modifying Hash Table Keys}.
  656. @item 4.
  657. The @i{hash-code} is intended for hashing. This places no verifiable
  658. constraint on a @i{conforming implementation}, but the intent is that
  659. an @i{implementation} should make a good-faith effort to produce
  660. @i{hash-codes} that are well distributed within the range of
  661. non-negative @i{fixnums}.
  662. @item 5.
  663. Computation of the @i{hash-code} must terminate,
  664. even if the @i{object} contains circularities.
  665. @end table
  666. @subsubheading Examples::
  667. @example
  668. (= (sxhash (list 'list "ab")) (sxhash (list 'list "ab"))) @result{} @i{true}
  669. (= (sxhash "a") (sxhash (make-string 1 :initial-element #\a))) @result{} @i{true}
  670. (let ((r (make-random-state)))
  671. (= (sxhash r) (sxhash (make-random-state r))))
  672. @result{} @i{implementation-dependent}
  673. @end example
  674. @subsubheading Affected By::
  675. The @i{implementation}.
  676. @subsubheading Notes::
  677. Many common hashing needs are satisfied by @b{make-hash-table} and the
  678. related functions on @i{hash tables}. @b{sxhash} is intended for use
  679. where the pre-defined abstractions are insufficient. Its main intent is to
  680. allow the user a convenient means of implementing more complicated hashing
  681. paradigms than are provided through @i{hash tables}.
  682. The hash codes returned by @b{sxhash} are not necessarily related to
  683. any hashing strategy used by any other @i{function} in @r{Common Lisp}.
  684. For @i{objects} of @i{types} that @b{equal} compares
  685. with @b{eq}, item 3 requires that the @i{hash-code} be
  686. based on some immutable quality of the identity of the object.
  687. Another legitimate implementation technique would be to have
  688. @b{sxhash} assign (and cache) a random hash code for these
  689. @i{objects}, since there is no requirement that @i{similar} but
  690. non-@b{eq} objects have the same hash code.
  691. Although @i{similarity} is defined for @i{symbols} in terms
  692. of both the @i{symbol}'s @i{name} and the @i{packages} in which
  693. the @i{symbol} is @i{accessible}, item 3 disallows using @i{package}
  694. information to compute the hash code, since changes to the package status
  695. of a symbol are not visible to @i{equal}.
  696. @c end of including dict-hash-tables
  697. @c %**end of chapter