list.c 10.4 KB
Newer Older
Warren Dukes's avatar
Warren Dukes committed
1
/* the Music Player Daemon (MPD)
2
 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
Warren Dukes's avatar
Warren Dukes committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * This project's homepage is: http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "list.h"
20
#include "utils.h"
Warren Dukes's avatar
Warren Dukes committed
21

22 23 24
#include <assert.h>
#include <string.h>

Avuton Olrich's avatar
Avuton Olrich committed
25 26 27
static void makeListNodesArray(List * list)
{
	ListNode *node = list->firstNode;
Warren Dukes's avatar
Warren Dukes committed
28 29
	long i;

Avuton Olrich's avatar
Avuton Olrich committed
30 31
	if (!list->numberOfNodes)
		return;
32

33
	list->nodesArray = xrealloc(list->nodesArray,
Avuton Olrich's avatar
Avuton Olrich committed
34
				   sizeof(ListNode *) * list->numberOfNodes);
Warren Dukes's avatar
Warren Dukes committed
35

Avuton Olrich's avatar
Avuton Olrich committed
36
	for (i = 0; i < list->numberOfNodes; i++) {
Warren Dukes's avatar
Warren Dukes committed
37 38 39 40 41
		list->nodesArray[i] = node;
		node = node->nextNode;
	}
}

Avuton Olrich's avatar
Avuton Olrich committed
42 43 44 45
static void freeListNodesArray(List * list)
{
	if (!list->nodesArray)
		return;
Warren Dukes's avatar
Warren Dukes committed
46 47 48 49
	free(list->nodesArray);
	list->nodesArray = NULL;
}

Avuton Olrich's avatar
Avuton Olrich committed
50 51
List *makeList(ListFreeDataFunc * freeDataFunc, int strdupKeys)
{
52
	List *list = xmalloc(sizeof(List));
Warren Dukes's avatar
Warren Dukes committed
53

Avuton Olrich's avatar
Avuton Olrich committed
54
	assert(list != NULL);
Warren Dukes's avatar
Warren Dukes committed
55

56
	list->sorted = 0;
Warren Dukes's avatar
Warren Dukes committed
57 58 59 60 61
	list->firstNode = NULL;
	list->lastNode = NULL;
	list->freeDataFunc = freeDataFunc;
	list->numberOfNodes = 0;
	list->nodesArray = NULL;
62
	list->strdupKeys = strdupKeys;
Warren Dukes's avatar
Warren Dukes committed
63 64 65 66

	return list;
}

Avuton Olrich's avatar
Avuton Olrich committed
67
ListNode *insertInListBeforeNode(List * list, ListNode * beforeNode, int pos,
Max Kellermann's avatar
Max Kellermann committed
68
				 const char *key, void *data)
69
{
Avuton Olrich's avatar
Avuton Olrich committed
70
	ListNode *node;
71

Avuton Olrich's avatar
Avuton Olrich committed
72 73 74
	assert(list != NULL);
	assert(key != NULL);
	/*assert(data!=NULL); */
75

76
	node = xmalloc(sizeof(ListNode));
Avuton Olrich's avatar
Avuton Olrich committed
77
	assert(node != NULL);
78

79
	node->nextNode = beforeNode;
Avuton Olrich's avatar
Avuton Olrich committed
80 81 82
	if (beforeNode == list->firstNode) {
		if (list->firstNode == NULL) {
			assert(list->lastNode == NULL);
83
			list->lastNode = node;
Avuton Olrich's avatar
Avuton Olrich committed
84 85 86
		} else {
			assert(list->lastNode != NULL);
			assert(list->lastNode->nextNode == NULL);
87 88 89 90
			list->firstNode->prevNode = node;
		}
		node->prevNode = NULL;
		list->firstNode = node;
Avuton Olrich's avatar
Avuton Olrich committed
91 92
	} else {
		if (beforeNode) {
93 94
			node->prevNode = beforeNode->prevNode;
			beforeNode->prevNode = node;
Avuton Olrich's avatar
Avuton Olrich committed
95
		} else {
96 97 98 99 100
			node->prevNode = list->lastNode;
			list->lastNode = node;
		}
		node->prevNode->nextNode = node;
	}
101

Avuton Olrich's avatar
Avuton Olrich committed
102
	if (list->strdupKeys)
