Commit b0055566 authored by Warren Dukes's avatar Warren Dukes

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
parent f3e8d59d
...@@ -938,6 +938,7 @@ void readDirectoryInfo(FILE * fp,Directory * directory) { ...@@ -938,6 +938,7 @@ void readDirectoryInfo(FILE * fp,Directory * directory) {
insertInListBeforeNode( insertInListBeforeNode(
directory->subDirectories, directory->subDirectories,
nextDirNode, nextDirNode,
-1,
key, key,
(void *)subDirectory); (void *)subDirectory);
} }
......
...@@ -61,7 +61,7 @@ List * makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys) { ...@@ -61,7 +61,7 @@ List * makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys) {
return list; 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; ListNode * node;
...@@ -105,8 +105,20 @@ ListNode * insertInListBeforeNode(List * list, ListNode * beforeNode, char * ...@@ -105,8 +105,20 @@ ListNode * insertInListBeforeNode(List * list, ListNode * beforeNode, char *
list->numberOfNodes++; list->numberOfNodes++;
/*freeListNodesArray(list);*/ if(list->sorted) {
if(list->sorted) makeListNodesArray(list); 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; return node;
} }
...@@ -180,12 +192,12 @@ int insertInListWithoutKey(List * list, void * data) { ...@@ -180,12 +192,12 @@ int insertInListWithoutKey(List * list, void * data) {
return 1; return 1;
} }
int findNodeInList(List * list, char * key, ListNode ** node) { int findNodeInList(List * list, char * key, ListNode ** node, int * pos) {
static long high; long high;
static long low; long low;
static long cur; long cur;
static ListNode * tmpNode; ListNode * tmpNode;
static int cmp; int cmp;
assert(list!=NULL); assert(list!=NULL);
...@@ -200,6 +212,7 @@ int findNodeInList(List * list, char * key, ListNode ** node) { ...@@ -200,6 +212,7 @@ int findNodeInList(List * list, char * key, ListNode ** node) {
cmp = strcmp(tmpNode->key,key); cmp = strcmp(tmpNode->key,key);
if(cmp==0) { if(cmp==0) {
*node = tmpNode; *node = tmpNode;
*pos = cur;
return 1; return 1;
} }
else if(cmp>0) high = cur; else if(cmp>0) high = cur;
...@@ -212,16 +225,19 @@ int findNodeInList(List * list, char * key, ListNode ** node) { ...@@ -212,16 +225,19 @@ int findNodeInList(List * list, char * key, ListNode ** node) {
cur = high; cur = high;
if(cur>=0) { if(cur>=0) {
tmpNode = list->nodesArray[cur]; tmpNode = list->nodesArray[cur];
cmp = strcmp(tmpNode->key,key);
*node = tmpNode; *node = tmpNode;
*pos = high;
cmp = tmpNode ? strcmp(tmpNode->key,key) : -1;
if( 0 == cmp ) return 1; if( 0 == cmp ) return 1;
else if( cmp > 0) return 0; else if( cmp > 0) return 0;
else { else {
*pos = -1;
*node = NULL; *node = NULL;
return 0; return 0;
} }
} }
else { else {
*pos = 0;
*node = list->firstNode; *node = list->firstNode;
return 0; return 0;
} }
...@@ -242,8 +258,9 @@ int findNodeInList(List * list, char * key, ListNode ** node) { ...@@ -242,8 +258,9 @@ int findNodeInList(List * list, char * key, ListNode ** node) {
int findInList(List * list, char * key, void ** data) { int findInList(List * list, char * key, void ** data) {
ListNode * node; ListNode * node;
int pos;
if(findNodeInList(list, key, &node)) { if(findNodeInList(list, key, &node, &pos)) {
if(data) *data = node->data; if(data) *data = node->data;
return 1; return 1;
} }
......
...@@ -74,7 +74,7 @@ List * makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys); ...@@ -74,7 +74,7 @@ List * makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys);
ListNode * insertInList(List * list,char * key,void * data); ListNode * insertInList(List * list,char * key,void * data);
ListNode * insertInListBeforeNode(List * list, ListNode * beforeNode, ListNode * insertInListBeforeNode(List * list, ListNode * beforeNode,
char * key, void * data); int pos, char * key, void * data);
int insertInListWithoutKey(List * list,void * data); int insertInListWithoutKey(List * list,void * data);
...@@ -99,7 +99,7 @@ int findInList(List * list, char * key, void ** data); ...@@ -99,7 +99,7 @@ int findInList(List * list, char * key, void ** data);
/* if _key_ is not found, *_node_ is assigned to the node before which /* if _key_ is not found, *_node_ is assigned to the node before which
the info would be found */ the info would be found */
int findNodeInList(List * list, char * key, ListNode ** node); int findNodeInList(List * list, char * key, ListNode ** node, int * pos);
/* frees memory malloc'd for list and its nodes /* frees memory malloc'd for list and its nodes
* _list_ -> List to be free'd * _list_ -> List to be free'd
......
...@@ -189,7 +189,7 @@ void insertSongIntoList(SongList * list, ListNode ** nextSongNode, char * key, ...@@ -189,7 +189,7 @@ void insertSongIntoList(SongList * list, ListNode ** nextSongNode, char * key,
*nextSongNode = (*nextSongNode)->nextNode; *nextSongNode = (*nextSongNode)->nextNode;
} }
else { else {
insertInListBeforeNode(list, *nextSongNode, song->url, insertInListBeforeNode(list, *nextSongNode, -1, song->url,
(void *)song); (void *)song);
} }
} }
......
...@@ -23,6 +23,7 @@ typedef struct tagTrackerItem { ...@@ -23,6 +23,7 @@ typedef struct tagTrackerItem {
char * getTagItemString(int type, char * string) { char * getTagItemString(int type, char * string) {
ListNode * node; ListNode * node;
int pos;
/*if(type == TAG_ITEM_TITLE) return strdup(string);*/ /*if(type == TAG_ITEM_TITLE) return strdup(string);*/
...@@ -31,15 +32,16 @@ char * getTagItemString(int type, char * string) { ...@@ -31,15 +32,16 @@ char * getTagItemString(int type, char * string) {
sortList(tagLists[type]); sortList(tagLists[type]);
} }
if(findNodeInList(tagLists[type], string, &node)) { if(findNodeInList(tagLists[type], string, &node, &pos)) {
((TagTrackerItem *)node->data)->count++; ((TagTrackerItem *)node->data)->count++;
} }
else { else {
TagTrackerItem * item = malloc(sizeof(TagTrackerItem)); TagTrackerItem * item = malloc(sizeof(TagTrackerItem));
item->count = 1; item->count = 1;
item->visited = 0; item->visited = 0;
node = insertInListBeforeNode(tagLists[type], node, string, node = insertInListBeforeNode(tagLists[type], node, pos,
item); string, item);
} }
return node->key; return node->key;
...@@ -47,6 +49,7 @@ char * getTagItemString(int type, char * string) { ...@@ -47,6 +49,7 @@ char * getTagItemString(int type, char * string) {
void removeTagItemString(int type, char * string) { void removeTagItemString(int type, char * string) {
ListNode * node; ListNode * node;
int pos;
assert(string); assert(string);
...@@ -58,7 +61,7 @@ void removeTagItemString(int type, char * string) { ...@@ -58,7 +61,7 @@ void removeTagItemString(int type, char * string) {
return; return;
}*/ }*/
if(findNodeInList(tagLists[type], string, &node)) { if(findNodeInList(tagLists[type], string, &node, &pos)) {
TagTrackerItem * item = node->data; TagTrackerItem * item = node->data;
item->count--; item->count--;
if(item->count <= 0) deleteNodeFromList(tagLists[type], node); if(item->count <= 0) deleteNodeFromList(tagLists[type], node);
...@@ -126,23 +129,23 @@ void resetVisitedFlagsInTagTracker(int type) { ...@@ -126,23 +129,23 @@ void resetVisitedFlagsInTagTracker(int type) {
} }
int wasVisitedInTagTracker(int type, char * str) { int wasVisitedInTagTracker(int type, char * str) {
ListNode * node; TagTrackerItem * item;
if(!tagLists[type]) return 0; if(!tagLists[type]) return 0;
if(!findNodeInList(tagLists[type], str, &node)) return 0; if(!findInList(tagLists[type], str, &item)) return 0;
return ((TagTrackerItem *)node->data)->visited; return item->visited;
} }
void visitInTagTracker(int type, char * str) { void visitInTagTracker(int type, char * str) {
ListNode * node; TagTrackerItem * item;
if(!tagLists[type]) return; if(!tagLists[type]) return;
if(!findNodeInList(tagLists[type], str, &node)) return; if(!findInList(tagLists[type], str, &item)) return;
((TagTrackerItem *)node->data)->visited = 1; item->visited = 1;
} }
void printVisitedInTagTracker(FILE * fp, int type) { void printVisitedInTagTracker(FILE * fp, int type) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment