哈希表

与提供整数索引的向量/数组相比,哈希表允许你使用任何对象作为索引或是键(key).

现在键可以是一个字符串,一个函数对象等等。

Make-hash-table创建哈希表,默认是按eql进行键值的等价。除了一种情况,就是如果当使用字符串进行匹配的时候。这个时候就需要由:test进行设定了。注意在这里的:test只能是接受四个值:eq,eql,equal,equalp

函数Gethash 提供了对哈希表元素的访问,接受两个参数,键和哈希表,并且返回相应键下的值或NIL(不存在该键或键本身就对应NIL)

CL-USER> (defparameter *hash* (make-hash-table))
*HASH*
CL-USER> (gethash 'foo *hash*)      
NIL
NIL
CL-USER> (setf (gethash 'foo *hash*)  'qu)
QU
CL-USER> (gethash 'foo *hash*) 
QU
T
下面说一下我上面在 NIL 括号里面所说的问题,你该如何区分当返回 NIL 时,他到底是因为键不存在,还是因为键本身值就是 NIL 。就像我们前面默认值是用参数 -supplied-p 来区别,这里实际上 gethash 同样是返回了一个用于标示的值,也就是说 gethash 实际上返回了两个值,另一个值除非显示处理否则将被偷偷丢掉。你会发现上面gethash的时候能够返回了两个值。

多值返回处理函数Multiple-Value-Bind 宏可以处理这个额外值。注意他调用时的方式,会把后面跟着的函数值(multiple-value-bind vars value-form &body)

CL-USER> (defun show-value (key hash-table)
	   		(multiple-value-bind (value present)(gethash key hash-table)
	   		  (if present
				 (format t "Value ~a actually present" value)
		 		(format t "Value ~a because key not found" value))))
SHOW-VALUE
CL-USER> (setf (gethash 'bar *hash*) nil)
NIL
CL-USER> (show-value 'foo *hash*)
Value QU actually present
NIL
CL-USER> (show-value 'bar *hash*)
Value NIL actually present
NIL
CL-USER> (show-value 'b *hash*)
Value NIL because key not found
NIL

哈希表的迭代

我们在sequence序列中,就定义了很多序列迭代函数。同样哈希表也定义了一些用于循环hashtable的操作。使用这个函数可以显示出hashtable中的所有元素

Maphash 接受一个两个参数的函数和一个哈希表,我们在序列中就是循环取出一个单元然后进行函数处理。现在到哈希表同样也是一次取出一个单元即包含键和值。然后传递给前面的两参数的函数对象。

CL-USER> (maphash (lambda (k v)(format t "~a => ~a~%" k v)) *hash*)
FOO => QU
BAR => NIL



(clrhash hash-table)   Removes all the entries from hash-table and returns it.

(gethash key hash-table &optional default) Function Returns the object indexed under key in hash-table, or default if there isn't one. Returns a second value true iff an entry was found. Settable.

(hash-table-count hash-table) Returns the number of entries in hash-table.

(hash-table-p object) Returns true if object is a hash table.

(hash-table-rehash-size hash-table) Returns a number, with the same significance as the :rehash-size argument to make-hash-table, that indicates how much hash-table should grow if it has to be expanded.

(hash-table-rehash-threshold hash-table) Returns a number, with the same significance as the :rehash-threshold argument to make-hash-table, that indicates when hash-table will be expanded.

(hash-table-size hash-table) Returns the number of spaces in hash-table.

(hash-table-test hash-table) Returns the function used to determine key equality in hash-table.

(make-hash-table &key test size rehash-size rehashthreshold) 

Returns a new hash table that uses test (default: eql) to determine the equality of keys. The size is a suggestion of the number of entries expected. The rehashsize is a suggestion of how much the table should grow if it has to be expanded: if an integer, suggests that many spaces should

 be added; if a float, suggests that the number of spaces should be multiplied by that amount. The rehash-threshold is a number between zero and one that suggests how full the table should be allowed to get before being expanded.

(maphash function hash-table) Applies function, which must be a function of two arguments, to the key and value of each entry in hash-table.

(remhash key (hash-table)) Removes the object indexed under key from hash-table, returning true if there was one.

(sxhash object) Essentially, a hashing function for equal hash tables. Returns a unique nonnegative fixnum for each set of equal arguments.

(with-hash-table-iterator (.symbol hash-table) 

                      declaration* expression*)

Evaluates the expressions with symbol defined as a local macro that returns information about successive entries in the value of hash-table. The local macro usually returns three values: a value that is true if more values are returned (so nil indicates the stream has run dry); the key of an entry; and the

object indexed


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值