summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-10 13:28:47 +0100
committerTyge Løvset <[email protected]>2023-02-10 13:28:47 +0100
commit9f8fc0e6b9bb56ea5cf9fbb27e25326f5cb96891 (patch)
tree33387b7c11a80d9727927c79c33687db249409cf
parentfaf7ddf7a6d36b08b57a2bcd67d2d12815453d1d (diff)
downloadSTC-modified-9f8fc0e6b9bb56ea5cf9fbb27e25326f5cb96891.tar.gz
STC-modified-9f8fc0e6b9bb56ea5cf9fbb27e25326f5cb96891.zip
Added struct name to typedef to allow container pointer predecarations.
-rw-r--r--README.md59
-rw-r--r--include/stc/forward.h34
-rw-r--r--misc/examples/new_pque.c2
3 files changed, 65 insertions, 30 deletions
diff --git a/README.md b/README.md
index 341a8a9d..d374831f 100644
--- a/README.md
+++ b/README.md
@@ -73,7 +73,21 @@ Highlights
- **Compiles with C++ and C99** - C code can be compiled with C++ (container element types must be POD).
- **Forward declaration** - Templated containers may be forward declared without including the full API/implementation. See documentation below.
+Performance
+-----------
+![Benchmark](misc/benchmarks/pics/benchmark.gif)
+
+Benchmark notes:
+- The barchart shows average test times over three platforms: Mingw64 10.30, Win-Clang 12, VC19. CPU: Ryzen 7 2700X CPU @4Ghz.
+- Containers uses value types `uint64_t` and pairs of `uint64_t` for the maps.
+- Black bars indicates performance variation between various platforms/compilers.
+- Iterations are repeated 4 times over n elements.
+- **find()**: not executed for *forward_list*, *deque*, and *vector* because these c++ containers does not have native *find()*.
+- **deque**: *insert*: n/3 push_front(), n/3 push_back()+pop_front(), n/3 push_back().
+- **map and unordered map**: *insert*: n/2 random numbers, n/2 sequential numbers. *erase*: n/2 keys in the map, n/2 random keys.
+
Three standout features of STC
+------------------------------
1. ***Centralized analysis of template arguments***. Assigns good defaults to non-specified templates.
You may specify a number of "standard" template arguments for each container, but as minimum only one is
required (two for maps). In the latter case, STC assumes the elements are basic types. For more complex types,
@@ -90,18 +104,31 @@ same element access syntax. E.g. `c_foreach (it, IntContainer, container) printf
every type of container defined as `IntContainer` with `int` elements. Also the form `c_foreach (it, IntContainer, it1, it2)`
may be used to iterate from `it1` up to `it2`.
-Performance
------------
-![Benchmark](misc/benchmarks/pics/benchmark.gif)
-
-Benchmark notes:
-- The barchart shows average test times over three platforms: Mingw64 10.30, Win-Clang 12, VC19. CPU: Ryzen 7 2700X CPU @4Ghz.
-- Containers uses value types `uint64_t` and pairs of `uint64_t` for the maps.
-- Black bars indicates performance variation between various platforms/compilers.
-- Iterations are repeated 4 times over n elements.
-- **find()**: not executed for *forward_list*, *deque*, and *vector* because these c++ containers does not have native *find()*.
-- **deque**: *insert*: n/3 push_front(), n/3 push_back()+pop_front(), n/3 push_back().
-- **map and unordered map**: *insert*: n/2 random numbers, n/2 sequential numbers. *erase*: n/2 keys in the map, n/2 random keys.
+Naming conventions
+------------------
+- Container names are prefixed by `c`, e.g. `cvec`, `cstr`.
+- Public STC macros are prefixed by `c_`, e.g. `c_foreach`, `c_make`.
+- Template parameter macros are prefixed by `i_`, e.g. `i_val`, `i_type`.
+- Common types for container type Con:
+ - Con
+ - Con_value
+ - Con_raw
+ - Con_iter
+- Common function names for container type Con:
+ - Con_init()
+ - Con_reserve(&con, capacity)
+ - Con_drop(&con)
+ - Con_empty(&con)
+ - Con_size(&con)
+ - Con_push(&con, value)
+ - Con_put_n(&con, values, n)
+ - Con_clone(con)
+ - Con_erase_at(&con, iter)
+ - Con_front(&con)
+ - Con_back(&con)
+ - Con_begin(&con)
+ - Con_end(&con)
+ - Con_next(&iter)
Usage
-----
@@ -116,9 +143,9 @@ No casting is used, so containers are type-safe like templates in c++. A basic u
int main(void)
{
FVec vec = FVec_init();
- FVec_push_back(&vec, 10.f);
- FVec_push_back(&vec, 20.f);
- FVec_push_back(&vec, 30.f);
+ FVec_push(&vec, 10.f);
+ FVec_push(&vec, 20.f);
+ FVec_push(&vec, 30.f);
for (intptr_t i = 0; i < FVec_size(vec); ++i)
printf(" %g", vec.data[i]);
@@ -163,7 +190,7 @@ cvec_one v1 = cvec_one_init();
cvec_two v2 = cvec_two_init();
```
-With six different containers:
+An example using six different container types:
```c
#include <stdio.h>
#include <stc/ccommon.h>
diff --git a/include/stc/forward.h b/include/stc/forward.h
index d08b71b4..64d4370a 100644
--- a/include/stc/forward.h
+++ b/include/stc/forward.h
@@ -42,7 +42,11 @@
// csview
typedef const char csview_value;
-typedef struct { csview_value* str; intptr_t size; } csview;
+typedef struct csview {
+ csview_value* str;
+ intptr_t size;
+} csview;
+
typedef union {
csview_value* ref;
struct { csview chr; csview_value* end; } u8;
@@ -51,10 +55,11 @@ typedef union {
// cstr
typedef char cstr_value;
typedef struct { cstr_value* data; intptr_t size, cap; } cstr_buf;
-typedef union {
+typedef union cstr {
struct { cstr_value data[sizeof(cstr_buf) - 1]; unsigned char size; } sml;
struct { cstr_value* data; size_t size, ncap; } lon;
} cstr;
+
typedef union {
cstr_value* ref;
struct { csview chr; } u8;
@@ -65,22 +70,25 @@ typedef union {
#define _c_carc_types(SELF, VAL) \
typedef VAL SELF##_value; \
-\
- typedef struct { \
+ typedef struct SELF { \
SELF##_value* get; \
catomic_long* use_count; \
} SELF
#define _c_cbox_types(SELF, VAL) \
typedef VAL SELF##_value; \
- typedef struct { \
+ typedef struct SELF { \
SELF##_value* get; \
} SELF
#define _c_cdeq_types(SELF, VAL) \
typedef VAL SELF##_value; \
typedef struct { SELF##_value *ref, *end; } SELF##_iter; \
- typedef struct { SELF##_value *_base, *data; intptr_t _len, _cap; } SELF
+\
+ typedef struct SELF { \
+ SELF##_value *_base, *data; \
+ intptr_t _len, _cap; \
+ } SELF
#define _c_clist_types(SELF, VAL) \
typedef VAL SELF##_value; \
@@ -91,7 +99,7 @@ typedef union {
SELF##_node *const *_last, *prev; \
} SELF##_iter; \
\
- typedef struct { \
+ typedef struct SELF { \
SELF##_node *last; \
} SELF
@@ -114,7 +122,7 @@ typedef union {
uint8_t* _hx; \
} SELF##_iter; \
\
- typedef struct { \
+ typedef struct SELF { \
SELF##_value* table; \
uint8_t* _hashx; \
SELF##_sizet size, bucket_count; \
@@ -142,7 +150,7 @@ typedef union {
SELF##_sizet _tn, _st[36]; \
} SELF##_iter; \
\
- typedef struct { \
+ typedef struct SELF { \
SELF##_node *nodes; \
SELF##_sizet root, disp, head, size, cap; \
} SELF
@@ -150,20 +158,20 @@ typedef union {
#define _c_cstack_fixed(SELF, VAL, CAP) \
typedef VAL SELF##_value; \
typedef struct { SELF##_value *ref, *end; } SELF##_iter; \
- typedef struct { SELF##_value data[CAP]; intptr_t _len; } SELF
+ typedef struct SELF { SELF##_value data[CAP]; intptr_t _len; } SELF
#define _c_cstack_types(SELF, VAL) \
typedef VAL SELF##_value; \
typedef struct { SELF##_value *ref, *end; } SELF##_iter; \
- typedef struct { SELF##_value* data; intptr_t _len, _cap; } SELF
+ typedef struct SELF { SELF##_value* data; intptr_t _len, _cap; } SELF
#define _c_cvec_types(SELF, VAL) \
typedef VAL SELF##_value; \
typedef struct { SELF##_value *ref, *end; } SELF##_iter; \
- typedef struct { SELF##_value *data; intptr_t _len, _cap; } SELF
+ typedef struct SELF { SELF##_value *data; intptr_t _len, _cap; } SELF
#define _c_cpque_types(SELF, VAL) \
typedef VAL SELF##_value; \
- typedef struct { SELF##_value* data; intptr_t _len, _cap; } SELF
+ typedef struct SELF { SELF##_value* data; intptr_t _len, _cap; } SELF
#endif // STC_FORWARD_H_INCLUDED
diff --git a/misc/examples/new_pque.c b/misc/examples/new_pque.c
index 1442f376..57f27dc4 100644
--- a/misc/examples/new_pque.c
+++ b/misc/examples/new_pque.c
@@ -4,7 +4,7 @@ struct Point { int x, y; } typedef Point;
#define i_type PointQ
#define i_val Point
-#define i_less(a, b) a->x < b->x || a->x == b->x && a->y < b->y
+#define i_less(a, b) a->x < b->x || (a->x == b->x && a->y < b->y)
#include <stc/cpque.h>