Warning, /include/Geant4/tools/glutess/dict is written in an unsupported language. File is not indexed.
0001 // see license file for original license.
0002
0003 #ifndef tools_glutess_dict_list
0004 #define tools_glutess_dict_list
0005
0006 /* Use #define's so that another heap implementation can use this one */
0007
0008 #define DictKey DictListKey
0009 #define Dict DictList
0010 #define DictNode DictListNode
0011
0012 #define dictNewDict(frame,leq) __gl_dictListNewDict(frame,leq)
0013 #define dictDeleteDict(dict) __gl_dictListDeleteDict(dict)
0014
0015 #define dictSearch(dict,key) __gl_dictListSearch(dict,key)
0016 #define dictInsert(dict,key) __gl_dictListInsert(dict,key)
0017 #define dictInsertBefore(dict,node,key) __gl_dictListInsertBefore(dict,node,key)
0018 #define dictDelete(dict,node) __gl_dictListDelete(dict,node)
0019
0020 #define dictKey(n) __gl_dictListKey(n)
0021 #define dictSucc(n) __gl_dictListSucc(n)
0022 #define dictPred(n) __gl_dictListPred(n)
0023 #define dictMin(d) __gl_dictListMin(d)
0024 #define dictMax(d) __gl_dictListMax(d)
0025
0026 typedef void *DictKey;
0027 typedef struct Dict Dict;
0028 typedef struct DictNode DictNode;
0029
0030 #define __gl_dictListKey(n) ((n)->key)
0031 #define __gl_dictListSucc(n) ((n)->next)
0032 #define __gl_dictListPred(n) ((n)->prev)
0033 #define __gl_dictListMin(d) ((d)->head.next)
0034 #define __gl_dictListMax(d) ((d)->head.prev)
0035 #define __gl_dictListInsert(d,k) (dictInsertBefore((d),&(d)->head,(k)))
0036
0037 /*** Private data structures ***/
0038
0039 struct DictNode {
0040 DictKey key;
0041 DictNode *next;
0042 DictNode *prev;
0043 };
0044
0045 struct Dict {
0046 DictNode head;
0047 void *frame;
0048 int (*leq)(void *frame, DictKey key1, DictKey key2);
0049 };
0050
0051 ////////////////////////////////////////////////////////
0052 /// inlined C code : ///////////////////////////////////
0053 ////////////////////////////////////////////////////////
0054 #include <cstddef>
0055 #include "memalloc"
0056
0057 inline Dict *dictNewDict( void *frame,int (*leq)(void *frame, DictKey key1, DictKey key2) ) {
0058 Dict *dict = (Dict *) memAlloc( sizeof( Dict ));
0059 DictNode *head;
0060
0061 if (dict == NULL) return NULL;
0062
0063 head = &dict->head;
0064
0065 head->key = NULL;
0066 head->next = head;
0067 head->prev = head;
0068
0069 dict->frame = frame;
0070 dict->leq = leq;
0071
0072 return dict;
0073 }
0074
0075 inline void dictDeleteDict( Dict *dict ) {
0076 DictNode *node, *next;
0077
0078 for( node = dict->head.next; node != &dict->head; node = next ) {
0079 next = node->next;
0080 memFree( node );
0081 }
0082 memFree( dict );
0083 }
0084
0085 /* Search returns the node with the smallest key greater than or equal
0086 * to the given key. If there is no such key, returns a node whose
0087 * key is NULL. Similarly, Succ(Max(d)) has a NULL key, etc.
0088 */
0089
0090 inline DictNode *dictInsertBefore( Dict *dict, DictNode *node, DictKey key ) {
0091 DictNode *newNode;
0092
0093 do {
0094 node = node->prev;
0095 } while( node->key != NULL && ! (*dict->leq)(dict->frame, node->key, key));
0096
0097 newNode = (DictNode *) memAlloc( sizeof( DictNode ));
0098 if (newNode == NULL) return NULL;
0099
0100 newNode->key = key;
0101 newNode->next = node->next;
0102 node->next->prev = newNode;
0103 newNode->prev = node;
0104 node->next = newNode;
0105
0106 return newNode;
0107 }
0108
0109 inline void dictDelete( Dict * /*dict*/, DictNode *node ) /*ARGSUSED*/
0110 {
0111 node->next->prev = node->prev;
0112 node->prev->next = node->next;
0113 memFree( node );
0114 }
0115
0116 inline DictNode *dictSearch( Dict *dict, DictKey key )
0117 {
0118 DictNode *node = &dict->head;
0119
0120 do {
0121 node = node->next;
0122 } while( node->key != NULL && ! (*dict->leq)(dict->frame, key, node->key));
0123
0124 return node;
0125 }
0126
0127 #endif