str/vector-hashcode

cvs
Slava Pestov 2004-09-27 01:34:25 +00:00
parent babd2b7494
commit 10d070d3ec
19 changed files with 97 additions and 34 deletions

View File

@ -44,8 +44,6 @@ FFI:
- add a socket timeout - add a socket timeout
- fix error postoning -- not all errors thrown by i/o code are - fix error postoning -- not all errors thrown by i/o code are
postponed postponed
- sbuf-hashcode
- vector-hashcode
- some way to run httpd from command line - some way to run httpd from command line
+ bignums: + bignums:

View File

@ -204,6 +204,20 @@ public class FactorArray implements FactorExternalizable, PublicCloneable
} }
} //}}} } //}}}
//{{{ hashCode() method
public int hashCode()
{
int hashCode = 0;
for(int i = 0; i < Math.min(top,4); i++)
{
Object obj = stack[i];
if(obj != null)
hashCode ^= obj.hashCode();
}
return hashCode;
} //}}}
//{{{ equals() method //{{{ equals() method
public boolean equals(Object obj) public boolean equals(Object obj)
{ {

View File

@ -80,6 +80,7 @@ IN: strings
DEFER: str= DEFER: str=
DEFER: str-hashcode DEFER: str-hashcode
DEFER: sbuf= DEFER: sbuf=
DEFER: sbuf-hashcode
DEFER: sbuf-clone DEFER: sbuf-clone
IN: files IN: files
@ -221,6 +222,7 @@ IN: image
sbuf-reverse sbuf-reverse
sbuf-clone sbuf-clone
sbuf= sbuf=
sbuf-hashcode
arithmetic-type arithmetic-type
number? number?
>fixnum >fixnum

View File

@ -67,9 +67,9 @@ USE: vectors
[ drop 0 ] [ drop 0 ]
[ drop 0 ] [ drop 0 ]
[ drop 0 ] [ drop 0 ]
[ drop 0 ] [ vector-hashcode ]
[ str-hashcode ] [ str-hashcode ]
[ drop 0 ] [ sbuf-hashcode ]
[ drop 0 ] [ drop 0 ]
[ >fixnum ] [ >fixnum ]
[ >fixnum ] [ >fixnum ]

View File

@ -75,6 +75,7 @@ USE: words
[ sbuf-reverse | " sbuf -- " ] [ sbuf-reverse | " sbuf -- " ]
[ sbuf-clone | " sbuf -- sbuf " ] [ sbuf-clone | " sbuf -- sbuf " ]
[ sbuf= | " sbuf sbuf -- ? " ] [ sbuf= | " sbuf sbuf -- ? " ]
[ sbuf-hashcode | " sbuf -- n " ]
[ arithmetic-type | " n n -- type " ] [ arithmetic-type | " n n -- type " ]
[ number? | " obj -- ? " ] [ number? | " obj -- ? " ]
[ >fixnum | " n -- fixnum " ] [ >fixnum | " n -- fixnum " ]

View File

@ -66,3 +66,11 @@ USE: stack
] [ ] [
2drop f 2drop f
] ifte ; ] ifte ;
: ?vector-nth ( n vec -- obj/f )
2dup vector-length >= [ 2drop f ] [ vector-nth ] ifte ;
: vector-hashcode ( vec -- n )
0 swap 4 [
over ?vector-nth hashcode rot bitxor swap
] times* drop ;

View File

