Skip to content

Commit

Permalink
Moved algo/mics.h => algo/utility.h; misc.h is DEPRECATED.
Browse files Browse the repository at this point in the history
  • Loading branch information
tylov committed Dec 18, 2023
1 parent a399ed3 commit 6afaf90
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 123 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,10 @@ STC is generally very memory efficient. Memory usage for the different container
## Version 4.3
- Breaking changes:
- **cstr** and **csview** now uses *shared linking* by default. Implement by either defining `i_implement` or `i_static` before including.
- Renamed "stc/calgo.h> => `<stc/algorithm.h"`
- Moved "stc/algo/coroutine.h> => `<stc/coroutine.h"`
- Renamed "stc/calgo.h" => `"stc/algorithm.h"`
- Moved "stc/algo/coroutine.h" => `"stc/coroutine.h"`
- Much improved with some new API and added features.
- Removed deprecated "stc/crandom.h>. Use `<stc/crand.h"` with the new API.
- Removed deprecated "stc/crandom.h". Use `"stc/crand.h"` with the new API.
- Reverted names _unif and _norm back to `_uniform` and `_normal`.
- Removed default comparison for **list**, **vec** and **deq**:
- Define `i_use_cmp` to enable comparison for built-in i_key types (<, ==).
Expand Down Expand Up @@ -732,8 +732,7 @@ Major changes:
- Added: **crange**: number generator type, which can be iterated (e.g. with *c_forfilter*).
- Added back **coption** - command line argument parsing.
- New + renamed loop iteration/scope macros:
- `c_forlist`: macro replacing `c_forarray` and `c_apply`. Iterate a compound literal list.
- `c_forrange`: macro replacing `c_forrange`. Iterate a `long long` type number sequence.
- `c_forlist`: macro replacing *c_forarray* and *c_apply*. Iterate a compound literal list.
- Updated **cstr**, now always takes self as pointer, like all containers except csview.
- Updated **vec**, **deq**, changed `*_range*` function names.

Expand Down
114 changes: 2 additions & 112 deletions include/stc/algo/misc.h
Original file line number Diff line number Diff line change
@@ -1,112 +1,2 @@
/* MIT License
*
* Copyright (c) 2023 Tyge Løvset
*
* 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.
*/
#ifndef STC_MISC_H_INCLUDED
#define STC_MISC_H_INCLUDED

// ----------------------------------
// c_auto init+drop containers (RAII)
// ----------------------------------

#define c_auto(...) c_MACRO_OVERLOAD(c_auto, __VA_ARGS__)
#define c_auto_2(C, a) \
c_with_2(C a = C##_init(), C##_drop(&a))
#define c_auto_3(C, a, b) \
c_with_2(c_EXPAND(C a = C##_init(), b = C##_init()), \
(C##_drop(&b), C##_drop(&a)))
#define c_auto_4(C, a, b, c) \
c_with_2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init()), \
(C##_drop(&c), C##_drop(&b), C##_drop(&a)))

// --------------------------------
// stc_find_if
// --------------------------------

#define stc_find_if(...) c_MACRO_OVERLOAD(stc_find_if, __VA_ARGS__)
#define stc_find_if_4(it_p, C, cnt, pred) \
stc_find_if_5(it_p, C, C##_begin(&cnt), C##_end(&cnt), pred)

#define stc_find_if_5(it_p, C, start, end, pred) do { \
intptr_t index = 0; \
const C##_value *_endref = (end).ref, *value; \
for (*(it_p) = start; (value = (it_p)->ref) != _endref; C##_next(it_p), ++index) \
if (pred) goto c_JOIN(findif_, __LINE__); \
(it_p)->ref = NULL; c_JOIN(findif_, __LINE__):; \
} while (0)

// --------------------------------
// stc_erase_if
// --------------------------------

// Use with: clist, cmap, cset, csmap, csset:
#define stc_erase_if(C, cnt_p, pred) do { \
C* _cnt = cnt_p; \
const C##_value* value; \
for (C##_iter _it = C##_begin(_cnt); (value = _it.ref); ) { \
if (pred) _it = C##_erase_at(_cnt, _it); \
else C##_next(&_it); \
} \
} while (0)

// --------------------------------
// stc_eraseremove_if
// --------------------------------

// Use with: stack, vec, deq, queue:
#define stc_eraseremove_if(C, cnt_p, pred) do { \
C* _cnt = cnt_p; \
intptr_t _n = 0; \
const C##_value* value; \
C##_iter _i, _it = C##_begin(_cnt); \
while ((value = _it.ref) && !(pred)) \
C##_next(&_it); \
for (_i = _it; (value = _it.ref); C##_next(&_it)) { \
if (pred) C##_value_drop(_it.ref), ++_n; \
else *_i.ref = *_it.ref, C##_next(&_i); \
} \
C##_adjust_end_(_cnt, -_n); \
} while (0)


// --------------------------------
// stc_all_of, stc_any_of, stc_none_of:
// --------------------------------

#define stc_all_of(boolptr, C, cnt, pred) do { \
C##_iter it; \
stc_find_if_4(&it, C, cnt, !(pred)); \
*(boolptr) = it.ref == NULL; \
} while (0)

#define stc_any_of(boolptr, C, cnt, pred) do { \
C##_iter it; \
stc_find_if_4(&it, C, cnt, pred); \
*(boolptr) = it.ref != NULL; \
} while (0)

#define stc_none_of(boolptr, C, cnt, pred) do { \
C##_iter it; \
stc_find_if_4(&it, C, cnt, pred); \
*(boolptr) = it.ref == NULL; \
} while (0)

#endif // STC_MISC_H_INCLUDED
// THIS HEADER FILE IS DEPRECATED
#include "utility.h"
112 changes: 112 additions & 0 deletions include/stc/algo/utility.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* MIT License
*
* Copyright (c) 2023 Tyge Løvset
*
* 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.
*/
#ifndef STC_UTILITY_H_INCLUDED
#define STC_UTILITY_H_INCLUDED

// ----------------------------------
// c_auto init+drop containers (RAII)
// ----------------------------------

#define c_auto(...) c_MACRO_OVERLOAD(c_auto, __VA_ARGS__)
#define c_auto_2(C, a) \
c_with_2(C a = C##_init(), C##_drop(&a))
#define c_auto_3(C, a, b) \
c_with_2(c_EXPAND(C a = C##_init(), b = C##_init()), \
(C##_drop(&b), C##_drop(&a)))
#define c_auto_4(C, a, b, c) \
c_with_2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init()), \
(C##_drop(&c), C##_drop(&b), C##_drop(&a)))

// --------------------------------
// stc_find_if
// --------------------------------

#define stc_find_if(...) c_MACRO_OVERLOAD(stc_find_if, __VA_ARGS__)
#define stc_find_if_4(it_p, C, cnt, pred) \
stc_find_if_5(it_p, C, C##_begin(&cnt), C##_end(&cnt), pred)

#define stc_find_if_5(it_p, C, start, end, pred) do { \
intptr_t index = 0; \
const C##_value *_endref = (end).ref, *value; \
for (*(it_p) = start; (value = (it_p)->ref) != _endref; C##_next(it_p), ++index) \
if (pred) goto c_JOIN(findif_, __LINE__); \
(it_p)->ref = NULL; c_JOIN(findif_, __LINE__):; \
} while (0)

// --------------------------------
// stc_erase_if
// --------------------------------

// Use with: clist, cmap, cset, csmap, csset:
#define stc_erase_if(C, cnt_p, pred) do { \
C* _cnt = cnt_p; \
const C##_value* value; \
for (C##_iter _it = C##_begin(_cnt); (value = _it.ref); ) { \
if (pred) _it = C##_erase_at(_cnt, _it); \
else C##_next(&_it); \
} \
} while (0)

// --------------------------------
// stc_eraseremove_if
// --------------------------------

// Use with: stack, vec, deq, queue:
#define stc_eraseremove_if(C, cnt_p, pred) do { \
C* _cnt = cnt_p; \
intptr_t _n = 0; \
const C##_value* value; \
C##_iter _i, _it = C##_begin(_cnt); \
while ((value = _it.ref) && !(pred)) \
C##_next(&_it); \
for (_i = _it; (value = _it.ref); C##_next(&_it)) { \
if (pred) C##_value_drop(_it.ref), ++_n; \
else *_i.ref = *_it.ref, C##_next(&_i); \
} \
C##_adjust_end_(_cnt, -_n); \
} while (0)


// --------------------------------
// stc_all_of, stc_any_of, stc_none_of:
// --------------------------------

#define stc_all_of(boolptr, C, cnt, pred) do { \
C##_iter it; \
stc_find_if_4(&it, C, cnt, !(pred)); \
*(boolptr) = it.ref == NULL; \
} while (0)

#define stc_any_of(boolptr, C, cnt, pred) do { \
C##_iter it; \
stc_find_if_4(&it, C, cnt, pred); \
*(boolptr) = it.ref != NULL; \
} while (0)

#define stc_none_of(boolptr, C, cnt, pred) do { \
C##_iter it; \
stc_find_if_4(&it, C, cnt, pred); \
*(boolptr) = it.ref == NULL; \
} while (0)

#endif // STC_UTILITY_H_INCLUDED
2 changes: 1 addition & 1 deletion include/stc/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include "algo/crange.h"
#include "algo/filter.h"
#include "algo/misc.h"
#include "algo/utility.h"

#endif // STC_ALGORITHM_H_INCLUDED
2 changes: 1 addition & 1 deletion misc/benchmarks/various/string_bench_STC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "stc/cstr.h" // string
#define i_implement
#include "stc/csview.h" // string_view
#include "stc/algo/misc.h"
#include "stc/algo/utility.h"

#define i_key_str
#include "stc/vec.h" // vec of cstr with const char* lookup
Expand Down
8 changes: 4 additions & 4 deletions misc/tests/cregex_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#define i_import
#include "stc/cregex.h"
#include "stc/csview.h"
#include "stc/algo/misc.h"
#include "stc/algo/utility.h"
#include "ctest.h"

#define M_START(m) ((m).buf - inp)
Expand Down Expand Up @@ -64,8 +64,8 @@ CTEST(cregex, compile_match_quantifiers1)
}

CTEST(cregex, compile_match_quantifiers2)
{
const char* inp;
{
const char* inp;
c_auto (cregex, re) {
re = cregex_from("bä*");
ASSERT_EQ(re.error, 0);
Expand Down Expand Up @@ -229,7 +229,7 @@ CTEST(cregex, captures_cap)
ASSERT_EQ(M_END(cap[1]), 4);
ASSERT_EQ(M_START(cap[2]), 4);
ASSERT_EQ(M_END(cap[2]), 8);

ASSERT_TRUE(cregex_is_match(&re, "abcdcde"));
ASSERT_TRUE(cregex_is_match(&re, "abcdcdcd"));
}
Expand Down

0 comments on commit 6afaf90

Please sign in to comment.