Max 5 API Reference
00001 /** 00002 @page chapter_datastructures Data Structures 00003 00004 00005 @section chapter_datastructures_linkedlists Linked Lists 00006 00007 The Max #t_linklist data structure is useful for maintaining ordered lists of items where you want to be able to insert and delete items efficiently. Random access of individual items, however, gets appreciably slower as the list grows in size. The #t_linklist is thread-safe by default, but thread safety can be turned off for performance benefits in single-threaded applications. However, ensure that your use of the linked list is truly single-threaded (based on an understanding of Max's @ref chapter_threading model) before turning off the thread safety features. 00008 00009 By default, the #t_linklist holds pointers to Max objects. However, you can treat what the linklist holds as data rather than objects to be freed by using the linklist_flags() function. 00010 00011 Here is a simple example of the use of #t_linklist. The code below stores five symbols, sorts them, searches for a specific item, deletes an item, prints all items, and then frees the entire structure. Since symbols in Max are never freed, linklist_flags() is used to specify that data, rather than object pointers, are being stored. 00012 00013 @code 00014 void mylistfun() 00015 { 00016 t_linklist *list; 00017 00018 list = (t_linklist *)linklist_new(); 00019 00020 linklist_flags(list, OBJ_FLAG_DATA); 00021 00022 // add some data 00023 linklist_append(list, gensym("one")); 00024 linklist_append(list, gensym("two")); 00025 linklist_append(list, gensym("three")); 00026 linklist_append(list, gensym("four")); 00027 linklist_append(list, gensym("five")); 00028 00029 // sort 00030 00031 linklist_sort(list, (t_cmpfn)mysortfun); 00032 00033 // search 00034 00035 index = linklist_findfirst(list, &found, mysearchfun, gensym("four")); // find the "four" symbol 00036 00037 if (index != -1) // found 00038 linklist_chuckindex(list, index); 00039 00040 // iterate 00041 00042 linklist_funall(list, myprintfun, NULL); 00043 00044 // delete 00045 00046 linklist_chuck(list); 00047 00048 } 00049 @endcode 00050 00051 The sorting function compares two items in the list and returns non-zero if the first one should go before the second one. 00052 00053 @code 00054 long mysortfun(void *a, void *b) 00055 { 00056 t_symbol *sa = (t_symbol *)a; 00057 t_symbol *sb = (t_symbol *)b; 00058 00059 return strcmp(sa->s_name, sb->s_name) > 0; 00060 } 00061 @endcode 00062 00063 The search function is passed the final argument to linklist_findfirst() and, in this case, just returns the symbol that matches, which is just testing for pointer equivalence since all Max symbols are unique. You could do more sophisticated searching if you store more complex data in a linklist. 00064 00065 @code 00066 long mysearchfun(t_symbol *elem, t_symbol *match) 00067 { 00068 return elem == match; 00069 } 00070 @endcode 00071 00072 The iteration function takes some action on all items in the list. The third argument to linklist_funall() is passed as the second argument to your iteration function. In this example, we don't do anything with it. 00073 00074 @code 00075 void myprintfun(t_symbol *item, void *dummy) 00076 { 00077 post("%s",item->s_name); 00078 } 00079 @endcode 00080 00081 There are many more functions for operating on linked lists you can read about by exploring the @ref linklist function reference. 00082 00083 00084 @section chapter_datastructures_hashtabs Hash Tables 00085 00086 A hash table is a data structure that associates some data with a unique key. If you know the key, you can get back the data much more quickly than with a linked list, particularly as the number of items stored grows larger. The Max hash table #t_hashtab is optimized to work with symbol pointers as keys, but you can use any pointer or number, as long as it is unique. 00087 00088 To create a #t_hashtab, you use hashtab_new(). To add items, use hashtab_store(). To find items that have been added, use hashtab_lookup(). 00089 00090 By contrast with linked lists and arrays, hash tables do not have a strong sense of ordering. You can iterate through all items using hashtab_funall(), but the exact order is not under your control as items are added and removed. There is also no way to "sort" a hash table. 00091 00092 Example: 00093 00094 The following example creates a hashtab, shows how to add some data (in this case, just a number), look it up, and delete the hashtab. 00095 @code 00096 t_hashtab *tab = (t_hashtab *)hashtab_new(0); 00097 long result, value; 00098 00099 hashtab_store(tab, gensym("a great number"), (t_object *)74); 00100 00101 result = hashtab_lookup(tab, gensym("a great number"), (t_object **)value); 00102 00103 if (!result) 00104 post("found the value and it is %ld",value); 00105 else 00106 post("did not find the value"); 00107 00108 hashtab_chuck(tab); 00109 @endcode 00110 00111 Note that the Max #t_dictionary used for managing patcher data is implemented as a #t_hashtab. 00112 00113 00114 */
Copyright © 2008, Cycling '74