summaryrefslogtreecommitdiff
path: root/test/lib/list.c
blob: e72b50c69fb1229fceaaa32df63054e55dba1412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * This file is part of libdom test suite.
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 * Copyright 2007 James Shaw <jshaw@netsurf-browser.org>
 */


#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "comparators.h"
#include "list.h"
#include "testassert.h"

/**
 * Private helper function.
 * Create a new list_elt and initialise it.
 */
struct list_elt* list_new_elt(void* data);

struct list_elt* list_new_elt(void* data) {
	struct list_elt* elt = malloc(sizeof(struct list_elt));
	assert(elt != NULL);
	elt->data = data;
	elt->next = NULL;
	return elt;
}

struct list* list_new(void)
{
	struct list* list = malloc(sizeof(struct list));
	assert(list != NULL);
	list->size = 0;
	list->head = NULL;
	list->tail = NULL;
	return list;
}

void list_destroy(struct list* list)
{
	struct list_elt* elt = list->head;
	while (elt != NULL) {
		struct list_elt* nextElt = elt->next;
		free(elt);
		elt = nextElt;
	}
	free(list);
}

void list_add(struct list* list, void* data)
{
	struct list_elt* elt = list_new_elt(data);
	struct list_elt* tail = list->tail;

	/* if tail was set, make its 'next' ptr point to elt */
	if (tail != NULL) {
		tail->next = elt;
	}

	/* make elt the new tail */
	list->tail = elt;

	if (list->head == NULL) {
		list->head = elt;
	}

	/* inc the size of the list */
	list->size++;
}

bool list_remove(struct list* list, void* data)
{	
	struct list_elt* prevElt = NULL;
	struct list_elt* elt = list->head;
	
	bool found = false;
	
	while (elt != NULL) {
		struct list_elt* nextElt = elt->next;
		
		/* if data is identical, fix up pointers, and free the element */
		if (data == elt->data) {
			if (prevElt == NULL) {
				list->head = nextElt;
			} else {
				prevElt->next = nextElt;
			}
			free(elt);
			list->size--;
			found = true;
			break;
		}
		
		prevElt = elt;
		elt = nextElt;
	}
	
	return found;
}

struct list* list_clone(struct list* list)
{
	struct list* newList = list_new();
	struct list_elt* elt = list->head;
	
	while (elt != NULL) {
		list_add(newList, elt->data);
		elt = elt->next;
	}
	
	return newList;
}

bool list_contains(struct list* list, void* data, comparator comparator)
{
	struct list_elt* elt = list->head;
	while (elt != NULL) {
		if (comparator(elt->data, data) == 0) {
			return true;
		}
		elt = elt->next;
	}
	return false;
}

bool list_contains_all(struct list* superList, struct list* subList, 
		comparator comparator)
{
	struct list_elt* subElt = subList->head;
	struct list* superListClone = list_clone(superList);
	
	bool found = true;
	
	while (subElt != NULL) {
		struct list_elt* superElt = superListClone->head;

		found = false;
		while (superElt != NULL && found == false) {
			if (comparator(subElt->data, superElt->data) == 0) {
				found = true;
				list_remove(superListClone, superElt->data);
			}
			superElt = superElt->next;
		}
		
		subElt = subElt->next;
	}
	free(superListClone);
	
	return found;
}