Common Lisp专题2:hash-table


1)创建哈希表

用make-hash-table函数创建哈希表:


CL-USER> (defparameter *my-hash* (make-hash-table))
*MY-HASH*
CL-USER> (setf (gethash 'one-entry *my-hash*) "one")    ;;赋值
"one"
CL-USER> (setf (gethash 'another-entry *my-hash*) 2/4)
1/2
CL-USER> (gethash 'one-entry *my-hash*)
"one"
T
CL-USER> (gethash 'another-entry *my-hash*)
1/2


2)检测所创建的哈希表

CL-USER> (if (gethash 'one-entry *my-hash*)
         "Key exists"
         "Key does not exist")
"Key exists"
CL-USER> (if (gethash 'another-entry *my-hash*)
         "Key exists"
         "Key does not exist")
"Key exists"
CL-USER> (setf (gethash 'another-entry *my-hash*) nil)
NIL
CL-USER> (if (gethash 'another-entry *my-hash*)
         "Key exists"
         "Key does not exist")
"Key does not exist"
CL-USER> (if (nth-value 1 (gethash 'another-entry *my-hash*))
         "Key eixst"
         "Key does not exist")
"Key eixst"
CL-USER> (if (nth-value 1 (gethash 'no-entry *my-hash*))
         "Key eixts"
         "Key does not exits")
"Key does not exits"


3)删除哈希表

CL-USER> (defparameter *my-hash* (make-hash-table))
*MY-HASH*
CL-USER> (setf (gethash 'first-key *my-hash*) 'one)
ONE
CL-USER> (gethash 'first-key *my-hash*)
ONE
T
CL-USER> (remhash 'first-key *my-hash*)
T
CL-USER> (gethash 'first-key *my-hash*)
NIL
NIL


4)遍历哈希表

    a、可以用maphash函数遍历哈希表:

CL-USER> (defparameter *my-hash* (make-hash-table))
*MY-HASH*
CL-USER> (setf (gethash 'first-key *my-hash*) 'one)
ONE
CL-USER> (setf (gethash 'second-key *my-hash*) 'two)
TWO
CL-USER> (setf (gethash 'third-key *my-hash*) nil)
NIL
CL-USER> (setf (gethash nil *my-hash*) 'nil-value)
NIL-VALUE
CL-USER> (defun print-hash-entry (key value)
       (format t "The value associated with the key ~S is ~S~%" key value))
PRINT-HASH-ENTRY
CL-USER> (maphash #'print-hash-entry *my-hash*)
The value associated with the key FIRST-KEY is ONE
The value associated with the key SECOND-KEY is TWO
The value associated with the key THIRD-KEY is NIL
The value associated with the key NIL is NIL-VALUE
NIL
CL-USER> (defun print-hash-entry (key value)
       (format t "~S  ~S~%" key value))
CL-USER> (maphash #'print-hash-entry *my-hash*)
FIRST-KEY  ONE
SECOND-KEY  TWO
THIRD-KEY  NIL
NIL  NIL-VALUE
NIL

    

     b、也可以用with-hash-table-iterator函数进行遍历,相对复杂一点:

CL-USER> (defparameter *my-hash* (make-hash-table))
*MY-HASH*
CL-USER> (setf (gethash 'first-key *my-hash*) 'one)
ONE
CL-USER> (setf (gethash 'second-key *my-hash*) 'two)
TWO
CL-USER> (setf (gethash 'third-key *my-hash*) nil)
NIL
CL-USER> (setf (gethash 'nil *my-hash*) 'nothing)
NOTHING
CL-USER> (with-hash-table-iterator (my-iterator *my-hash*)
       (loop
        (multiple-value-bind (entry-p key value)
            (my-iterator)
          (if entry-p
              (print-hash-entry key value)
              (return)))))
FIRST-KEY  ONE
SECOND-KEY  TWO
THIRD-KEY  NIL
NIL  NOTHING
NIL


    c、同样用loop循环遍历

CL-USER> (loop for key being the hash-keys of *my-hash* do
          (print key))

FIRST-KEY 
SECOND-KEY 
THIRD-KEY 
NIL 
NIL


    d、loop循环遍历:

CL-USER> (loop for key being the hash-keys of *my-hash* 
          using (hash-value value) do
          (format t "The value associated with the key ~S is ~S~%" key value))
The value associated with the key FIRST-KEY is ONE
The value associated with the key SECOND-KEY is TWO
The value associated with the key THIRD-KEY is NIL
The value associated with the key NIL is NOTHING
NIL
CL-USER> (loop for value being the hash-values of *my-hash* do
          (print value))

ONE 
TWO 
NIL 
NOTHING 
NIL
CL-USER> (loop for value being the hash-values of *my-hash*
          using (hash-key key) do
          (format t "~&~A -> ~A" key value))
FIRST-KEY -> ONE
SECOND-KEY -> TWO
THIRD-KEY -> NIL
NIL -> NOTHING
NIL


5)计算哈希表数据数量

用hash-table-count函数:

CL-USER> (defparameter *my-hash2* (make-hash-table))
*MY-HASH2*
CL-USER> (hash-table-count *my-hash2*) ;;count the entries in a hash table
0
CL-USER> (setf (gethash 'first *my-hash2*) 'one)
ONE
CL-USER> (setf (gethash 'second *my-hash2*) 2)
2
CL-USER> (setf (gethash 'third *my-hash2*) 3)
3
CL-USER> (hash-table-count *my-hash2*)
3
CL-USER> (setf (gethash 'second *my-hash2*) 'two)
TWO
CL-USER> (hash-table-count *my-hash2*)
3
CL-USER> (clrhash *my-hash2*) ;;clean hash table
#<HASH-TABLE :TEST EQL :COUNT 0 {25045971}>
CL-USER> (hash-table-count *my-hash2*)
0





转载于:https://my.oschina.net/u/241930/blog/552042

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值