diff options
| author | Tyge Løvset <[email protected]> | 2021-09-07 22:14:55 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-09-07 22:14:55 +0200 |
| commit | 89987eee11ad1ef0b5fcbfd5beb3c392e2cb5471 (patch) | |
| tree | a61490ff13b818ad350c60c5b394e41465a85654 /include/stc/cstack.h | |
| parent | fc19b966a73793e73a898af4d2974d289fbc555c (diff) | |
| download | STC-modified-89987eee11ad1ef0b5fcbfd5beb3c392e2cb5471.tar.gz STC-modified-89987eee11ad1ef0b5fcbfd5beb3c392e2cb5471.zip | |
Added cstack and cpque (priority queue) + test.
Diffstat (limited to 'include/stc/cstack.h')
| -rw-r--r-- | include/stc/cstack.h | 99 |
1 files changed, 48 insertions, 51 deletions
diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 30fc4f71..b754525f 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -20,63 +20,60 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
+
#ifndef CSTACK_H_INCLUDED
#define CSTACK_H_INCLUDED
+#include <stdlib.h>
+#include "ccommon.h"
+#include "forward.h"
+#endif
-/* Stack adapter, default uses cvec.
-
- #include <stc/cstack.h>
- #include <stdio.h>
-
- using_cvec(i, int);
- using_cstack(i, cvec_i);
+#define i_module cstack
+#include "template.h"
- int main() {
- c_forvar (cstack_i stack = cstack_i_init(), cstack_i_del(&stack))
- {
- for (int i=0; i<100; ++i)
- cstack_i_push(&stack, i*i);
+#if !defined i_fwd
+ cx_deftypes(_c_cstack_types, Self, i_val);
+#endif
+typedef i_valraw cx_rawvalue_t;
- for (int i=0; i<90; ++i)
- cstack_i_pop(&stack);
+STC_INLINE Self cx_memb(_init)(void)
+ { return (Self){0, 0, 0}; }
+STC_INLINE void cx_memb(_clear)(Self* self)
+ { while (self->size) i_valdel(&self->data[--self->size]); }
+STC_INLINE size_t cx_memb(_size)(Self v)
+ { return v.size; }
+STC_INLINE bool cx_memb(_empty)(Self v)
+ { return !v.size; }
+STC_INLINE size_t cx_memb(_capacity)(Self v)
+ { return v.capacity; }
+STC_INLINE void cx_memb(_pop)(Self* self)
+ { --self->size; }
+STC_INLINE cx_value_t* cx_memb(_top)(const Self* self)
+ { return &self->data[self->size - 1]; }
+STC_INLINE void cx_memb(_push)(Self* self, cx_value_t value) {
+ if (self->size == self->capacity)
+ self->data = realloc(self->data, (self->capacity = self->size*3/2 + 4)*sizeof value);
+ self->data[ self->size++ ] = value;
+}
+STC_INLINE void cx_memb(_emplace)(Self* self, cx_rawvalue_t raw)
+ { cx_memb(_push)(self, i_valfrom(raw)); }
- printf("top: %d\n", *cstack_i_top(&stack));
- }
- }
-*/
-#include "cvec.h"
+STC_INLINE void cx_memb(_del)(Self* self) {
+ size_t i = self->size;
+ while (i--) i_valdel(&self->data[i]);
+ free(self->data);
+}
-#define using_cstack(X, ctype) \
- _c_using_cstack(cstack_##X, ctype)
+STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self)
+ { return c_make(cx_iter_t){self->data}; }
+STC_INLINE cx_iter_t cx_memb(_end)(const Self* self)
+ { return c_make(cx_iter_t){self->data + self->size}; }
+STC_INLINE void cx_memb(_next)(cx_iter_t* it) {++it->ref; }
-#define _c_using_cstack(Self, ctype) \
- typedef ctype Self; \
- typedef ctype##_value_t cx_value_t; \
- typedef ctype##_rawvalue_t cx_rawvalue_t; \
- typedef ctype##_iter_t cx_iter_t; \
-\
- STC_INLINE Self cx_memb(_init)(void) { return ctype##_init(); } \
- STC_INLINE Self cx_memb(_clone)(Self st) { return ctype##_clone(st); } \
- STC_INLINE cx_value_t cx_memb(_value_clone)(cx_value_t val) \
- { return ctype##_value_clone(val); } \
- STC_INLINE void cx_memb(_clear)(Self* self) {ctype##_clear(self); } \
- STC_INLINE void cx_memb(_del)(Self* self) {ctype##_del(self); } \
-\
- STC_INLINE size_t cx_memb(_size)(Self st) { return ctype##_size(st); } \
- STC_INLINE bool cx_memb(_empty)(Self st) { return ctype##_empty(st); } \
- STC_INLINE cx_value_t* cx_memb(_top)(const Self* self) { return ctype##_back(self); } \
-\
- STC_INLINE void cx_memb(_pop)(Self* self) {ctype##_pop_back(self); } \
- STC_INLINE void cx_memb(_push)(Self* self, ctype##_value_t value) \
- {ctype##_push_back(self, value); } \
- STC_INLINE void cx_memb(_emplace)(Self* self, cx_rawvalue_t raw) \
- {ctype##_emplace_back(self, raw); } \
- STC_INLINE void cx_memb(_emplace_items)(Self *self, const cx_rawvalue_t arr[], size_t n) \
- {ctype##_emplace_items(self, arr, n); } \
-\
- STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) { return ctype##_begin(self); } \
- STC_INLINE cx_iter_t cx_memb(_end)(const Self* self) { return ctype##_end(self); } \
- STC_INLINE void cx_memb(_next)(cx_iter_t* it) {ctype##_next(it); } \
- struct stc_trailing_semicolon
+STC_INLINE Self cx_memb(_clone)(Self v) {
+ Self out = {(cx_value_t*) c_malloc(v.size*sizeof(cx_value_t)), v.size, v.size};
+ for (cx_value_t *a = out.data, *b = a + v.size; a != b; ++a) *a = i_valfrom(i_valto(v.data++));
+ return out;
+}
-#endif
+#include "template.h"
|
