Browse Source

Add list module

Ilja Kartašov 9 months ago
parent
commit
0d6fca9b26
2 changed files with 196 additions and 0 deletions
  1. 132
    0
      list.c
  2. 64
    0
      list.h

+ 132
- 0
list.c View File

@@ -0,0 +1,132 @@
1
+/******************************************************************************
2
+ *
3
+ *                Copyright (c) 2017-2019 by Löwenware Ltd
4
+ *             Please, refer LICENSE file for legal information
5
+ *
6
+ ******************************************************************************/
7
+
8
+/**
9
+ * @file list.c
10
+ * @author Ilja Kartašov <ik@lowenware.com>
11
+ * @brief cStuff list module
12
+ *
13
+ * @see https://lowenware.com/
14
+ */
15
+
16
+#include <stdlib.h>
17
+#include <string.h>
18
+#include "list.h"
19
+
20
+CStuffRetcode
21
+cstuff_list_init(CStuffList self, int size)
22
+{
23
+	if (!(self->items = calloc(size, sizeof (void *))))
24
+		return CSTUFF_MALLOC_ERROR;
25
+
26
+	self->length = 0;
27
+	self->size = size;
28
+
29
+	return CSTUFF_SUCCESS;
30
+}
31
+
32
+
33
+void
34
+cstuff_list_release(CStuffList self, CStuffListFree item_free)
35
+{
36
+	if (self->items) {
37
+		if (item_free) {
38
+			int i;
39
+			for (i = 0; i < self->length; i++) {
40
+				void *ptr = self->items[i];
41
+				if (ptr) {
42
+					item_free(ptr);
43
+				}
44
+			}
45
+		}
46
+		free(self->items);
47
+	}
48
+}
49
+
50
+
51
+int
52
+cstuff_list_append(CStuffList self, void *item)
53
+{
54
+	void **items;
55
+	int size = self->length + 1;
56
+
57
+	if (size > self->size) {
58
+		if (!(items = realloc(self->items, size * sizeof (void*))))
59
+			return -1;
60
+
61
+		self->items = items;
62
+		self->size = size;
63
+	}
64
+
65
+	self->items[self->length] = item;
66
+
67
+	return self->length++;
68
+}
69
+
70
+
71
+int
72
+cstuff_list_insert(CStuffList self, void *item, int index)
73
+{
74
+	int r;
75
+
76
+	if ((r = cstuff_list_append(self, item)) == -1)
77
+		return -1;
78
+
79
+	if (index >= r || index < 0)
80
+		return r;
81
+
82
+	memmove(&self->items[index + 1], &self->items[index], 
83
+			(self->length - index - 1) * sizeof (void*));
84
+
85
+	self->items[index] = item;
86
+
87
+	return index;
88
+}
89
+
90
+
91
+void *
92
+cstuff_list_remove(CStuffList self, int index)
93
+{
94
+	void *result;
95
+	int i;
96
+	if (index < self->length) {
97
+		result = self->items[index];
98
+		self->length--;
99
+		for (i = index; i < self->length; i++) {
100
+			self->items[i] = self->items[i + 1];
101
+		}
102
+	} else {
103
+		result = NULL;
104
+	}
105
+	return result;
106
+}
107
+
108
+
109
+void
110
+cstuff_list_remove_item(CStuffList self, void *item)
111
+{
112
+	int i;
113
+	for (i = 0; i < self->length; i++) {
114
+		if (self->items[i] == item) {
115
+			cstuff_list_remove(self, i);
116
+			break;
117
+		}
118
+	}
119
+}
120
+
121
+
122
+void
123
+cstuff_list_set_item(CStuffList self, int index, void *value)
124
+{
125
+	if (self->size > index) {
126
+		self->items[index] = value;
127
+		if (index >= self->length)
128
+			self->length = index + 1;
129
+	}
130
+}
131
+
132
+

+ 64
- 0
list.h View File

@@ -0,0 +1,64 @@
1
+/******************************************************************************
2
+ *
3
+ *                Copyright (c) 2017-2019 by Löwenware Ltd
4
+ *             Please, refer LICENSE file for legal information
5
+ *
6
+ ******************************************************************************/
7
+
8
+/**
9
+ * @file list.h
10
+ * @author Ilja Kartašov <ik@lowenware.com>
11
+ * @brief cStuff List module header file
12
+ *
13
+ * @see https://lowenware.com/
14
+ */
15
+
16
+#ifndef CSTUFF_LIST_H_372E2D14_8DEE_4A7C_A5AB_8808A87DF436
17
+#define CSTUFF_LIST_H_372E2D14_8DEE_4A7C_A5AB_8808A87DF436
18
+
19
+#include "retcode.h"
20
+
21
+#define cstuff_list_index(LIST, IDX) (LIST->items[IDX])
22
+
23
+
24
+struct cstuff_list {
25
+	void **items;
26
+	int length;
27
+	int size;
28
+};
29
+
30
+typedef struct cstuff_list * CStuffList;
31
+
32
+typedef void
33
+(*CStuffListFree)(void *item);
34
+
35
+
36
+CStuffRetcode
37
+cstuff_list_init(CStuffList self, int size);
38
+
39
+
40
+void
41
+cstuff_list_release(CStuffList self, CStuffListFree item_free);
42
+
43
+
44
+int
45
+cstuff_list_append(CStuffList self, void *item);
46
+
47
+
48
+int
49
+cstuff_list_insert(CStuffList self, void *item, int index);
50
+
51
+
52
+void *
53
+cstuff_list_remove(CStuffList self, int index);
54
+
55
+
56
+void
57
+cstuff_list_remove_item(CStuffList self, void *item);
58
+
59
+
60
+void
61
+cstuff_list_set_item(CStuffList self, int index, void *value);
62
+
63
+
64
+#endif /* !LIST_H */

Loading…
Cancel
Save