! (c)2010 Joe Groff bsd license\r
-USING: accessors arrays assocs fry hashtables kernel parser\r
-sequences vocabs.loader ;\r
+USING: accessors arrays assocs hashtables hashtables.wrapped\r
+kernel parser sequences vocabs.loader ;\r
IN: hashtables.identity\r
\r
-TUPLE: identity-wrapper\r
- { underlying read-only } ;\r
+TUPLE: identity-wrapper < wrapped-key ;\r
+\r
C: <identity-wrapper> identity-wrapper\r
\r
M: identity-wrapper equal?\r
M: identity-wrapper hashcode*\r
nip underlying>> identity-hashcode ; inline\r
\r
-TUPLE: identity-hashtable\r
- { underlying hashtable read-only } ;\r
+TUPLE: identity-hashtable < wrapped-hashtable ;\r
\r
: <identity-hashtable> ( n -- ihash )\r
<hashtable> identity-hashtable boa ; inline\r
\r
-<PRIVATE\r
-: identity@ ( key ihash -- ikey hash )\r
- [ <identity-wrapper> ] [ underlying>> ] bi* ; inline\r
-PRIVATE>\r
-\r
-M: identity-hashtable at*\r
- identity@ at* ; inline\r
-\r
-M: identity-hashtable clear-assoc\r
- underlying>> clear-assoc ; inline\r
-\r
-M: identity-hashtable delete-at\r
- identity@ delete-at ; inline\r
-\r
-M: identity-hashtable assoc-size\r
- underlying>> assoc-size ; inline\r
-\r
-M: identity-hashtable set-at\r
- identity@ set-at ; inline\r
+M: identity-hashtable wrap-key drop <identity-wrapper> ;\r
\r
-: identity-associate ( value key -- hash )\r
- 2 <identity-hashtable> [ set-at ] keep ; inline\r
-\r
-M: identity-hashtable >alist\r
- underlying>> >alist [ [ first underlying>> ] [ second ] bi 2array ] map ;\r
- \r
M: identity-hashtable clone\r
underlying>> clone identity-hashtable boa ; inline\r
\r
-M: identity-hashtable equal?\r
- over identity-hashtable? [ [ underlying>> ] bi@ = ] [ 2drop f ] if ;\r
+: identity-associate ( value key -- hash )\r
+ 2 <identity-hashtable> [ set-at ] keep ; inline\r
\r
: >identity-hashtable ( assoc -- ihashtable )\r
- dup assoc-size <identity-hashtable> [ '[ swap _ set-at ] assoc-each ] keep ;\r
+ [ assoc-size <identity-hashtable> ] keep assoc-union! ;\r
\r
SYNTAX: IH{ \ } [ >identity-hashtable ] parse-literal ;\r
\r
-! (c)2010 Joe Groff bsd license\r
-USING: assocs continuations hashtables.identity kernel\r
-namespaces prettyprint.backend prettyprint.config\r
-prettyprint.custom ;\r
+! Copyright (C) 2010-2011 Joe Groff\r
+! See http://factorcode.org/license.txt for BSD license\r
+\r
+USING: hashtables.identity kernel prettyprint.custom ;\r
+\r
IN: hashtables.identity.prettyprint\r
\r
-M: identity-hashtable >pprint-sequence >alist ;\r
M: identity-hashtable pprint-delims drop \ IH{ \ } ;\r
-\r
-M: identity-hashtable pprint*\r
- nesting-limit inc\r
- [ pprint-object ] [ nesting-limit dec ] [ ] cleanup ;\r
--- /dev/null
+John Benediktsson
--- /dev/null
+! Copyright (C) 2011 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: hashtables.sequences kernel prettyprint.custom ;
+
+IN: hashtables.sequences.prettyprint
+
+M: sequence-hashtable pprint-delims drop \ SH{ \ } ;
--- /dev/null
+! Copyright (C) 2011 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: assocs hashtables.sequences kernel literals sequences
+tools.test ;
+
+IN: hashtables.identity.tests
+
+[ 1000 ] [ 0 4 "asdf" <slice> SH{ { "asdf" 1000 } } at ] unit-test
+
+[ 1001 ] [
+ 1001 0 4 "asdf" <slice> SH{ { "asdf" 1000 } }
+ [ set-at ] [ at ] 2bi
+] unit-test
+
+[ 1001 ] [
+ SH{ } clone 1001 0 4 "asdf" <slice> pick set-at
+ "asdf" swap at
+] unit-test
+
+[ { { "asdf" 1000 } } ] [ SH{ { "asdf" 1000 } } >alist ] unit-test
+
--- /dev/null
+! Copyright (C) 2011 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: accessors assocs combinators hashtables
+hashtables.wrapped kernel parser sequences vocabs.loader ;
+
+IN: hashtables.sequences
+
+TUPLE: sequence-wrapper < wrapped-key ;
+
+C: <sequence-wrapper> sequence-wrapper
+
+M: sequence-wrapper equal?
+ over sequence-wrapper?
+ [ [ underlying>> ] bi@ sequence= ]
+ [ 2drop f ] if ; inline
+
+M: sequence-wrapper hashcode*
+ underlying>> [ sequence-hashcode ] recursive-hashcode ; inline
+
+TUPLE: sequence-hashtable < wrapped-hashtable ;
+
+: <sequence-hashtable> ( n -- ihash )
+ <hashtable> sequence-hashtable boa ; inline
+
+M: sequence-hashtable wrap-key drop <sequence-wrapper> ;
+
+M: sequence-hashtable clone
+ underlying>> clone sequence-hashtable boa ; inline
+
+: >sequence-hashtable ( assoc -- shashtable )
+ [ assoc-size <sequence-hashtable> ] keep assoc-union! ;
+
+SYNTAX: SH{ \ } [ >sequence-hashtable ] parse-literal ;
+
+{ "hashtables.sequences" "prettyprint" } "hashtables.sequences.prettyprint" require-when
--- /dev/null
+John Benediktsson
--- /dev/null
+! Copyright (C) 2011 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: accessors arrays assocs fry hashtables kernel parser
+sequences vocabs.loader ;
+
+IN: hashtables.wrapped
+
+TUPLE: wrapped-key
+ { underlying read-only } ;
+
+TUPLE: wrapped-hashtable
+ { underlying hashtable read-only } ;
+
+GENERIC: wrap-key ( key wrapped-hash -- wrapped-key )
+
+<PRIVATE
+
+: wrapper@ ( key wrapped-hash -- wrapped-key hash )
+ [ wrap-key ] [ nip underlying>> ] 2bi ; inline
+
+PRIVATE>
+
+M: wrapped-hashtable at*
+ wrapper@ at* ; inline
+
+M: wrapped-hashtable clear-assoc
+ underlying>> clear-assoc ; inline
+
+M: wrapped-hashtable delete-at
+ wrapper@ delete-at ; inline
+
+M: wrapped-hashtable assoc-size
+ underlying>> assoc-size ; inline
+
+M: wrapped-hashtable set-at
+ wrapper@ set-at ; inline
+
+M: wrapped-hashtable >alist
+ underlying>> >alist [ [ first underlying>> ] [ second ] bi 2array ] map ;
+
+M: wrapped-hashtable equal?
+ over wrapped-hashtable? [ [ underlying>> ] bi@ = ] [ 2drop f ] if ;
+
+{ "hashtables.wrapped" "prettyprint" } "hashtables.wrapped.prettyprint" require-when