Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b57d8a95a9 | |||
| 6dc339f0f2 | |||
| af716348a6 | |||
| eefe5e2c8f | |||
| c0294bc1f0 | |||
| f90afd3df8 | |||
| 828fe1caac | |||
| 98cee61c83 |
@@ -12,6 +12,11 @@ typedef struct libcdsb_stack_node {
|
||||
void* value;
|
||||
} stack_t;
|
||||
|
||||
typedef struct libcdsb_queue {
|
||||
struct libcdsb_stack_node* back;
|
||||
struct libcdsb_stack_node* front;
|
||||
} queue_t;
|
||||
|
||||
extern void libcdsb_stack_init (stack_t* stack) Nonnull__(1);
|
||||
extern void libcdsb_stack_push (stack_t* stack, void* value) Nonnull__(1);
|
||||
extern void libcdsb_stack_push_many(stack_t* stack, size_t n, ...) Nonnull__(1);
|
||||
@@ -19,6 +24,12 @@ extern void* libcdsb_stack_pop (stack_t* stack) Nonnull__(1)
|
||||
extern void libcdsb_stack_reverse (stack_t* stack) Nonnull__(1);
|
||||
extern void libcdsb_stack_flush (stack_t* stack) Nonnull__(1);
|
||||
|
||||
extern void libcdsb_queue_init (queue_t* queue) Nonnull__(1);
|
||||
extern void libcdsb_queue_push (queue_t* queue, void* value) Nonnull__(1);
|
||||
extern void libcdsb_queue_push_many(queue_t* queue, size_t n, ...) Nonnull__(1);
|
||||
extern void* libcdsb_queue_pop (queue_t* queue) Nonnull__(1);
|
||||
extern void libcdsb_queue_flush (queue_t* queue) Nonnull__(1);
|
||||
|
||||
extern void* libcdsb_aalloc (size_t a, size_t n) Warn_unused_result__;
|
||||
extern void* libcdsb_malloc (size_t n) Warn_unused_result__;
|
||||
extern void* libcdsb_calloc (size_t n, size_t c) Warn_unused_result__;
|
||||
|
||||
+3
-3
@@ -104,7 +104,7 @@ extern vtype_map map_copy (const vtype_map* s) Pure__
|
||||
extern vtype_set vset_copy (const vtype_set* s) Pure__ Warn_unused_result__ Nonnull__(1);
|
||||
extern vtype_dict dict_copy (const vtype_dict* s) Pure__ Warn_unused_result__ Nonnull__(1);
|
||||
extern vtype_list dict_copy_keys(const vtype_dict* s) Pure__ Warn_unused_result__ Nonnull__(1);
|
||||
#define map_copy_keys(s) vset_copy((vtype_set*)s)
|
||||
#define map_copy_keys(s) vset_copy(_LIBCDSB_nothing(s))
|
||||
|
||||
inline vtype_string* string_duplicate (const vtype_string* s) Always_inline__ Warn_unused_result__ Nonnull__(1);
|
||||
extern vtype_array* array_duplicate (const vtype_array* s) Warn_unused_result__ Nonnull__(1);
|
||||
@@ -113,7 +113,7 @@ extern vtype_map* map_duplicate (const vtype_map* s)
|
||||
extern vtype_set* vset_duplicate (const vtype_set* s) Warn_unused_result__ Nonnull__(1);
|
||||
extern vtype_dict* dict_duplicate (const vtype_dict* s) Warn_unused_result__ Nonnull__(1);
|
||||
extern vtype_list* dict_duplicate_keys(const vtype_dict* s) Warn_unused_result__ Nonnull__(1);
|
||||
#define map_duplicate_keys(s) vset_duplicate((vtype_set*)s)
|
||||
#define map_duplicate_keys(s) vset_duplicate(_LIBCDSB_nothing(s))
|
||||
|
||||
inline void string_copy_init (vtype_string* x, const vtype_string* s) Always_inline__ Nonnull__(1,2);
|
||||
extern void array_copy_init (vtype_array* x, const vtype_array* s) Nonnull__(1,2);
|
||||
@@ -122,7 +122,7 @@ extern void map_copy_init (vtype_map* x, const vtype_map* s)
|
||||
extern void vset_copy_init (vtype_set* x, const vtype_set* s) Nonnull__(1,2);
|
||||
extern void dict_copy_init (vtype_dict* x, const vtype_dict* s) Nonnull__(1,2);
|
||||
extern void dict_init_keys (vtype_list* x, const vtype_dict* s) Nonnull__(1,2);
|
||||
#define map_copy_init_keys(x, s) vset_duplicate(x, (vtype_set*)s)
|
||||
#define map_copy_init_keys(x, s) vset_copy_init(x, _LIBCDSB_nothing(s))
|
||||
|
||||
inline void string_free(vtype_string* x) Always_inline__;
|
||||
extern void array_free (vtype_array* x);
|
||||
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
/* This software is licensed by the MIT License, see LICENSE file */
|
||||
/* Copyright © 2022 Gregory Lirent */
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include "__internal/include.h"
|
||||
#undef malloc
|
||||
#undef free
|
||||
|
||||
void libcdsb_queue_init(queue_t* x) {
|
||||
memset(x, 0, sizeof(*x));
|
||||
}
|
||||
|
||||
|
||||
void libcdsb_queue_push(queue_t* x, void* value) {
|
||||
stack_t* n;
|
||||
|
||||
if (!(n = malloc(sizeof(*n))))
|
||||
abort();
|
||||
|
||||
n->prev = 0;
|
||||
|
||||
if (!x->back) {
|
||||
x->front = n;
|
||||
x->back = n;
|
||||
} else x->back->prev = n;
|
||||
|
||||
n->value = value;
|
||||
}
|
||||
|
||||
|
||||
void libcdsb_queue_push_many(queue_t* x, size_t c, ...) {
|
||||
va_list args;
|
||||
stack_t* n;
|
||||
va_start(args, c);
|
||||
|
||||
if (c) {
|
||||
|
||||
if (!x->back) {
|
||||
if (!(n = malloc(sizeof(*n))))
|
||||
abort();
|
||||
|
||||
x->front = n;
|
||||
x->back = n;
|
||||
n->prev = 0;
|
||||
n->value = va_arg(args, void*);
|
||||
--c;
|
||||
}
|
||||
|
||||
while (c--) {
|
||||
if (!(n = malloc(sizeof(*n))))
|
||||
abort();
|
||||
|
||||
x->back->prev = n;
|
||||
n->prev = 0;
|
||||
n->value = va_arg(args, void*);
|
||||
}
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
|
||||
void* libcdsb_queue_pop(queue_t* x) {
|
||||
stack_t* n;
|
||||
void* v;
|
||||
|
||||
if (x->front) {
|
||||
n = x->front;
|
||||
v = n->value;
|
||||
|
||||
if (!(x->front = n->prev)) {
|
||||
x->back = 0;
|
||||
}
|
||||
|
||||
free(n);
|
||||
} else v = 0;
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
void libcdsb_queue_flush(queue_t* x) {
|
||||
stack_t* c;
|
||||
|
||||
c = x->front;
|
||||
|
||||
while (x->front) {
|
||||
c = x->front;
|
||||
x->front = c->prev;
|
||||
free(c);
|
||||
}
|
||||
|
||||
x->back = 0;
|
||||
}
|
||||
@@ -47,6 +47,7 @@ void libcdsb_stack_push_many(stack_t* x, size_t c, ...) {
|
||||
n->prev = x->prev;
|
||||
n->value = x->value;
|
||||
x->prev = n;
|
||||
x->value = va_arg(args, void*);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user