-
Notifications
You must be signed in to change notification settings - Fork 0
/
llist.c
144 lines (118 loc) · 2.61 KB
/
llist.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "llist.h"
#include "objtable.h"
SF_API llnode_t *
sf_ll_new (void *val, llnode_t *prev, llnode_t *nxt)
{
llnode_t *t = sfmalloc (sizeof (*t));
t->val = val;
t->prev = prev;
t->next = nxt;
t->meta.ref_count = 0;
return t;
}
SF_API llnode_t *
sf_ll_add_next_r (llnode_t *head, void *val)
{
if (head->next == NULL)
{
head->next = sf_ll_new (val, head, NULL);
return head->next;
}
return sf_ll_add_next_r (head->next, val);
}
SF_API llnode_t *
sf_ll_add_prev_r (llnode_t *head, void *val)
{
if (head->prev == NULL)
{
head->prev = sf_ll_new (val, NULL, head);
return head->prev;
}
return sf_ll_add_prev_r (head->prev, val);
}
SF_API void
sf_ll_unlink_node (llnode_t **child)
{
llnode_t *c = *child;
if (c->prev != NULL && c->next != NULL)
{
c->prev->next = c->next;
c->next->prev = c->prev;
}
else if (c->prev == NULL && c->next != NULL) // top node
{
*child = c->next;
}
else if (c->next == NULL && c->prev != NULL)
{
c->prev->next = NULL;
}
else // both are NULL
{
e_printf ("[PENDING] child->prev == child->next == NULL\n");
}
}
SF_API llnode_t *
sf_ll_insert (llnode_t *top, int idx, void *val)
{
if (!idx)
{
llnode_t *n = sf_ll_new (val, top->prev, top);
top->prev->next = n;
top->next->prev = n;
return n;
}
return sf_ll_insert (top->next, --idx, val);
}
SF_API void
sf_ll_reverse (llnode_t **top)
{
llnode_t *t = *top;
llnode_t *n = sf_ll_new (t->val, NULL, NULL);
t = t->next;
while (t)
{
n->prev = sf_ll_new (t->val, NULL, n);
n = n->prev;
t = t->next;
}
*top = n;
}
SF_API llnode_t *
sf_ll_getnode_fromval (llnode_t *top, void *val, int (*cmrt) (void *, void *))
{
while (top)
{
// printf ("%s %s %d\n", top->val, val, cmrt (top->val, val));
if (cmrt (top->val, val))
return top;
top = top->next;
}
return NULL;
}
SF_API void
sf_ll_set_meta_refcount (llnode_t *node, int nval)
{
if (node->meta.ref_count > nval && nval)
{
obj_t *p = (obj_t *)node->val;
// printf ("%d\n", p->type);
if (p->meta.pa_size)
{
for (size_t i = 0; i < p->meta.pa_size; i++)
{
llnode_t *t = p->meta.passargs[i]->meta.mem_ref;
sf_ll_set_meta_refcount (t, t->meta.ref_count - 1);
}
sffree (p->meta.passargs);
p->meta.passargs = NULL;
p->meta.pa_size = 0;
}
}
node->meta.ref_count = nval;
if (node->meta.ref_count < 1)
{
// here;
sf_ot_removeobj (node);
}
}