summaryrefslogtreecommitdiff
path: root/pkgmgr_livebox/src/dlist.c
blob: cf2cb63e18d8f36d71357b8c31239e6004201e8e (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
180
/*
 * Copyright 2013  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

#include "dlist.h"

/*!
 * \brief
 * This dlist is called Modified Doubly Linked List.
 *
 * Noramlly, The dobule linked list contains address of previous and next element.
 * This dlist also contains them, but the tail element only contains prev address.
 *
 * The head element's prev pointer indicates the last element.
 * But the last element's next pointer indicates NIL.
 *
 * So we can find the last element while crawling this DList
 * But we have to remember the address of the head element.
 */

struct dlist {
	struct dlist *next;
	struct dlist *prev;
	void *data;
};

struct dlist *dlist_append(struct dlist *list, void *data)
{
	struct dlist *item;

	item = malloc(sizeof(*item));
	if (!item)
		return NULL;

	item->next = NULL;
	item->data = data;

	if (!list) {
		item->prev = item;

		list = item;
	} else {
		item->prev = list->prev;
		item->prev->next = item;
		list->prev = item;
	}

	assert(!list->prev->next && "item NEXT");

	return list;
}

struct dlist *dlist_prepend(struct dlist *list, void *data)
{
	struct dlist *item;

	item = malloc(sizeof(*item));
	if (!item)
		return NULL;

	item->data = data;

	if (!list) {
		item->prev = item;
		item->next = NULL;
	} else {
		if (list->prev->next)
			list->prev->next = item;

		item->prev = list->prev;
		item->next = list;

		list->prev = item;

	}

	return item;
}

struct dlist *dlist_remove(struct dlist *list, struct dlist *l)
{
	if (!list || !l)
		return NULL;

	if (l == list)
		list = l->next;
	else
		l->prev->next = l->next;

	if (l->next)
		l->next->prev = l->prev;
	/*!
	 * \note
	 * If the removed entry 'l' has no next element, it is the last element.
	 * In this case, check the existence of the list first,
	 * and if the list is not empty, update the 'prev' of the list (which is a head element of the list) 
	 *
	 * If we didn't care about this, the head element(list) can indicates the invalid element.
	 */
	else if (list)
		list->prev = l->prev;

	free(l);
	return list;
}

struct dlist *dlist_find_data(struct dlist *list, void *data)
{
	struct dlist *l;
	void *_data;

	dlist_foreach(list, l, _data) {
		if (data == _data)
			return l;
	}

	return NULL;
}

void *dlist_data(struct dlist *l)
{
	return l ? l->data : NULL;
}

struct dlist *dlist_next(struct dlist *l)
{
	return l ? l->next : NULL;
}

struct dlist *dlist_prev(struct dlist *l)
{
	return l ? l->prev : NULL;
}

int dlist_count(struct dlist *l)
{
	register int i;
	struct dlist *n;
	void *data;

	i = 0;
	dlist_foreach(l, n, data) {
		i++;
	}

	return i;
}

struct dlist *dlist_nth(struct dlist *l, int nth)
{
	register int i;
	struct dlist *n;

	i = 0;
	for (n = l; n; n = n->next) {
		if (i == nth)
			return n;
		i++;
	}

	return NULL;
}

/* End of a file */