소스 검색

Since we anticipate repeated comparisons, add in a test for sorted lists, which can reduce
cost for sorted objects to O(N)


git-svn-id: svn://svn.code.sf.net/p/cjson/code@72 e3330c51-1366-4df0-8b21-3ccf24e3d50e

Dave Gamble 10 년 전
부모
커밋
174c62902c
1개의 변경된 파일3개의 추가작업 그리고 0개의 파일을 삭제
  1. 3 0
      cJSON_Utils.c

+ 3 - 0
cJSON_Utils.c

@@ -304,6 +304,9 @@ static cJSON *cJSONUtils_SortList(cJSON *list)
 	cJSON *first=list,*second=list,*ptr=list;
 
 	if (!list || !list->next) return list;	/* One entry is sorted already. */
+	
+	while (ptr && ptr->next && cJSONUtils_strcasecmp(ptr->string,ptr->next->string)<0) ptr=ptr->next;	/* Test for list sorted. */
+	if (!ptr || !ptr->next) return list;	/* Leave sorted lists unmodified. */
 
 	while (ptr) {second=second->next;ptr=ptr->next;if (ptr) ptr=ptr->next;}	/* Walk two pointers to find the middle. */
 	if (second && second->prev) second->prev->next=0;	/* Split the lists */