103
		node->key = xstrdup(key);
Avuton Olrich's avatar
Avuton Olrich committed
104 105
	else
		node->key = key;
106

107
	node->data = data;
108

109
	list->numberOfNodes++;
Avuton Olrich's avatar
Avuton Olrich committed
110 111

	if (list->sorted) {
112
		list->nodesArray = xrealloc(list->nodesArray,
Avuton Olrich's avatar
Avuton Olrich committed
113 114 115 116 117 118
					   list->numberOfNodes *
					   sizeof(ListNode *));
		if (node == list->lastNode) {
			list->nodesArray[list->numberOfNodes - 1] = node;
		} else if (pos < 0)
			makeListNodesArray(list);
119
		else {
Avuton Olrich's avatar
Avuton Olrich committed
120 121 122 123
			memmove(list->nodesArray + pos + 1,
				list->nodesArray + pos,
				sizeof(ListNode *) * (list->numberOfNodes -
						      pos - 1));
124 125 126
			list->nodesArray[pos] = node;
		}
	}
127 128

	return node;
129 130
}

Max Kellermann's avatar
Max Kellermann committed
131
ListNode *insertInList(List * list, const char *key, void *data)
Avuton Olrich's avatar
Avuton Olrich committed
132 133
{
	ListNode *node;
Warren Dukes's avatar
Warren Dukes committed
134

Avuton Olrich's avatar
Avuton Olrich committed
135 136 137
	assert(list != NULL);
	assert(key != NULL);
	/*assert(data!=NULL); */
Warren Dukes's avatar
Warren Dukes committed
138

139
	node = xmalloc(sizeof(ListNode));
Avuton Olrich's avatar
Avuton Olrich committed
140
	assert(node != NULL);
Warren Dukes's avatar
Warren Dukes committed
141

Avuton Olrich's avatar
Avuton Olrich committed
142 143
	if (list->nodesArray)
		freeListNodesArray(list);
Warren Dukes's avatar
Warren Dukes committed
144

Avuton Olrich's avatar
Avuton Olrich committed
145 146
	if (list->firstNode == NULL) {
		assert(list->lastNode == NULL);
Warren Dukes's avatar
Warren Dukes committed
147
		list->firstNode = node;
Avuton Olrich's avatar
Avuton Olrich committed
148 149 150
	} else {
		assert(list->lastNode != NULL);
		assert(list->lastNode->nextNode == NULL);
Warren Dukes's avatar
Warren Dukes committed
151 152
		list->lastNode->nextNode = node;
	}
153

Avuton Olrich's avatar
Avuton Olrich committed
154
	if (list->strdupKeys)
155
		node->key = xstrdup(key);
Avuton Olrich's avatar
Avuton Olrich committed
156 157
	else
		node->key = key;
158

Warren Dukes's avatar
Warren Dukes committed
159 160 161 162 163 164 165
	node->data = data;
	node->nextNode = NULL;
	node->prevNode = list->lastNode;

	list->lastNode = node;

	list->numberOfNodes++;
Avuton Olrich's avatar
Avuton Olrich committed
166

167
	return node;
Warren Dukes's avatar
Warren Dukes committed
168 169
}

Avuton Olrich's avatar
Avuton Olrich committed
170 171 172
int insertInListWithoutKey(List * list, void *data)
{
	ListNode *node;
Warren Dukes's avatar
Warren Dukes committed
173

Avuton Olrich's avatar
Avuton Olrich committed
174 175
	assert(list != NULL);
	assert(data != NULL);
Warren Dukes's avatar
Warren Dukes committed
176

177
	node = xmalloc(sizeof(ListNode));
Avuton Olrich's avatar
Avuton Olrich committed
178 179 180 181
	assert(node != NULL);

	if (list->nodesArray)
		freeListNodesArray(list);
Warren Dukes's avatar
Warren Dukes committed
182

Avuton Olrich's avatar
Avuton Olrich committed
183 184
	if (list->firstNode == NULL) {
		assert(list->lastNode == NULL);
Warren Dukes's avatar
Warren Dukes committed
185
		list->firstNode = node;
Avuton Olrich's avatar
Avuton Olrich committed
186 187 188
	} else {
		assert(list->lastNode != NULL);
		assert(list->lastNode->nextNode == NULL);
Warren Dukes's avatar
Warren Dukes committed
189 190 191 192 193 194 195 196 197 198 199
		list->lastNode->nextNode = node;
	}

	node->key = NULL;
	node->data = data;
	node->nextNode = NULL;
	node->prevNode = list->lastNode;

	list->lastNode = node;

	list->numberOfNodes++;
Avuton Olrich's avatar
Avuton Olrich committed
200

Warren Dukes's avatar
Warren Dukes committed
201 202 203
	return 1;
}

