Skip to content

Commit 2dd9dff

Browse files
authored
Merge pull request #174 from Kaaserne/dev
Assertions & docs. Minor bug fixes
2 parents babe831 + c572915 commit 2dd9dff

File tree

214 files changed

+4348
-2508
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

214 files changed

+4348
-2508
lines changed

CMakeLists.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ set(CPP-LAZY_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE STRING "Installatio
2020
option(CPP-LAZY_USE_STANDALONE "Standalone library without {fmt}" NO)
2121
option(CPP-LAZY_USE_INSTALLED_FMT "Import {fmt} using find_package" NO)
2222
option(CPP-LAZY_USE_MODULES "Enable C++20's modules, experimental" NO)
23-
option(CPP-LAZY_DEBUG_ASSERTIONS "Debug assertions (always enabled in debug mode)" NO)
23+
24+
if (NOT DEFINED CPP-LAZY_DEBUG_ASSERTIONS)
25+
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
26+
set(CPP-LAZY_DEBUG_ASSERTIONS ON)
27+
else()
28+
set(CPP-LAZY_DEBUG_ASSERTIONS OFF)
29+
endif()
30+
endif()
31+
option(CPP-LAZY_DEBUG_ASSERTIONS "Debug assertions" ${CPP-LAZY_DEBUG_ASSERTIONS})
2432

2533
if (CPP-LAZY_USE_MODULES)
2634
set(CPP-LAZY_SOURCE_FILES "src/lz.cppm")
@@ -52,6 +60,7 @@ else()
5260
URL https://github.com/fmtlib/fmt/archive/refs/tags/10.2.1.tar.gz
5361
UPDATE_DISCONNECTED YES
5462
URL_MD5 dc09168c94f90ea890257995f2c497a5
63+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
5564
)
5665
FetchContent_MakeAvailable(fmt)
5766
endif()
@@ -64,9 +73,16 @@ message(STATUS "cpp-lazy: Debug assertions: ${CPP-LAZY_DEBUG_ASSERTIONS}.")
6473
add_library(cpp-lazy ${CPP-LAZY_LIB_TYPE} "${CPP-LAZY_SOURCE_FILES}")
6574
add_library(cpp-lazy::cpp-lazy ALIAS cpp-lazy)
6675

