summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/stc/cdeq.h54
-rw-r--r--include/stc/cmap.h35
-rw-r--r--include/stc/cstack.h6
-rw-r--r--include/stc/cvec.h40
-rw-r--r--include/stc/forward.h2
5 files changed, 64 insertions, 73 deletions
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index c5e08393..da7f258f 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -95,10 +95,10 @@ STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) // == _pop() when _
STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self)
{ return c_make(_cx_iter){self->data}; }
STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
- { return c_make(_cx_iter){self->data + cdeq_rep_(self)->size}; }
-STC_INLINE void _cx_memb(_next)(_cx_iter* it) { ++it->ref; }
-STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs)
- { it.ref += offs; return it; }
+ { return c_make(_cx_iter){NULL, self->data + cdeq_rep_(self)->size}; }
+STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->_end) it->ref = NULL; }
+STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t offs)
+ { if ((it.ref += offs) >= it._end) it.ref = NULL; return it; }
#if !defined _i_queue
@@ -127,7 +127,7 @@ _cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], con
}
STC_INLINE _cx_value*
_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) {
- return _cx_memb(_insert_range_p)(self, it.ref, &value, &value + 1);
+ return _cx_memb(_insert_range_p)(self, (it.ref ? it.ref : it._end), &value, &value + 1);
}
STC_INLINE _cx_iter
@@ -139,16 +139,10 @@ _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) {
return _cx_memb(_erase_range_p)(self, it.ref, it.ref + 1);
}
STC_INLINE _cx_iter
-_cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) {
- return _cx_memb(_erase_range_p)(self, it1.ref, it2.ref);
+_cx_memb(_erase_range)(_cx_self* self, _cx_iter i1, _cx_iter i2) {
+ return _cx_memb(_erase_range_p)(self, i1.ref, (i2.ref ? i2.ref : i2._end));
}
-#if !defined _i_no_clone
-STC_INLINE _cx_value*
-_cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2) {
- return _cx_memb(_clone_range_p)(self, it.ref, it1.ref, it2.ref);
-}
-#endif // !_i_no_clone
#if !defined _i_no_emplace
STC_INLINE _cx_value*
_cx_memb(_emplace_front)(_cx_self* self, _cx_raw raw) {
@@ -165,7 +159,7 @@ _cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], cons
}
STC_INLINE _cx_value*
_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) {
- return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1);
+ return _cx_memb(_emplace_range_p)(self, (it.ref ? it.ref : it._end), &raw, &raw + 1);
}
#endif // !_i_no_emplace
@@ -178,9 +172,7 @@ _cx_memb(_find)(const _cx_self* self, _cx_raw raw) {
STC_INLINE const _cx_value*
_cx_memb(_get)(const _cx_self* self, _cx_raw raw) {
- _cx_iter end = _cx_memb(_end)(self);
- _cx_value* val = _cx_memb(_find_in)(_cx_memb(_begin)(self), end, raw).ref;
- return val == end.ref ? NULL : val;
+ return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw).ref;
}
STC_INLINE _cx_value*
@@ -188,9 +180,9 @@ _cx_memb(_get_mut)(_cx_self* self, _cx_raw raw)
{ return (_cx_value *) _cx_memb(_get)(self, raw); }
STC_INLINE void
-_cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2,
- int(*_cmp_)(const _cx_value*, const _cx_value*)) {
- qsort(i1.ref, i2.ref - i1.ref, sizeof *i1.ref, (int(*)(const void*, const void*)) _cmp_);
+_cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2, int(*cmp)(const _cx_value*, const _cx_value*)) {
+ qsort(i1.ref, (i2.ref ? i2.ref : i2._end) - i1.ref, sizeof *i1.ref,
+ (int(*)(const void*, const void*)) cmp);
}
STC_INLINE void
@@ -379,16 +371,13 @@ _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos,
STC_DEF _cx_iter
_cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) {
- const size_t n = p2 - p1;
- if (n > 0) {
- _cx_value* p = p1, *end = self->data + cdeq_rep_(self)->size;
- for (; p != p2; ++p) { i_keydrop(p); }
- if (p1 == self->data)
- self->data += n;
- else memmove(p1, p2, (end - p2) * sizeof(i_key));
- cdeq_rep_(self)->size -= n;
- }
- return c_make(_cx_iter){p1};
+ intptr_t len = p2 - p1;
+ _cx_value* p = p1, *end = self->data + cdeq_rep_(self)->size;
+ for (; p != p2; ++p)
+ { i_keydrop(p); }
+ memmove(p1, p2, (end - p2) * sizeof *p1);
+ cdeq_rep_(self)->size -= len;
+ return c_make(_cx_iter){p2 == end ? NULL : p1, end - len};
}
#if !defined _i_no_emplace
@@ -418,8 +407,9 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
STC_DEF _cx_iter
_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) {
- for (; i1.ref != i2.ref; ++i1.ref) {
- _cx_raw r = i_keyto(i1.ref);
+ const _cx_value* p2 = i2.ref ? i2.ref : i2._end;
+ for (; i1.ref != p2; ++i1.ref) {
+ const _cx_raw r = i_keyto(i1.ref);
if (i_eq((&raw), (&r)))
return i1;
}
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index 1394b3a7..0d1c13d9 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -188,11 +188,28 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) {
return _res;
}
+
+STC_INLINE _cx_iter
+_cx_memb(_end)(const _cx_self* self)
+ { return c_make(_cx_iter){NULL}; }
+
+STC_INLINE void
+_cx_memb(_next)(_cx_iter* it) {
+ while ((++it->ref, *++it->_hx == 0)) ;
+ if (it->ref == it->_end) it->ref = NULL;
+}
+
+STC_INLINE _cx_iter
+_cx_memb(_advance)(_cx_iter it, size_t n) {
+ while (n-- && it.ref) _cx_memb(_next)(&it);
+ return it;
+}
+
STC_INLINE _cx_iter
_cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) {
i_size idx;
if (!(self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]))
- return c_make(_cx_iter){NULL};
+ return _cx_memb(_end)(self);
return c_make(_cx_iter){self->table+idx, self->table+self->bucket_count, self->_hashx+idx};
}
@@ -218,22 +235,6 @@ _cx_memb(_begin)(const _cx_self* self) {
return it;
}
-STC_INLINE _cx_iter
-_cx_memb(_end)(const _cx_self* self)
- { return c_make(_cx_iter){NULL}; }
-
-STC_INLINE void
-_cx_memb(_next)(_cx_iter* it) {
- while ((++it->ref, *++it->_hx == 0)) ;
- if (it->ref == it->_end) it->ref = NULL;
-}
-
-STC_INLINE _cx_iter
-_cx_memb(_advance)(_cx_iter it, size_t n) {
- while (n-- && it.ref) _cx_memb(_next)(&it);
- return it;
-}
-
STC_INLINE size_t
_cx_memb(_erase)(_cx_self* self, _cx_rawkey rkey) {
if (self->size == 0)
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index 3986e366..811479c4 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -173,11 +173,11 @@ STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self)
{ return c_make(_cx_iter){self->size ? (_cx_value*)self->data : NULL, (_cx_value*)self->data + self->size}; }
STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
- { return c_make(_cx_iter){NULL}; }
+ { return c_make(_cx_iter){NULL, (_cx_value*)self->data + self->size}; }
STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->_end) it->ref = NULL; }
-STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs)
- { it.ref += offs; return it; }
+STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t offs)
+ { if ((it.ref += offs) >= it._end) it.ref = NULL ; return it; }
#include "template.h"
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index a4308628..99df920c 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -106,7 +106,7 @@ _cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], cons
}
STC_INLINE _cx_value*
_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) {
- return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1);
+ return _cx_memb(_emplace_range_p)(self, (it.ref ? it.ref : it._end), &raw, &raw + 1);
}
#endif // !_i_no_emplace
@@ -121,10 +121,6 @@ STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) {
_cx_memb(_drop)(self);
*self = _cx_memb(_clone)(*other);
}
-STC_INLINE _cx_value*
-_cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2) {
- return _cx_memb(_clone_range_p)(self, it.ref, it1.ref, it2.ref);
-}
#endif // !_i_no_clone
STC_INLINE size_t _cx_memb(_size)(const _cx_self* cx) { return cvec_rep_(cx)->size; }
@@ -142,7 +138,8 @@ STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value)
STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); }
STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self)
{ size_t n = cvec_rep_(self)->size; return c_make(_cx_iter){n ? self->data : NULL, self->data + n}; }
-STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) { return c_make(_cx_iter){NULL}; }
+STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
+ { return c_make(_cx_iter){NULL, self->data + cvec_rep_(self)->size}; }
STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->_end) it->ref = NULL; }
STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t offs)
{ if ((it.ref += offs) >= it._end) it.ref = NULL; return it; }
@@ -182,7 +179,7 @@ _cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], con
}
STC_INLINE _cx_value*
_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) {
- return _cx_memb(_insert_range_p)(self, it.ref, &value, &value + 1);
+ return _cx_memb(_insert_range_p)(self, (it.ref ? it.ref : it._end), &value, &value + 1);
}
STC_INLINE _cx_iter
@@ -194,8 +191,8 @@ _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) {
return _cx_memb(_erase_range_p)(self, it.ref, it.ref + 1);
}
STC_INLINE _cx_iter
-_cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) {
- return _cx_memb(_erase_range_p)(self, it1.ref, it2.ref);
+_cx_memb(_erase_range)(_cx_self* self, _cx_iter i1, _cx_iter i2) {
+ return _cx_memb(_erase_range_p)(self, i1.ref, (i2.ref ? i2.ref : i2._end));
}
STC_INLINE const _cx_value*
@@ -237,10 +234,11 @@ _cx_memb(_lower_bound)(const _cx_self* self, _cx_raw raw) {
}
STC_INLINE void
-_cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2,
- int(*_cmp_)(const _cx_value*, const _cx_value*)) {
- qsort(i1.ref, i2.ref - i1.ref, sizeof(_cx_value), (int(*)(const void*, const void*)) _cmp_);
+_cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2, int(*cmp)(const _cx_value*, const _cx_value*)) {
+ qsort(i1.ref, (i2.ref ? i2.ref : i2._end) - i1.ref, sizeof(_cx_value),
+ (int(*)(const void*, const void*)) cmp);
}
+
STC_INLINE void
_cx_memb(_sort)(_cx_self* self) {
_cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_cmp));
@@ -356,7 +354,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) {
{ i_keydrop(p); }
memmove(p1, p2, (end - p2) * sizeof *p1);
cvec_rep_(self)->size -= len;
- return c_make(_cx_iter){.ref = p2 == end ? NULL : p1, end - len};
+ return c_make(_cx_iter){p2 == end ? NULL : p1, end - len};
}
#if !defined _i_no_clone
@@ -395,7 +393,8 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
#if !c_option(c_no_cmp)
STC_DEF _cx_iter
_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) {
- for (; i1.ref != i2.ref; ++i1.ref) {
+ const _cx_value* p2 = i2.ref ? i2.ref : i2._end;
+ for (; i1.ref != p2; ++i1.ref) {
const _cx_raw r = i_keyto(i1.ref);
if (i_eq((&raw), (&r)))
return i1;
@@ -405,18 +404,19 @@ _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) {
STC_DEF _cx_iter
_cx_memb(_binary_search_in)(_cx_iter i1, _cx_iter i2, const _cx_raw raw, _cx_iter* lower_bound) {
- _cx_iter mid, last = i2;
- while (i1.ref != i2.ref) {
- mid.ref = i1.ref + (i2.ref - i1.ref)/2;
+ const _cx_value* p2 = i2.ref ? i2.ref : i2._end;
+ _cx_iter mid = i1;
+ while (i1.ref != p2) {
+ mid.ref = i1.ref + (p2 - i1.ref)/2;
const _cx_raw m = i_keyto(mid.ref);
const int c = i_cmp((&raw), (&m));
if (!c) return *lower_bound = mid;
- else if (c < 0) i2.ref = mid.ref;
+ else if (c < 0) p2 = mid.ref;
else i1.ref = mid.ref + 1;
}
- *lower_bound = i1;
- return last;
+ *lower_bound = i1.ref == i2._end ? i2 : i1;
+ return i2;
}
STC_DEF int
diff --git a/include/stc/forward.h b/include/stc/forward.h
index 1a85f1ed..9be399af 100644
--- a/include/stc/forward.h
+++ b/include/stc/forward.h
@@ -194,7 +194,7 @@ typedef union {
#define _c_cstack_fixed(SELF, VAL, CAP) \
typedef VAL SELF##_value; \
- typedef struct { SELF##_value *ref; } SELF##_iter; \
+ typedef struct { SELF##_value *ref, *_end; } SELF##_iter; \
typedef struct SELF { \
SELF##_value data[CAP]; \
size_t size; \