-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_str.c
183 lines (149 loc) · 3.28 KB
/
queue_str.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "queue_str.h"
#include <string.h>
#include <assert.h>
size_t QUE_STR_START_CAP = 50;
#define QUE_STR_ALLOCATOR(x) ((x) * 2)
char* mystrdup(const char* str)
{
size_t len = strlen(str);
char* temp = (char*)calloc(len+1, sizeof(char));
if (!temp) {
assert(temp != NULL);
return NULL;
}
return (char*)memcpy(temp, str, len);
}
int que_str(queue_str* q, size_t capacity)
{
q->head = q->tail = 0;
q->lastop = QUE_READ;
q->capacity = capacity ? capacity : QUE_STR_START_CAP;
if (!(q->buf = (char**)malloc(q->capacity*sizeof(char*)))) {
assert(q->buf != NULL);
q->capacity = 0;
return 0;
}
return 1;
}
int que_push_str(queue_str* q, char* a)
{
if (q->head == q->tail && q->lastop == QUE_WRITE) {
assert(q->head != q->tail || q->lastop != QUE_WRITE);
return 0;
}
q->buf[q->tail] = mystrdup(a);
q->tail++;
q->tail %= q->capacity;
q->lastop = QUE_WRITE;
return 1;
}
int que_pushe_str(queue_str* q, char* a)
{
char** tmp;
size_t tmp_sz, inc;
if (q->head == q->tail && q->lastop == QUE_WRITE) {
tmp_sz = QUE_STR_ALLOCATOR(q->capacity);
inc = tmp_sz - q->capacity;
if (!(tmp = (char**)realloc(q->buf, sizeof(char*)*tmp_sz))) {
assert(tmp != NULL);
return 0;
}
q->buf = tmp;
/* hmmm */
if (q->head) {
memmove(&q->buf[q->head+inc], &q->buf[q->head], (q->capacity-q->head)*sizeof(char*));
} else {
q->tail = q->capacity;
}
q->capacity = tmp_sz;
}
q->buf[q->tail] = mystrdup(a);
q->tail++;
q->tail %= q->capacity;
q->lastop = QUE_WRITE;
return 1;
}
/*
* it's programmers responsibility
* to make sure it's not empty
*/
void que_pop_str(queue_str* q)
{
assert(q->head != q->tail || q->lastop == QUE_WRITE);
free(q->buf[q->head]);
q->head++;
q->head %= q->capacity;
q->lastop = QUE_READ;
if (q->head == q->tail)
q->head = q->tail = 0;
}
int que_resize_str(queue_str* q, size_t size)
{
size_t sz;
char** tmp = NULL;
sz = que_size_str(q);
if (size < sz) {
assert(size >= sz);
return 0;
}
if (size > q->capacity && q->tail > q->head) {
if (!(tmp = (char**)realloc(q->buf, sizeof(char*)*size))) {
assert(tmp != NULL);
return 0;
}
q->buf = tmp;
} else {
tmp = (char**)malloc(size*sizeof(char*));
if (q->tail <= q->head) {
memcpy(tmp, &q->buf[q->head], (q->capacity-q->head)*sizeof(char*));
memcpy(&tmp[q->capacity-q->head], q->buf, q->tail*sizeof(char*));
} else {
memcpy(tmp, &q->buf[q->head], (q->tail-q->head)*sizeof(char*));
}
free(q->buf);
q->buf = tmp;
q->head = 0;
q->tail = sz;
}
q->capacity = size;
return 1;
}
char* que_front_str(queue_str* q)
{
return q->buf[q->head];
}
char* que_back_str(queue_str* q)
{
return q->buf[q->tail-1];
}
int que_is_empty_str(queue_str* q)
{
return q->head == q->tail && q->lastop == QUE_READ;
}
int que_is_full_str(queue_str* q)
{
return q->head == q->tail && q->lastop == QUE_WRITE;
}
size_t que_size_str(queue_str* q)
{
if (q->tail < q->head) {
return q->capacity - q->head + q->tail;
} else if (q->tail > q->head) {
return q->tail - q->head;
} else if (q->lastop == QUE_WRITE) {
return q->capacity;
} else {
return 0;
}
}
void free_que_str(void* q)
{
size_t sz, i;
queue_str* tmp = (queue_str*)q;
sz = que_size_str(tmp);
for (i=0; i<sz; ++i) {
free(tmp->buf[(tmp->head+i)%tmp->capacity]);
}
free(tmp->buf);
tmp->capacity = 0;
}