The proposed implementation below addresses separation of key/value
spaces and tries to take advantage of REBOL built-ins for performance
wherever feasible. Critiques and comments welcome!
-jn-
===============(begin code listing)==================
REBOL [
Title: "Minimal Associative Data Store"
File: %assoc.r
Date: 14-Sep-2000
Author: "Joel Neely"
Purpose: {
Quick, minimal implementation of a data store that
associates arbitrary values with (string!) keys.
}
]
assoc: make object! [
_keys: copy []
_vals: copy []
clear: func [] [_keys: copy [] _vals: copy []]
clear?: func [] [empty? head _keys]
count?: func [] [length? _keys]
new: func [/local r] [r: make assoc [] r/clear r]
put: func [k [string!] v [any-type!] /local p] [
either none? p: find _keys k [
append _keys k
append _vals v
][
change at _vals index? p v
]
v
]
get: func [k [string!] /local p] [
either none? p: find _keys k [
none
][
pick _vals index? p
]
]
keys: func [] [copy _keys]
]
=================(end code listing)==================
This (minimal) implementation can be used as follows:
=================(begin transcript)==================
>> do %assoc.r
>> phonelist: assoc/new
>> phonelist/clear?
== true
>> phonelist/put "Bill" "555-1111"
== "555-1111"
>> phonelist/put "Al" "555-2222"
== "555-2222"
>> phonelist/put "George" "555-3333"
== "555-3333"
>> phonelist/put "Dick" "555-4444"
== "555-4444"
>> phonelist/get "Al"
== "555-2222"
>> phonelist/count?
== 4
>> phonelist/keys
== ["Bill" "Al" "George" "Dick"]
>> foreach who phonelist/keys [print [who "^-" phonelist/get who]]
Bill 555-1111
Al 555-2222
George 555-3333
Dick 555-4444
>> foreach who sort phonelist/keys [print [who "^-" phonelist/get who]]
Al 555-2222
Bill 555-1111
Dick 555-4444
George 555-3333
>> privatelist: phonelist/new
>> privatelist/count?
== 0
>>
>> foreach who head reverse sort phonelist/keys [
[ print [who "^-" phonelist/get who]
[ ]
George 555-3333
Dick 555-4444
Bill 555-1111
Al 555-2222
>>==================(end transcript)===================
Enjoy!
-jn-
--
; Joel Neely [EMAIL PROTECTED] 901-263-4460 38017/HKA/9677
REBOL [] print to-string debase/64 decompress #{
789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC
B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}