changeset 119: | 85f27597cf60 |
parent: | b828a3caa758 |
child: | e48427b30ecd |
author: | ellis <ellis@rwest.io> |
date: | Fri, 22 Dec 2023 18:43:53 -0500 |
permissions: | -rw-r--r-- |
description: | castable added, still testing |
118
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
1 | ;;; lib/obj/hash/hasher.lisp --- Hash Functions |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
2 | |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
3 | ;; |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
4 | |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
5 | ;;; Code: |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
6 | (in-package :obj/hash) |
119 | 7 | (eval-always |
8 | (defvar *global-hasher* #'sxhash)) |
|
9 | ||
10 | (defconstant +global-hash+ |
|
11 | (if (boundp '+global-hash+) |
|
12 | +global-hash+ |
|
13 | (funcall *global-hasher* (get-universal-time)))) |
|
118
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
14 | |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
15 | (macrolet ((specialize (str body) ; TODO 2023-12-21: test if this actually compiles to fastpath |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
16 | `(if (typep ,str '(simple-array character 1)) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
17 | ,body |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
18 | ,body))) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
19 | (defun djb (string) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
20 | (declare (string string) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
21 | (optimize speed)) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
22 | (let ((hash 5381)) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
23 | (declare ((and unsigned-byte fixnum) hash)) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
24 | (specialize |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
25 | string |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
26 | (dotimes (n (min 6 (length string))) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
27 | (setf hash |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
28 | (logand most-positive-fixnum |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
29 | (logxor (* hash 33) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
30 | (char-code (schar string n))))))) |
b828a3caa758
object library stuff, removed DOT - api doesnt jive.
ellis <ellis@rwest.io>
parents:
diff
changeset
|
31 | hash))) |
119 | 32 | |
33 | (defun hash-object-address (obj &optional (test *global-hasher*)) |
|
34 | "Given some object OBJ, lookup the address with |
|
35 | SB-KERNEL:GET-LISP-OBJ-ADDRESS and return a hash." |
|
36 | (funcall test (sb-kernel:get-lisp-obj-address obj))) |
|
37 | ||
38 | (defun object-address-hash-equalp (a b) |
|
39 | (= (hash-object-address a) (hash-object-address b))) |
|
40 | ||
41 | (sb-ext:define-hash-table-test object-address-hash-equalp hash-object-address) |
|
42 | ||
43 | (defgeneric hash-object (obj)) |