]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: change to use tuple-hashcode algorithm, make string-hashcode work like...
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 6 Oct 2011 02:22:50 +0000 (19:22 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 6 Oct 2011 02:22:50 +0000 (19:22 -0700)
core/sequences/sequences-docs.factor
core/sequences/sequences.factor
core/strings/strings.factor

index f256c965d52fc0eb714bfa568d940a1d29f66d31..c06cdb191c0589534f32c659f13bb6f40660d0eb 100644 (file)
@@ -1309,13 +1309,7 @@ HELP: sequence-hashcode
 { $values
      { "n" integer } { "seq" sequence }
      { "x" integer } }
-{ $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them using " { $link sequence-hashcode-step } "." } ;
-
-HELP: sequence-hashcode-step
-{ $values
-     { "oldhash" integer } { "newpart" integer }
-     { "newhash" integer } }
-{ $description "An implementation word that computes a running hashcode of a sequence using some bit-twiddling. The resulting hashcode is always a fixnum." } ;
+{ $description "Iterates over a sequence, computes a hashcode with " { $link hashcode* } " for each element, and combines them." } ;
 
 HELP: short
 { $values
index 153ef91809238af1f4da8683a759d3644518bf47..942df32852c24c7a682043ada94d633d1262dd35 100644 (file)
@@ -600,18 +600,14 @@ ERROR: assert-sequence got expected ;
 : assert-sequence= ( a b -- )
     2dup sequence= [ 2drop ] [ assert-sequence ] if ;
 
-<PRIVATE
-
-: sequence-hashcode-step ( oldhash newpart -- newhash )
-    >fixnum swap [
-        [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
-        fixnum+fast fixnum+fast
-    ] keep fixnum-bitxor ; inline
-
-PRIVATE>
-
-: sequence-hashcode ( n seq -- x )
-    [ 0 ] 2dip [ hashcode* sequence-hashcode-step ] with each ; inline
+: sequence-hashcode ( depth seq -- hash )
+    [
+        [ drop 1000003 HEX: 345678 ] dip length
+        [ dup fixnum+fast 82520 fixnum+fast ] [ iota ] bi
+    ] 2keep [
+        swapd nth-unsafe hashcode* rot fixnum-bitxor
+        pick fixnum*fast [ [ fixnum+fast ] keep ] dip swap
+    ] 2curry each drop nip 97531 fixnum+fast ; inline
 
 M: reversed equal? over reversed? [ sequence= ] [ 2drop f ] if ;
 
index e57edf282aea2a6a585f5074e1ed2e03c8683961..e2f9f279e8a88cf309931c860a376c6a18b9f596 100644 (file)
@@ -13,8 +13,14 @@ IN: strings
 : reset-string-hashcode ( str -- )
     f swap set-string-hashcode ; inline
 
+: string-hashcode-step ( oldhash newpart -- newhash )
+    >fixnum swap [
+        [ -2 fixnum-shift-fast ] [ 5 fixnum-shift-fast ] bi
+        fixnum+fast fixnum+fast
+    ] keep fixnum-bitxor ; inline
+
 : rehash-string ( str -- )
-    1 over sequence-hashcode swap set-string-hashcode ; inline
+    [ 0 [ string-hashcode-step ] reduce ] keep set-string-hashcode ; inline
 
 : (aux) ( n string -- byte-array m )
     aux>> { byte-array } declare swap 1 fixnum-shift-fast ; inline