tclhash.c

来自「tcl是工具命令语言」· C语言 代码 · 共 1,204 行 · 第 1/3 页

C
1,204
字号
	    index = RANDOM_INDEX (tablePtr, hash);	} else {	    index = hash & tablePtr->mask;	}    } else {	hash = (unsigned int) key;	index = RANDOM_INDEX (tablePtr, hash);    }    /*     * Search all of the entries in the appropriate bucket.     */    if (typePtr->compareKeysProc) {	for (hPtr = tablePtr->buckets[index]; hPtr != NULL;	        hPtr = hPtr->nextPtr) {#if TCL_HASH_KEY_STORE_HASH	    if (hash != (unsigned int) hPtr->hash) {		continue;	    }#endif	    if (typePtr->compareKeysProc ((VOID *) key, hPtr)) {		*newPtr = 0;		return hPtr;	    }	}    } else {	for (hPtr = tablePtr->buckets[index]; hPtr != NULL;	        hPtr = hPtr->nextPtr) {#if TCL_HASH_KEY_STORE_HASH	    if (hash != (unsigned int) hPtr->hash) {		continue;	    }#endif	    if (key == hPtr->key.oneWordValue) {		*newPtr = 0;		return hPtr;	    }	}    }    /*     * Entry not found.  Add a new one to the bucket.     */    *newPtr = 1;    if (typePtr->allocEntryProc) {	hPtr = typePtr->allocEntryProc (tablePtr, (VOID *) key);    } else {	hPtr = (Tcl_HashEntry *) ckalloc((unsigned) sizeof(Tcl_HashEntry));	hPtr->key.oneWordValue = (char *) key;    }					     hPtr->tablePtr = tablePtr;#if TCL_HASH_KEY_STORE_HASH#   if TCL_PRESERVE_BINARY_COMPATABILITY    hPtr->hash = (VOID *) hash;#   else    hPtr->hash = hash;#   endif    hPtr->nextPtr = tablePtr->buckets[index];    tablePtr->buckets[index] = hPtr;#else    hPtr->bucketPtr = &(tablePtr->buckets[index]);    hPtr->nextPtr = *hPtr->bucketPtr;    *hPtr->bucketPtr = hPtr;#endif    hPtr->clientData = 0;    tablePtr->numEntries++;    /*     * If the table has exceeded a decent size, rebuild it with many     * more buckets.     */    if (tablePtr->numEntries >= tablePtr->rebuildSize) {	RebuildTable(tablePtr);    }    return hPtr;}/* *---------------------------------------------------------------------- * * Tcl_DeleteHashEntry -- * *	Remove a single entry from a hash table. * * Results: *	None. * * Side effects: *	The entry given by entryPtr is deleted from its table and *	should never again be used by the caller.  It is up to the *	caller to free the clientData field of the entry, if that *	is relevant. * *---------------------------------------------------------------------- */voidTcl_DeleteHashEntry(entryPtr)    Tcl_HashEntry *entryPtr;{    register Tcl_HashEntry *prevPtr;    Tcl_HashKeyType *typePtr;    Tcl_HashTable *tablePtr;    Tcl_HashEntry **bucketPtr;#if TCL_HASH_KEY_STORE_HASH    int index;#endif    tablePtr = entryPtr->tablePtr;#if TCL_PRESERVE_BINARY_COMPATABILITY    if (tablePtr->keyType == TCL_STRING_KEYS) {	typePtr = &tclStringHashKeyType;    } else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) {	typePtr = &tclOneWordHashKeyType;    } else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS	       || tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) {	typePtr = tablePtr->typePtr;    } else {	typePtr = &tclArrayHashKeyType;    }#else    typePtr = tablePtr->typePtr;#endif    #if TCL_HASH_KEY_STORE_HASH    if (typePtr->hashKeyProc == NULL	|| typePtr->flags & TCL_HASH_KEY_RANDOMIZE_HASH) {	index = RANDOM_INDEX (tablePtr, entryPtr->hash);    } else {	index = ((unsigned int) entryPtr->hash) & tablePtr->mask;    }    bucketPtr = &(tablePtr->buckets[index]);#else    bucketPtr = entryPtr->bucketPtr;#endif        if (*bucketPtr == entryPtr) {	*bucketPtr = entryPtr->nextPtr;    } else {	for (prevPtr = *bucketPtr; ; prevPtr = prevPtr->nextPtr) {	    if (prevPtr == NULL) {		panic("malformed bucket chain in Tcl_DeleteHashEntry");	    }	    if (prevPtr->nextPtr == entryPtr) {		prevPtr->nextPtr = entryPtr->nextPtr;		break;	    }	}    }    tablePtr->numEntries--;    if (typePtr->freeEntryProc) {	typePtr->freeEntryProc (entryPtr);    } else {	ckfree((char *) entryPtr);    }}/* *---------------------------------------------------------------------- * * Tcl_DeleteHashTable -- * *	Free up everything associated with a hash table except for *	the record for the table itself. * * Results: *	None. * * Side effects: *	The hash table is no longer useable. * *---------------------------------------------------------------------- */voidTcl_DeleteHashTable(tablePtr)    register Tcl_HashTable *tablePtr;		/* Table to delete. */{    register Tcl_HashEntry *hPtr, *nextPtr;    Tcl_HashKeyType *typePtr;    int i;#if TCL_PRESERVE_BINARY_COMPATABILITY    if (tablePtr->keyType == TCL_STRING_KEYS) {	typePtr = &tclStringHashKeyType;    } else if (tablePtr->keyType == TCL_ONE_WORD_KEYS) {	typePtr = &tclOneWordHashKeyType;    } else if (tablePtr->keyType == TCL_CUSTOM_TYPE_KEYS	       || tablePtr->keyType == TCL_CUSTOM_PTR_KEYS) {	typePtr = tablePtr->typePtr;    } else {	typePtr = &tclArrayHashKeyType;    }#else    typePtr = tablePtr->typePtr;#endif    /*     * Free up all the entries in the table.     */    for (i = 0; i < tablePtr->numBuckets; i++) {	hPtr = tablePtr->buckets[i];	while (hPtr != NULL) {	    nextPtr = hPtr->nextPtr;	    if (typePtr->freeEntryProc) {		typePtr->freeEntryProc (hPtr);	    } else {		ckfree((char *) hPtr);	    }	    hPtr = nextPtr;	}    }    /*     * Free up the bucket array, if it was dynamically allocated.     */    if (tablePtr->buckets != tablePtr->staticBuckets) {	ckfree((char *) tablePtr->buckets);    }    /*     * Arrange for panics if the table is used again without     * re-initialization.     */#if TCL_PRESERVE_BINARY_COMPATABILITY    tablePtr->findProc = BogusFind;    tablePtr->createProc = BogusCreate;#else    tablePtr->typePtr = NULL;#endif}/* *---------------------------------------------------------------------- * * Tcl_FirstHashEntry -- * *	Locate the first entry in a hash table and set up a record *	that can be used to step through all the remaining entries *	of the table. * * Results: *	The return value is a pointer to the first entry in tablePtr, *	or NULL if tablePtr has no entries in it.  The memory at *	*searchPtr is initialized so that subsequent calls to *	Tcl_NextHashEntry will return all of the entries in the table, *	one at a time. * * Side effects: *	None. * *---------------------------------------------------------------------- */Tcl_HashEntry *Tcl_FirstHashEntry(tablePtr, searchPtr)    Tcl_HashTable *tablePtr;		/* Table to search. */    Tcl_HashSearch *searchPtr;		/* Place to store information about					 * progress through the table. */{    searchPtr->tablePtr = tablePtr;    searchPtr->nextIndex = 0;    searchPtr->nextEntryPtr = NULL;    return Tcl_NextHashEntry(searchPtr);}/* *---------------------------------------------------------------------- * * Tcl_NextHashEntry -- * *	Once a hash table enumeration has been initiated by calling *	Tcl_FirstHashEntry, this procedure may be called to return *	successive elements of the table. * * Results: *	The return value is the next entry in the hash table being *	enumerated, or NULL if the end of the table is reached. * * Side effects: *	None. * *---------------------------------------------------------------------- */Tcl_HashEntry *Tcl_NextHashEntry(searchPtr)    register Tcl_HashSearch *searchPtr;	/* Place to store information about					 * progress through the table.  Must					 * have been initialized by calling					 * Tcl_FirstHashEntry. */{    Tcl_HashEntry *hPtr;    while (searchPtr->nextEntryPtr == NULL) {	if (searchPtr->nextIndex >= searchPtr->tablePtr->numBuckets) {	    return NULL;	}	searchPtr->nextEntryPtr =		searchPtr->tablePtr->buckets[searchPtr->nextIndex];	searchPtr->nextIndex++;    }    hPtr = searchPtr->nextEntryPtr;    searchPtr->nextEntryPtr = hPtr->nextPtr;    return hPtr;}/* *---------------------------------------------------------------------- * * Tcl_HashStats -- * *	Return statistics describing the layout of the hash table *	in its hash buckets. * * Results: *	The return value is a malloc-ed string containing information *	about tablePtr.  It is the caller's responsibility to free *	this string. * * Side effects: *	None. * *---------------------------------------------------------------------- */CONST char *Tcl_HashStats(tablePtr)    Tcl_HashTable *tablePtr;		/* Table for which to produce stats. */{#define NUM_COUNTERS 10    int count[NUM_COUNTERS], overflow, i, j;    double average, tmp;    register Tcl_HashEntry *hPtr;    char *result, *p;    /*     * Compute a histogram of bucket usage.     */    for (i = 0; i < NUM_COUNTERS; i++) {	count[i] = 0;    }    overflow = 0;    average = 0.0;    for (i = 0; i < tablePtr->numBuckets; i++) {	j = 0;	for (hPtr = tablePtr->buckets[i]; hPtr != NULL; hPtr = hPtr->nextPtr) {	    j++;	}	if (j < NUM_COUNTERS) {	    count[j]++;	} else {	    overflow++;	}	tmp = j;	average += (tmp+1.0)*(tmp/tablePtr->numEntries)/2.0;    }    /*     * Print out the histogram and a few other pieces of information.     */    result = (char *) ckalloc((unsigned) ((NUM_COUNTERS*60) + 300));    sprintf(result, "%d entries in table, %d buckets\n",	    tablePtr->numEntries, tablePtr->numBuckets);    p = result + strlen(result);    for (i = 0; i < NUM_COUNTERS; i++) {	sprintf(p, "number of buckets with %d entries: %d\n",		i, count[i]);	p += strlen(p);    }    sprintf(p, "number of buckets with %d or more entries: %d\n",	    NUM_COUNTERS, overflow);    p += strlen(p);    sprintf(p, "average search distance for entry: %.1f", average);    return result;}/* *---------------------------------------------------------------------- * * AllocArrayEntry -- * *	Allocate space for a Tcl_HashEntry containing the array key. * * Results: *	The return value is a pointer to the created entry. * * Side effects: *	None. *

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?