Mark
Mark

Reputation: 8678

common lisp array and hashtable oddity?

I am trying to store attack moves for chess pieces in a 64 square bitboard. Basically I am using any array of hash-tables to do this:

(defvar attacks (make-array '(64) :initial-element (make-hash-table))

However, I have noticed that when I fill up each hashtable in the array (with about 1000 elements each) it intersects with another hashtable. That is one hashtable has values from another hashtable, even though I didn't put it there.

Am I imagining things? Is this a bug?

Upvotes: 1

Views: 282

Answers (2)

Patrick
Patrick

Reputation: 525

You should use :INITFORM instead of :initial-element to get different hashtables

(My answer is wrong, see Vatine's)

Upvotes: 0

Vatine
Vatine

Reputation: 21238

You create a single hash table (with make-hash-table) that you then set in all elements of an array. To do what you want, you'd want to do one of:

  • Create an empty array, loop through and set each index to a fresh hash table
  • Create a list of 64 fresh hash tables and use :INITIAL-CONTENTS to populate the array on creation

That way, the hash tables would be separate instead of the same hash table stored 64 times.

Upvotes: 4

Related Questions