diff options
| author | Tyge Løvset <[email protected]> | 2022-10-04 16:38:41 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-10-04 20:05:49 +0200 |
| commit | 5daba7ad2291dd7f02299eabeb650d0d0b77a1a6 (patch) | |
| tree | d56fa2a14c5f2a84022c67a6268574ef4f1241a7 /include | |
| parent | 6bfc24241e2dea76dc14e3de771d98ecf6bb698e (diff) | |
| download | STC-modified-5daba7ad2291dd7f02299eabeb650d0d0b77a1a6.tar.gz STC-modified-5daba7ad2291dd7f02299eabeb650d0d0b77a1a6.zip | |
- Removed deprecated c_forrange() (replaced by c_forloop + crange type)
- Removed csview_new(literal) macro. Use c_sv(literal) instead.
- Added stc/views.h: moved crange from ccommon.h and added templated type c_listview. Instantiate by: using_listview(ViewName, ValueType) after #include, does not use #define i_val .... See examples in views.h.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/ccommon.h | 27 | ||||
| -rw-r--r-- | include/stc/csview.h | 2 | ||||
| -rw-r--r-- | include/stc/views.h | 109 |
3 files changed, 110 insertions, 28 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index fc164e28..c93c41b9 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -196,17 +196,6 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, ; _.it.ref && (_.key = &_.it.ref->first, _.val = &_.it.ref->second) \ ; C##_next(&_.it)) -// [deprecated]: -#define c_forrange(...) c_MACRO_OVERLOAD(c_forrange, __VA_ARGS__) -#define c_forrange1(stop) c_forrange4(_c_i, size_t, 0, stop) -#define c_forrange2(i, stop) c_forrange4(i, size_t, 0, stop) -#define c_forrange3(i, itype, stop) c_forrange4(i, itype, 0, stop) -#define c_forrange4(i, itype, start, stop) \ - for (itype i=start, _end=stop; i < _end; ++i) -#define c_forrange5(i, itype, start, stop, step) \ - for (itype i=start, _inc=step, _end=(stop) - (_inc > 0) \ - ; (_inc > 0) ^ (i > _end); i += _inc) -// [replacement]: #define c_forloop(...) c_MACRO_OVERLOAD(c_forloop, __VA_ARGS__) #define c_forloop1(stop) for (long long _i=0, _end=stop; _i < _end; ++_i) #define c_forloop2(i, stop) c_forloop4(i, 0, stop, 1) @@ -215,22 +204,6 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, for (long long i=start, _inc=step, _end=(stop) - (_inc > 0) \ ; (_inc > 0) ^ (i > _end); i += _inc) -typedef long long crange_value; -struct {crange_value start, end, step, val; } typedef crange; -struct {crange_value *ref, end, step; } typedef crange_iter; -#define crange_make(...) c_MACRO_OVERLOAD(crange_make, __VA_ARGS__) -#define crange_make1(stop) crange_make3(0, stop, 1) -#define crange_make2(start, stop) crange_make3(start, stop, 1) -#define c_range(...) (*(crange[]){crange_make(__VA_ARGS__)}) -STC_INLINE crange crange_make3(crange_value start, crange_value stop, crange_value step) - { crange r = {start, stop - (step > 0), step}; return r; } -STC_INLINE crange_iter crange_begin(crange* self) - { self->val = self->start; crange_iter it = {&self->val, self->end, self->step}; return it; } -STC_INLINE crange_iter crange_end(crange* self) - { crange_iter it = {NULL}; return it; } -STC_INLINE void crange_next(crange_iter* it) - { *it->ref += it->step; if ((it->step > 0) == (*it->ref > it->end)) it->ref = NULL; } - #define c_forlist(it, T, ...) \ for (struct {T* data; T* ref; int size, index;} \ it = {.data=(T[])__VA_ARGS__, .ref=it.data, .size=sizeof((T[])__VA_ARGS__)/sizeof(T)} \ diff --git a/include/stc/csview.h b/include/stc/csview.h index 2be30cb0..3d8bd044 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -28,12 +28,12 @@ #include "utf8.h" #define csview_null c_sv("") -#define csview_new(literal) c_sv(literal) #define csview_npos (SIZE_MAX >> 1) #define csview_init() csview_null #define csview_drop c_default_drop #define csview_clone c_default_clone +#define csview_from_n c_sv STC_INLINE csview csview_from(const char* str) { return c_make(csview){str, strlen(str)}; } diff --git a/include/stc/views.h b/include/stc/views.h new file mode 100644 index 00000000..d4186caf --- /dev/null +++ b/include/stc/views.h @@ -0,0 +1,109 @@ +/* MIT License + * + * Copyright (c) 2022 Tyge Løvset, NORCE, www.norceresearch.no + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ +/* +#include <stdio.h> +#include <stc/views.h> +using_listview(IView, int); + +int main() +{ + int array[] = {1, 2, 3, 4, 5}; + IView iv = IView_init(array, c_arraylen(array)); + + c_foreach (i, IView, iv) printf(" %d", *i.ref); + puts(""); + + c_forfilter (i, IView, c_listview(IView, {10, 20, 30, 22, 23}) + , c_flt_skipwhile(i, *i.ref < 25) + , c_flt_take(i, 2)) + printf(" %d", *i.ref); + puts(""); + + crange r1 = crange_init(80, 90); + c_foreach (i, crange, r1) printf(" %lld", *i.ref); + puts(""); + + c_foreach (i, crange, c_range(0, 100, 8)) printf(" %lld", *i.ref); + puts(""); +} +*/ +#ifndef STC_VIEWS_H_INCLUDED +#define STC_VIEWS_H_INCLUDED + +#include <stc/ccommon.h> + +#define c_listview(C, ...) \ + ((C){.data = (C##_value[])__VA_ARGS__, \ + .size = sizeof((C##_value[])__VA_ARGS__)/sizeof(C##_value)}) + +#define using_listview(Self, T) \ +typedef T Self##_raw; typedef const Self##_raw Self##_value; \ +typedef struct { Self##_value *data; size_t size; } Self; \ +typedef struct { Self##_value *ref, *end; } Self##_iter; \ + \ +STC_INLINE Self Self##_init(Self##_value* data, size_t size) \ + { Self me = {.data=data, .size=size}; return me; } \ + \ +STC_INLINE Self##_value* Self##_at(const Self* self, size_t idx) \ + { assert(idx < self->size); return self->data + idx; } \ + \ +STC_INLINE Self##_iter Self##_begin(const Self* self) { \ + Self##_iter it = {self->data, self->data + self->size}; \ + return it; \ +} \ + \ +STC_INLINE Self##_iter Self##_end(const Self* self) { \ + Self##_iter it = {NULL, self->data + self->size}; \ + return it; \ +} \ + \ +STC_INLINE void Self##_next(Self##_iter* it) \ + { if (++it->ref == it->end) it->ref = NULL; } \ +struct stc_nostruct + + +#define c_range(...) \ + (*(crange[]){crange_init(__VA_ARGS__)}) +#define crange_MAX INT64_MAX + +typedef long long crange_value; +typedef struct { crange_value start, end, step, val; } crange; +typedef struct { crange_value *ref, end, step; } crange_iter; + +#define crange_init(...) c_MACRO_OVERLOAD(crange_init, __VA_ARGS__) +#define crange_init1(stop) crange_init3(0, stop, 1) +#define crange_init2(start, stop) crange_init3(start, stop, 1) + +STC_INLINE crange crange_init3(crange_value start, crange_value stop, crange_value step) + { crange r = {start, stop - (step > 0), step}; return r; } + +STC_INLINE crange_iter crange_begin(crange* self) + { self->val = self->start; crange_iter it = {&self->val, self->end, self->step}; return it; } + +STC_INLINE crange_iter crange_end(crange* self) + { crange_iter it = {NULL}; return it; } + +STC_INLINE void crange_next(crange_iter* it) + { *it->ref += it->step; if ((it->step > 0) == (*it->ref > it->end)) it->ref = NULL; } + +#endif |
