aboutsummaryrefslogtreecommitdiff
path: root/src/linked_list.inc.h
blob: 3984e485ec3bb5818fecdca9e80cb6aa678813de (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef TYPE
#error "Set TYPE before including self file"
#else

INIT_F ( LLIST(TYPE) ) {
	self->length = 0;
	NEW(LINK(TYPE), head);
	NEW(LINK(TYPE), tail);
	self->head   = head;
	self->tail   = tail;
	head->after  = tail;
	tail->before = head;
	self->cur    = head;
	self->cval   = head->value;
	return 0;
}

FREE_F (LINK(TYPE)) {
	UNLINK(LINK(TYPE))(self);

	if (self->value != NULL) FFREE(TYPE, self->value);
	return 0;
}

FREE_F( LLIST(TYPE) ) {
	LINK(TYPE) *n, *next;
	n = self->head;
	while ( n != NULL ) {
		next = n->after;
		FFREE(LINK(TYPE), n);
		n = next;
	}

	self->length = -1;

	return 0;
}

INIT_F ( LINK(TYPE) ) {
	self->before = NULL;
	self->after  = NULL;
	self->value  = NULL;
	return 0;
}

INIT_F ( LINK(TYPE), TYPE* val ) {
	self->before = NULL;
	self->after  = NULL;
	self->value  = val;
	return 0;
}

int UNLINK(LINK(TYPE)) ( LINK(TYPE)* self ) {
	if (self->before != NULL) self->before->after = self->after;
	if (self->after  != NULL) self->after->before = self->before;
	return 0;
}
	 

int PUSH(LLIST(TYPE)) ( LLIST(TYPE)* lst, TYPE* val) {
	NEW(LINK(TYPE), link, val);

	link->after         = FIRST(lst);
	FIRST(lst)          = link;

	link->after->before = link;
	link->before        = lst->head;

	++lst->length;

	// TODO do I want to change that?
	lst->cur = link;
	lst->cval = link->value;

	return 0;
}

TYPE* PEEK(LLIST(TYPE)) ( LLIST(TYPE)* lst ) {
	if (EMPTY(LLIST(TYPE))(lst)) return NULL;

	return FIRST(lst)->value;
}

TYPE* POP(LLIST(TYPE)) ( LLIST(TYPE)* lst) {
	if (EMPTY(LLIST(TYPE))(lst)) return NULL;

	LINK(TYPE)* frst = FIRST(lst);
	UNLINK(LINK(TYPE))(frst);

	TYPE* retval = frst->value;
	--lst->length;
	free (frst);
	return retval;
}

int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) {
	LINK(TYPE)* n = FIRST(src);

	while (n->after != NULL) {
		NEW(TYPE, cpy);
		DEEP_COPY(TYPE)(cpy, n->value);
		PUSH(LLIST(TYPE)) ( dest, cpy );
		n = n->after;
	}

	return 0;
}

/*
 * Adds two linked lists together.
 * O(1) time.
 * destroys new__ in the process, but keeps the elements.
 * make sure to free(new__) after.
 */
int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new__ ) {

	/* Link end of dest onto start of new__. */
	LAST(dest)->after  = FIRST(new__);
	FIRST(new__)->before = LAST(dest);

	/* Free the two now not needed end links.  */
	free(new__->head);
	free(dest->tail);

	/* Update dest with new__ tail ptr. */
	dest->tail = new__->tail;

	dest->length += new__->length;

	return 0;
}

int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {
	return llist->length;
}

int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {
	return FIRST(llist) == llist->tail;
}

LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new__) {
	if (dest == NULL) return new__;
	APPEND(LLIST(TYPE))(dest, new__);
	free(new__);
	return dest;
}

int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {

	LINK(TYPE) *link = FIRST(llist), *next;
	/*
	 * Manual looping rather than itterators since we destroyed the
	 * loop variable.
	 */
	while (link != llist->tail) {
		next = link->after;
		FFREE(LINK(TYPE), link);
		link = next;
	}

	llist->cur = llist->head;
	llist->cval = llist->head->value;

	return 0;
}

FMT_F(LLIST(TYPE)) {
	int seek = 0;
	fmtf("(");
	FOR(LLIST, TYPE, v, self) {
		seek += FMT(TYPE)(v, buf + seek);
		fmtf(" ");
	}
	fmtf(")");

	return seek;
}

#endif /* TYPE */