line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/*Queue implementation*/ |
2
|
|
|
|
|
|
|
typedef struct queue_element queue_element; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
struct queue_element { |
5
|
|
|
|
|
|
|
void *val; |
6
|
|
|
|
|
|
|
struct queue_element *next; |
7
|
|
|
|
|
|
|
}; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
typedef struct { |
10
|
|
|
|
|
|
|
queue_element *first; |
11
|
|
|
|
|
|
|
queue_element *last; |
12
|
|
|
|
|
|
|
int size; |
13
|
|
|
|
|
|
|
} queue; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
typedef struct { |
16
|
|
|
|
|
|
|
queue_element *prev; |
17
|
|
|
|
|
|
|
queue_element *cur; |
18
|
|
|
|
|
|
|
} queue_iterator; |
19
|
|
|
|
|
|
|
|
20
|
74
|
|
|
|
|
|
queue_iterator* queue_iterator_new(queue *q) { |
21
|
74
|
|
|
|
|
|
queue_iterator *it = malloc(sizeof(queue_iterator)); |
22
|
74
|
|
|
|
|
|
it->prev = NULL; |
23
|
74
|
|
|
|
|
|
it->cur = q->first; |
24
|
|
|
|
|
|
|
|
25
|
74
|
|
|
|
|
|
return it; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
49
|
|
|
|
|
|
void queue_iterator_next(queue_iterator *it) { |
29
|
49
|
|
|
|
|
|
it->prev = it->cur; |
30
|
49
|
50
|
|
|
|
|
if (it->cur != NULL) { |
31
|
49
|
|
|
|
|
|
it->cur = it->cur->next; |
32
|
|
|
|
|
|
|
} |
33
|
49
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
132
|
|
|
|
|
|
char queue_iterator_end(queue_iterator *it) { |
36
|
132
|
|
|
|
|
|
return it->cur == NULL; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
74
|
|
|
|
|
|
void queue_iterator_destroy(queue_iterator *it) { |
40
|
74
|
|
|
|
|
|
free(it); |
41
|
74
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
30
|
|
|
|
|
|
queue* queue_new() { |
44
|
30
|
|
|
|
|
|
queue *q = malloc(sizeof(queue)); |
45
|
30
|
|
|
|
|
|
q->first = q->last = NULL; |
46
|
30
|
|
|
|
|
|
q->size = 0; |
47
|
|
|
|
|
|
|
|
48
|
30
|
|
|
|
|
|
return q; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
59
|
|
|
|
|
|
void queue_push(queue *q, void *val) { |
52
|
59
|
|
|
|
|
|
queue_element *q_e = malloc(sizeof(queue_element)); |
53
|
59
|
|
|
|
|
|
q_e->val = val; |
54
|
59
|
|
|
|
|
|
q_e->next = NULL; |
55
|
|
|
|
|
|
|
|
56
|
59
|
100
|
|
|
|
|
if (q->first == NULL) { |
57
|
38
|
|
|
|
|
|
q->first = q->last = q_e; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
21
|
|
|
|
|
|
q->last = q->last->next = q_e; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
59
|
|
|
|
|
|
q->size++; |
64
|
59
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
49
|
|
|
|
|
|
void* queue_shift(queue *q) { |
67
|
49
|
100
|
|
|
|
|
if (q->first == NULL) { |
68
|
15
|
|
|
|
|
|
return NULL; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
34
|
|
|
|
|
|
queue_element *q_e = q->first; |
72
|
34
|
|
|
|
|
|
void *val = q_e->val; |
73
|
34
|
|
|
|
|
|
q->first = q_e->next; |
74
|
34
|
|
|
|
|
|
free(q_e); |
75
|
|
|
|
|
|
|
|
76
|
34
|
|
|
|
|
|
q->size--; |
77
|
34
|
|
|
|
|
|
return val; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
74
|
|
|
|
|
|
void* queue_at(queue *q, queue_iterator *it) { |
81
|
74
|
50
|
|
|
|
|
if (it->cur == NULL) |
82
|
0
|
|
|
|
|
|
return NULL; |
83
|
|
|
|
|
|
|
|
84
|
74
|
|
|
|
|
|
return it->cur->val; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
25
|
|
|
|
|
|
void queue_del(queue *q, queue_iterator *it) { |
88
|
25
|
50
|
|
|
|
|
if (it->cur == NULL) |
89
|
0
|
|
|
|
|
|
return; |
90
|
|
|
|
|
|
|
|
91
|
25
|
50
|
|
|
|
|
if (it->prev != NULL) { |
92
|
0
|
|
|
|
|
|
it->prev->next = it->cur->next; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
else { |
95
|
25
|
|
|
|
|
|
q->first = it->cur->next; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
25
|
100
|
|
|
|
|
if (q->last == it->cur) |
99
|
14
|
|
|
|
|
|
q->last = it->prev; |
100
|
|
|
|
|
|
|
|
101
|
25
|
|
|
|
|
|
queue_element *cur = it->cur; |
102
|
25
|
|
|
|
|
|
it->cur = it->cur->next; |
103
|
25
|
|
|
|
|
|
free(cur); |
104
|
|
|
|
|
|
|
|
105
|
25
|
|
|
|
|
|
q->size--; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
void queue_clear(queue *q) { |
109
|
|
|
|
|
|
|
queue_element *q_e, *old; |
110
|
0
|
0
|
|
|
|
|
for (q_e = q->first; q_e != NULL; old = q_e, q_e = q_e->next, free(old)); |
111
|
0
|
|
|
|
|
|
q->first = q->last = NULL; |
112
|
0
|
|
|
|
|
|
q->size = 0; |
113
|
0
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
371
|
|
|
|
|
|
int queue_size(queue *q) { |
116
|
371
|
|
|
|
|
|
return q->size; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
25
|
|
|
|
|
|
void queue_destroy(queue *q) { |
120
|
|
|
|
|
|
|
queue_element *q_e, *old; |
121
|
25
|
50
|
|
|
|
|
for (q_e = q->first; q_e != NULL; old = q_e, q_e = q_e->next, free(old)); |
122
|
25
|
|
|
|
|
|
free(q); |
123
|
25
|
|
|
|
|
|
} |