add ordering capability to the list

git-svn-id: svn://10.0.0.236/trunk@105418 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
ian.mcgreer%sun.com
2001-10-15 16:50:01 +00:00
parent af22d378ae
commit 2c439f174f
2 changed files with 36 additions and 3 deletions

View File

@@ -35,7 +35,7 @@
#define BASET_H
#ifdef DEBUG
static const char BASET_CVS_ID[] = "@(#) $RCSfile: baset.h,v $ $Revision: 1.5 $ $Date: 2001-09-20 20:33:26 $ $Name: not supported by cvs2svn $";
static const char BASET_CVS_ID[] = "@(#) $RCSfile: baset.h,v $ $Revision: 1.6 $ $Date: 2001-10-15 16:50:01 $ $Name: not supported by cvs2svn $";
#endif /* DEBUG */
/*
@@ -102,6 +102,7 @@ typedef struct nssArenaMarkStr nssArenaMark;
typedef struct nssListStr nssList;
typedef struct nssListIteratorStr nssListIterator;
typedef PRBool (* nssListCompareFunc)(void *a, void *b);
typedef PRIntn (* nssListSortFunc)(void *a, void *b);
typedef void (* nssListElementDestructorFunc)(void *el);
typedef struct nssHashStr nssHash;

View File

@@ -32,7 +32,7 @@
*/
#ifdef DEBUG
static const char CVS_ID[] = "@(#) $RCSfile: list.c,v $ $Revision: 1.3 $ $Date: 2001-10-08 19:26:02 $ $Name: not supported by cvs2svn $";
static const char CVS_ID[] = "@(#) $RCSfile: list.c,v $ $Revision: 1.4 $ $Date: 2001-10-15 16:50:01 $ $Name: not supported by cvs2svn $";
#endif /* DEBUG */
/*
@@ -58,6 +58,7 @@ struct nssListStr {
nssListElement *head;
PRUint32 count;
nssListCompareFunc compareFunc;
nssListSortFunc sortFunc;
};
struct nssListIteratorStr {
@@ -145,6 +146,13 @@ nssList_SetCompareFunction(nssList *list, nssListCompareFunc compareFunc)
list->compareFunc = compareFunc;
}
NSS_IMPLEMENT void
nssList_SetSortFunction(nssList *list, nssListSortFunc sortFunc)
{
/* XXX if list already has elements, sort them */
list->sortFunc = sortFunc;
}
NSS_IMPLEMENT nssListCompareFunc
nssList_GetCompareFunction(nssList *list)
{
@@ -176,7 +184,31 @@ nsslist_add_element(nssList *list, void *data)
PR_INIT_CLIST(&node->link);
node->data = data;
if (list->head) {
PR_APPEND_LINK(&node->link, &list->head->link);
if (list->sortFunc) {
PRCList *link;
nssListElement *currNode;
currNode = list->head;
/* insert in ordered list */
while (currNode) {
link = &currNode->link;
if (list->sortFunc(data, currNode->data) <= 0) {
/* new element goes before current node */
PR_INSERT_BEFORE(&node->link, link);
/* reset head if this is first */
if (currNode == list->head) list->head = node;
break;
}
if (link == PR_LIST_TAIL(&list->head->link)) {
/* reached end of list, append */
PR_INSERT_AFTER(&node->link, link);
break;
}
currNode = (nssListElement *)PR_NEXT_LINK(&currNode->link);
}
} else {
/* not sorting */
PR_APPEND_LINK(&node->link, &list->head->link);
}
} else {
list->head = node;
}