1 ! Copyright (C) 2010 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs hashtables hashtables.wrapped kernel
5 IN: hashtables.identity
9 TUPLE: identity-wrapper
10 { underlying read-only } identity-hashcode ;
12 : <identity-wrapper> ( wrapped-key -- identity-wrapper )
13 dup identity-hashcode identity-wrapper boa ; inline
15 M: identity-wrapper equal?
16 over identity-wrapper?
17 [ [ underlying>> ] bi@ eq? ]
18 [ 2drop f ] if ; inline
20 M: identity-wrapper hashcode* nip identity-hashcode>> ; inline
24 TUPLE: identity-hashtable < wrapped-hashtable ;
26 : <identity-hashtable> ( n -- ihashtable )
27 <hashtable> identity-hashtable boa ; inline
29 M: identity-hashtable wrap-key drop <identity-wrapper> ;
31 M: identity-hashtable clone
32 underlying>> clone identity-hashtable boa ; inline
34 : identity-associate ( value key -- ihashtable )
35 2 <identity-hashtable> [ set-at ] keep ; inline
37 : >identity-hashtable ( assoc -- ihashtable )
38 [ assoc-size <identity-hashtable> ] keep assoc-union! ;
40 M: identity-hashtable assoc-like
41 drop dup identity-hashtable? [ >identity-hashtable ] unless ; inline
43 M: identity-hashtable new-assoc drop <identity-hashtable> ;
45 SYNTAX: IH{ \ } [ >identity-hashtable ] parse-literal ;
47 { "hashtables.identity" "prettyprint" } "hashtables.identity.prettyprint" require-when
48 { "hashtables.identity" "mirrors" } "hashtables.identity.mirrors" require-when