mirror of
https://github.com/DaveGamble/cJSON.git
synced 2025-09-15 12:58:50 +08:00
reformatting: cJSONUtils_SortList
This commit is contained in:
parent
ec9d1cfedb
commit
8964287ec4
128
cJSON_Utils.c
128
cJSON_Utils.c
@ -611,44 +611,114 @@ cJSON* cJSONUtils_GeneratePatches(cJSON *from, cJSON *to)
|
|||||||
return patches;
|
return patches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* sort lists using mergesort */
|
||||||
static cJSON *cJSONUtils_SortList(cJSON *list)
|
static cJSON *cJSONUtils_SortList(cJSON *list)
|
||||||
{
|
{
|
||||||
cJSON *first=list,*second=list,*ptr=list;
|
cJSON *first = list;
|
||||||
|
cJSON *second = list;
|
||||||
|
cJSON *ptr = list;
|
||||||
|
|
||||||
if (!list || !list->next) return list; /* One entry is sorted already. */
|
if (!list || !list->next)
|
||||||
|
{
|
||||||
while (ptr && ptr->next && cJSONUtils_strcasecmp(ptr->string,ptr->next->string)<0) ptr=ptr->next; /* Test for list sorted. */
|
/* One entry is sorted already. */
|
||||||
if (!ptr || !ptr->next) return list; /* Leave sorted lists unmodified. */
|
return list;
|
||||||
ptr=list;
|
}
|
||||||
|
|
||||||
while (ptr) {second=second->next;ptr=ptr->next;if (ptr) ptr=ptr->next;} /* Walk two pointers to find the middle. */
|
while (ptr && ptr->next && (cJSONUtils_strcasecmp(ptr->string, ptr->next->string) < 0))
|
||||||
if (second && second->prev) second->prev->next=0; /* Split the lists */
|
{
|
||||||
|
/* Test for list sorted. */
|
||||||
|
ptr = ptr->next;
|
||||||
|
}
|
||||||
|
if (!ptr || !ptr->next)
|
||||||
|
{
|
||||||
|
/* Leave sorted lists unmodified. */
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
first=cJSONUtils_SortList(first); /* Recursively sort the sub-lists. */
|
/* reset ptr to the beginning */
|
||||||
second=cJSONUtils_SortList(second);
|
ptr = list;
|
||||||
list=ptr=0;
|
while (ptr)
|
||||||
|
{
|
||||||
|
/* Walk two pointers to find the middle. */
|
||||||
|
second = second->next;
|
||||||
|
ptr = ptr->next;
|
||||||
|
/* advances ptr two steps at a time */
|
||||||
|
if (ptr)
|
||||||
|
{
|
||||||
|
ptr = ptr->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (second && second->prev)
|
||||||
|
{
|
||||||
|
/* Split the lists */
|
||||||
|
second->prev->next = 0;
|
||||||
|
}
|
||||||
|
|
||||||
while (first && second) /* Merge the sub-lists */
|
/* Recursively sort the sub-lists. */
|
||||||
{
|
first = cJSONUtils_SortList(first);
|
||||||
if (cJSONUtils_strcasecmp(first->string,second->string)<0)
|
second = cJSONUtils_SortList(second);
|
||||||
{
|
list = ptr = 0;
|
||||||
if (!list) list=ptr=first;
|
|
||||||
else {ptr->next=first;first->prev=ptr;ptr=first;}
|
|
||||||
first=first->next;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!list) list=ptr=second;
|
|
||||||
else {ptr->next=second;second->prev=ptr;ptr=second;}
|
|
||||||
second=second->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (first) { if (!list) return first; ptr->next=first; first->prev=ptr; } /* Append any tails. */
|
|
||||||
if (second) { if (!list) return second; ptr->next=second; second->prev=ptr; }
|
|
||||||
|
|
||||||
return list;
|
while (first && second) /* Merge the sub-lists */
|
||||||
|
{
|
||||||
|
if (cJSONUtils_strcasecmp(first->string, second->string) < 0)
|
||||||
|
{
|
||||||
|
if (!list)
|
||||||
|
{
|
||||||
|
/* start merged list with the first element of the first list */
|
||||||
|
list = ptr = first;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* add first element of first list to merged list */
|
||||||
|
ptr->next = first;
|
||||||
|
first->prev = ptr;
|
||||||
|
ptr = first;
|
||||||
|
}
|
||||||
|
first = first->next;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!list)
|
||||||
|
{
|
||||||
|
/* start merged list with the first element of the second list */
|
||||||
|
list = ptr = second;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* add first element of second list to merged list */
|
||||||
|
ptr->next = second;
|
||||||
|
second->prev = ptr;
|
||||||
|
ptr = second;
|
||||||
|
}
|
||||||
|
second = second->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (first)
|
||||||
|
{
|
||||||
|
/* Append rest of first list. */
|
||||||
|
if (!list)
|
||||||
|
{
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
ptr->next = first;
|
||||||
|
first->prev = ptr;
|
||||||
|
}
|
||||||
|
if (second)
|
||||||
|
{
|
||||||
|
/* Append rest of second list */
|
||||||
|
if (!list)
|
||||||
|
{
|
||||||
|
return second;
|
||||||
|
}
|
||||||
|
ptr->next = second;
|
||||||
|
second->prev = ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void cJSONUtils_SortObject(cJSON *object) {object->child=cJSONUtils_SortList(object->child);}
|
void cJSONUtils_SortObject(cJSON *object) {object->child=cJSONUtils_SortList(object->child);}
|
||||||
|
|
||||||
cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
|
cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user