summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-09-26 18:01:47 +0200
committerTyge Løvset <[email protected]>2021-09-26 18:01:47 +0200
commit050675e6a9e62d9a7abf420b6da19a600487181f (patch)
tree7c57d787316a77067d51c16f1fcb51f199c7dc08 /include
parentfdadea4fbdff996e968d920d4fae9f8b73efa74d (diff)
downloadSTC-modified-050675e6a9e62d9a7abf420b6da19a600487181f.tar.gz
STC-modified-050675e6a9e62d9a7abf420b6da19a600487181f.zip
Added some extensions to cstack and cpque. cstack can now be used as a vector-lite.
Diffstat (limited to 'include')
-rw-r--r--include/stc/cpque.h15
-rw-r--r--include/stc/cstack.h27
2 files changed, 26 insertions, 16 deletions
diff --git a/include/stc/cpque.h b/include/stc/cpque.h
index 7a81d486..642f04cb 100644
--- a/include/stc/cpque.h
+++ b/include/stc/cpque.h
@@ -51,6 +51,11 @@ STC_INLINE Self cx_memb(_with_capacity)(size_t cap) {
return out;
}
+STC_INLINE void cx_memb(_reserve)(Self* self, size_t n) {
+ if (n >= self->size)
+ self->data = (cx_value_t *)c_realloc(self->data, (self->capacity = n)*sizeof(cx_rawvalue_t));
+}
+
STC_INLINE void cx_memb(_clear)(Self* self) {
size_t i = self->size; self->size = 0;
while (i--) i_valdel(&self->data[i]);
@@ -85,17 +90,9 @@ STC_INLINE void cx_memb(_emplace)(Self* self, cx_rawvalue_t raw)
STC_INLINE i_val cx_memb(_value_clone)(cx_value_t val)
{ return i_valfrom(i_valto(&val)); }
-STC_INLINE int cx_memb(_value_compare)(const cx_value_t* x, const cx_value_t* y) {
- cx_rawvalue_t rx = i_valto(x), ry = i_valto(y);
- return i_cmp(&rx, &ry);
-}
-
STC_INLINE void
cx_memb(_push_back)(Self* self, cx_value_t value) {
- if (self->size == self->capacity) {
- self->capacity = self->size*3/2 + 4;
- self->data = (cx_value_t *)c_realloc(self->data, self->capacity*sizeof value);
- }
+ if (self->size == self->capacity) cx_memb(_reserve)(self, self->size*3/2 + 4);
self->data[ self->size++ ] = value;
}
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index 2aaff4d9..7bc0f3fb 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -42,7 +42,13 @@ STC_INLINE Self cx_memb(_init)(void)
{ return c_make(Self){0, 0, 0}; }
STC_INLINE Self cx_memb(_with_capacity)(size_t cap) {
- Self out = {(cx_value_t *) c_malloc(cap*sizeof(cx_value_t)), 0, cap};
+ Self out = {(cx_value_t *) c_malloc(cap*sizeof(i_val)), 0, cap};
+ return out;
+}
+
+STC_INLINE Self cx_memb(_with_size)(size_t size, i_val fill) {
+ Self out = {(cx_value_t *) c_malloc(size*sizeof fill), size, size};
+ while (size) out.data[--size] = fill;
return out;
}
@@ -69,12 +75,17 @@ STC_INLINE cx_value_t* cx_memb(_top)(const Self* self)
STC_INLINE void cx_memb(_pop)(Self* self)
{ --self->size; i_valdel(&self->data[self->size]); }
-STC_INLINE void cx_memb(_push)(Self* self, cx_value_t value) {
- if (self->size == self->capacity) {
- self->capacity = self->size*3/2 + 4;
- self->data = (cx_value_t *)c_realloc(self->data, self->capacity*sizeof value);
- }
- self->data[ self->size++ ] = value;
+STC_INLINE void cx_memb(_reserve)(Self* self, size_t n) {
+ if (n >= self->size)
+ self->data = (cx_value_t *)c_realloc(self->data, (self->capacity = n)*sizeof(cx_rawvalue_t));
+}
+
+STC_INLINE void cx_memb(_shrink_to_fit)(Self* self)
+ { cx_memb(_reserve)(self, self->size); }
+
+STC_INLINE void cx_memb(_push)(Self* self, cx_value_t val) {
+ if (self->size == self->capacity) cx_memb(_reserve)(self, self->size*3/2 + 4);
+ self->data[ self->size++ ] = val;
}
STC_INLINE void cx_memb(_emplace)(Self* self, cx_rawvalue_t raw)
@@ -93,6 +104,8 @@ STC_INLINE void cx_memb(_copy)(Self *self, Self other) {
STC_INLINE i_val cx_memb(_value_clone)(cx_value_t val)
{ return i_valfrom(i_valto(&val)); }
+STC_INLINE i_valraw cx_memb(_value_toraw)(cx_value_t* val)
+ { return i_valto(val); }
STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self)
{ return c_make(cx_iter_t){self->data}; }