Skip to content

Commit

Permalink
Fixed i_aux support, and made breaking change: definition must be enc…
Browse files Browse the repository at this point in the history
…losed with curly braces, e.g.: `#define i_aux { int ext; }`
  • Loading branch information
tylov committed Jan 24, 2025
1 parent a6012b6 commit 45f264d
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 45 deletions.
55 changes: 29 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ library for C99 with excellent ergonomics and ease of use.
<details>
<summary><b>Version 5 NEWS</b></summary>

- Added build system with Meson. Makefile provided as well.
- Added build system/CI with Meson. Makefile provided as well.
- Added support for extending templated containers by `#define i_aux { ... }`.
- Changed ranged for-loop macros to use more natural C-syntax (v5.0.2)
- Added **sum type** (tagged union), included via `algorithm.h`
- Added single/multi-dimensional generic **span** type, with numpy-like slicing.
- Updated coroutines support with *structured concurrency* and *symmetric coroutines*.
- Updated coroutines support with proper *error handling* and *error recovery*.
- Changed for-loop range-style macros to more natural C-syntax
- Template parameter `i_type` lets you define `i_type`, `i_key`, and `i_val` all in one line (comma separated).
- Template parameter `i_type` lets you define container type plus `i_key` and `i_val` (or `i_opt`) all in one line.
- Template parameters `i_keyclass` and `i_valclass` to specify types with `_drop()` and `_clone()` functions defined.
- Template parameters `i_keypro` and `i_valpro` to specify `cstr`, `box` and `arc` types (users may also define pro-types).
- **hmap** now uses *Robin Hood hashing* (very fast on clang compiler).
- Several new algorithms added, e.g. `c_filter` (ranges-like).
- Several new algorithms added, e.g. `c_filter` (ranges-like), `c_shuffle`, `c_reverse`.
- A lot of improvements and bug fixes.

