Skip to content

Commit

Permalink
Added possibility to define i_opt as the third argument to i_type
Browse files Browse the repository at this point in the history
… definition (i.e. not for maps). Useful for specifying e.g. c_use_cmp, c_use_eq, c_no_clone for vector, deque, list, queue...
  • Loading branch information
tylov committed Jan 21, 2025
1 parent 5abe884 commit 5c15d42
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 61 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ RM_F ?= rm -f

ifeq ($(OS),Windows_NT)
DOTEXE := .exe
BUILDDIR := bld_Windows/$(CC)
BUILDDIR := build_Windows/$(CC)
else
# CC_VER := $(shell $(CC) -dumpversion | cut -f1 -d.)
BUILDDIR := bld_$(shell uname)/$(CC)
BUILDDIR := build_$(shell uname)/$(CC)
LDFLAGS += -lm
ifneq ($(CC),clang)
CFLAGS += -Wno-clobbered
Expand Down
11 changes: 4 additions & 7 deletions docs/algorithm_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,10 @@ Erase linearily in containers using a predicate. `value` is a pointer to each el
#include "stc/cstr.h"
#include "stc/algorithm.h"
#define i_type Vec, int
#define i_use_cmp
#define i_type Vec, int, c_use_eq
#include "stc/stack.h"
#define i_type List, int
#define i_use_cmp
#define i_type List, int, c_use_eq
#include "stc/list.h"
#define i_type Map
Expand Down Expand Up @@ -567,9 +565,8 @@ int main(void) {
}
```
```c++
#define i_type MyDeq, int
#define i_use_cmp // enable sorting
#include "stc/deque.h" // can be swapped with any of the above
#define i_type MyDeq, int, c_use_cmp // int elements, enable sorting
#include "stc/deque.h"
#include <stdio.h>

