-USING: help.markup help.syntax math sequences sets ;
+USING: assocs help.markup help.syntax kernel math sequences sets ;
IN: hash-sets
ARTICLE: "hash-sets" "Hash sets"
HELP: >hash-set
{ $values { "members" sequence } { "hash-set" hash-set } }
{ $description "Creates a new hash set with the given members." } ;
+
+HELP: intern
+{ $values { "obj" object } { "hash-set" hash-set } { "obj'" "a previously retained or the original " { $link object } } }
+{ $description "If a value equal to " { $snippet "obj" } " is present in the " { $snippet "hash-set" } ", return the copy from the set, otherwise add " { $snippet "obj" } " to the " { $snippet "hash-set" } " and return the original. When used with strings, this word can be used to implement string interning, see https://en.wikipedia.org/wiki/String_interning." }
+{ $side-effects "hash-set" }
+{ $see-also cache } ;
! non-integer capacity not allowed
[ 0.75 <hash-set> ] must-fail
+
+{ t } [ "test" dup HS{ } clone intern eq? ] unit-test
+{ t } [ "aoeu" dup clone HS{ } clone intern = ] unit-test
+{ t } [ "snth" dup clone HS{ } clone intern eq? not ] unit-test
M: hash-set set-like
drop dup hash-set? [ ?members >hash-set ] unless ; inline
+: intern ( obj hash-set -- obj' )
+ 2dup key@ [ swap nth 2nip ] [ 2drop [ adjoin ] keepd ] if ;
+
INSTANCE: hash-set set
! Overrides for performance