Max 5 API Reference
00001 #ifndef __HASHTAB_H__ 00002 #define __HASHTAB_H__ 00003 00004 #if C74_PRAGMA_STRUCT_PACKPUSH 00005 #pragma pack(push, 2) 00006 #elif C74_PRAGMA_STRUCT_PACK 00007 #pragma pack(2) 00008 #endif 00009 00010 /** 00011 Default number of slots in the hash table. 00012 Creating a hashtab using hashtab_new() with an argument of 0 will use the default number of slots. 00013 Primes typically work well for the number of slots. 00014 00015 @ingroup hashtab 00016 */ 00017 #define HASH_DEFSLOTS 57 // 00018 00019 00020 /** A hashtab entry. This struct is provided for debugging convenience, 00021 but should be considered opaque and is subject to change without notice. 00022 00023 @ingroup hashtab 00024 @see t_hashtab 00025 */ 00026 typedef struct _hashtab_entry 00027 { 00028 t_object ob; 00029 t_symbol *key; 00030 t_object *value; 00031 } t_hashtab_entry; 00032 00033 00034 /** The hashtab object. This struct is provided for debugging convenience, 00035 but should be considered opaque and is subject to change without notice. 00036 00037 @ingroup hashtab 00038 @see t_hashtab 00039 */ 00040 typedef struct _hashtab 00041 { 00042 t_object ob; 00043 long slotcount; 00044 t_linklist **slots; 00045 long readonly; 00046 long flags; 00047 } t_hashtab; 00048 00049 00050 #if C74_PRAGMA_STRUCT_PACKPUSH 00051 #pragma pack(pop) 00052 #elif C74_PRAGMA_STRUCT_PACK 00053 #pragma pack() 00054 #endif 00055 00056 00057 #ifdef __cplusplus 00058 extern "C" { 00059 #endif // __cplusplus 00060 00061 00062 // private 00063 void hashtab_init(void); 00064 00065 00066 /** 00067 Create a new hashtab object. 00068 You can free the hashtab by calling object_free() on the hashtab's pointer, 00069 or by using hashtab_chuck(). 00070 00071 @ingroup hashtab 00072 @param slotcount The number of slots in the hash table. Prime numbers typically work well. 00073 Pass 0 to get the default size. 00074 @return Pointer to the new hashtab object. 00075 00076 @see HASH_DEFSLOTS 00077 @see object_free() 00078 @see hashtab_chuck() 00079 */ 00080 t_hashtab *hashtab_new(long slotcount); 00081 00082 00083 /** 00084 Store an item in a hashtab with an associated key. 00085 00086 @ingroup hashtab 00087 00088 @param x The hashtab instance. 00089 @param key The key in the hashtab with which to associate the value. 00090 @param val The value to store. 00091 00092 @return A Max error code. 00093 @see hashtab_lookup() 00094 */ 00095 t_max_err hashtab_store(t_hashtab *x, t_symbol *key, t_object *val); 00096 00097 00098 /** Store an item in a hashtab with an associated key. 00099 The difference between hashtab_store_safe() and hashtab_store() is what happens in the event of a collision in the hash table. 00100 The normal hashtab_store() function will free the existing value at the collision location with sysmem_freeptr() and then replaces it. 00101 This version doesn't try to free the existing value at the collision location, but instead just over-writes it. 00102 00103 @ingroup hashtab 00104 @param x The hashtab instance. 00105 @param key The key in the hashtab with which to associate the value. 00106 @param val The value to store. 00107 @return A Max error code. 00108 @see hashtab_store() 00109 */ 00110 t_max_err hashtab_store_safe(t_hashtab *x, t_symbol *key, t_object *val); 00111 00112 00113 /** Store an item in a hashtab with an associated key and also flags that define the behavior of the item. 00114 The hashtab_store() method is the same as calling this method with the default (0) flags. 00115 00116 @ingroup hashtab 00117 @param x The hashtab instance. 00118 @param key The key in the hashtab with which to associate the value. 00119 @param val The value to store. 00120 @param flags One of the values listed in #e_max_datastore_flags. 00121 @return A Max error code. 00122 @see hashtab_store() 00123 */ 00124 t_max_err hashtab_storeflags(t_hashtab *x, t_symbol *key, t_object *val, long flags); 00125 00126 00127 /** 00128 Return an item stored in a hashtab with the specified key. 00129 00130 @ingroup hashtab 00131 00132 @param x The hashtab instance. 00133 @param key The key in the hashtab to fetch. 00134 @param val The address of a pointer to which the fetched value will be assigned. 00135 00136 @return A Max error code. 00137 @see hashtab_store() 00138 */ 00139 t_max_err hashtab_lookup(t_hashtab *x, t_symbol *key, t_object **val); 00140 00141 00142 // private 00143 t_max_err hashtab_lookupentry(t_hashtab *x, t_symbol *key, t_hashtab_entry **entry); 00144 00145 00146 /** Return an item stored in a hashtab with the specified key, also returning the items flags. 00147 @ingroup hashtab 00148 @param x The hashtab instance. 00149 @param key The key in the hashtab to fetch. 00150 @param val The address of a pointer to which the fetched value will be assigned. 00151 @param flags The address of a value to which the fetched flags will be assigned. 00152 @return A Max error code. 00153 @see hashtab_lookup() 00154 @see hashtab_store_flags() 00155 */ 00156 t_max_err hashtab_lookupflags(t_hashtab *x, t_symbol *key, t_object **val, long *flags); 00157 00158 00159 /** 00160 Remove an item from a hashtab associated with the specified key and free it. 00161 00162 The hashtab can contain a variety of different types of data. 00163 By default, the hashtab assumes that all items are max objects with a valid 00164 #t_object header. Thus by default, it frees items by calling object_free() on them. 00165 00166 You can alter the hashtab's notion of what it contains by using the 00167 hashtab_flags() method. 00168 00169 If you wish to remove an item from the hashtab and free it yourself, then you 00170 should use hashtab_chuckkey(). 00171 00172 @ingroup hashtab 00173 00174 @param x The hashtab instance. 00175 @param key The key of the item to delete. 00176 @return A Max error code. 00177 00178 @see hashtab_chuckkey() 00179 @see hashtab_clear() 00180 @see hashtab_flags() 00181 */ 00182 t_max_err hashtab_delete(t_hashtab *x, t_symbol *key); 00183 00184 00185 /** 00186 Delete all items stored in a hashtab. 00187 This is the equivalent of calling hashtab_delete() on every item in a hashtab. 00188 00189 @ingroup hashtab 00190 @return A max error code. 00191 @see hashtab_flags() 00192 @see hashtab_delete() 00193 */ 00194 t_max_err hashtab_clear(t_hashtab *x); 00195 00196 00197 /** 00198 Remove an item from a hashtab associated with a given key. 00199 00200 You are responsible for freeing any memory associated with the item that is 00201 removed from the hashtab. 00202 00203 @ingroup hashtab 00204 00205 @param x The hashtab instance. 00206 @param key The key of the item to delete. 00207 @return A Max error code. 00208 00209 @see hashtab_delete 00210 */ 00211 t_max_err hashtab_chuckkey(t_hashtab *x, t_symbol *key); 00212 00213 00214 /** 00215 Free a hashtab, but don't free the items it contains. 00216 00217 The hashtab can contain a variety of different types of data. 00218 By default, the hashtab assumes that all items are max objects with a valid 00219 #t_object header. 00220 00221 You can alter the hashtab's notion of what it contains by using the 00222 hashtab_flags() method. 00223 00224 When you free the hashtab by calling object_free() it then tries to free all of the items it contains. 00225 If the hashtab is storing a custom type of data, or should otherwise not free the data it contains, 00226 then call hashtab_chuck() to free the object instead of object_free(). 00227 00228 @ingroup hashtab 00229 @param x The hashtab object to be freed. 00230 @return A max error code. 00231 @see object_free 00232 */ 00233 t_max_err hashtab_chuck(t_hashtab *x); 00234 00235 00236 /** 00237 Search the hash table for the first item meeting defined criteria. 00238 The items in the hashtab are iteratively processed, calling a specified comparison function on each 00239 until the comparison function returns true. 00240 00241 @ingroup hashtab 00242 @param x The hashtab instance. 00243 @param o The address to pointer that will be set with the matching item. 00244 @param cmpfn The function used to determine a match in the list. 00245 @param cmpdata An argument to be passed to the #t_cmpfn. 00246 This will be passed as the second of the two args to the #t_cmpfn. 00247 The first arg will be the hashtab item at each iteration in the list. 00248 @return A max error code. 00249 00250 @see linklist_findfirst() 00251 @see t_cmpfn 00252 */ 00253 t_max_err hashtab_findfirst(t_hashtab *x, void **o, long cmpfn(void *, void *), void *cmpdata); 00254 00255 00256 /** 00257 Call the named message on every object in the hashtab. 00258 The hashtab_methodall() function requires that all items in the hashtab are 00259 object instances with a valid #t_object header. 00260 00261 @ingroup hashtab 00262 @param x The hashtab instance. 00263 @param s The name of the message to send to the objects. 00264 @param ... Any arguments to be sent with the message. 00265 @return A max error code. 00266 00267 @remark Internally, this function uses object_method(), meaning that no errors will be 00268 posted if the message name does not exist for the object. It also means that 00269 messages sent methods with #A_GIMME definitions will need to be given a symbol 00270 argument prior to the argc and argv array information. 00271 */ 00272 t_max_err hashtab_methodall(t_hashtab *x, t_symbol *s, ...); 00273 00274 00275 /** 00276 Call the specified function for every item in the hashtab. 00277 00278 @ingroup hashtab 00279 @param x The hashtab instance. 00280 @param fun The function to call, specified as function pointer cast to a Max #method. 00281 @param arg An argument that you would like to pass to the function being called. 00282 @return A max error code. 00283 00284 @remark The hashtab_funall() method will call your function for every item in the list. 00285 It will pass both a pointer to the item in the list, and any argument that you 00286 provide. The following example shows a function that could be called by hashtab_funall(). 00287 @code 00288 void myFun(t_hashtab_entry *e, void *myArg) 00289 { 00290 if (e->key && e->value) { 00291 // do something with e->key, e->value, and myArg here as appropriate 00292 } 00293 } 00294 @endcode 00295 */ 00296 t_max_err hashtab_funall(t_hashtab *x, method fun, void *arg); 00297 00298 00299 // private 00300 t_max_err hashtab_objfunall(t_hashtab *x, method fun, void *arg); 00301 00302 00303 /** 00304 Return the number of items stored in a hashtab. 00305 00306 @ingroup hashtab 00307 @param x The hashtab instance. 00308 @return The number of items in the hash table. 00309 */ 00310 long hashtab_getsize(t_hashtab *x); 00311 00312 00313 /** 00314 Post a hashtab's statistics to the Max window. 00315 00316 @ingroup hashtab 00317 @param x The hashtab instance. 00318 */ 00319 void hashtab_print(t_hashtab *x); 00320 00321 00322 /** 00323 Set the hashtab's readonly bit. 00324 00325 By default the readonly bit is 0, indicating that it is threadsafe for both reading and writing. 00326 Setting the readonly bit to 1 will disable the hashtab's theadsafety mechanism, increasing 00327 performance but at the expense of threadsafe operation. 00328 Unless you can guarantee the threading context for a hashtab's use, you should leave this set to 0. 00329 00330 @ingroup hashtab 00331 @param x The hashtab instance. 00332 @param readonly A 1 or 0 for setting the readonly bit. 00333 */ 00334 void hashtab_readonly(t_hashtab *x, long readonly); 00335 00336 00337 /** 00338 Set the hashtab's datastore flags. The available flags are enumerated in #e_max_datastore_flags. 00339 These flags control the behavior of the hashtab, particularly when removing items from the list 00340 using functions such as hashtab_clear(), hashtab_delete(), or when freeing the hashtab itself. 00341 00342 @ingroup hashtab 00343 @param x The hashtab instance. 00344 @param flags A valid value from the #e_max_datastore_flags. The default is #OBJ_FLAG_OBJ. 00345 */ 00346 void hashtab_flags(t_hashtab *x, long flags); 00347 00348 00349 /** 00350 Get the hashtab's datastore flags. 00351 00352 @ingroup hashtab 00353 @param x The hashtab instance. 00354 @return The current state of the hashtab flags as enumerated in #e_max_datastore_flags. 00355 */ 00356 long hashtab_getflags(t_hashtab *x); 00357 00358 00359 /** Change the flags for an item stored in the hashtab with a given key. 00360 @ingroup hashtab 00361 @param x The hashtab instance. 00362 @param key The key in the hashtab whose flags will be changed. 00363 @param flags One of the values listed in #e_max_datastore_flags. 00364 @return A Max error code. 00365 @see hashtab_store_flags() 00366 */ 00367 t_max_err hashtab_keyflags(t_hashtab *x, t_symbol *key, long flags); 00368 00369 00370 /** Retrieve the flags for an item stored in the hashtab with a given key. 00371 @ingroup hashtab 00372 @param x The hashtab instance. 00373 @param key The key in the hashtab whose flags will be returned. 00374 @return The flags for the given key. 00375 @see hashtab_store_flags() 00376 */ 00377 long hashtab_getkeyflags(t_hashtab *x, t_symbol *key); 00378 00379 00380 /** 00381 Retrieve all of the keys stored in a hashtab. 00382 00383 If the kc and kv parameters are properly initialized to zero, then hashtab_getkeys() will allocate memory 00384 for the keys it returns. You are then responsible for freeing this memory using sysmem_freeptr(). 00385 00386 @ingroup hashtab 00387 @param x The hashtab instance. 00388 @param kc The address of a long where the number of keys retrieved will be set. 00389 @param kv The address of the first of an array #t_symbol pointers where the retrieved keys will be set. 00390 @return A max error code. 00391 00392 @remark The following example demonstrates fetching all of the keys from a hashtab in order to iterate through 00393 each item stored in the hashtab. 00394 @code 00395 t_symbol **keys = NULL; 00396 long numKeys = 0; 00397 long i; 00398 t_object *anItem; 00399 00400 hashtab_getkeys(aHashtab, &numKeys, &keys); 00401 for(i=0; i<numKeys; i++){ 00402 hashtab_lookup(aHashtab, keys[i], &anItem); 00403 // Do something with anItem here... 00404 } 00405 if(keys) 00406 sysmem_freeptr(keys); 00407 @endcode 00408 */ 00409 t_max_err hashtab_getkeys(t_hashtab *x, long *kc, t_symbol ***kv); 00410 00411 00412 // private 00413 t_hashtab_entry *hashtab_entry_new(t_symbol *key, t_object *val); 00414 00415 00416 // private 00417 void hashtab_entry_free(t_hashtab_entry *x); 00418 00419 00420 00421 #ifdef __cplusplus 00422 } 00423 #endif // __cplusplus 00424 00425 #endif // __HASHTAB_H__ 00426
Copyright © 2008, Cycling '74