diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-02-11 09:40:01 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-02-11 09:40:01 -0200 |
| commit | 205ee1ec84c0f0e9c2df923fdcfa29207e8e72b3 (patch) | |
| tree | 1e2683e673e5fbf2230343df4faac3b252eee38c /tree.c | |
| parent | b48847c5fac055f0d6120029f6fe1a50c852a8ac (diff) | |
| download | lua-205ee1ec84c0f0e9c2df923fdcfa29207e8e72b3.tar.gz lua-205ee1ec84c0f0e9c2df923fdcfa29207e8e72b3.tar.bz2 lua-205ee1ec84c0f0e9c2df923fdcfa29207e8e72b3.zip | |
userdata can handle arbitrary binary data;
user tag is stored with data;
Diffstat (limited to 'tree.c')
| -rw-r--r-- | tree.c | 38 |
1 files changed, 23 insertions, 15 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_tree="$Id: tree.c,v 1.19 1996/02/22 20:34:33 roberto Exp $"; | 6 | char *rcs_tree="$Id: tree.c,v 1.20 1996/03/14 15:56:26 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | #include <string.h> | 9 | #include <string.h> |
| @@ -28,14 +28,14 @@ static int initialized = 0; | |||
| 28 | 28 | ||
| 29 | static stringtable string_root[NUM_HASHS]; | 29 | static stringtable string_root[NUM_HASHS]; |
| 30 | 30 | ||
| 31 | static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 2, {0}}; | 31 | static TaggedString EMPTY = {LUA_T_STRING, 0, NOT_USED, NOT_USED, 0, 2, {0}}; |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | static unsigned long hash (char *str) | 34 | static unsigned long hash (char *buff, long size) |
| 35 | { | 35 | { |
| 36 | unsigned long h = 0; | 36 | unsigned long h = 0; |
| 37 | while (*str) | 37 | while (size--) |
| 38 | h = ((h<<5)-h)^(unsigned char)*(str++); | 38 | h = ((h<<5)-h)^(unsigned char)*(buff++); |
| 39 | return h; | 39 | return h; |
| 40 | } | 40 | } |
| 41 | 41 | ||
| @@ -71,10 +71,10 @@ static void grow (stringtable *tb) | |||
| 71 | tb->hash = newhash; | 71 | tb->hash = newhash; |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | static TaggedString *insert (char *str, stringtable *tb) | 74 | static TaggedString *insert (char *buff, long size, int tag, stringtable *tb) |
| 75 | { | 75 | { |
| 76 | TaggedString *ts; | 76 | TaggedString *ts; |
| 77 | unsigned long h = hash(str); | 77 | unsigned long h = hash(buff, size); |
| 78 | int i; | 78 | int i; |
| 79 | int j = -1; | 79 | int j = -1; |
| 80 | if ((Long)tb->nuse*3 >= (Long)tb->size*2) | 80 | if ((Long)tb->nuse*3 >= (Long)tb->size*2) |
| @@ -84,12 +84,13 @@ static TaggedString *insert (char *str, stringtable *tb) | |||
| 84 | grow(tb); | 84 | grow(tb); |
| 85 | } | 85 | } |
| 86 | i = h%tb->size; | 86 | i = h%tb->size; |
| 87 | while (tb->hash[i]) | 87 | while ((ts = tb->hash[i]) != NULL) |
| 88 | { | 88 | { |
| 89 | if (tb->hash[i] == &EMPTY) | 89 | if (ts == &EMPTY) |
| 90 | j = i; | 90 | j = i; |
| 91 | else if (strcmp(str, tb->hash[i]->str) == 0) | 91 | else if (ts->size == size && ts->tag == tag && |
| 92 | return tb->hash[i]; | 92 | memcmp(buff, ts->str, size) == 0) |
| 93 | return ts; | ||
| 93 | i = (i+1)%tb->size; | 94 | i = (i+1)%tb->size; |
| 94 | } | 95 | } |
| 95 | /* not found */ | 96 | /* not found */ |
| @@ -98,17 +99,24 @@ static TaggedString *insert (char *str, stringtable *tb) | |||
| 98 | i = j; | 99 | i = j; |
| 99 | else | 100 | else |
| 100 | tb->nuse++; | 101 | tb->nuse++; |
| 101 | ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str)); | 102 | ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+size-1); |
| 102 | strcpy(ts->str, str); | 103 | memcpy(ts->str, buff, size); |
| 104 | ts->tag = tag; | ||
| 105 | ts->size = size; | ||
| 103 | ts->marked = 0; | 106 | ts->marked = 0; |
| 104 | ts->hash = h; | 107 | ts->hash = h; |
| 105 | ts->varindex = ts->constindex = NOT_USED; | 108 | ts->varindex = ts->constindex = NOT_USED; |
| 106 | return ts; | 109 | return ts; |
| 107 | } | 110 | } |
| 108 | 111 | ||
| 109 | TaggedString *lua_createstring (char *str) | 112 | TaggedString *luaI_createuserdata (char *buff, long size, int tag) |
| 110 | { | 113 | { |
| 111 | return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]); | 114 | return insert(buff, size, tag, &string_root[(unsigned)buff[0]%NUM_HASHS]); |
| 115 | } | ||
| 116 | |||
| 117 | TaggedString *lua_createstring (char *str) | ||
| 118 | { | ||
| 119 | return luaI_createuserdata(str, strlen(str)+1, LUA_T_STRING); | ||
| 112 | } | 120 | } |
| 113 | 121 | ||
| 114 | 122 | ||