76+
# Link lib stacktrace if debug assertions are enabled, on Linux with C++23 or later
77+
set(IS_CXX_23 $<VERSION_GREATER_EQUAL:${CMAKE_CXX_STANDARD},23>)
78+
set(IS_LINUX_COMPILER $<OR:$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:Clang>>)
79+
set(LZ_LINK_LIBBACKTRACE_CONDITION
80+
$<AND:$<BOOL:${CPP-LAZY_DEBUG_ASSERTIONS}>,${IS_LINUX_COMPILER},${IS_CXX_23}>
81+
)
6782
target_link_libraries(cpp-lazy
6883
${CPP-LAZY_LINK_VISIBILITY}
6984
$<$<NOT:$<BOOL:${CPP-LAZY_USE_STANDALONE}>>:fmt::fmt>
85+
$<${LZ_LINK_LIBBACKTRACE_CONDITION}:stdc++_libbacktrace>
7086
)
7187
target_compile_definitions(cpp-lazy
7288
${CPP-LAZY_COMPILE_DEFINITIONS_VISIBLITY}

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
Examples can be found [here](https://github.com/Kaaserne/cpp-lazy/tree/master/examples). Installation can be found [here](https://github.com/Kaaserne/cpp-lazy#installation).
88

99
# cpp-lazy
10-
`cpp-lazy` is an easy and fast lazy evaluation library for C++11/14/17/20. The library tries to reduce redundant data usage for begin/end iterator pairs. For instance: `lz::random_iterable::end()` will return a `lz::default_sentinel` to prevent duplicate data that is also present in `lz::random_iterable::begin()`. If a 'symmetrical' end-begin iterator pair is needed, one can use `lz::common` or `lz::common_random`. Generally, `lz` *forward* iterators will return a `lz::default_sentinel` (or if the input iterable is sentinelled) because forward iterators can only go forward, so there is no need to store the end iterator, is the philosophy.
10+
`cpp-lazy` is an easy and fast lazy evaluation library for C++11/14/17/20. The library tries to reduce redundant data usage for begin/end iterator pairs. For instance: `lz::random_iterable::end()` will return a `lz::default_sentinel_t` to prevent duplicate data that is also present in `lz::random_iterable::begin()`. If a 'symmetrical' end-begin iterator pair is needed, one can use `lz::common` or `lz::common_random`. Generally, `lz` *forward* iterators will return a `lz::default_sentinel_t` (or if the input iterable is sentinelled) because forward iterators can only go forward, so there is no need to store the end iterator, is the philosophy. Lz random access iterators can also return a `default_sentinel` if the internal data of `begin` can already decide whether end is reached, such as `lz::repeat`.
1111

1212
The library uses one optional dependency: the library `{fmt}`, more of which can be found out in the [installation section](https://github.com/Kaaserne/cpp-lazy#Installation). This dependency is only used for printing and formatting.
1313

1414
# Features
1515
- C++11/14/17/20 compatible
1616
- Easy printing/formatting using `lz::format`, `fmt::print` or `std::cout`
1717
- Tested with `-Wpedantic -Wextra -Wall -Wshadow -Wno-unused-function -Werror -Wconversion` and `/WX` for MSVC
18-
- One optional dependency ([`{fmt}`](https://github.com/fmtlib/fmt)), can be turned off by using option `CPP-LAZY_USE_STANDALONE=TRUE` in CMake
19-
- STL compatible
20-
- Little overhead, as little as data usage possible
18+
- One optional dependency ([`{fmt}`](https://github.com/fmtlib/fmt)), can be turned off by using option `CPP-LAZY_USE_STANDALONE=TRUE`/`set(CPP-LAZY_USE_STANDALONE TRUE)` in CMake
19+
- STL compatible (if the input iterable is not sentinelled, otherwise use `lz::*` equivalents)
20+
- Little overhead, as little data usage as possible
2121
- Any compiler with at least C++11 support should be suitable
2222
- [Easy installation](https://github.com/Kaaserne/cpp-lazy#installation)
2323
- [Clear Examples](https://github.com/Kaaserne/cpp-lazy/tree/master/examples)
@@ -90,9 +90,9 @@ int main() {
9090

9191
// Some iterables will return sentinels, for instance (specific rules about when sentinels are returned can be found in the documentation):
9292
std::vector<int> vec = {1, 2, 3, 4};
93-
auto forward = lz::c_string("Hello World"); // .end() returns default_sentinel
94-
auto inf_loop = lz::loop(vec); // .end() returns default_sentinel
95-
auto random = lz::random(0, 32, 4); // .end() returns default_sentinel
93+
auto forward = lz::c_string("Hello World"); // .end() returns default_sentinel_t
94+
auto inf_loop = lz::loop(vec); // .end() returns default_sentinel_t
95+
auto random = lz::random(0, 32, 4); // .end() returns default_sentinel_t
9696

9797
// Some iterables are sized, if the input iterable is also sized:
9898
auto sized = lz::map(vec, [](int i) { return i + 1; });
@@ -112,7 +112,7 @@ int main() {
112112
```
113113

114114
## Ownership
115-
`lz` iterables will hold a reference to the input iterable if the input iterable is *not* inherited from `lz::lazy_view`. This means that the `lz` iterables will hold a reference to (but not excluded to) containers such as `std::vector`, `std::array` and `std::string`, as they do not inherit from `lz::lazy_view`. This is done by the class `lz::ref_or_view`. This can be altered using `lz::copied_iterable` or `lz::as_copied_iterable`. This will copy the input iterable instead of holding a reference to it. This is useful for cheap to copy iterables that are not inherited from `lz::lazy_view` (for example `boost::iterator_range`).
115+
`lz` iterables will hold a reference to the input iterable if the input iterable is *not* inherited from `lz::lazy_view`. This means that the `lz` iterables will hold a reference to (but not excluded to) containers such as `std::vector`, `std::array` and `std::string`, as they do not inherit from `lz::lazy_view`. This is done by the class `lz::maybe_owned`. This can be altered using `lz::copied` or `lz::as_copied`. This will copy the input iterable instead of holding a reference to it. This is useful for cheap to copy iterables that are not inherited from `lz::lazy_view` (for example `boost::iterator_range`).
116116

117117
```cpp
118118
#include <Lz/lz.hpp>
@@ -143,18 +143,18 @@ int main() {
143143
// str will *not* hold a reference to random, because random is a lazy iterable and is trivial to copy
144144
auto str = lz::map(random, [](int i) { return std::to_string(i); });
145145

146-
lz::ref_or_view<std::vector<int>> ref(vec); // Holds a reference to vec
146+
lz::maybe_owned<std::vector<int>> ref(vec); // Holds a reference to vec
147147

148148
using random_iterable = decltype(random);
149-
lz::ref_or_view<random_iterable> ref2(random); // Does NOT hold a reference to random
149+
lz::maybe_owned<random_iterable> ref2(random); // Does NOT hold a reference to random
150150

151151
non_lz_iterable non_lz(vec.data(), vec.data() + vec.size());
152-
lz::ref_or_view<non_lz_iterable> ref(non_lz); // Holds a reference of non_lz! Watch out for this!
152+
lz::maybe_owned<non_lz_iterable> ref(non_lz); // Holds a reference of non_lz! Watch out for this!
153153

154-
// Instead, if you don't want this behaviour, you can use lz::copied_iterable:
155-
lz::copied_iterable<non_lz_iterable> copied(non_lz); // Holds a copy of non_lz = cheap to copy
154+
// Instead, if you don't want this behaviour, you can use `lz::copied`:
155+
lz::copied<non_lz_iterable> copied(non_lz); // Holds a copy of non_lz = cheap to copy
156156
// Or use the helper function:
157-
copied = lz::as_copied_iterable(non_lz); // Holds a copy of non_lz = cheap to copy
157+
copied = lz::as_copied(non_lz); // Holds a copy of non_lz = cheap to copy
158158
}
159159
```
160160
@@ -209,7 +209,7 @@ The following CMake options are available:
209209
- `CPP-LAZY_USE_STANDALONE`: Use the standalone version of cpp-lazy. This will not use the library `{fmt}`. Default is `FALSE`
210210
- `CPP-LAZY_LZ_USE_MODULES` (experimental): Use C++20 modules. Default is `FALSE`
211211
- `CPP-LAZY_USE_INSTALLED_FMT`: Use the installed version of `{fmt}`. This will not use the bundled version. Will use `find_package(fmt)` if enabled. Default is `FALSE`.
212-
- `CPP-LAZY_DEBUG_ASSERTIONS`: Enable debug assertions in other build configurations than debug (debug is always enabled). Default is `FALSE`.
212+
- `CPP-LAZY_DEBUG_ASSERTIONS`: Enable debug assertions. Default is `TRUE` for debug mode, `FALSE` for release.
213213

214214
### Using `FetchContent`
215215
The following way is recommended (cpp-lazy version >= 5.0.1). Note that you choose the cpp-lazy-src.zip, and not the source-code.zip/source-code.tar.gz. This prevents you from downloading stuff that you don't need, and thus preventing pollution of the cmake build directory:

bench/benchmarks-iterators-11.png

-32.9 KB
Loading

bench/benchmarks-iterators-14.png

18.2 KB
Loading

bench/benchmarks-iterators-17.png

20.2 KB
Loading

bench/benchmarks-iterators-20.png

15.6 KB
Loading

bench/benchmarks-iterators-23.png

355 KB
Loading

0 commit comments

Comments
 (0)