Changeset 2166
- Timestamp:
- 09/21/08 21:59:57 (4 months ago)
- Files:
-
- libswish3/trunk/src/libswish3/hash.c (modified) (1 diff)
- libswish3/trunk/src/libswish3/libswish3.h (modified) (3 diffs)
- libswish3/trunk/src/libswish3/tokenizer.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libswish3/trunk/src/libswish3/hash.c
r2135 r2166 58 58 59 59 return ret; 60 } 61 62 int 63 swish_hash_exists_or_add( 64 xmlHashTablePtr hash, 65 xmlChar *key, 66 xmlChar *value 67 ) 68 { 69 if (!swish_hash_exists(hash, key)) { 70 return swish_hash_add(hash, key, swish_xstrdup( value )); 71 } 72 else { 73 return 1; 74 } 60 75 } 61 76 libswish3/trunk/src/libswish3/libswish3.h
r2165 r2166 284 284 swish_MetaName *meta; 285 285 xmlChar *value; 286 xmlChar *context; // TODO refactor this into array of ints287 unsigned int start_byte;286 xmlChar *context; 287 unsigned int cpts; // num of codepoints (characters) 288 288 unsigned int len; 289 289 int ref_cnt; … … 295 295 unsigned int n; 296 296 unsigned int pos; // track position in document 297 xmlHashTablePtr contexts; // cache contexts 297 298 xmlBufferPtr buf; 298 299 swish_Token** tokens; … … 404 405 int swish_hash_delete( xmlHashTablePtr hash, xmlChar *key ); 405 406 boolean swish_hash_exists( xmlHashTablePtr hash, xmlChar *key ); 407 int swish_hash_exists_or_add( xmlHashTablePtr hash, xmlChar *key, xmlChar *value ); 406 408 void swish_hash_merge( xmlHashTablePtr hash1, xmlHashTablePtr hash2 ); 407 409 void * swish_hash_fetch( xmlHashTablePtr hash, xmlChar *key ); libswish3/trunk/src/libswish3/tokenizer.c
r2165 r2166 319 319 tl->ref_cnt = 0; 320 320 tl->tokens = swish_xmalloc(sizeof(swish_Token *) * SWISH_TOKEN_LIST_SIZE); 321 tl->contexts = swish_init_hash(8); 321 322 322 323 if (!ascii_init) … … 349 350 swish_xfree(tl->tokens); 350 351 xmlBufferFree(tl->buf); 352 swish_hash_free(tl->contexts); // MUST free **after** tokens 351 353 swish_xfree(tl); 352 354 } … … 373 375 374 376 stoken = swish_init_token(); 375 stoken->start_byte = xmlBufferLength(tl->buf);376 377 377 378 /* grab the current buffer and point ->value into the buffer */ 378 379 buf = xmlBufferContent(tl->buf); 379 buf += stoken->start_byte;380 buf += xmlBufferLength(tl->buf); // point at last byte which till become first byte 380 381 stoken->value = (xmlChar*)buf; 381 382 stoken->cpts = swish_utf8_num_chrs(token); 382 383 stoken->len = token_len - 1; // -1 to exlude the NULL 383 384 stoken->pos = ++tl->pos; 384 385 stoken->meta = meta; 385 386 stoken->meta->ref_cnt++; 386 stoken->context = swish_xstrdup(context); 387 388 /* cache the context string and point at the cached value */ 389 swish_hash_exists_or_add( tl->contexts, context, context ); 390 stoken->context = swish_hash_fetch( tl->contexts, context ); 391 387 392 stoken->ref_cnt++; 388 393 swish_set_token_value(tl, token, token_len); … … 418 423 { 419 424 int ret; 420 ret = xmlBufferAdd(tl->buf, token, len); 425 ret = xmlBufferAdd(tl->buf, token, len); // include the NULL 421 426 if (ret != 0) { 422 427 SWISH_CROAK("error appending token to buffer: %d", ret); … … 435 440 t->context = NULL; 436 441 t->value = NULL; 437 t-> start_byte= 0;442 t->cpts = 0; 438 443 t->len = 0; 439 444 t->ref_cnt = 0; … … 449 454 if (t->ref_cnt != 0) { 450 455 SWISH_WARN("freeing Token with ref_cnt != 0 (%d)", t->ref_cnt); 451 }452 if (t->context != NULL) {453 swish_xfree(t->context);454 456 } 455 457 … … 475 477 t->context = %s\n\ 476 478 t->meta = %d [%s]\n\ 477 t-> start_byte= %d\n\479 t->cpts = %d\n\ 478 480 t->len = %d\n\ 479 481 t->value = %s\n\ 480 ", t->ref_cnt, t->pos, t->context, t->meta->id, t->meta->name, t-> start_byte, t->len, t->value);482 ", t->ref_cnt, t->pos, t->context, t->meta->id, t->meta->name, t->cpts, t->len, t->value); 481 483 482 484 }
