[REBOL] Hash: Re: Rebol API to DyBASE
From: fsievert::uos::de at: 18-Dec-2003 13:50
Maybe you will want to use this. You can add /case to the find to
make it faster for strings.
; -- script: %hash.r
REBOL [
title: "hash"
author: "Frank Sievertsen"
; version: 1.0.0
purpose: {
Hash-Test
---------
hash! - Datatype
key-type: string
put: 0:00:07
get: 0:00:30
remove: 0:00:25
key-type: integer
put: 0:00:21
get: 0:00:26
remove: 0:00:37
hash - Object
key-type: string
put: 0:00:01
get: 0:00:14
remove: 0:00:01
key-type: integer
put: 0:00
get: 0:00:13
remove: 0:00:01
}
]
hash: make object! [
size: 1000
block: none
init: func [/with size [integer!]] [
if with [self/size: size]
block: array reduce [self/size 0]
idcount: 0
()
]
checksum: func [key] [
if not any-string? key [
key: mold key
]
system/words/checksum key
]
insert: func [key val /local blk tmp] [
blk: pick block (checksum key) // (size - 1) + 1
either tmp: find/skip blk key 2 [
change/only next tmp :val
] [
repend blk [:key :val]
]
:val
]
remove: func [key /local blk tmp] [
blk: pick block (checksum key) // (size - 1) + 1
if tmp: find/skip blk key 2 [
system/words/remove/part tmp 2
]
self
]
select: func [key /local blk tmp] [
blk: pick block (checksum key) // (size - 1) + 1
if tmp: find/skip blk key 2 [
return second tmp
]
]
idcount: 0
id: func [key] [
any [
select key
insert key idcount: idcount + 1
]
]
add: func [key val] [
insert key (any [(select key) 0]) + val
]
to-block: func [/local out] [
out: copy []
foreach bl block [append out bl]
out
]
init
]
; Script: hash-test
REBOL []
do %hash.r
hash-test: func [
put [function!]
get [function!]
remove [function!]
/local t many
] [
foreach conv reduce [string! integer!] [
print ["key-type:" conv]
print [conv type? conv]
many: 10000
t: now/time
repeat z many [
put to conv z z
]
print [" put:" now/time - t]
t: now/time
loop 20 [
repeat z many [
get to conv z
]
]
print [" get:" now/time - t]
t: now/time
repeat z many [
remove to conv z
]
print [" remove:" now/time - t]
]
]
print "Hash-Test"
print "---------"
print "^/hash! - Datatype^/"
h: make hash! 10000
hash-test
func [key val] [
repend h [key val]
]
func [key] [
select/skip h key 2
]
func [key] [
remove/part find/skip h key 2 2
]
print "^/hash - Object^/"
h: make hash []
hash-test
func [key val] [
h/insert key val
]
func [key] [
h/select key
]
func [key] [
h/remove key
]