dict-hash-tables.tex 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. % -*- Mode: TeX -*-
  2. %
  3. % !!!
  4. % Barrett:
  5. % What about errors for these?
  6. %
  7. % !!!
  8. % JonL:
  9. % I'll bet we don't define "grow" (as a verb -- trivial point would be to
  10. % put this in passive voice rather than active; better, simply say "before
  11. % the table size has to be enlarged.")
  12. % Given that fuzziness (about "grow") it is still unclear what the interaction
  13. % between SIZE and THRESHOLD is. Since THRESHOLD later is described as being
  14. % merely a "hint", then maybe all one needs to say is that the the programmer
  15. % is permitted to express his intent that no enlargements will be forced until
  16. % after about SIZE different entries have been made, and at that the time of
  17. % enlargement, the actual storage size of the table may be larger than SIZE by
  18. % an amount indicated by the THRESHOLD parameter -- that is the SIZE parameter
  19. % will be at the "threshold" value rather than the "actual size" value.
  20. %-------------------- Hash-Table Type --------------------
  21. \begincom{hash-table}\ftype{System Class}
  22. \label Class Precedence List::
  23. \typeref{hash-table},
  24. \typeref{t}
  25. \label Description::
  26. %% 16.0.0 3 (has been mostly gutted)
  27. \term{Hash tables} provide a way of mapping any \term{object} (a \term{key})
  28. to an associated \term{object} (a \term{value}).
  29. \label See Also::
  30. {\secref\HashTableConcepts},
  31. {\secref\PrintingOtherObjects}
  32. \label Notes::
  33. % This is to satisfy JonL.
  34. The intent is that this mapping be implemented by a hashing mechanism,
  35. such as that described in Section 6.4 ``Hashing'' of {\KnuthVolThree}
  36. (pp506-549). In spite of this intent, no \term{conforming implementation}
  37. is required to use any particular technique to implement the mapping.
  38. \endcom%{hash-table}\ftype{System Class}
  39. %%% ========== MAKE-HASH-TABLE
  40. \begincom{make-hash-table}\ftype{Function}
  41. \label Syntax::
  42. \DefunWithValues make-hash-table {{\key} test size rehash-size rehash-threshold}
  43. {hash-table}
  44. \label Arguments and Values::
  45. \param{test}---a \term{designator} for one of the \term{functions}
  46. \funref{eq},
  47. \funref{eql},
  48. \funref{equal}, or
  49. \issue{HASH-TABLE-TESTS:ADD-EQUALP}
  50. \funref{equalp}.
  51. \endissue{HASH-TABLE-TESTS:ADD-EQUALP}
  52. \Default{\funref{eql}}
  53. %% 16.0.0 13
  54. \issue{ARGUMENTS-UNDERSPECIFIED:SPECIFY}
  55. \param{size}---a non-negative \term{integer}.
  56. \endissue{ARGUMENTS-UNDERSPECIFIED:SPECIFY}
  57. \Default{\term{implementation-dependent}}
  58. %% 16.0.0 14
  59. \param{rehash-size}---a \term{real} of \term{type} \f{(or (integer 1 *) (float (1.0) *))}.
  60. \Default{\term{implementation-dependent}}
  61. %% 16.0.0 15
  62. \issue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  63. \param{rehash-threshold}---a \term{real} of \term{type} \f{(real 0 1)}.
  64. \Default{\term{implementation-dependent}}
  65. % KMP thinks HASH-TABLE-SIZE:INTENDED-ENTRIES implies this can no longer be an integer.
  66. % Moon concurs.
  67. \endissue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  68. \param{hash-table}---a \term{hash table}.
  69. \label Description::
  70. %% 16.0.0 12
  71. Creates and returns a new \term{hash table}.
  72. \param{test} determines how \term{keys} are compared.
  73. %This seemed to need elaboration, so I added the rest of this paragraph. -kmp 27-Feb-91
  74. An \term{object} is said to be present in the \param{hash-table}
  75. if that \term{object} is the \term{same} under the \term{test}
  76. as the \term{key} for some entry in the \param{hash-table}.
  77. \param{size} is a hint to the \term{implementation} about how much initial space
  78. to allocate in the \param{hash-table}.
  79. \issue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  80. %It specifies
  81. %% For JonL, I substituted the next line:
  82. This information, taken together with the \param{rehash-threshold}, controls
  83. the approximate number of entries which it should be possible
  84. to insert before the table has to grow.
  85. \endissue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  86. The actual size might be rounded up from \param{size} to the next `good' size;
  87. for example, some \term{implementations} might round to the next prime number.
  88. \param{rehash-size} specifies a minimum amount to increase the size of the
  89. \param{hash-table} when it becomes full
  90. enough to require rehashing;
  91. see \param{rehash-theshold} below.
  92. \issue{HASH-TABLE-REHASH-SIZE-INTEGER}
  93. If \param{rehash-size} is an \term{integer},
  94. the expected growth rate for the table is additive and
  95. the \term{integer} is the number of entries to add;
  96. if it is a \term{float},
  97. the expected growth rate for the table is multiplicative and
  98. the \term{float} is the ratio of the new size to the old size.
  99. \endissue{HASH-TABLE-REHASH-SIZE-INTEGER}
  100. As with \param{size}, the actual size of the increase might be rounded up.
  101. \param{rehash-threshold} specifies how full the \param{hash-table} can get
  102. before it must grow.
  103. \issue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  104. It specifies the maximum desired hash-table occupancy level.
  105. %If \param{rehash-threshold} is an \term{integer}
  106. %it will be scaled whenever the table is grown.
  107. \issue{HASH-TABLE-REHASH-SIZE-INTEGER}
  108. The \term{values} of \param{rehash-size} and \param{rehash-threshold} do not constrain the
  109. \term{implementation} to use any particular method for computing when and by how much
  110. the size of \param{hash-table} should be enlarged. Such decisions are
  111. \term{implementation-dependent}, and these \term{values} only hints
  112. from the \term{programmer} to the \term{implementation}, and the \term{implementation}
  113. is permitted to ignore them.
  114. \endissue{HASH-TABLE-REHASH-SIZE-INTEGER}
  115. \endissue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  116. \label Examples::
  117. %% 16.0.0 16
  118. \code
  119. (setq table (make-hash-table)) \EV #<HASH-TABLE EQL 0/120 46142754>
  120. (setf (gethash "one" table) 1) \EV 1
  121. (gethash "one" table) \EV NIL, \term{false}
  122. (setq table (make-hash-table :test 'equal)) \EV #<HASH-TABLE EQUAL 0/139 46145547>
  123. (setf (gethash "one" table) 1) \EV 1
  124. (gethash "one" table) \EV 1, T
  125. (make-hash-table :rehash-size 1.5 :rehash-threshold 0.7)
  126. \EV #<HASH-TABLE EQL 0/120 46156620>
  127. \endcode
  128. \issue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  129. % I removed this example because it uses an integer :rehash-threshold. -kmp 21-May-91
  130. % (make-hash-table :size 100 :rehash-size 50 :rehash-threshold 75)
  131. % \EV #<HASH-TABLE EQL 0/100 46152651>
  132. \endissue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  133. \label Affected By:\None.
  134. \label Exceptional Situations:\None.
  135. \label See Also::
  136. \funref{gethash},
  137. \typeref{hash-table}
  138. \label Notes:\None.
  139. %% JonL says this is "redundant, and perhaps confusing". -kmp 3-Feb-92
  140. % There are \term{hash tables} that hash on \term{object} identity
  141. % (using \funref{eq} or \funref{eql})
  142. % and there are \term{hash tables} that hash on \term{object} structure
  143. % (using \funref{equal} and \funref{equalp}).
  144. \endcom
  145. %%% ========== HASH-TABLE-P
  146. \begincom{hash-table-p}\ftype{Function}
  147. \label Syntax::
  148. \DefunWithValues hash-table-p {object} {generalized-boolean}
  149. \label Arguments and Values::
  150. \param{object}---an \term{object}.
  151. \param{generalized-boolean}---a \term{generalized boolean}.
  152. \label Description::
  153. %% 16.0.0 17
  154. \TypePredicate{object}{hash-table}
  155. \label Examples::
  156. \code
  157. (setq table (make-hash-table)) \EV #<HASH-TABLE EQL 0/120 32511220>
  158. (hash-table-p table) \EV \term{true}
  159. (hash-table-p 37) \EV \term{false}
  160. (hash-table-p '((a . 1) (b . 2))) \EV \term{false}
  161. \endcode
  162. \label Side Effects:\None.
  163. \label Affected By:\None.
  164. \label Exceptional Situations:\None!
  165. \label See Also:\None.
  166. \label Notes::
  167. \code
  168. (hash-table-p \param{object}) \EQ (typep \param{object} 'hash-table)
  169. \endcode
  170. \endcom
  171. %%% ========== HASH-TABLE-COUNT
  172. \begincom{hash-table-count}\ftype{Function}
  173. \label Syntax::
  174. \DefunWithValues hash-table-count {hash-table} {count}
  175. \label Arguments and Values::
  176. \param{hash-table}---a \term{hash table}.
  177. \param{count}---a non-negative \term{integer}.
  178. \label Description::
  179. %% 16.0.0 25
  180. Returns the number of entries in the \param{hash-table}.
  181. If \param{hash-table} has just been created
  182. or newly cleared (see \funref{clrhash})
  183. the entry count is \f{0}.
  184. \label Examples::
  185. \code
  186. (setq table (make-hash-table)) \EV #<HASH-TABLE EQL 0/120 32115135>
  187. (hash-table-count table) \EV 0
  188. (setf (gethash 57 table) "fifty-seven") \EV "fifty-seven"
  189. (hash-table-count table) \EV 1
  190. (dotimes (i 100) (setf (gethash i table) i)) \EV NIL
  191. (hash-table-count table) \EV 100
  192. \endcode
  193. \label Side Effects:\None.
  194. \label Affected By::
  195. \funref{clrhash},
  196. \funref{remhash},
  197. \SETFof{gethash}
  198. \label Exceptional Situations:\None.
  199. \label See Also::
  200. \funref{hash-table-size}
  201. \label Notes::
  202. The following relationships are functionally correct, although in practice
  203. using \funref{hash-table-count} is probably much faster:
  204. \code
  205. (hash-table-count \param{table}) \EQ
  206. (loop for value being the hash-values of \param{table} count t) \EQ
  207. (let ((total 0))
  208. (maphash #'(lambda (key value)
  209. (declare (ignore key value))
  210. (incf total))
  211. \param{table})
  212. total)
  213. \endcode
  214. \endcom
  215. %%% ========== HASH-TABLE-REHASH-SIZE
  216. \begincom{hash-table-rehash-size}\ftype{Function}
  217. \issue{HASH-TABLE-ACCESS:X3J13-MAR-89}
  218. \label Syntax::
  219. \DefunWithValues hash-table-rehash-size {hash-table} {rehash-size}
  220. \label Arguments and Values::
  221. \param{hash-table}---a \term{hash table}.
  222. \param{rehash-size}---a \term{real} of \term{type} \f{(or (integer 1 *) (float (1.0) *))}.
  223. \label Description::
  224. Returns the current rehash size of \param{hash-table},
  225. suitable for use in a call to \funref{make-hash-table}
  226. in order to produce a \term{hash table}
  227. with state corresponding to the current state of the \param{hash-table}.
  228. \label Examples::
  229. \code
  230. (setq table (make-hash-table :size 100 :rehash-size 1.4))
  231. \EV #<HASH-TABLE EQL 0/100 2556371>
  232. (hash-table-rehash-size table) \EV 1.4
  233. \endcode
  234. \label Side Effects:\None.
  235. \label Affected By:\None.
  236. \label Exceptional Situations::
  237. \Shouldchecktype{hash-table}{a \term{hash table}}
  238. \label See Also::
  239. \funref{make-hash-table},
  240. \funref{hash-table-rehash-threshold}
  241. \label Notes::
  242. \issue{HASH-TABLE-REHASH-SIZE-INTEGER}
  243. If the hash table was created with an \term{integer} rehash size,
  244. the result is an \term{integer},
  245. indicating that the rate of growth of the \param{hash-table} when rehashed
  246. is intended to be additive;
  247. otherwise,
  248. the result is a \term{float},
  249. indicating that the rate of growth of the \param{hash-table} when rehashed
  250. is intended to be multiplicative.
  251. However, this value is only advice to the \term{implementation};
  252. the actual amount by which the \param{hash-table} will grow upon rehash is
  253. \term{implementation-dependent}.
  254. \endissue{HASH-TABLE-REHASH-SIZE-INTEGER}
  255. \endissue{HASH-TABLE-ACCESS:X3J13-MAR-89}
  256. \endcom
  257. %%% ========== HASH-TABLE-REHASH-THRESHOLD
  258. \begincom{hash-table-rehash-threshold}\ftype{Function}
  259. \issue{HASH-TABLE-ACCESS:X3J13-MAR-89}
  260. \label Syntax::
  261. \DefunWithValues hash-table-rehash-threshold {hash-table} {rehash-threshold}
  262. \label Arguments and Values::
  263. \param{hash-table}---a \term{hash table}.
  264. \issue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  265. \param{rehash-threshold}---a \term{real} of \term{type} \f{(real 0 1)}.
  266. % KMP thinks HASH-TABLE-SIZE:INTENDED-ENTRIES implies this can no longer be an integer.
  267. % Moon concurs.
  268. \endissue{HASH-TABLE-SIZE:INTENDED-ENTRIES}
  269. \label Description::
  270. Returns the current rehash threshold of \param{hash-table}, which is
  271. suitable for use in a call to \funref{make-hash-table} in order to
  272. produce a \term{hash table} with state corresponding to the current
  273. state of the \param{hash-table}.
  274. \label Examples::
  275. \code
  276. (setq table (make-hash-table :size 100 :rehash-threshold 0.5))
  277. \EV #<HASH-TABLE EQL 0/100 2562446>
  278. (hash-table-rehash-threshold table) \EV 0.5
  279. \endcode
  280. \label Side Effects:\None.
  281. \label Affected By:\None.
  282. \label Exceptional Situations::
  283. \Shouldchecktype{hash-table}{a \term{hash table}}
  284. \label See Also::
  285. \funref{make-hash-table},
  286. \funref{hash-table-rehash-size}
  287. %% Per X3J13. -kmp 05-Oct-93
  288. \label Notes:\None.
  289. \endissue{HASH-TABLE-ACCESS:X3J13-MAR-89}
  290. \endcom
  291. %%% ========== HASH-TABLE-SIZE
  292. \begincom{hash-table-size}\ftype{Function}
  293. \issue{HASH-TABLE-ACCESS:X3J13-MAR-89}
  294. \label Syntax::
  295. \DefunWithValues hash-table-size {hash-table} {size}
  296. \label Arguments and Values::
  297. \param{hash-table}---a \term{hash table}.
  298. \param{size}---a non-negative \term{integer}.
  299. \label Description::
  300. Returns the current size of \param{hash-table}, which is suitable for use in
  301. a call to \funref{make-hash-table} in order to produce a \term{hash table}
  302. with state corresponding to the current state of the \param{hash-table}.
  303. \label Examples:\None.
  304. \label Side Effects:\None.
  305. \label Affected By:\None.
  306. \label Exceptional Situations::
  307. \Shouldchecktype{hash-table}{a \term{hash table}}
  308. \label See Also::
  309. \funref{hash-table-count},
  310. \funref{make-hash-table}
  311. %% Per X3J13. -kmp 05-Oct-93
  312. \label Notes:\None.
  313. \endissue{HASH-TABLE-ACCESS:X3J13-MAR-89}
  314. \endcom
  315. %%% ========== HASH-TABLE-TEST
  316. \begincom{hash-table-test}\ftype{Function}
  317. \issue{HASH-TABLE-ACCESS:X3J13-MAR-89}
  318. \label Syntax::
  319. \DefunWithValues hash-table-test {hash-table} {test}
  320. \label Arguments and Values::
  321. \param{hash-table}---a \term{hash table}.
  322. \param{test}---a \term{function designator}.
  323. For the four \term{standardized} \term{hash table} test \term{functions}
  324. (see \funref{make-hash-table}), the \param{test} value returned
  325. is always a \term{symbol}. If an \term{implementation} permits additional
  326. tests, it is \term{implementation-dependent} whether such tests are
  327. returned as \term{function} \term{objects} or \term{function names}.
  328. \label Description::
  329. Returns the test used for comparing \term{keys} in \param{hash-table}.
  330. \label Examples:\None.
  331. \label Side Effects:\None.
  332. \label Affected By:\None.
  333. \label Exceptional Situations::
  334. \Shouldchecktype{hash-table}{a \term{hash table}}
  335. \label See Also::
  336. \funref{make-hash-table}
  337. %% Per X3J13. -kmp 05-Oct-93
  338. \label Notes:\None.
  339. \endissue{HASH-TABLE-ACCESS:X3J13-MAR-89}
  340. \endcom
  341. %%% ========== GETHASH
  342. \begincom{gethash}\ftype{Accessor}
  343. \label Syntax::
  344. \DefunWithValues gethash {key hash-table {\opt} default} {value, present-p}
  345. \Defsetf gethash {key hash-table {\opt} default} {new-value}
  346. \label Arguments and Values::
  347. \param{key}---an \term{object}.
  348. \param{hash-table}---a \term{hash table}.
  349. \param{default}---an \term{object}.
  350. \Default{\nil}
  351. \param{value}---an \term{object}.
  352. \param{present-p}---a \term{generalized boolean}.
  353. \label Description::
  354. %% 16.0.0 18
  355. \param{Value} is the \term{object} in \param{hash-table} whose \term{key}
  356. is the \term{same} as \param{key} under the \param{hash-table}'s equivalence test.
  357. If there is no such entry, \param{value} is the \param{default}.
  358. %% 16.0.0 19
  359. \param{Present-p} is \term{true} if an entry is found; otherwise, it is \term{false}.
  360. %% 16.0.0 20
  361. \macref{setf} may be used with \funref{gethash} to modify the \term{value}
  362. associated with a given \term{key}, or to add a new entry.
  363. \issue{SETF-GET-DEFAULT:EVALUATED-BUT-IGNORED}
  364. When a \funref{gethash} \term{form} is used as a \macref{setf} \param{place},
  365. any \param{default} which is supplied is evaluated according to normal
  366. left-to-right evaluation rules, but its \term{value} is ignored.
  367. % \param{Default} may be supplied to \funref{gethash}
  368. % in this context; it is ignored by the
  369. % \macref{setf} expander function for \funref{gethash}, but
  370. % may be useful in such macros
  371. % as \macref{incf} that are related to \macref{setf}:
  372. \endissue{SETF-GET-DEFAULT:EVALUATED-BUT-IGNORED}
  373. \label Examples::
  374. \code
  375. (setq table (make-hash-table)) \EV #<HASH-TABLE EQL 0/120 32206334>
  376. (gethash 1 table) \EV NIL, \term{false}
  377. (gethash 1 table 2) \EV 2, \term{false}
  378. (setf (gethash 1 table) "one") \EV "one"
  379. (setf (gethash 2 table "two") "two") \EV "two"
  380. (gethash 1 table) \EV "one", \term{true}
  381. (gethash 2 table) \EV "two", \term{true}
  382. (gethash nil table) \EV NIL, \term{false}
  383. (setf (gethash nil table) nil) \EV NIL
  384. (gethash nil table) \EV NIL, \term{true}
  385. (defvar *counters* (make-hash-table)) \EV *COUNTERS*
  386. (gethash 'foo *counters*) \EV NIL, \term{false}
  387. (gethash 'foo *counters* 0) \EV 0, \term{false}
  388. (defmacro how-many (obj) `(values (gethash ,obj *counters* 0))) \EV HOW-MANY
  389. (defun count-it (obj) (incf (how-many obj))) \EV COUNT-IT
  390. (dolist (x '(bar foo foo bar bar baz)) (count-it x))
  391. (how-many 'foo) \EV 2
  392. (how-many 'bar) \EV 3
  393. (how-many 'quux) \EV 0
  394. \endcode
  395. \label Side Effects:\None.
  396. \label Affected By:\None.
  397. \label Exceptional Situations:\None.
  398. \label See Also::
  399. \funref{remhash}
  400. \label Notes::
  401. The \term{secondary value}, \param{present-p},
  402. can be used to distinguish the absence of an entry
  403. from the presence of an entry that has a value of \param{default}.
  404. \endcom
  405. %%% ========== REMHASH
  406. \begincom{remhash}\ftype{Function}
  407. \label Syntax::
  408. \DefunWithValues remhash {key hash-table} {generalized-boolean}
  409. \label Arguments and Values::
  410. \param{key}---an \term{object}.
  411. \param{hash-table}---a \term{hash table}.
  412. \param{generalized-boolean}---a \term{generalized boolean}.
  413. \label Description::
  414. %% 16.0.0 21
  415. Removes the entry for \param{key} in \param{hash-table}, if any.
  416. Returns \term{true} if there was such an entry, or \term{false} otherwise.
  417. \label Examples::
  418. \code
  419. (setq table (make-hash-table)) \EV #<HASH-TABLE EQL 0/120 32115666>
  420. (setf (gethash 100 table) "C") \EV "C"
  421. (gethash 100 table) \EV "C", \term{true}
  422. (remhash 100 table) \EV \term{true}
  423. (gethash 100 table) \EV NIL, \term{false}
  424. (remhash 100 table) \EV \term{false}
  425. \endcode
  426. \label Side Effects::
  427. The \param{hash-table} is modified.
  428. \label Affected By:\None.
  429. \label Exceptional Situations:\None.
  430. \label See Also:\None.
  431. \label Notes:\None.
  432. \endcom
  433. %%% ========== MAPHASH
  434. \begincom{maphash}\ftype{Function}
  435. \label Syntax::
  436. %% 16.0.0 23
  437. \DefunWithValues maphash {function hash-table} {\nil}
  438. \label Arguments and Values::
  439. \param{function}---a \term{designator} for a \term{function} of two \term{arguments},
  440. the \term{key} and the \term{value}.
  441. \param{hash-table}---a \term{hash table}.
  442. \label Description::
  443. %% 16.0.0 22
  444. Iterates over all entries in the \param{hash-table}. For each entry,
  445. the \param{function} is called with two \term{arguments}--the \term{key}
  446. and the \term{value} of that entry.
  447. The consequences are unspecified if any attempt is made to add or remove
  448. an entry from the \param{hash-table} while a \funref{maphash} is in progress,
  449. with two exceptions:
  450. the \param{function} can use can use \macref{setf} of \funref{gethash}
  451. to change the \term{value} part of the entry currently being processed,
  452. or it can use \funref{remhash} to remove that entry.
  453. \label Examples::
  454. \code
  455. (setq table (make-hash-table)) \EV #<HASH-TABLE EQL 0/120 32304110>
  456. (dotimes (i 10) (setf (gethash i table) i)) \EV NIL
  457. (let ((sum-of-squares 0))
  458. (maphash #'(lambda (key val)
  459. (let ((square (* val val)))
  460. (incf sum-of-squares square)
  461. (setf (gethash key table) square)))
  462. table)
  463. sum-of-squares) \EV 285
  464. (hash-table-count table) \EV 10
  465. (maphash #'(lambda (key val)
  466. (when (oddp val) (remhash key table)))
  467. table) \EV NIL
  468. (hash-table-count table) \EV 5
  469. (maphash #'(lambda (k v) (print (list k v))) table)
  470. (0 0)
  471. (8 64)
  472. (2 4)
  473. (6 36)
  474. (4 16)
  475. \EV NIL
  476. \endcode
  477. \label Side Effects::
  478. None, other than any which might be done by the \param{function}.
  479. \label Affected By:\None.
  480. \label Exceptional Situations:\None.
  481. \label See Also::
  482. \macref{loop},
  483. \macref{with-hash-table-iterator},
  484. \issue{MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE}
  485. {\secref\TraversalRules}
  486. \endissue{MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE}
  487. \label Notes:\None.
  488. \endcom
  489. %%% ========== WITH-HASH-TABLE-ITERATOR
  490. \begincom{with-hash-table-iterator}\ftype{Macro}
  491. \issue{DECLS-AND-DOC}
  492. \issue{HASH-TABLE-PACKAGE-GENERATORS:ADD-WITH-WRAPPER}
  493. \label Syntax::
  494. \DefmacWithValues with-hash-table-iterator
  495. {\paren{name hash-table}
  496. \starparam{declaration} \starparam{form}}
  497. {\starparam{result}}
  498. \label Arguments and Values::
  499. \param{name}---a name suitable for the first argument to \specref{macrolet}.
  500. \param{hash-table}---a \term{form}, evaluated once, that should produce a \term{hash table}.
  501. \param{declaration}---a \misc{declare} \term{expression}; \noeval.
  502. \param{forms}---an \term{implicit progn}.
  503. \param{results}---the \term{values} returned by \param{forms}.
  504. \label Description::
  505. %!!! KMP: Are declarations permitted?
  506. % JonL: Let's say No because this provides no name bindings, except for the MACROLET.
  507. % Can always use LOCALLY anyway.
  508. % KMP&Barrett: We added them anyway.
  509. Within the lexical scope of the body, \param{name} is defined via \specref{macrolet}
  510. such that successive invocations of \f{(\param{name})} return the items,
  511. one by one, from the \term{hash table} that is obtained by evaluating
  512. \param{hash-table} only once.
  513. An invocation \f{(\param{name})} returns three values as follows:
  514. \beginlist
  515. %\itemitem{1.} A flag indicating whether an entry is returned
  516. % (true means an entry is returned).
  517. \itemitem{1.} A \term{generalized boolean} that is \term{true} if an entry is returned.
  518. \itemitem{2.} The key from the \param{hash-table} entry.
  519. \itemitem{3.} The value from the \param{hash-table} entry.
  520. \endlist
  521. After all entries have been returned by successive invocations of
  522. \f{(\param{name})}, then only one value is returned, namely \nil.
  523. %!!! Barrett: Doesn't this just mean the macro has dynamic extent?
  524. It is unspecified what happens if any of the implicit interior state
  525. of an iteration is returned outside the dynamic extent of the
  526. \macref{with-hash-table-iterator} \term{form}
  527. such as by returning some \term{closure} over the invocation \term{form}.
  528. Any number of invocations of \macref{with-hash-table-iterator}
  529. can be nested, and the body of the innermost one can invoke all of the
  530. locally \term{established} \term{macros}, provided all of those \term{macros}
  531. have \term{distinct} names.
  532. \label Examples::
  533. The following function should return \t\ on any
  534. \term{hash table}, and signal
  535. an error if the usage of \macref{with-hash-table-iterator} does not agree
  536. with the corresponding usage of \funref{maphash}.
  537. \code
  538. (defun test-hash-table-iterator (hash-table)
  539. (let ((all-entries '())
  540. (generated-entries '())
  541. (unique (list nil)))
  542. (maphash #'(lambda (key value) (push (list key value) all-entries))
  543. hash-table)
  544. (with-hash-table-iterator (generator-fn hash-table)
  545. (loop
  546. (multiple-value-bind (more? key value) (generator-fn)
  547. (unless more? (return))
  548. (unless (eql value (gethash key hash-table unique))
  549. (error "Key ~S not found for value ~S" key value))
  550. (push (list key value) generated-entries))))
  551. (unless (= (length all-entries)
  552. (length generated-entries)
  553. (length (union all-entries generated-entries
  554. :key #'car :test (hash-table-test hash-table))))
  555. (error "Generated entries and Maphash entries don't correspond"))
  556. t))
  557. \endcode
  558. % "#'equal" => "(hash-table-test hash-table)" in the above per JonL.
  559. % There are maybe other problems as well. Mail sent to Quinquevirate.
  560. % -kmp 2-Feb-92
  561. The following could be an acceptable definition of
  562. \funref{maphash}, implemented by \macref{with-hash-table-iterator}.
  563. \code
  564. (defun maphash (function hash-table)
  565. (with-hash-table-iterator (next-entry hash-table)
  566. (loop (multiple-value-bind (more key value) (next-entry)
  567. (unless more (return nil))
  568. (funcall function key value)))))
  569. \endcode
  570. \label Side Effects:\None.
  571. \label Affected By:\None.
  572. \label Exceptional Situations::
  573. The consequences are undefined if the local function named \param{name}
  574. \term{established} by \macref{with-hash-table-iterator} is called after it has
  575. returned \term{false} as its \term{primary value}.
  576. \label See Also::
  577. \issue{MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE}
  578. {\secref\TraversalRules}
  579. \endissue{MAPPING-DESTRUCTIVE-INTERACTION:EXPLICITLY-VAGUE}
  580. \label Notes:\None.
  581. \endissue{HASH-TABLE-PACKAGE-GENERATORS:ADD-WITH-WRAPPER}
  582. \endissue{DECLS-AND-DOC}
  583. \endcom
  584. %%% ========== CLRHASH
  585. \begincom{clrhash}\ftype{Function}
  586. \label Syntax::
  587. \DefunWithValues clrhash {hash-table} {hash-table}
  588. \label Arguments and Values::
  589. \param{hash-table}---a \term{hash table}.
  590. \label Description::
  591. %% 16.0.0 24
  592. Removes all entries from \param{hash-table},
  593. and then returns that empty \term{hash table}.
  594. \label Examples::
  595. \code
  596. (setq table (make-hash-table)) \EV #<HASH-TABLE EQL 0/120 32004073>
  597. (dotimes (i 100) (setf (gethash i table) (format nil "~R" i))) \EV NIL
  598. (hash-table-count table) \EV 100
  599. (gethash 57 table) \EV "fifty-seven", \term{true}
  600. (clrhash table) \EV #<HASH-TABLE EQL 0/120 32004073>
  601. (hash-table-count table) \EV 0
  602. (gethash 57 table) \EV NIL, \term{false}
  603. \endcode
  604. \label Side Effects::
  605. The \param{hash-table} is modified.
  606. \label Affected By:\None.
  607. \label Exceptional Situations:\None.
  608. \label See Also:\None.
  609. \label Notes:\None.
  610. \endcom
  611. %%% ========== SXHASH
  612. \begincom{sxhash}\ftype{Function}
  613. \issue{SXHASH-DEFINITION:SIMILAR-FOR-SXHASH}
  614. \label Syntax::
  615. \DefunWithValues sxhash {object} {hash-code}
  616. \label Arguments and Values::
  617. \param{object}---an \term{object}.
  618. \param{hash-code}---a non-negative \term{fixnum}.
  619. \label Description::
  620. %% 16.0.0 27
  621. \funref{sxhash} returns a hash code for \param{object}.
  622. The manner in which the hash code is computed is \term{implementation-dependent},
  623. but subject to certain constraints:
  624. \beginlist
  625. \itemitem{1.}
  626. \f{(equal \param{x} \param{y})} implies \f{(= (sxhash \param{x}) (sxhash \param{y}))}.
  627. \itemitem{2.}
  628. For any two \term{objects}, \param{x} and \param{y},
  629. both of which are
  630. \term{bit vectors},
  631. \term{characters},
  632. \term{conses},
  633. \term{numbers},
  634. \term{pathnames},
  635. \term{strings},
  636. or \term{symbols},
  637. and which are \term{similar},
  638. \f{(sxhash \param{x})} and \f{(sxhash \param{y})}
  639. \term{yield} the same mathematical value
  640. even if \param{x} and \param{y} exist in different \term{Lisp images} of
  641. the same \term{implementation}.
  642. \Seesection\LiteralsInCompiledFiles.
  643. \itemitem{3.}
  644. The \param{hash-code} for an \term{object} is always the \term{same}
  645. within a single \term{session} provided that the \term{object} is not
  646. visibly modified with regard to the equivalence test \funref{equal}.
  647. \Seesection\ModifyingHashKeys.
  648. \itemitem{4.}
  649. The \param{hash-code} is intended for hashing. This places no verifiable
  650. constraint on a \term{conforming implementation}, but the intent is that
  651. an \term{implementation} should make a good-faith effort to produce
  652. \param{hash-codes} that are well distributed within the range of
  653. non-negative \term{fixnums}.
  654. \itemitem{5.}
  655. Computation of the \param{hash-code} must terminate,
  656. even if the \param{object} contains circularities.
  657. \endlist
  658. \label Examples::
  659. \code
  660. (= (sxhash (list 'list "ab")) (sxhash (list 'list "ab"))) \EV \term{true}
  661. (= (sxhash "a") (sxhash (make-string 1 :initial-element #\\a))) \EV \term{true}
  662. (let ((r (make-random-state)))
  663. (= (sxhash r) (sxhash (make-random-state r))))
  664. \EV \term{implementation-dependent}
  665. \endcode
  666. \label Side Effects:\None.
  667. \label Affected By::
  668. The \term{implementation}.
  669. \label Exceptional Situations:\None!
  670. \label See Also:\None.
  671. \label Notes::
  672. Many common hashing needs are satisfied by \funref{make-hash-table} and the
  673. related functions on \term{hash tables}. \funref{sxhash} is intended for use
  674. where the pre-defined abstractions are insufficient. Its main intent is to
  675. allow the user a convenient means of implementing more complicated hashing
  676. paradigms than are provided through \term{hash tables}.
  677. The hash codes returned by \funref{sxhash} are not necessarily related to
  678. any hashing strategy used by any other \term{function} in \clisp.
  679. For \term{objects} of \term{types} that \funref{equal} compares
  680. with \funref{eq}, item 3 requires that the \param{hash-code} be
  681. based on some immutable quality of the identity of the object.
  682. Another legitimate implementation technique would be to have
  683. \funref{sxhash} assign (and cache) a random hash code for these
  684. \term{objects}, since there is no requirement that \term{similar} but
  685. non-\funref{eq} objects have the same hash code.
  686. Although \term{similarity} is defined for \term{symbols} in terms
  687. of both the \term{symbol}'s \term{name} and the \term{packages} in which
  688. the \term{symbol} is \term{accessible}, item 3 disallows using \term{package}
  689. information to compute the hash code, since changes to the package status
  690. of a symbol are not visible to \param{equal}.
  691. \endissue{SXHASH-DEFINITION:SIMILAR-FOR-SXHASH}
  692. \endcom