changelog shortlog graph tags branches changeset files file revisions raw help

Mercurial > core / annotate lisp/lib/obj/hash/hasher.lisp

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
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
7
 (eval-always
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
8
   (defvar *global-hasher* #'sxhash))
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
9
 
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
10
 (defconstant +global-hash+ 
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
11
   (if (boundp '+global-hash+)
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
12
       +global-hash+
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
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
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
32
 
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
33
 (defun hash-object-address (obj &optional (test *global-hasher*))
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
34
   "Given some object OBJ, lookup the address with
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
35
   SB-KERNEL:GET-LISP-OBJ-ADDRESS and return a hash."
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
36
   (funcall test (sb-kernel:get-lisp-obj-address obj)))
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
37
 
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
38
 (defun object-address-hash-equalp (a b)
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
39
   (= (hash-object-address a) (hash-object-address b)))
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
40
 
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
41
 (sb-ext:define-hash-table-test object-address-hash-equalp hash-object-address)
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
42
 
85f27597cf60 castable added, still testing
ellis <ellis@rwest.io>
parents: 118
diff changeset
43
 (defgeneric hash-object (obj))