百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

cpp-lazy

> 编程语言
开源

C++11(及以后)的懒惰求值库

364 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

C++11(及以后)的懒惰求值库

Examples can be found here. Installation can be found here.

cpp-lazy

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.

The library uses one optional dependency: the library {fmt}, more of which can be found out in the installation section. This dependency is only used for printing and formatting.

Features

  • C++11/14/17/20 compatible
  • C++20's module compatible
  • pkgconfig compatible
  • Easy printing/formatting using lz::format, fmt::print or std::cout
  • One optional dependency ({fmt}), can be turned off by using option CPP_LAZY_USE_STANDALONE=TRUE/set(CPP_LAZY_USE_STANDALONE TRUE) in CMake
  • STL compatible (if the input iterable is not sentinelled, otherwise use lz::* equivalents)
  • Little overhead, as little data usage as possible
  • Any compiler with at least C++11 support should be suitable
  • Easy installation
  • Clear Examples
  • Piping/chaining using | operator
  • Tested with very strict GCC/Clang/MSVC flags
  • Bidirectional sentinelled iterables can be reversed using lz::common

What is lazy?

Lazy evaluation is an evaluation strategy which holds the evaluation of an expression until its value is needed. In this library, this is the case for all iterables/iteartors. It holds all the elements that are needed for the operation:

…

Philosophy behind cpp-lazy

Cpp-lazy is implemented in such a way, that it tries to reduce redundant data usage as much as possible. For instance, if an iterable can determine whether the end is reached by only using the begin iterator ánd is less than a bidirectional iterator, it will return a sentinel instead of storing the end iterator as well. Or, if the iterable is already sentinelled, it will return the same sentinel type as the input iterable.

This is the case for lz::take(std::forward_list<>{}) for instance, but not for lz::take(std::list<>{}) or lz::take(std::vector<>{}). This is done to reduce memory usage. If you need a symmetrical begin/end iterator pair, you can use lz::common. Use lz::common as late as possible in your chain, because every lz iterable has the potential to return sentinels instead of storing the end iterator.

cpp
a | lz::map(...) | lz::filter(...) | lz::common; // OK
a | lz::common | lz::map(...) | lz::filter(...); // Not recommended, map or filter could have returned sentinels

lz::common tries to make the begin/end iterator pair symmetrical. Only as last resort it will use common_iterator which is basically a fancy [std::]variant internally. It depends on the following conditions whether lz::common will use common_iterator:

  • If begin and end are already the same type, the same instance will be returned;
  • If the iterable passed is random access, begin + (size_t)distance(begin, end) will be used to create the end iterator;
  • If begin is assignable from end, assign end to begin to create the end iterator. It will keep its .size() if applicable;
  • Otherwise, common_iterator will be used. As will be the case for all std:: sentinelled iterables, but not for lz sentinelled iterables.

Ownership

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).

…

Iterating

Iterating over iterables with sentinels using range-based for loops is possible. However, a workaround for C++ versions #include

int main() { auto iterable_with_sentinel = lz::c_string("Hello World"); // Possible in C++17 and higher for (auto i : iterable_with_sentinel) { std::cout . If neither is available, it will use std::cout/std::ostringstream`:

cpp
#include 
#include 
#include 

int main() {
  std::vector vec = {1, 2, 3, 4};
  auto filtered = vec | lz::filter([](int i) { return i % 2 == 0; }); // == {2, 4}

  // To a stream
  std::cout )
  lz::format(filtered, std::cout, ", "); // prints "2, 4"
  fmt::print("{}", fmt::join(filtered, ", ")); // prints "2, 4" (only with {fmt} installed)

  filtered | lz::format(std::cout, ", "); // prints "2, 4"
  filtered | lz::format; // prints "2, 4"
  filtered | lz::format(std::cout, ", ", "{:02d}"); // prints "02, 04" (only with {fmt} installed or C++20's )
}

Installation

Options

The following CMake options are available, all of which are optional:

  • CPP_LAZY_USE_STANDALONE: Use the standalone version of cpp-lazy. This will not use the library {fmt}. Default is FALSE
  • CPP_LAZY_LZ_USE_MODULES: Use C++20 modules. Default is FALSE
  • CPP_LAZY_DEBUG_ASSERTIONS: Enable debug assertions. Default is TRUE for debug mode, FALSE for release.
  • CPP_LAZY_USE_INSTALLED_FMT: Use the system installed version of {fmt}. This will not use the bundled version. Default is FALSE and uses find_package. Use CPP_LAZY_FMT_DEP_VERSION to specify the version of {fmt} to use if needed.
  • CPP_LAZY_INSTALL: Install cpp-lazy targets and config files. Default is FALSE.
  • CPP_LAZY_FMT_DEP_VERSION: version of {fmt} to use. Used if CPP_LAZY_USE_INSTALLED_FMT is TRUE or CPP_LAZY_USE_STANDALONE is FALSE. May be empty.

Using FetchContent

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:

…

An alternative ('less' recommended), add to your CMakeLists.txt the following:

…

Using find_package

First, install the library. Then:

cmake
find_package(cpp-lazy 9.0.0 REQUIRED CONFIG)

Without CMake

Without {fmt}

  • Download the source code from the releases page
  • Specify the include directory to cpp-lazy/include.
  • Include files as follows:
cpp
// Important, preprocessor macro 'LZ_STANDALONE' has to be defined already
// or
// #define LZ_STANDALONE
#include 
#include 

int main() {
  std::array arr = {1, 2, 3, 4};
  auto result = lz::map(arr, [](int i) { return i + 1; }) | lz::to(); // == {2, 3, 4, 5}
  // or
  auto result = lz::to(arr | lz::map([](int i) { return i + 1; })); // == {2, 3, 4, 5}
}

With {fmt}

  • Clone the repository
  • Specify the include directory to cpp-lazy/include and fmt/include.
  • Define FMT_HEADER_ONLY before including any lz files.
  • Include files as follows:
cpp
#define FMT_HEADER_ONLY

#include 
#include 

int main() {
  std::array arr = {1, 2, 3, 4};
  auto result = lz::map(arr, [](int i) { return i + 1; }) | lz::to(); // == {2, 3, 4, 5}
  // or
  auto result = lz::to(arr | lz::map([](int i) { return i + 1; })); // == {2, 3, 4, 5}
}

Using git clone

Clone the repository using git clone https://github.com/Kaaserne/cpp-lazy/ and add to CMakeLists.txt the following:

cmake
add_subdirectory(cpp-lazy)
add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} cpp-lazy::cpp-lazy)

Benchmarks

The time is equal to one iteration. One iteration excludes the creation of the iterable and includes one iteration of that iterable. Compiled with: gcc version 13.3.0.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C++

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言