204 205
/* if _key_ is not found, *_node_ is assigned to the node before which
	the info would be found */
Max Kellermann's avatar
Max Kellermann committed
206
int findNodeInList(List * list, const char *key, ListNode ** node, int *pos)
Avuton Olrich's avatar
Avuton Olrich committed
207
{
208 209 210
	long high;
	long low;
	long cur;
Avuton Olrich's avatar
Avuton Olrich committed
211
	ListNode *tmpNode;
212
	int cmp;
Warren Dukes's avatar
Warren Dukes committed
213

Avuton Olrich's avatar
Avuton Olrich committed
214
	assert(list != NULL);
Warren Dukes's avatar
Warren Dukes committed
215

Avuton Olrich's avatar
Avuton Olrich committed
216 217
	if (list->sorted && list->nodesArray) {
		high = list->numberOfNodes - 1;
Warren Dukes's avatar
Warren Dukes committed
218 219 220
		low = 0;
		cur = high;

Avuton Olrich's avatar
Avuton Olrich committed
221 222
		while (high > low) {
			cur = (high + low) / 2;
Warren Dukes's avatar
Warren Dukes committed
223
			tmpNode = list->nodesArray[cur];
Avuton Olrich's avatar
Avuton Olrich committed
224 225
			cmp = strcmp(tmpNode->key, key);
			if (cmp == 0) {
226
				*node = tmpNode;
227
				*pos = cur;
228
				return 1;
Avuton Olrich's avatar
Avuton Olrich committed
229 230
			} else if (cmp > 0)
				high = cur;
Warren Dukes's avatar
Warren Dukes committed
231
			else {
Avuton Olrich's avatar
Avuton Olrich committed
232 233
				if (low == cur)
					break;
Warren Dukes's avatar
Warren Dukes committed
234 235 236 237 238
				low = cur;
			}
		}

		cur = high;
Avuton Olrich's avatar
Avuton Olrich committed
239
		if (cur >= 0) {
Warren Dukes's avatar
Warren Dukes committed
240
			tmpNode = list->nodesArray[cur];
241
			*node = tmpNode;
242
			*pos = high;
Avuton Olrich's avatar
Avuton Olrich committed
243 244 245 246 247
			cmp = tmpNode ? strcmp(tmpNode->key, key) : -1;
			if (0 == cmp)
				return 1;
			else if (cmp > 0)
				return 0;
248
			else {
249
				*pos = -1;
250 251 252
				*node = NULL;
				return 0;
			}
Avuton Olrich's avatar
Avuton Olrich committed
253
		} else {
254
			*pos = 0;
255 256
			*node = list->firstNode;
			return 0;
Warren Dukes's avatar
Warren Dukes committed
257
		}
Avuton Olrich's avatar
Avuton Olrich committed
258
	} else {
Warren Dukes's avatar
Warren Dukes committed
259
		tmpNode = list->firstNode;
Avuton Olrich's avatar
Avuton Olrich committed
260 261

		while (tmpNode != NULL && strcmp(tmpNode->key, key) != 0) {
Warren Dukes's avatar
Warren Dukes committed
262 263
			tmpNode = tmpNode->nextNode;
		}
Avuton Olrich's avatar
Avuton Olrich committed
264 265 266 267

		*node = tmpNode;
		if (tmpNode)
			return 1;
268 269
	}

270
	return 0;
271 272
}

Max Kellermann's avatar
Max Kellermann committed
273
int findInList(List * list, const char *key, void **data)
Avuton Olrich's avatar
Avuton Olrich committed
274 275
{
	ListNode *node;
276
	int pos;
Avuton Olrich's avatar
Avuton Olrich committed
277 278 279 280

	if (findNodeInList(list, key, &node, &pos)) {
		if (data)
			*data = node->data;
281
		return 1;
Warren Dukes's avatar
Warren Dukes committed
282 283 284 285 286
	}

	return 0;
}

Max Kellermann's avatar
Max Kellermann committed
287
int deleteFromList(List * list, const char *key)
Avuton Olrich's avatar
Avuton Olrich committed
288 289
{
	ListNode *tmpNode;
Warren Dukes's avatar
Warren Dukes committed
290

Avuton Olrich's avatar
Avuton Olrich committed
291
	assert(list != NULL);
Warren Dukes's avatar
Warren Dukes committed
292 293 294

	tmpNode = list->firstNode;

Avuton Olrich's avatar
Avuton Olrich committed
295
	while (tmpNode != NULL && strcmp(tmpNode->key, key) != 0) {
Warren Dukes's avatar
Warren Dukes committed
296 297 298
		tmpNode = tmpNode->nextNode;
	}

Avuton Olrich's avatar
Avuton Olrich committed
299 300
	if (tmpNode != NULL)
		deleteNodeFromList(list, tmpNode);
Warren Dukes's avatar
Warren Dukes committed
301 302 303 304 305 306
	else
		return 0;

	return 1;
}

Avuton Olrich's avatar
Avuton Olrich committed
307 308 309 310 311 312
void deleteNodeFromList(List * list, ListNode * node)
{
	assert(list != NULL);
	assert(node != NULL);

	if (node->prevNode == NULL) {
Warren Dukes's avatar
Warren Dukes committed
313
		list->firstNode = node->nextNode;
Avuton Olrich's avatar
Avuton Olrich committed
314
	} else {
Warren Dukes's avatar
Warren Dukes committed
315 316
		node->prevNode->nextNode = node->nextNode;
	}
Avuton Olrich's avatar
Avuton Olrich committed
317
	if (node->nextNode == NULL) {
Warren Dukes's avatar
Warren Dukes committed
318
		list->lastNode = node->prevNode;
Avuton Olrich's avatar
Avuton Olrich committed
319
	} else {
Warren Dukes's avatar
Warren Dukes committed
320 321
		node->nextNode->prevNode = node->prevNode;
	}
Avuton Olrich's avatar
Avuton Olrich committed
322
	if (list->freeDataFunc) {
Warren Dukes's avatar
Warren Dukes committed
323 324
		list->freeDataFunc(node->data);
	}
325

Avuton Olrich's avatar
Avuton Olrich committed
326
	if (list->strdupKeys)
327
		xfree(node->key);
Warren Dukes's avatar
Warren Dukes committed
328 329 330
	free(node);
	list->numberOfNodes--;

Avuton Olrich's avatar
Avuton Olrich committed
331
	if (list->nodesArray) {
Warren Dukes's avatar
Warren Dukes committed
332
		freeListNodesArray(list);
Avuton Olrich's avatar
Avuton Olrich committed
333 334
		if (list->sorted)
			makeListNodesArray(list);
Warren Dukes's avatar
Warren Dukes committed
335 336 337 338
	}

}

Avuton Olrich's avatar
Avuton Olrich committed
339 340 341 342 343 344
void freeList(void *list)
{
	ListNode *tmpNode;
	ListNode *tmpNode2;

	assert(list != NULL);
Warren Dukes's avatar
Warren Dukes committed
345

Avuton Olrich's avatar
Avuton Olrich committed
346
	tmpNode = ((List *) list)->firstNode;
Warren Dukes's avatar
Warren Dukes committed
347

Avuton Olrich's avatar
Avuton Olrich committed
348 349
	if (((List *) list)->nodesArray)
		free(((List *) list)->nodesArray);
Warren Dukes's avatar
Warren Dukes committed
350

Avuton Olrich's avatar
Avuton Olrich committed
351
	while (tmpNode != NULL) {
Warren Dukes's avatar
Warren Dukes committed
352
		tmpNode2 = tmpNode->nextNode;
Avuton Olrich's avatar
Avuton Olrich committed
353
		if (((List *) list)->strdupKeys)
354
			xfree(tmpNode->key);
Avuton Olrich's avatar
Avuton Olrich committed
355 356
		if (((List *) list)->freeDataFunc) {
			((List *) list)->freeDataFunc(tmpNode->data);
Warren Dukes's avatar
Warren Dukes committed
357 358 359 360 361 362 363 364
		}
		free(tmpNode);
		tmpNode = tmpNode2;
	}

	free(list);
}

Avuton Olrich's avatar
Avuton Olrich committed
365 366
static void swapNodes(ListNode * nodeA, ListNode * nodeB)
{
Max Kellermann's avatar
Max Kellermann committed
367
	const char *key;
Avuton Olrich's avatar
Avuton Olrich committed
368 369 370 371
	void *data;

	assert(nodeA != NULL);
	assert(nodeB != NULL);
Warren Dukes's avatar
Warren Dukes committed
372 373 374

	key = nodeB->key;
	data = nodeB->data;
Avuton Olrich's avatar
Avuton Olrich committed
375

Warren Dukes's avatar
Warren Dukes committed
376 377 378 379 380 381 382
	nodeB->key = nodeA->key;
	nodeB->data = nodeA->data;

	nodeA->key = key;
	nodeA->data = data;
}

Avuton Olrich's avatar
Avuton Olrich committed
383 384
static void bubbleSort(ListNode ** nodesArray, long start, long end)
{
Warren Dukes's avatar
Warren Dukes committed
385 386
	long i;
	long j;
Avuton Olrich's avatar
Avuton Olrich committed
387
	ListNode *node;
Warren Dukes's avatar
Warren Dukes committed
388

Avuton Olrich's avatar
Avuton Olrich committed
389 390
	if (start >= end)
		return;
Warren Dukes's avatar
Warren Dukes committed
391

Avuton Olrich's avatar
Avuton Olrich committed
392 393
	for (j = start; j < end; j++) {
		for (i = end - 1; i >= start; i--) {
Warren Dukes's avatar
Warren Dukes committed
394
			node = nodesArray[i];
Avuton Olrich's avatar
Avuton Olrich committed
395 396
			if (strcmp(node->key, node->nextNode->key) > 0) {
				swapNodes(node, node->nextNode);
Warren Dukes's avatar
Warren Dukes committed
397 398 399 400 401
			}
		}
	}
}

Avuton Olrich's avatar
Avuton Olrich committed
402 403 404 405 406 407
static void quickSort(ListNode ** nodesArray, long start, long end)
{
	if (start >= end)
		return;
	else if (end - start < 5)
		bubbleSort(nodesArray, start, end);
Warren Dukes's avatar
Warren Dukes committed
408 409
	else {
		long i;
Avuton Olrich's avatar
Avuton Olrich committed
410
		ListNode *node;
Warren Dukes's avatar
Warren Dukes committed
411
		long pivot;
Avuton Olrich's avatar
Avuton Olrich committed
412
		ListNode *pivotNode;
Max Kellermann's avatar
Max Kellermann committed
413
		const char *pivotKey;
Warren Dukes's avatar
Warren Dukes committed
414

Avuton Olrich's avatar
Avuton Olrich committed
415 416
		List *startList = makeList(free, 0);
		List *endList = makeList(free, 0);
417 418
		long *startPtr = xmalloc(sizeof(long));
		long *endPtr = xmalloc(sizeof(long));
Warren Dukes's avatar
Warren Dukes committed
419 420
		*startPtr = start;
		*endPtr = end;
Avuton Olrich's avatar
Avuton Olrich committed
421 422
		insertInListWithoutKey(startList, (void *)startPtr);
		insertInListWithoutKey(endList, (void *)endPtr);
Warren Dukes's avatar
Warren Dukes committed
423

Avuton Olrich's avatar
Avuton Olrich committed
424
		while (startList->numberOfNodes) {
Warren Dukes's avatar
Warren Dukes committed
425 426 427
			start = *((long *)startList->lastNode->data);
			end = *((long *)endList->lastNode->data);

Avuton Olrich's avatar
Avuton Olrich committed
428 429 430 431 432 433 434
			if (end - start < 5) {
				bubbleSort(nodesArray, start, end);
				deleteNodeFromList(startList,
						   startList->lastNode);
				deleteNodeFromList(endList, endList->lastNode);
			} else {
				pivot = (start + end) / 2;
Warren Dukes's avatar
Warren Dukes committed
435 436 437
				pivotNode = nodesArray[pivot];
				pivotKey = pivotNode->key;

Avuton Olrich's avatar
Avuton Olrich committed
438
				for (i = pivot - 1; i >= start; i--) {
Warren Dukes's avatar
Warren Dukes committed
439
					node = nodesArray[i];
Avuton Olrich's avatar
Avuton Olrich committed
440
					if (strcmp(node->key, pivotKey) > 0) {
Warren Dukes's avatar
Warren Dukes committed
441
						pivot--;
Avuton Olrich's avatar
Avuton Olrich committed
442 443 444 445
						if (pivot > i) {
							swapNodes(node,
								  nodesArray
								  [pivot]);
Warren Dukes's avatar
Warren Dukes committed
446
						}
Avuton Olrich's avatar
Avuton Olrich committed
447 448
						swapNodes(pivotNode,
							  nodesArray[pivot]);
Warren Dukes's avatar
Warren Dukes committed
449 450 451
						pivotNode = nodesArray[pivot];
					}
				}
Avuton Olrich's avatar
Avuton Olrich committed
452
				for (i = pivot + 1; i <= end; i++) {
Warren Dukes's avatar
Warren Dukes committed
453
					node = nodesArray[i];
Avuton Olrich's avatar
Avuton Olrich committed
454
					if (strcmp(pivotKey, node->key) > 0) {
Warren Dukes's avatar
Warren Dukes committed
455
						pivot++;
Avuton Olrich's avatar
Avuton Olrich committed
456 457 458 459
						if (pivot < i) {
							swapNodes(node,
								  nodesArray
								  [pivot]);
Warren Dukes's avatar
Warren Dukes committed
460
						}
Avuton Olrich's avatar
Avuton Olrich committed
461 462
						swapNodes(pivotNode,
							  nodesArray[pivot]);
Warren Dukes's avatar
Warren Dukes committed
463 464 465 466
						pivotNode = nodesArray[pivot];
					}
				}

Avuton Olrich's avatar
Avuton Olrich committed
467 468 469
				deleteNodeFromList(startList,
						   startList->lastNode);
				deleteNodeFromList(endList, endList->lastNode);
Warren Dukes's avatar
Warren Dukes committed
470

Avuton Olrich's avatar
Avuton Olrich committed
471
				if (pivot - 1 - start > 0) {
472 473
					startPtr = xmalloc(sizeof(long));
					endPtr = xmalloc(sizeof(long));
Warren Dukes's avatar
Warren Dukes committed
474
					*startPtr = start;
Avuton Olrich's avatar
Avuton Olrich committed
475 476 477 478 479 480
					*endPtr = pivot - 1;
					insertInListWithoutKey(startList,
							       (void *)
							       startPtr);
					insertInListWithoutKey(endList,
							       (void *)endPtr);
Warren Dukes's avatar
Warren Dukes committed
481 482
				}

Avuton Olrich's avatar
Avuton Olrich committed
483
				if (end - pivot - 1 > 0) {
484 485
					startPtr = xmalloc(sizeof(long));
					endPtr = xmalloc(sizeof(long));
Avuton Olrich's avatar
Avuton Olrich committed
486
					*startPtr = pivot + 1;
Warren Dukes's avatar
Warren Dukes committed
487
					*endPtr = end;
Avuton Olrich's avatar
Avuton Olrich committed
488 489 490 491 492
					insertInListWithoutKey(startList,
							       (void *)
							       startPtr);
					insertInListWithoutKey(endList,
							       (void *)endPtr);
Warren Dukes's avatar
Warren Dukes committed
493 494 495 496 497 498 499 500 501
				}
			}
		}

		freeList(startList);
		freeList(endList);
	}
}

Avuton Olrich's avatar
Avuton Olrich committed
502 503 504
void sortList(List * list)
{
	assert(list != NULL);
Warren Dukes's avatar
Warren Dukes committed
505

506 507
	list->sorted = 1;

Avuton Olrich's avatar
Avuton Olrich committed
508 509 510 511 512
	if (list->numberOfNodes < 2)
		return;

	if (list->nodesArray)
		freeListNodesArray(list);
Warren Dukes's avatar
Warren Dukes committed
513 514
	makeListNodesArray(list);

Avuton Olrich's avatar
Avuton Olrich committed
515
	quickSort(list->nodesArray, 0, list->numberOfNodes - 1);
Warren Dukes's avatar
Warren Dukes committed
516
}