[go: up one dir, main page]

File: util_list.c

package info (click to toggle)
s390-tools 2.35.0-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 12,220 kB
  • sloc: ansic: 184,236; sh: 12,152; cpp: 4,954; makefile: 2,763; perl: 2,519; asm: 1,085; python: 697; xml: 29
file content (259 lines) | stat: -rw-r--r-- 4,919 bytes parent folder | download | duplicates (5)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * util - Utility function library
 *
 * Linked list functions
 *
 * Copyright IBM Corp. 2013, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

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

#include "lib/util_libc.h"
#include "lib/util_list.h"

/*
 * Node to entry
 */
static inline void *n2e(struct util_list *list, struct util_list_node *node)
{
	return ((void *) node) - list->offset;
}

/*
 * Entry to node
 */
static inline struct util_list_node *e2n(struct util_list *list, void *entry)
{
	return entry + list->offset;
}

/*
 * Initialize linked list
 */
void util_list_init_offset(struct util_list *list, unsigned long offset)
{
	memset(list, 0, sizeof(*list));
	list->offset = offset;
}

/*
 * Create new linked list
 */
struct util_list *util_list_new_offset(unsigned long offset)
{
	struct util_list *list = util_malloc(sizeof(*list));

	util_list_init_offset(list, offset);
	return list;
}

/*
 * Free linked list
 */
void util_list_free(struct util_list *list)
{
	free(list);
}

/*
 * Add new element to end of list
 */
void util_list_add_tail(struct util_list *list, void *entry)
{
	struct util_list_node *node = e2n(list, entry);

	node->next = NULL;
	if (!list->start) {
		list->start = node;
		node->prev = NULL;
	} else {
		list->end->next = node;
		node->prev = list->end;
	}
	list->end = node;
}

/*
 * Add new element to front of list
 */
void util_list_add_head(struct util_list *list, void *entry)
{
	struct util_list_node *node = e2n(list, entry);

	node->prev = NULL;
	node->next = NULL;
	if (!list->start) {
		list->end = node;
	} else {
		list->start->prev = node;
		node->next = list->start;
	}
	list->start = node;
}

/*
 * Add new element (entry) after an existing element (list_entry)
 */
void util_list_add_next(struct util_list *list, void *entry, void *list_entry)
{
	struct util_list_node *node = e2n(list, entry);
	struct util_list_node *list_node = e2n(list, list_entry);

	node->next = list_node->next;
	node->prev = list_node;
	if (list_node->next)
		list_node->next->prev = node;
	else
		list->end = node;
	list_node->next = node;
}

/*
 * Add new element (entry) before an existing element (list_entry)
 */
void util_list_add_prev(struct util_list *list, void *entry, void *list_entry)
{
	struct util_list_node *node = e2n(list, entry);
	struct util_list_node *list_node = e2n(list, list_entry);

	node->prev = list_node->prev;
	node->next = list_node;
	if (list_node->prev)
		list_node->prev->next = node;
	else
		list->start = node;
	list_node->prev = node;
}

/*
 * Remove element from list
 */
void util_list_remove(struct util_list *list, void *entry)
{
	struct util_list_node *node = e2n(list, entry);

	if (list->start == node)
		list->start = node->next;
	if (list->end == node)
		list->end = node->prev;
	if (node->prev)
		node->prev->next = node->next;
	if (node->next)
		node->next->prev = node->prev;
}

/*
 * Get first element of list
 */
void *util_list_start(struct util_list *list)
{
	if (!list->start)
		return NULL;
	return ((void *) list->start) - list->offset;
}

/*
 * Get last element of list
 */
void *util_list_end(struct util_list *list)
{
	if (!list->end)
		return NULL;
	return n2e(list, list->end);
}

/*
 * Get next element after entry
 */
void *util_list_next(struct util_list *list, void *entry)
{
	struct util_list_node *node;

	if (!entry)
		return NULL;
	node = e2n(list, entry);
	node = node->next;
	if (!node)
		return NULL;
	return n2e(list, node);
}

/*
 * Get previous element before entry
 */
void *util_list_prev(struct util_list *list, void *entry)
{
	struct util_list_node *node;

	if (!entry)
		return NULL;
	node = e2n(list, entry);
	node = node->prev;
	if (!node)
		return NULL;
	return n2e(list, node);
}

/*
 * Get number of list entries
 */
unsigned long util_list_len(struct util_list *list)
{
	unsigned long cnt = 0;
	void *entry;

	util_list_iterate(list, entry)
		cnt++;
	return cnt;
}

/*
 * Sort table (bubble sort)
 */
void util_list_sort(struct util_list *list, util_list_cmp_fn cmp_fn,
		    void *data)
{
	struct util_list_node *node1, *node2;
	unsigned long list_cnt, i, j;
	void *entry1, *entry2;

	list_cnt = util_list_len(list);

	for (i = 1; i < list_cnt; i++) {
		node1 = list->start;
		for (j = 0; j < list_cnt - i; j++) {
			node2 = node1->next;
			entry1 = n2e(list, node1);
			entry2 = n2e(list, node2);
			if (cmp_fn(entry1, entry2, data) > 0) {
				node1->next = node2->next;
				if (node1->next)
					node1->next->prev = node1;
				else
					list->end = node1;
				node2->next = node1;
				node2->prev = node1->prev;
				if (node2->prev)
					node2->prev->next = node2;
				else
					list->start = node2;
				node1->prev = node2;
			} else {
				node1 = node2;
			}
		}
	}
}

/*
 * Check if list is empty
 */
int util_list_is_empty(struct util_list *list)
{
	return list->start == NULL;
}