1 ! Copyright (C) 2011 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors arrays assocs hashtables kernel sequences
8 TUPLE: wrapped-hashtable
9 { underlying hashtable read-only } ;
11 GENERIC: wrap-key ( key wrapped-hash -- wrapped-key )
15 : wrapper@ ( key wrapped-hash -- wrapped-key hash )
16 [ wrap-key ] [ nip underlying>> ] 2bi ; inline
20 M: wrapped-hashtable at*
23 M: wrapped-hashtable clear-assoc
24 underlying>> clear-assoc ; inline
26 M: wrapped-hashtable delete-at
27 wrapper@ delete-at ; inline
29 M: wrapped-hashtable assoc-size
30 underlying>> assoc-size ; inline
32 M: wrapped-hashtable set-at
33 wrapper@ set-at ; inline
35 M: wrapped-hashtable >alist
37 [ 0 swap [ underlying>> ] change-nth ] each
40 M: wrapped-hashtable keys
41 underlying>> keys [ underlying>> ] map! ;
43 M: wrapped-hashtable values
46 M: wrapped-hashtable equal?
47 over wrapped-hashtable? [ [ underlying>> ] same? ] [ 2drop f ] if ;
49 INSTANCE: wrapped-hashtable assoc
51 { "hashtables.wrapped" "prettyprint" } "hashtables.wrapped.prettyprint" require-when