Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Associative data store Re:(3)

From: joel:neely:fedex at: 14-Sep-2000 19:32

[rebol--keithdevens--com] wrote:
> ... Just wondering how performance will be. :) >
That was the motivation for using paired blocks rather than interleaving the keys and values in a single block. Notice that all of the heavy lifting is still done by find , remove , and change , which are all primitives (and presumably heavily optimized... right RT? ;-) The other built-in, append , is a VERY thin mezzanine wrapper onto insert , which IS primitive. so I think performance will be tolerable. The main overhead is function entry/exit (I suspect) which is unavoidable if we are to add any functionality at all. The main issue is to avoid loops in high-level code. If I had used interleaved keys and values, get would have to be written something like the following: ;!!! WARNING: UNTESTED CODE FRAGMENT !!! assoc: make object! [ _kvpairs: copy [] ; ; etc... ; get: func [t [string!] / local k v] [ foreach [k v] _kvpairs [ if k = t [return v] ] none ] ; ; etc... ; ] which puts real work (proportional to the size of the store) up into interpreted code. That's why I avoided it.
> I still want something like this built into the language. Also, that way we > could use built ins like 'length? on it. > > Hmm... this brings up another question. There's no way to do any kind of > operator overloading in Rebol, so that we *could* use 'length? on this data > structure, is there? >
Your wish is my command! (At least, this once! ;-) ================================================================ 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 []] empty?: func [] [system/words/empty? head _keys] length?: func [] [system/words/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 ] ] delete: func [k [string!] /local p v] [ either none? p: find _keys k [ none ][ k: pick _vals p: index? p remove at _keys p remove at _vals p k ] ] keys: func [] [copy _keys] ] ================================================================ -jn- -- ; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677 REBOL [] print to-string debase/64 decompress #{ 789C0BCE0BAB4A7176CA48CAB53448740FABF474F3720BCC B6F4F574CFC888342AC949CE74B50500E1710C0C24000000}