This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
forked from matthiaskramm/mrscake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
131 lines (118 loc) · 3.27 KB
/
list.c
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
/* list.c
Linked list implementation
Part of the data prediction package.
Copyright (c) 2010-2011 Matthias Kramm <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <stdlib.h>
#include <memory.h>
#include "list.h"
struct _commonlist;
typedef struct _listinfo {
int size;
struct _commonlist*last;
} listinfo_t;
typedef struct _commonlist {
void*entry;
struct _commonlist*next;
listinfo_t info[0];
} commonlist_t;
int list_length_(void*_list)
{
commonlist_t*l = (commonlist_t*)_list;
if(!l)
return 0;
return l->info[0].size;
}
void list_concat_(void*_l1, void*_l2)
{
commonlist_t**l1 = (commonlist_t**)_l1;
commonlist_t**l2 = (commonlist_t**)_l2;
if(!*l1) {
*l1 = *l2;
} else if(*l2) {
(*l1)->info[0].last->next = *l2;
(*l1)->info[0].last = (*l2)->info[0].last;
(*l1)->info[0].size += (*l2)->info[0].size;
}
*l2 = 0;
}
void list_append_(void*_list, void*entry)
{
commonlist_t**list = (commonlist_t**)_list;
commonlist_t* n = 0;
if(!*list) {
n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
*list = n;
(*list)->info[0].size = 0;
} else {
n = malloc(sizeof(commonlist_t));
(*list)->info[0].last->next = n;
}
n->next = 0;
n->entry = entry;
(*list)->info[0].last = n;
(*list)->info[0].size++;
}
/* notice: prepending uses slighly more space than appending */
void list_prepend_(void*_list, void*entry)
{
commonlist_t**list = (commonlist_t**)_list;
commonlist_t* n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
int size = 0;
commonlist_t* last = 0;
if(*list) {
last = (*list)->info[0].last;
size = (*list)->info[0].size;
}
n->next = *list;
n->entry = entry;
*list = n;
(*list)->info[0].last = last;
(*list)->info[0].size = size+1;
}
void list_free_(void*_list)
{
commonlist_t**list = (commonlist_t**)_list;
commonlist_t*l = *list;
while(l) {
commonlist_t*next = l->next;
free(l);
l = next;
}
*list = 0;
}
void list_deep_free_(void*_list)
{
commonlist_t**list = (commonlist_t**)_list;
commonlist_t*l = *list;
while(l) {
commonlist_t*next = l->next;
if(l->entry) {
free(l->entry);l->entry=0;
}
free(l);
l = next;
}
*list = 0;
}
void*list_clone_(void*_list)
{
commonlist_t*l = *(commonlist_t**)_list;
void*dest = 0;
while(l) {
commonlist_t*next = l->next;
list_append_(&dest, l->entry);
l = next;
}
return dest;
}