"#hashing.spl" import "#linkedlist.spl" import construct HashMap { size buckets function maxload optload ; construct { this | with this ; 0 this:=size; 16 anew this:=buckets; default-hashing-function this:=function; 3 this:=maxload; 2 this:=optload; this } balance { | with this ; this:size this:buckets:len / this:maxload gt if { def new-buckets this:size this:optload / anew =new-buckets new-buckets:len:foreach<{ | with i ; LinkedList:new i new-buckets:set; } { | with bucket ; { | :to-stack with key value ; [ key value ] (key this:function:call new-buckets:len %) new-buckets:get:push; } bucket:foreach; } this:buckets:foreach; } } get { value | with key this ; null key this:function:call this:buckets:len % this:buckets:get :foreach <{ | :to-stack with k v ; k key eq if { pop v } }> } set { oldvalue | with value key this ; null def bk key this:function:call this:buckets:len % this:buckets:get dup =bk :foreach <{ | with entry ; entry:0 key eq if { pop entry:1 value entry:=1; } }> dup null eq if { [ key value ] bk:push } } }