-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
110 lines (101 loc) · 1.99 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "alloc.h"
#include "gc.h"
#include "new.h"
#include "list.h"
word insert(word d, word list)
{
root_add(&list);
word n = new(2);
// no need to add n to the rootset
mem[n] = WORD_OF_DATA(d);
mem[n+1] = WORD_OF_POINTER(list);
root_pop(1);
return n;
}
word list(word size)
{
word i;
word list = null;
root_add(&list);
for (i=0; i<size; i++)
list = insert(i, list);
root_pop(1);
return list;
}
word append(word x, word y)
{
// no need to keep track of rootset
if (y == null) return x;
if (x == null) return y;
word p = x;
while (POINTER_OF_WORD(mem[p+1]) != null)
p = POINTER_OF_WORD(mem[p+1]);
mem[p+1] = WORD_OF_POINTER(y);
return x;
}
int length (word list)
{
// no need to keep track of rootset
int n = 0;
while (list != null) {
list = POINTER_OF_WORD(mem[list+1]);
n++;
}
return n;
}
word reverse (word list)
{
root_add(&list);
word acc = null;
root_add(&acc);
while (list != null) {
word d = DATA_OF_WORD(mem[list]);
acc = insert(d, acc);
list = POINTER_OF_WORD(mem[list+1]);
}
root_pop(2);
return acc;
}
word f (word x) {
return x % 3 == 0;
}
word filter (word (*f)(word), word list)
{
root_add(&list);
word acc = null;
root_add(&acc);
while (list != null) {
word d = DATA_OF_WORD(mem[list]);
if (f(d) != 0)
acc = insert(d, acc);
list = POINTER_OF_WORD(mem[list+1]);
}
root_pop(2);
return acc;
}
int equal(word x, word y)
{
// no need to keep track of rootset
while (x != null && y != null) {
word dx = DATA_OF_WORD(mem[x]);
word dy = DATA_OF_WORD(mem[y]);
if (dx != dy) return 0;
x = POINTER_OF_WORD(mem[x+1]);
y = POINTER_OF_WORD(mem[y+1]);
}
return x == y;
}
void print (word list)
{
// no need to keep track of rootset
printf("[");
while (list != null) {
word d = DATA_OF_WORD(mem[list]);
printf("(%d, %d)", list, d);
list = POINTER_OF_WORD(mem[list+1]);
}
printf("]\n");
}