Hash-table type in Lisp
Asked Answered
C

1

5

I found out, that in CL hash table has a type HASH-TABLE (surprisingly). However, a vector can be just VECTOR, but it also can be specified further as (vector number 12), for example.

It seems only natural for hash-table to have a list type, like, (hash-table number cons) or something, but it doesn't seem to work, and I cannot find any reference to it. Any advice?

Context answered 5/10, 2018 at 22:55 Comment(1)
Right, this does not exist in standard Common Lisp. Could be useful.Keitloa
Z
7

TL;DR typed vectors may be optimized for memory usage, but typed hash-tables are mostly pointless.


Disclaimer: this is mostly based on intuition and it's not even close to an authoritative answer.

Typed vectors are useful because they're the most practical way to store data contiguously in memory -- if you know the type (and thanks to that, also the size) of all elements, it's trivial to allocate just enough memory to store all of them together. As you may already know, CL's bit vectors are just that: an abstraction for optimally stored, individually accessible bits. Without type information, you must store a vector of pointers to scattered pieces of actual data.

If you're familiar with how a simple hash-table is implemented, then you know that type information is less useful here. It is kinda awkward to store the actual data in the table (which is usually a vector of pointers instead), either because dealing with hash-key collisions becomes harder (or else you would end up with a linked list anyway), or because resizing the table would involve copying all the data to a new table, instead of just changing a few pointers. Of course resizing a vector also requires copying everything, but it's done in a single step, while for the hash-table it must be done once for every element, because their positions in the table will have changed. There's mostly no benefit.

Also, a typed hash-table doesn't sound very Lispy.

Zoubek answered 6/10, 2018 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.