1 /* ----------------------------------------------------------------- 2 "@(#) hash.h version 1.7 9/22/91" 3 FILE: hash.h 4 DESCRIPTION:Insert file for threaded hash routines. 5 CONTENTS: 6 DATE: Jul 17, 1988 7 REVISIONS: Nov 6, 1988 - added user delete function to delTable. 8 Jan 18, 1989 - added DELETE operation to hash_search. 9 Apr 27, 1989 - added Y prefix. 10 Thu Apr 18 00:52:26 EDT 1991 - renamed functions. 11 ----------------------------------------------------------------- */ 12 #ifndef HASH_H 13 #define HASH_H 14 15 #include <yalecad/base.h> 16 17 typedef struct ytable_rec { 18 char *key ; 19 char *data ; 20 struct ytable_rec *next ; 21 struct ytable_rec *threadNext ; /* thread thru entire table */ 22 } YTABLEBOX, *YTABLEPTR ; 23 24 typedef struct { 25 YTABLEPTR *hash_table ; 26 YTABLEPTR thread ; 27 INT size ; 28 } YHASHBOX, *YHASHPTR ; 29 30 #define ENTER (INT) 1 31 #define FIND (INT) 0 32 #define DELETE (INT) 2 33 34 extern YHASHPTR Yhash_table_create( P1(INT numentries) ) ; 35 /* 36 Function: 37 Returns a hash table with the given number of entries. 38 More that one hash table can coexist at the same time. 39 */ 40 41 extern INT Yhash_table_get( P1( YHASHPTR hashtable ) ) ; 42 /* 43 Function: 44 Returns the current size of hash table set by Yhash_table_create 45 */ 46 47 extern char *Yhash_search( P4(YHASHPTR hashtable, char *key, VOIDPTR data, 48 INT operation ) ) ; 49 /* 50 Function: 51 Hash table search routine. Given a hashtable and a key, perform 52 the following operations: 53 ENTER:if key is in table it, returns a pointer to the item. 54 if key is not in table, add it to the table. returns NULL. 55 FIND:if key is in table, it returns a pointer to the item. 56 if key is not in the table, it returns NULL. 57 DELETE:if key is in table, it returns -1 58 if key is not in table, it return NULL. 59 Memory is not freed in the delete case, but marked dirty. 60 Data is a pointer to the information to be store with the given key. 61 */ 62 63 extern char *Yhash_add( P4( YHASHPTR hashtable, char *key, 64 char *(*add_function)(), BOOL *new_flag ) ) ; 65 /* 66 Function: 67 Hash table search convenience routine. It avoids two calls to hash_search 68 in the case of always adding to the table it the item does not exist. 69 Hash_add adds to table if it doesn't already exist - new flag 70 will be set to true. If key already exists in table then 71 hash add will not add it to the hash table but will notify the 72 user by setting new flag to false. The add_function is used 73 to associate the information with the key. It should return a 74 pointer to the memory where the information is stored. This 75 function always returns a pointer to the data. 76 */ 77 78 79 extern void Yhash_table_delete( P2(YHASHPTR hashtable,INT (*userdelete)() ) ) ; 80 /* 81 Function: 82 Frees the memory associated with a hash table. The user 83 make supply a function which deletes the memory associated 84 with the data field. This function must have the data 85 pointer supplied by the hash add routines as an argument,ie. 86 Yhash_table_delete( my_hash_table, my_free_func ) ; 87 my_free_func( data ) 88 char *data ; 89 { 90 } 91 */ 92 93 extern INT Yhash_set_size( P1( YHASHPTR hashtable ) ) ; 94 /* 95 Function: 96 Since this is a threaded hash table we can find the size of 97 all the valid entries in the table in O(n) where n is the 98 number of valid entries. Returns the number of valid entries. 99 One may enumerate the hash table by writing the following loop. 100 TABLEPTR ptr ; 101 for( ptr = mytable->thread; ptr ; ptr= ptr->threadNext ){ 102 ... 103 } 104 */ 105 #endif /* HASH_H */ 106