[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

emptying an hashtable



    Date: 31 May 90  8:51 +0100
    From: baechler%eldi.epfl.ch@Warbucks.AI.SRI.COM

      I use a lot of hashtables having the form described below:

    (defvar *a-table*
	    (make-hash-table :test #'equal
			     :rehash-threshold 0.8 
			     :rehash-size 2.0 
			     :size 100))

      When I have to empty them, I use the following function:

    (defun reset-hashtable (table)
      (maphash #'(lambda (key value)
		   (declare (ignore value))
		   (print key)
		   (print (remhash key table))
		   (print (gethash key table)))
	       table))

Why not just use CLRHASH?  This is equivalent, and will work:

  (defun reset-hashtable (table)
    (clrhash table))

although there isn't any particular reason to have a separate function.

    (defvar *another-table*
	    (make-hash-table :test #'eq               ; instead of EQUAL
			     :rehash-threshold 0.8 
			     :rehash-size 2.0 
			     :size 100))

    From the CLtl, EQ is also usable. But, When I tried to empty *another-table*, 
    only one element was removed. I had to recreate it with the test being EQUAL 
    so that my procedure worked.

This works for tables which contain more than a minimum number of elements; for
some reason, when the table is small enough (under ten associations), the
REMHASH implementation interferes with the operation of MAPHASH (the internal
representation is at property list rather than a hash table, but that shouldn't
matter).  This doesn't happen for EQUAL hash tables above size 3.

Anyway, CLRHASH works, and is faster than anything you can write by hand.  Of
course, if you wanted to remove only certain elements from your hash table,
you'd be out of luck.  I will send a bug report to Symbolics.