See also [version history](#version-history) for breaking changes in V5.0.
Expand All @@ -32,11 +33,11 @@ See also [version history](#version-history) for breaking changes in V5.0.
* A wide set of high performance, generic/templated typesafe container types, including smart pointers and bitsets.
* String type with utf8 support and short string optimization (sso), plus two string-view types.
* Typesafe and ergonomic **sum type** implementation, aka. tagged union or variant.
* A **coroutine** implementation with excellent ergonomics, error recovery and cleanup support.
* A **coroutine** implementation with good ergonomics, error handling/recovery and cleanup support.
* Fast, modern **regular expressions** with full utf8 and a subset of unicode character classes support.
* Ranges algorithms like *iota* and filter views like *take, skip, take-while, skip-while, map*.
* Generic algorithms, iterators and loop abstactions. Blazing fast *sort, binary search* and *lower bound*.
* Single/multi-dimensional generic **span view** with arbitrary array dimensions (numpy array-like slicing).
* Single/multi-dimensional generic **span view** with arbitrary array dimensions and numpy array-like slicing.

#### B. Improved safety and increased productivity
* Abstractions for raw loops, ranged iteration over containers, and generic ranges algorithms. All this
Expand Down Expand Up @@ -686,7 +687,7 @@ allocated size of the given pointer, unlike standard `realloc` and `free`.
#define pgs_realloc(p, old_sz, sz) (p ? repalloc(p, sz) : pgs_malloc(sz))
#define pgs_free(p, sz) (p ? pfree(p) : (void)0) // pfree/repalloc does not accept NULL.
#define i_aux MemoryContext memctx;
#define i_aux { MemoryContext memctx; } // NB: enclose in curly braces!
#define i_allocator pgs
#define i_no_clone
```
Expand All @@ -708,9 +709,9 @@ void maptest()
IMap_drop(&map);
}
```
Another example is to sort struct elements using an 'active' field:
Another example is to sort struct elements by the *active field* and *reverse* flag:

[ [Run this code](https://godbolt.org/z/K3h4dK5jv) ]
[ [Run this code](https://godbolt.org/z/4WdT5ze1x) ]
```c++
#include <stdio.h>
#include <time.h>
Expand All @@ -726,42 +727,44 @@ typedef struct {

enum FMDActive {FMD_fileName, FMD_directory, FMD_size, FMD_lastWriteTime};

int FileMetaData_cmp(enum FMDActive active, const FileMetaData* a, const FileMetaData* b);
struct FMDVector_aux; // defined when specifying i_aux
int FileMetaData_cmp(const struct FMDVector_aux*, const FileMetaData*, const FileMetaData*);
void FileMetaData_drop(FileMetaData*);

#define i_type FMDVector, FileMetaData
#define i_aux enum FMDActive active; bool reverse;
#define i_cmp(x, y) FileMetaData_cmp(self->aux.active, x, y)
#define i_aux { enum FMDActive activeField; bool reverse; }
#define i_cmp(x, y) FileMetaData_cmp(&self->aux, x, y)
#define i_keydrop FileMetaData_drop
#define i_no_clone
#include <stc/stack.h>

// --------------

int FileMetaData_cmp(enum FMDActive active, const FileMetaData* a, const FileMetaData* b) {
switch (active) {
case FMD_fileName: return cstr_cmp(&a->fileName, &b->fileName);
case FMD_directory: return cstr_cmp(&a->directory, &b->directory);
case FMD_size: return c_default_cmp(&a->size, &b->size);
case FMD_lastWriteTime: return c_default_cmp(&a->lastWriteTime, &b->lastWriteTime);
int FileMetaData_cmp(const struct FMDVector_aux* aux, const FileMetaData* a, const FileMetaData* b) {
int dir = aux->reverse ? -1 : 1;
switch (aux->activeField) {
case FMD_fileName: return dir*cstr_cmp(&a->fileName, &b->fileName);
case FMD_directory: return dir*cstr_cmp(&a->directory, &b->directory);
case FMD_size: return dir*c_default_cmp(&a->size, &b->size);
case FMD_lastWriteTime: return dir*c_default_cmp(&a->lastWriteTime, &b->lastWriteTime);
}
return 0;
return 0;
}

void FileMetaData_drop(FileMetaData* fmd) {
cstr_drop(&fmd->fileName);
cstr_drop(&fmd->directory);
cstr_drop(&fmd->fileName);
cstr_drop(&fmd->directory);
}

int main(void) {
FMDVector vec = c_make(FMDVector, {
FMDVector vec = c_make(FMDVector, {
{cstr_from("WScript.cpp"), cstr_from("code/unix"), 3624, 123567},
{cstr_from("CanvasBackground.cpp"), cstr_from("code/unix/canvas"), 38273, 12398},
{cstr_from("Brush_test.cpp"), cstr_from("code/tests"), 67236, 7823},
});

for (c_range(active, FMD_lastWriteTime + 1)) {
vec.aux.active = (enum FMDActive)active;
vec.aux.reverse = true;
for (c_range(activeField, FMD_lastWriteTime + 1)) {
vec.aux.activeField = (enum FMDActive)activeField;
FMDVector_sort(&vec);

for (c_each(i, FMDVector, vec)) {
Expand All @@ -771,7 +774,7 @@ int main(void) {
}
puts("");
}
FMDVector_drop(&vec);
FMDVector_drop(&vec);
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples/priorityqueues/functor.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdio.h>

#define i_type IPQueue, int
#define i_aux bool(*less)(const int*, const int*);
#define i_aux { bool(*less)(const int*, const int*); }
#define i_less(x, y) self->aux.less(x, y)
#include "stc/pqueue.h"

Expand Down
2 changes: 1 addition & 1 deletion include/stc/arc.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ int main(void) {
}
*/
#include "priv/linkage.h"
#include "types.h"

#ifndef STC_ARC_H_INCLUDED
#define STC_ARC_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>

#if defined __GNUC__ || defined __clang__ || defined _MSC_VER || defined i_no_atomic
Expand Down
2 changes: 1 addition & 1 deletion include/stc/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ int main(void) {
}
*/
#include "priv/linkage.h"
#include "types.h"

#ifndef STC_BOX_H_INCLUDED
#define STC_BOX_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>

#define cbox_null {0}
Expand Down
2 changes: 1 addition & 1 deletion include/stc/deque.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
// Deque - double ended queue. Implemented as a ring buffer, extension of queue.

#include "priv/linkage.h"
#include "types.h"

#ifndef STC_DEQUE_H_INCLUDED
#define STC_DEQUE_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>
#endif // STC_DEQUE_H_INCLUDED

Expand Down
2 changes: 1 addition & 1 deletion include/stc/hmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ int main(void) {
}
*/
#include "priv/linkage.h"
#include "types.h"

#ifndef STC_HMAP_H_INCLUDED
#define STC_HMAP_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>
#define _hashmask 0x3fU
#define _distmask 0x3ffU
Expand Down
2 changes: 1 addition & 1 deletion include/stc/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
}
*/
#include "priv/linkage.h"
#include "types.h"

#ifndef STC_LIST_H_INCLUDED
#define STC_LIST_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>

#define _c_list_complete_types(SELF, dummy) \
Expand Down
2 changes: 1 addition & 1 deletion include/stc/pqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
* SOFTWARE.
*/
#include "priv/linkage.h"
#include "types.h"

#ifndef STC_PQUEUE_H_INCLUDED
#define STC_PQUEUE_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>
#endif // STC_PQUEUIE_H_INCLUDED

Expand Down
7 changes: 0 additions & 7 deletions include/stc/priv/linkage.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@
#define i_realloc c_JOIN(i_allocator, _realloc)
#define i_free c_JOIN(i_allocator, _free)
#endif

#ifdef i_aux
#define _i_aux_struct struct { i_aux } aux;
#else
#define _i_aux_struct
#endif

#if defined __clang__ && !defined __cplusplus
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wall"
Expand Down
2 changes: 1 addition & 1 deletion include/stc/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

// Queue. Implemented as a ring buffer.
#include "priv/linkage.h"
#include "types.h"

#ifndef STC_QUEUE_H_INCLUDED
#define STC_QUEUE_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>
#endif // STC_QUEUE_H_INCLUDED

Expand Down
2 changes: 1 addition & 1 deletion include/stc/smap.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ int main(void) {
}
*/
#include "priv/linkage.h"
#include "types.h"

#ifndef STC_SMAP_H_INCLUDED
#define STC_SMAP_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>
#endif // STC_SMAP_H_INCLUDED

Expand Down
2 changes: 1 addition & 1 deletion include/stc/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
* SOFTWARE.
*/
#include "priv/linkage.h"
#include "types.h"

// Stack - a simplified vec type without linear search and insert/erase inside the stack.

#ifndef STC_STACK_H_INCLUDED
#define STC_STACK_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>
#endif // STC_STACK_H_INCLUDED

Expand Down
2 changes: 1 addition & 1 deletion include/stc/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/

#ifdef i_aux
#define _i_aux_struct struct { i_aux } aux;
#define _i_aux_struct struct c_JOIN(Self, _aux) i_aux aux;
#else
#define _i_aux_struct
#endif
Expand Down
2 changes: 1 addition & 1 deletion include/stc/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ int main(void) {
}
*/
#include "priv/linkage.h"
#include "types.h"

#ifndef STC_VEC_H_INCLUDED
#define STC_VEC_H_INCLUDED
#include "common.h"
#include "types.h"
#include <stdlib.h>

#define _it2_ptr(it1, it2) (it1.ref && !it2.ref ? it1.end : it2.ref)
Expand Down

0 comments on commit 45f264d

Please sign in to comment.