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

[REBOL] Revised associative data storage code

From: joel::neely::fedex::com at: 22-Sep-2000 17:20

This is a multi-part message in MIME format. --------------7B2D0141EEB652ADAD6A733C Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii Thanks to Gabriele and others, who re-lit my dim bulb to the fact that find block1 block2 select block1 block2 are looking for the first SUBSEQUENCE of the first argument equal to the second argument, whereas find/only block1 block2 select/only block1 block2 are looking for the first ELEMENT of the first argument equal to the second argument. That loud "smack" you may have heard a few nights ago was my palm smartly hitting my not-so-smart forehead as I uttered those immortal words, "Silly me!" Having gotten that far(with a little help from my friends), I did a bit of testing and found that find/only and select/only seem quite willing to work properly with non-block second arguments as well! With that discovery firmly in hand, I put together an updated object-oriented associative data store, attached below. Enjoy all, and (as always) comments/feedback are welcome! -jn- --------------7B2D0141EEB652ADAD6A733C Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii; name="assoco.r" Content-Disposition: inline; filename="assoco.r" REBOL [ Title: "Minimal Associative Data Store" File: %assoco.r Date: 20-Sep-2000 Author: "Joel Neely" Purpose: { Revised implementation of associative data store, using arbitrary data types for keys and values. Updated to use /only for key processing. } ] assoco: make object! [ _keys: copy [] _vals: copy [] clear: func [] [_keys: copy [] _vals: copy []] empty?: func [] [system/words/empty? _keys] length?: func [] [system/words/length? _keys] new: func [/local r] [r: make self [] r/clear r] put: func [k [any-type!] v [any-type!] /local p] [ either found? p: find/only _keys k [ change/only at _vals index? p v ][ append/only _keys k append/only _vals v ] v ] get: func [k [any-type!] /local p] [ either none? p: find/only _keys k [ none ][ pick _vals index? p ] ] delete: func [k [any-type!] /local p v] [ either none? p: find/only _keys k [ none ][ k: pick _vals p: index? p remove at _keys p remove at _vals p k ] ] keys: func [] [copy _keys] ] --------------7B2D0141EEB652ADAD6A733C--