int main(void) {
Expand Down
42 changes: 23 additions & 19 deletions docs/stack_api.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
# STC [stack](../include/stc/stack.h): Stack
![Stack](pics/stack.jpg)

The **stack** is a container that gives the programmer the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure. The stack pushes and pops the element from the back of the container, known as the top of the stack.
The **stack** is a container that gives the programmer the functionality of a stack - specifically,
a LIFO (last-in, first-out) data structure. The stack pushes and pops the element from the back of
the container, known as the top of the stack. A stack may be defined ***inplace***, i.e. with a
fixed size on the stack by specifying `i_capacity CAP`.

See the c++ class [std::stack](https://en.cppreference.com/w/cpp/container/stack) for a functional description.

## Header file and declaration

```c++
#define i_type <ct>,<kt> // shorthand for defining i_type, i_key
#define i_type <t> // container type name (default: stack_{i_key})
#define i_type <ct> // container type name (default: stack_{i_key})
#define i_type <ct>, <kt> // define both i_type and i_key types
#define i_capacity <CAP> // define an inplace stack (on the stack) with CAP capacity.
// One of the following:
#define i_key <t> // key type
#define i_keyclass <t> // key type, and bind <t>_clone() and <t>_drop() function names
#define i_keypro <t> // key "pro" type, use for cstr, arc, box types

#define i_keydrop <fn> // destroy value func - defaults to empty destruct
#define i_keyclone <fn> // REQUIRED IF i_keydrop defined

#define i_use_cmp // enable sorting, binary_search and lower_bound
#define i_cmp <fn> // three-way compare two i_keyraw's
#define i_less <fn> // less comparison. Alternative to i_cmp
#define i_eq <fn> // equality comparison. Implicitly defined with i_cmp, but not i_less.

#define i_keyraw <t> // convertion "raw" type - defaults to i_key
#define i_rawclass <t> // convertion "raw class". binds <t>_cmp(), <t>_eq(), <t>_hash()
#define i_keyfrom <fn> // convertion func i_keyraw => i_key
#define i_keytoraw <fn> // convertion func i_key* => i_keyraw
#define i_key <t> // key type
#define i_keyclass <t> // key type, and bind <t>_clone() and <t>_drop() function names
#define i_keypro <t> // key "pro" type, use for cstr, arc, box types

#define i_keydrop <fn> // destroy value func - defaults to empty destruct
#define i_keyclone <fn> // REQUIRED IF i_keydrop defined

#define i_use_cmp // enable sorting, binary_search and lower_bound
#define i_cmp <fn> // three-way compare two i_keyraw's
#define i_less <fn> // less comparison. Alternative to i_cmp
#define i_eq <fn> // equality comparison. Implicitly defined with i_cmp, but not i_less.
-----------------------
#define i_keyraw <t> // convertion "raw" type - defaults to i_key
#define i_rawclass <t> // convertion "raw class". binds <t>_cmp(), <t>_eq(), <t>_hash()
#define i_keyfrom <fn> // convertion func i_keyraw => i_key
#define i_keytoraw <fn> // convertion func i_key* => i_keyraw


#include "stc/stack.h"
Expand Down
3 changes: 1 addition & 2 deletions examples/linkedlists/intrusive.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

#include <stdio.h>

#define i_type List,int
#define i_use_cmp
#define i_type List, int, c_use_cmp
#include "stc/list.h"

void printList(List list) {
Expand Down
3 changes: 1 addition & 2 deletions examples/linkedlists/lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include "stc/algorithm.h"
#include "stc/random.h"

#define i_type DList,double
#define i_use_cmp
#define i_type DList, double, c_use_cmp
#include "stc/list.h"

int main(void) {
Expand Down
5 changes: 3 additions & 2 deletions examples/smartpointers/arcvec_erase.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

void show_drop(int* x) { printf("drop: %d\n", *x); }

#define i_type Arc,int

#define i_type Arc, int
#define i_keydrop show_drop
#define i_use_cmp // enable sort/search for int type
#define i_use_cmp // enable sort/search for Arc int type
#include "stc/arc.h" // Shared pointer to int

#define i_type Vec
Expand Down
5 changes: 2 additions & 3 deletions examples/vectors/lower_bound.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include <stdio.h>

#define i_type IVec,int
#define i_use_cmp
#define i_type IVec, int, c_use_cmp
#include "stc/vec.h"

#define i_type ISet,int
#define i_type ISet, int
#include "stc/sset.h"

int main(void)
Expand Down
2 changes: 1 addition & 1 deletion include/stc/arc.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct _arc_metadata { catomic_long counter; };
#include "priv/template.h"
typedef i_keyraw _m_raw;

#if c_option(c_no_atomic)
#if c_OPTION(c_no_atomic)
#define i_no_atomic
#endif
#if !defined i_no_atomic
Expand Down
4 changes: 2 additions & 2 deletions include/stc/cbits.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ STC_INLINE bool _cbits_disjoint(const uintptr_t* set, const uintptr_t* other, co
#endif // STC_CBITS_H_INCLUDED

#if defined i_type
#define Self c_SELECT(c_ARG_1, i_type)
#define _i_length c_SELECT(c_ARG_2, i_type)
#define Self c_GETARG(1, i_type)
#define _i_length c_GETARG(2, i_type)
#else
#define Self cbits
#endif
Expand Down
6 changes: 3 additions & 3 deletions include/stc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ typedef ptrdiff_t isize;
#define _c_RSEQ_N 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
#define _c_ARG_N(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,N,...) N

// Select arg, e.g. for #define i_type A,B then c_SELECT(c_ARG_2, i_type) is B
#define c_SELECT(X, ...) c_EXPAND(X(__VA_ARGS__,,)) // need c_EXPAND for MSVC
// Select arg, e.g. for #define i_type A,B then c_GETARG(2, i_type) is B
#define c_GETARG(N, ...) c_EXPAND(c_ARG_##N(__VA_ARGS__,)) // need c_EXPAND for MSVC
#define c_ARG_1(a, ...) a
#define c_ARG_2(a, b, ...) b
#define c_ARG_3(a, b, c, ...) c
Expand Down Expand Up @@ -103,7 +103,7 @@ typedef ptrdiff_t isize;

#define c_static_assert(expr) (1 ? 0 : (int)sizeof(int[(expr) ? 1 : -1]))
#if defined STC_NDEBUG || defined NDEBUG
#define c_assert(expr) ((void)0)
#define c_assert(expr) (void)sizeof(expr)
#else
#define c_assert(expr) assert(expr)
#endif
Expand Down
24 changes: 14 additions & 10 deletions include/stc/priv/template.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#ifndef STC_TEMPLATE_H_INCLUDED
#define STC_TEMPLATE_H_INCLUDED
#define c_option(flag) ((i_opt) & (flag))
#define c_OPTION(flag) ((i_opt) & (flag))
#define c_declared (1<<0)
#define c_no_atomic (1<<1)
#define c_no_clone (1<<2)
Expand Down Expand Up @@ -69,10 +69,14 @@

#if defined i_type && !(defined i_key || defined i_keyclass || \
defined i_keypro || defined i_rawclass)
#define Self c_SELECT(c_ARG_1, i_type)
#define i_key c_SELECT(c_ARG_2, i_type)
#if defined _i_is_map && !defined i_val
#define i_val c_SELECT(c_ARG_3, i_type)
#define Self c_GETARG(1, i_type)
#define i_key c_GETARG(2, i_type)
#if c_NUMARGS(i_type) == 3
#if defined _i_is_map
#define i_val c_GETARG(3, i_type)
#else
#define i_opt c_GETARG(3, i_type)
#endif
#endif
#elif !defined Self && defined i_type
#define Self i_type
Expand All @@ -93,19 +97,19 @@

#define i_no_emplace

#if c_option(c_declared)
#if c_OPTION(c_declared)
#define i_declared
#endif
#if c_option(c_no_hash)
#if c_OPTION(c_no_hash)
#define i_no_hash
#endif
#if c_option(c_use_cmp)
#if c_OPTION(c_use_cmp)
#define i_use_cmp
#endif
#if c_option(c_use_eq)
#if c_OPTION(c_use_eq)
#define i_use_eq
#endif
#if c_option(c_no_clone) || defined _i_is_arc
#if c_OPTION(c_no_clone) || defined _i_is_arc
#define i_no_clone
#endif

Expand Down
4 changes: 2 additions & 2 deletions include/stc/sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ int main(void) {

#define _i_is_array
#if defined i_type && !defined i_key
#define Self c_SELECT(c_ARG_1, i_type)
#define i_key c_SELECT(c_ARG_2, i_type)
#define Self c_GETARG(1, i_type)
#define i_key c_GETARG(2, i_type)
#elif defined i_type
#define Self i_type
#else
Expand Down
3 changes: 1 addition & 2 deletions tests/deque_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "ctest.h"

#define i_type IDeq,int
#define i_use_cmp
#define i_type IDeq, int, c_use_cmp
#include "stc/deque.h"


Expand Down
3 changes: 1 addition & 2 deletions tests/list_test.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <stdio.h>
#include "ctest.h"

#define i_type IList, int
#define i_use_cmp
#define i_type IList, int, c_use_cmp
#include "stc/list.h"


Expand Down
3 changes: 1 addition & 2 deletions tests/vec_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "ctest.h"

#define i_type IVec,int
#define i_use_cmp
#define i_type IVec, int, c_use_eq
#include "stc/vec.h"


Expand Down

0 comments on commit 5c15d42

Please sign in to comment.