@ -19,7 +19,7 @@ USE: stack
] with-scope ] with-scope
] unit-test ] unit-test
[ "<img src='/resource/library/icons/File.png'>" ] [ "<img src='/responder/resource/library/icons/File.png'>" ]
[ [
[ [
"" ""

View File

@ -51,6 +51,6 @@ USE: lists
[ 12 ] [ 11 4 align ] unit-test [ 12 ] [ 11 4 align ] unit-test
[ 12 ] [ 12 4 align ] unit-test [ 12 ] [ 12 4 align ] unit-test
[ 12 ] [ 10 2 align ] unit-test [ 10 ] [ 10 2 align ] unit-test
[ 14 ] [ 13 2 align ] unit-test [ 14 ] [ 13 2 align ] unit-test
[ 11 ] [ 11 1 align ] unit-test [ 11 ] [ 11 1 align ] unit-test

29
library/test/sbuf.factor Normal file
View File

@ -0,0 +1,29 @@
IN: scratchpad
USE: combinators
USE: errors
USE: kernel
USE: logic
USE: math
USE: namespaces
USE: stack
USE: strings
USE: test
native? [
[ t ] [ "Foo" str>sbuf "Foo" str>sbuf = ] unit-test
[ f ] [ "Foo" str>sbuf "Foob" str>sbuf = ] unit-test
[ f ] [ 34 "Foo" str>sbuf = ] unit-test
[ "Hello" ] [
100 <sbuf> "buf" set
"Hello" "buf" get sbuf-append
"buf" get clone "buf-clone" set
"World" "buf-clone" get sbuf-append
"buf" get sbuf>str
] unit-test
[ t ] [
"Hello world" str>sbuf hashcode
"Hello world" hashcode =
] unit-test
] when

View File

@ -88,20 +88,6 @@ unit-test
[ "fedcba" ] [ "abcdef" str-reverse ] unit-test [ "fedcba" ] [ "abcdef" str-reverse ] unit-test
[ "edcba" ] [ "abcde" str-reverse ] unit-test [ "edcba" ] [ "abcde" str-reverse ] unit-test
native? [
[ t ] [ "Foo" str>sbuf "Foo" str>sbuf = ] unit-test
[ f ] [ "Foo" str>sbuf "Foob" str>sbuf = ] unit-test
[ f ] [ 34 "Foo" str>sbuf = ] unit-test
[ "Hello" ] [
100 <sbuf> "buf" set
"Hello" "buf" get sbuf-append
"buf" get clone "buf-clone" set
"World" "buf-clone" get sbuf-append
"buf" get sbuf>str
] unit-test
] when
[ f ] [ [ 0 10 "hello" substring ] [ not ] catch ] unit-test [ f ] [ [ 0 10 "hello" substring ] [ not ] catch ] unit-test
[ [ "hell" "o wo" "rld" ] ] [ 4 "hello world" split-n ] unit-test [ [ "hell" "o wo" "rld" ] ] [ 4 "hello world" split-n ] unit-test

View File

@ -72,6 +72,7 @@ USE: unparser
"errors" "errors"
"hashtables" "hashtables"
"strings" "strings"
"sbuf"
"namespaces/namespaces" "namespaces/namespaces"
"files" "files"
"format" "format"

View File

@ -27,3 +27,8 @@ USE: vectors
[ list>vector [ 3 > ] vector-all? ] test-word [ list>vector [ 3 > ] vector-all? ] test-word
[ t ] [ [ ] ] [ t ] [ [ ] ]
[ list>vector [ 3 > ] vector-all? ] test-word [ list>vector [ 3 > ] vector-all? ] test-word
[ t ] [ { } hashcode { } hashcode = ] unit-test
[ t ] [ { 1 2 3 } hashcode { 1 2 3 } hashcode = ] unit-test
[ t ] [ { 1 { 2 } 3 } hashcode { 1 { 2 } 3 } hashcode = ] unit-test
[ t ] [ { } hashcode { } hashcode = ] unit-test

View File

@ -34,6 +34,7 @@ XT primitives[] = {
primitive_sbuf_reverse, primitive_sbuf_reverse,
primitive_sbuf_clone, primitive_sbuf_clone,
primitive_sbuf_eq, primitive_sbuf_eq,
primitive_sbuf_hashcode,
primitive_arithmetic_type, primitive_arithmetic_type,
primitive_numberp, primitive_numberp,
primitive_to_fixnum, primitive_to_fixnum,

View File

@ -1,4 +1,4 @@
extern XT primitives[]; extern XT primitives[];
#define PRIMITIVE_COUNT 192 #define PRIMITIVE_COUNT 193
CELL primitive_to_xt(CELL primitive); CELL primitive_to_xt(CELL primitive);

View File

@ -20,7 +20,7 @@ void relocate_object()
fixup_vector((VECTOR*)relocating); fixup_vector((VECTOR*)relocating);
break; break;
case STRING_TYPE: case STRING_TYPE:
hash_string((STRING*)relocating); rehash_string((STRING*)relocating);
break; break;
case SBUF_TYPE: case SBUF_TYPE:
fixup_sbuf((SBUF*)relocating); fixup_sbuf((SBUF*)relocating);

View File

@ -100,7 +100,7 @@ void primitive_sbuf_to_string(void)
{ {
SBUF* sbuf = untag_sbuf(dpeek()); SBUF* sbuf = untag_sbuf(dpeek());
STRING* s = string_clone(sbuf->string,sbuf->top); STRING* s = string_clone(sbuf->string,sbuf->top);
hash_string(s); rehash_string(s);
drepl(tag_object(s)); drepl(tag_object(s));
} }
@ -120,7 +120,9 @@ void primitive_sbuf_clone(void)
bool sbuf_eq(SBUF* s1, SBUF* s2) bool sbuf_eq(SBUF* s1, SBUF* s2)
{ {
if(s1->top == s2->top) if(s1 == s2)
return true;
else if(s1->top == s2->top)
return (string_compare_head(s1->string,s2->string,s1->top) == 0); return (string_compare_head(s1->string,s2->string,s1->top) == 0);
else else
return false; return false;
@ -136,6 +138,12 @@ void primitive_sbuf_eq(void)
dpush(F); dpush(F);
} }
void primitive_sbuf_hashcode(void)
{
SBUF* sbuf = untag_sbuf(dpop());
dpush(tag_fixnum(hash_string(sbuf->string,sbuf->top)));
}
void fixup_sbuf(SBUF* sbuf) void fixup_sbuf(SBUF* sbuf)
{ {
sbuf->string = fixup_untagged_string(sbuf->string); sbuf->string = fixup_untagged_string(sbuf->string);

View File

@ -29,5 +29,6 @@ void primitive_sbuf_reverse(void);
void primitive_sbuf_clone(void); void primitive_sbuf_clone(void);
bool sbuf_eq(SBUF* s1, SBUF* s2); bool sbuf_eq(SBUF* s1, SBUF* s2);
void primitive_sbuf_eq(void); void primitive_sbuf_eq(void);
void primitive_sbuf_hashcode(void);
void fixup_sbuf(SBUF* sbuf); void fixup_sbuf(SBUF* sbuf);
void collect_sbuf(SBUF* sbuf); void collect_sbuf(SBUF* sbuf);

View File

@ -13,14 +13,20 @@ STRING* allot_string(FIXNUM capacity)
} }
/* call this after constructing a string */ /* call this after constructing a string */
/* uses same algorithm as java.lang.String for compatibility */ /* uses same algorithm as java.lang.String for compatibility with
void hash_string(STRING* str) images generated from Java Factor. */
FIXNUM hash_string(STRING* str, FIXNUM len)
{ {
FIXNUM hash = 0; FIXNUM hash = 0;
CELL i; CELL i;
for(i = 0; i < str->capacity; i++) for(i = 0; i < len; i++)
hash = 31*hash + string_nth(str,i); hash = 31*hash + string_nth(str,i);
str->hashcode = hash; return hash;
}
void rehash_string(STRING* str)
{
str->hashcode = hash_string(str,str->capacity);
} }
/* untagged */ /* untagged */
@ -33,7 +39,7 @@ STRING* string(FIXNUM capacity, CELL fill)
for(i = 0; i < capacity; i++) for(i = 0; i < capacity; i++)
cput(SREF(string,i),fill); cput(SREF(string,i),fill);
hash_string(string); rehash_string(string);
return string; return string;
} }
@ -66,7 +72,7 @@ STRING* from_c_string(const BYTE* c_string)
c_string++; c_string++;
} }
hash_string(s); rehash_string(s);
return s; return s;
} }
@ -158,7 +164,9 @@ void primitive_string_compare(void)
bool string_eq(STRING* s1, STRING* s2) bool string_eq(STRING* s1, STRING* s2)
{ {
if(s1->hashcode != s2->hashcode) if(s1 == s2)
return true;
else if(s1->hashcode != s2->hashcode)
return false; return false;
else else
return (string_compare(s1,s2) == 0); return (string_compare(s1,s2) == 0);
@ -258,7 +266,7 @@ INLINE STRING* substring(CELL start, CELL end, STRING* string)
memcpy(result + 1, memcpy(result + 1,
(void*)((CELL)(string + 1) + CHARS * start), (void*)((CELL)(string + 1) + CHARS * start),
CHARS * (end - start)); CHARS * (end - start));
hash_string(result); rehash_string(result);
return result; return result;
} }
@ -300,7 +308,7 @@ void primitive_string_reverse(void)
STRING* s = untag_string(dpeek()); STRING* s = untag_string(dpeek());
s = string_clone(s,s->capacity); s = string_clone(s,s->capacity);
string_reverse(s,s->capacity); string_reverse(s,s->capacity);
hash_string(s); rehash_string(s);
drepl(tag_object(s)); drepl(tag_object(s));
} }

View File

@ -14,7 +14,8 @@ INLINE STRING* untag_string(CELL tagged)
STRING* allot_string(FIXNUM capacity); STRING* allot_string(FIXNUM capacity);
STRING* string(FIXNUM capacity, CELL fill); STRING* string(FIXNUM capacity, CELL fill);
void hash_string(STRING* str); FIXNUM hash_string(STRING* str, FIXNUM len);
void rehash_string(STRING* str);
STRING* grow_string(STRING* string, FIXNUM capacity, CHAR fill); STRING* grow_string(STRING* string, FIXNUM capacity, CHAR fill);
BYTE* to_c_string(STRING* s); BYTE* to_c_string(STRING* s);
void box_c_string(const BYTE* c_string); void box_c_string(const BYTE* c_string);