From e8c98e56ffd44e68812dfe0bee77f02afd91b4c5 Mon Sep 17 00:00:00 2001 From: erana Date: Mon, 6 Mar 2017 06:29:49 +0100 Subject: [PATCH] db with prim hash --- s48/hawk-dns-server/db.scm | 2 +- s48/hawk-dns-server/db.scm~ | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/s48/hawk-dns-server/db.scm b/s48/hawk-dns-server/db.scm index 1a5b493..adda6a0 100644 --- a/s48/hawk-dns-server/db.scm +++ b/s48/hawk-dns-server/db.scm @@ -48,7 +48,7 @@ (set! *prime (getprime-rec *prime))) (define (getprime-rec prime) - (if (or (/ prime 2) (/ prime 3) (/ prime 5) (/ prime 7) (/ prime 10)) + (if (not (and (/ prime 2) (/ prime 3) (/ prime 5) (/ prime 7) (/ prime 10))) (getpreime-rec (+ prime 1)))) (define (integer->ascii diff --git a/s48/hawk-dns-server/db.scm~ b/s48/hawk-dns-server/db.scm~ index df7393d..adda6a0 100644 --- a/s48/hawk-dns-server/db.scm~ +++ b/s48/hawk-dns-server/db.scm~ @@ -15,11 +15,17 @@ (define (make-db) - (let ((*db '())) + (let ((*db '()) + ((*prime 1))) (define (add key value) - (set! *db (append *db (list (list key value)))) - ) + (let ((*hash (list (list key value)))) + (nextprime) + (do ((i 0 (+ i 1))) + ((eq? i *prime) + (set! *db (append *db *hash))) + (set! *db (append *db '(0 0)))) + )) (define (lookup key) (let ((*result #f)) @@ -30,6 +36,23 @@ (set! *result (cadr (list-ref *db i)))) ))) + (define (key->hash key) + (let ((*hash 0)) + (do ((i 0 (+ i 1))) + ((= i (string-length key)) + *hash) + (set! *hash (+ *hash (char->ascii (string-ref key i))))) + )) + + (define (nextprime) + (set! *prime (getprime-rec *prime))) + + (define (getprime-rec prime) + (if (not (and (/ prime 2) (/ prime 3) (/ prime 5) (/ prime 7) (/ prime 10))) + (getpreime-rec (+ prime 1)))) + + (define (integer->ascii + (define (dispatch msg) (cond ((eq? msg 'add) add) ((eq? msg 'lookup) lookup)