diff options
author | Warren Dukes <warren.dukes@gmail.com> | 2004-11-15 19:29:20 +0000 |
---|---|---|
committer | Warren Dukes <warren.dukes@gmail.com> | 2004-11-15 19:29:20 +0000 |
commit | b00555667522f812440572016f3c1237941a63b2 (patch) | |
tree | 59e5acb5ab6c27bf2807d57d36ea53b7c5409b21 /src/list.c | |
parent | f3e8d59d5c584f5925a8b23919aaa5b5c954cf85 (diff) |
this shit really needs to be cleaned up, but its good enough for testing,
intelligently use memmove, when inserting nodes in a sorted list
git-svn-id: https://svn.musicpd.org/mpd/trunk@2677 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'src/list.c')
-rw-r--r-- | src/list.c | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/src/list.c b/src/list.c index a777562d4..9161acdf2 100644 --- a/src/list.c +++ b/src/list.c @@ -61,7 +61,7 @@ List * makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys) { return list; } -ListNode * insertInListBeforeNode(List * list, ListNode * beforeNode, char * key, void * data) +ListNode * insertInListBeforeNode(List * list, ListNode * beforeNode, int pos, char * key, void * data) { ListNode * node; @@ -105,8 +105,20 @@ ListNode * insertInListBeforeNode(List * list, ListNode * beforeNode, char * list->numberOfNodes++; - /*freeListNodesArray(list);*/ - if(list->sorted) makeListNodesArray(list); + if(list->sorted) { + list->nodesArray = realloc(list->nodesArray, + list->numberOfNodes*sizeof(ListNode *)); + if(node == list->lastNode) { + list->nodesArray[list->numberOfNodes-1] = node; + } + else if(pos < 0) makeListNodesArray(list); + else { + memmove(list->nodesArray+pos+1, list->nodesArray+pos, + sizeof(ListNode *)* + (list->numberOfNodes-pos-1)); + list->nodesArray[pos] = node; + } + } return node; } @@ -180,12 +192,12 @@ int insertInListWithoutKey(List * list, void * data) { return 1; } -int findNodeInList(List * list, char * key, ListNode ** node) { - static long high; - static long low; - static long cur; - static ListNode * tmpNode; - static int cmp; +int findNodeInList(List * list, char * key, ListNode ** node, int * pos) { + long high; + long low; + long cur; + ListNode * tmpNode; + int cmp; assert(list!=NULL); @@ -200,6 +212,7 @@ int findNodeInList(List * list, char * key, ListNode ** node) { cmp = strcmp(tmpNode->key,key); if(cmp==0) { *node = tmpNode; + *pos = cur; return 1; } else if(cmp>0) high = cur; @@ -212,16 +225,19 @@ int findNodeInList(List * list, char * key, ListNode ** node) { cur = high; if(cur>=0) { tmpNode = list->nodesArray[cur]; - cmp = strcmp(tmpNode->key,key); *node = tmpNode; + *pos = high; + cmp = tmpNode ? strcmp(tmpNode->key,key) : -1; if( 0 == cmp ) return 1; else if( cmp > 0) return 0; else { + *pos = -1; *node = NULL; return 0; } } else { + *pos = 0; *node = list->firstNode; return 0; } @@ -242,8 +258,9 @@ int findNodeInList(List * list, char * key, ListNode ** node) { int findInList(List * list, char * key, void ** data) { ListNode * node; + int pos; - if(findNodeInList(list, key, &node)) { + if(findNodeInList(list, key, &node, &pos)) { if(data) *data = node->data; return 1; } |