-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring.c
564 lines (429 loc) · 10.7 KB
/
ring.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/**
* Easy to use single linked list data structure. Fifo and stack usage, as well as concatenation in O(1)
*/
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// HEADER
#include "ring.h"
#include <stdlib.h>
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// DEBUGGING
#if defined(INVARIANT_CHECKS) || defined(DEBUG)
#include <stdio.h>
bool ring_check_invariant(Ring r);
#endif
#ifdef INVARIANT_CHECKS
#define perr(format, ...) fprintf(stderr, "ERROR " format "\n", ## __VA_ARGS__)
#define EXIT_FAILURE_ASSERT 110
#define ASSERT(x, ...) \
if (!(x)) \
{\
perr("ASSERT FAILED: " #__VA_ARGS__ " (" #x ") File: " \
__FILE__ " Line: %d", __LINE__); \
exit(EXIT_FAILURE_ASSERT); \
}
#else
#define ASSERT(x, ...)
#endif
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS INTERN
/**
* Safe malloc. TODO implement user defind allocation function.
*/
static inline void* _smalloc(uint64_t s)
{
void * res = malloc(s);
if (!res)
abort();
return res;
}
// -----------------------------------------------------------------------------
/**
* Creates a new Node.
*/
static inline struct _Node* _ring_create_node(struct _Node* n, cp c)
{
struct _Node* res = _smalloc(sizeof(*res));
res->next = n;
res->contend = c;
return res;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// EXTERN INTERFACE FUNCTIONS
// -----------------------------------------------------------------------------
/**
* Create a new Ring.
* Complexity always O(1)
*/
Ring ring_create(void)
{
Ring res = _smalloc(sizeof(*res));
res->size = 0;
res->first = NULL;
res->last = NULL;
ASSERT(ring_check_invariant(res));
return res;
}
// -----------------------------------------------------------------------------
/**
* Destroys a Ring. All memory is released.
* Complexity always O(n)
*/
void ring_destroy(Ring r, void(*free_contend)(cp))
{
ASSERT(ring_check_invariant(r));
struct _Node* tmp;
while(r->first != NULL)
{
tmp = r->first;
r->first = r->first->next;
if(free_contend)
free_contend(tmp->contend);
free(tmp);
}
free(r);
r = NULL;
}
// -----------------------------------------------------------------------------
/**
* Adds one element at the beginning of the ring.
* Complexity always O(1)
*/
void ring_push(Ring r, cp c)
{
ASSERT(ring_check_invariant(r));
r->first = _ring_create_node(r->first, c);
if (ring_is_empty(r))
r->last = r->first;
r->size += 1;
ASSERT(ring_check_invariant(r));
}
// -----------------------------------------------------------------------------
/**
* Adds one element at the end of the ring.
* Complexity always O(1)
*/
void ring_append(Ring r, cp c)
{
ASSERT(ring_check_invariant(r));
struct _Node* tmp = _ring_create_node(NULL, c);
if (ring_is_empty(r))
r->first = tmp;
else
r->last->next = tmp;
r->last = tmp;
r->size += 1;
ASSERT(ring_check_invariant(r));
}
// -----------------------------------------------------------------------------
/**
* Removes and returns the first element of the ring.
* NULL if the ring is empty.
* Complexity always O(1)
*/
cp ring_pop(Ring r)
{
ASSERT(ring_check_invariant(r));
if (ring_is_empty(r))
return NULL;
cp res = r->first->contend;
struct _Node * delme = r->first;
r->first = r->first->next;
free(delme);
if(ring_size(r) == 1)
r->last = NULL;
r->size -= 1;
ASSERT(ring_check_invariant(r));
return res;
}
// -----------------------------------------------------------------------------
/**
* Removes and returns the last element of the ring.
* NULL if the ring is empty.
* CAUTION!!!! Complexity always O(n)
*/
cp ring_chop(Ring r)
{
ASSERT(ring_check_invariant(r));
if (ring_is_empty(r))
return NULL;
cp res = r->last->contend;
if(ring_size(r) == 1)
{
free(r->last);
r->last = NULL;
r->first = NULL;
}
else
{
struct _Node* tmp = r->first;
while(tmp->next->next != NULL)
{
tmp = tmp->next;
}
r->last = tmp;
free(tmp->next);
tmp->next = NULL;
}
r->size -= 1;
ASSERT(ring_check_invariant(r));
return res;
}
// -----------------------------------------------------------------------------
/**
* Return the contend of a specified position.
* NULL if out of bounds or the contend was NULL in the first place.
* Complexity always O(i)
*/
cp ring_at (Ring r, uint64_t i)
{
ASSERT(ring_check_invariant(r));
if (i >= ring_size(r))
return NULL;
struct _Node* step = r->first;
for(; i > 0; --i)
step = step->next;
return step->contend;
}
// -----------------------------------------------------------------------------
/**
* Extracts an element on a specified position.
* NULL if out of bounds or the contend was NULL in the first place.
* Complexity always O(i)
*/
cp ring_extract(Ring r, uint64_t i)
{
ASSERT(ring_check_invariant(r));
if (i >= ring_size(r))
return NULL;
if(i == 0)
{
return ring_pop(r);
}
else
{
struct _Node* step = r->first;
struct _Node* delme;
cp res;
for(; i > 1; --i)
step = step->next;
res = step->next->contend;
delme = step->next;
step->next = step->next->next;
if(delme == r->last)
r->last = step;
free(delme);
r->size -= 1;
ASSERT(ring_check_invariant(r));
return res;
}
}
// -----------------------------------------------------------------------------
/**
* Inserts an element on a specified position.
* Complexity always O(i)
*/
bool ring_insert_at(Ring r, cp c, uint64_t i)
{
ASSERT(ring_check_invariant(r));
if (i > ring_size(r))
return false;
if (i == 0)
{
ring_push(r, c);
}
else if (i == ring_size(r))
{
ring_append(r, c);
}
else
{
struct _Node * step = r->first;
for(; i > 1; --i)
step = step->next;
step->next = _ring_create_node(step->next, c);
r->size += 1;
}
ASSERT(ring_check_invariant(r));
return true;
}
// -----------------------------------------------------------------------------
/**
* Removes a group of elements out of the ring and stores them in an other
* Ring. del_func defines which element will be removed. Own userdata can
* be passed to the del_func with the last parameter.
* Complexity always O(n)
*/
Ring ring_remove_selected(Ring r, bool(*del_func)(cp c, void* ud), void* ud)
{
ASSERT(ring_check_invariant(r));
Ring res = ring_create();
while (!ring_is_empty(r))
{
if (del_func(ring_first(r), ud))
ring_append(res, ring_pop(r));
else
break;
}
// r->first guaranteed not in res
struct _Node* step = r->first;
struct _Node* delme = NULL;
while (step != NULL && step->next != NULL)
{
if (del_func(step->next->contend, ud))
{
ring_append(res, step->next->contend);
delme = step->next;
step->next = step->next->next;
if(delme == r->last)
r->last = step;
free(delme);
r->size -= 1;
}
else
{
step = step->next;
}
}
ASSERT(ring_check_invariant(r));
return res;
}
// -----------------------------------------------------------------------------
/**
* Concatenation of two rings. Don't use r1 & r2 after the call of this funktion.
* Only the returned ring is supposed to be touched again.
* Complexity always O(1)
*/
Ring ring_concat(Ring r1, Ring r2)
{
ASSERT(ring_check_invariant(r1));
ASSERT(ring_check_invariant(r2));
if(ring_is_empty(r1))
{
free(r1);
return r2;
}
else if(ring_is_empty(r2))
{
free(r2);
return r1;
}
r1->last->next = r2->first;
r1->last = r2->last;
r1->size += r2->size;
free(r2);
ASSERT(ring_check_invariant(r1));
return r1;
}
// -----------------------------------------------------------------------------
/**
* Distributes the contend of one ring over M other rings.
* Complexity always O(n). A call with M=0 will result in M=1.
* The input ring is still usable after this function.
*
* @return A ring containing M rings which comtain an nth part of the input ring.
*/
Ring ring_distribute(Ring r, uint64_t m)
{
ASSERT(ring_check_invariant(r));
if (!m)
m = 1;
Ring * ring_vector = _smalloc(sizeof(Ring*) * m);
uint64_t i = 0;
for(uint64_t i = 0; i < m; ++i)
{
ring_vector[i] = ring_create();
}
for(ring_iterator(r))
{
ring_append(ring_vector[ i%m ], ring_index);
++i;
}
Ring res = ring_create();
for(uint64_t i = 0; i < m; ++i)
{
ring_append(res, ring_vector[i]);
}
free(ring_vector);
return res;
}
// -----------------------------------------------------------------------------
/**
* Ring Invariant check.
* Complexity always O(n)
* @return If NULL -> Ring Ok. Else an error msg.
*/
char* ring_invariant(Ring r)
{
if(!r)
return "NULL POINTER EXCEP: Ring base struct undefinded";
if(r->size == 0)
{
if(r->first || r->last)
return "WRONG STRUCTURE: Ring size = 0, but pointer are not NULL";
}
else
{
uint64_t pos;
struct _Node* tmp = r->first;
if(!r->first)
return "WRONG STRUCTURE: Ring size > 0, but first = NULL";
if(!r->last)
return "WRONG STRUCTURE: Ring size > 0, but last = NULL";
if(r->last->next != NULL)
return "WRONG STRUCTURE: Last Link->Next is not NULL";
for(pos = 1; tmp->next != NULL; ++pos)
{
if(pos > r->size)
return "WRONG STRUCTURE: No NULL pointer found before size end reached";
tmp = tmp->next;
}
if(pos != r->size)
return "WRONG STRUCTURE: Ring size != Counted Links";
if(tmp != r->last)
return "WRONG STRUCTURE: Last pointer != last Link";
}
return NULL;
}
#ifdef INVARIANT_CHECKS
// -----------------------------------------------------------------------------
/**
* Invariant check suited for assert makros.
* Complexity always O(n)
*/
bool ring_check_invariant(Ring r)
{
char* msg = ring_invariant(r);
if(msg)
{
perr("%s", msg);
return false;
}
return true;
}
#endif
#ifdef DEBUG
// -----------------------------------------------------------------------------
/**
* Print stats about the ring.
*/
void ring_print(FILE* f, Ring r, void(*pfunc)(FILE* f, cp c, uint64_t pos))
{
int i = 0;
fprintf(f, "\n=========================================================\n");
fprintf(f, " RING SIZE : %u\n", ring_size(r));
fprintf(f, " MEMORY USED : %u\n", ring_size(r) * (uint64_t)(sizeof(struct _Node) + sizeof(*r)));
fprintf(f, "---------------------------------------------------------\n");
for (ring_iterator(r))
{
if (!pfunc)
fprintf(f, " Pos: %3d = %p\n", i++, ring_index);
else
pfunc(f, ring_index, i++);
}
fprintf(f, "---------------------------------------------------------\n");
}
#endif