// see license file for original license. #ifndef tools_glutess_dict_list #define tools_glutess_dict_list /* Use #define's so that another heap implementation can use this one */ #define DictKey DictListKey #define Dict DictList #define DictNode DictListNode #define dictNewDict(frame,leq) __gl_dictListNewDict(frame,leq) #define dictDeleteDict(dict) __gl_dictListDeleteDict(dict) #define dictSearch(dict,key) __gl_dictListSearch(dict,key) #define dictInsert(dict,key) __gl_dictListInsert(dict,key) #define dictInsertBefore(dict,node,key) __gl_dictListInsertBefore(dict,node,key) #define dictDelete(dict,node) __gl_dictListDelete(dict,node) #define dictKey(n) __gl_dictListKey(n) #define dictSucc(n) __gl_dictListSucc(n) #define dictPred(n) __gl_dictListPred(n) #define dictMin(d) __gl_dictListMin(d) #define dictMax(d) __gl_dictListMax(d) typedef void *DictKey; typedef struct Dict Dict; typedef struct DictNode DictNode; #define __gl_dictListKey(n) ((n)->key) #define __gl_dictListSucc(n) ((n)->next) #define __gl_dictListPred(n) ((n)->prev) #define __gl_dictListMin(d) ((d)->head.next) #define __gl_dictListMax(d) ((d)->head.prev) #define __gl_dictListInsert(d,k) (dictInsertBefore((d),&(d)->head,(k))) /*** Private data structures ***/ struct DictNode { DictKey key; DictNode *next; DictNode *prev; }; struct Dict { DictNode head; void *frame; int (*leq)(void *frame, DictKey key1, DictKey key2); }; //////////////////////////////////////////////////////// /// inlined C code : /////////////////////////////////// //////////////////////////////////////////////////////// #include #include "memalloc" inline Dict *dictNewDict( void *frame,int (*leq)(void *frame, DictKey key1, DictKey key2) ) { Dict *dict = (Dict *) memAlloc( sizeof( Dict )); DictNode *head; if (dict == NULL) return NULL; head = &dict->head; head->key = NULL; head->next = head; head->prev = head; dict->frame = frame; dict->leq = leq; return dict; } inline void dictDeleteDict( Dict *dict ) { DictNode *node, *next; for( node = dict->head.next; node != &dict->head; node = next ) { next = node->next; memFree( node ); } memFree( dict ); } /* Search returns the node with the smallest key greater than or equal * to the given key. If there is no such key, returns a node whose * key is NULL. Similarly, Succ(Max(d)) has a NULL key, etc. */ inline DictNode *dictInsertBefore( Dict *dict, DictNode *node, DictKey key ) { DictNode *newNode; do { node = node->prev; } while( node->key != NULL && ! (*dict->leq)(dict->frame, node->key, key)); newNode = (DictNode *) memAlloc( sizeof( DictNode )); if (newNode == NULL) return NULL; newNode->key = key; newNode->next = node->next; node->next->prev = newNode; newNode->prev = node; node->next = newNode; return newNode; } inline void dictDelete( Dict * /*dict*/, DictNode *node ) /*ARGSUSED*/ { node->next->prev = node->prev; node->prev->next = node->next; memFree( node ); } inline DictNode *dictSearch( Dict *dict, DictKey key ) { DictNode *node = &dict->head; do { node = node->next; } while( node->key != NULL && ! (*dict->leq)(dict->frame, key, node->key)); return node; } #endif