Saturday, April 14, 2007

More on assocs

So, assocs are done! I had a few problems, but it ultimately worked out. I think they'll be included in .89, but I can't say for sure; maybe .90. The final protocol ended up being a little bit different than the last time I wrote about them. Currently, these are the words that should be implemented:

GENERIC: at* ( key assoc -- value/f ? )
GENERIC: set-at ( value key assoc -- )
GENERIC: new-assoc ( size exemplar -- newassoc )
G: assoc-find ( assoc quot -- key value ? )
1 standard-combination ; inline ! quot: key value -- ?
GENERIC: delete-at ( key assoc -- )
GENERIC: clear-assoc ( assoc -- )
GENERIC: assoc-size ( assoc -- n )
GENERIC: assoc-like ( assoc exemplar -- newassoc )
! Additionally, clone should be implemented properly

The major changes from the protocol of a few days ago are the addition of assoc-like (which will increase efficiency in some cases) and the removal of key? (which can easily be, and is, implemented as at* nip). Additionally, I did some renaming: assoc* => at*, set-assoc => set-at and remove-assoc => delete-at.

It was a bit annoying to deal with the Factor internals, but 90% of the problems ended up being my fault anyway. I changed all the code in the Factor distribution (except for Ed's, becuase I heard he would want to do it himself. But if you're reading this, Ed, I'd be fine with changing it if you want me to) and all the unit tests passed--except the ones that failed to begin with. But Factor programmers probably want to know what these changes mean, too. So the rest of this post will be a summary of those changes.

The first thing to know is vocabularies. I moved everything that didn't specifically have to do with hashtables into the assocs vocab. All of the remaining words have hash in the name except for associate (which makes a new hashtable) and ?set-hash (which might make a new hashtable). Except for ?set-at everything in that vocabulary is keeping the same name. Any conforming implementation of the assocs protocol can expect to have all of the other words in the assocs vocab work properly. And most of them correspond to old, pre-existing hashtable words; they just work on more datatypes now. Some names weren't changed at all, like cache, which is suitably generic already.

In the taxonomy of renaming, there are three groups: dropped prefixes, hash=>assoc changes, and hash=>at changes. These, together with an oddball change, are described below.







































Old wordNew word(s)
Dropped prefixes
hash-updateupdate
hash-intersectintersect
hash-unionunion
hash-keyskeys
hash-valuesvalues
hash => at
hashat
hash*at*
?hash?at
?hash*?at*
set-hashset-at
change-hashchange-at
hash+at+
remove-hashdelete-at
?remove-hash?delete-at
hash-all-with?assoc-all-with?
hash => assoc
hash-eachassoc-each
hash-mapassoc-map
hash-each-withassoc-each-with
hash-all?assoc-all?
hash-all-with?assoc-all-with?
hash-subsetassoc-subset
hash-subset-withassoc-subset-with
hash-empty?assoc-empty?
hash-stackassoc-stack
clear-hashclear-assoc
hashtable=assoc=
subhash?subassoc?
map>hashH{ } map>assoc
make-hashH{ } make-assoc
Oddballs
hash-member?key?
hash>alist>alist
alist>hash>hashtable


Of course, there's a difference: every single one of these words is more general and will work on all assocs, not just hashtables. If you have any questions or problems about this change, feel free to ask.

No comments: