• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    C++
  • License
    MIT License
  • Created over 5 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿƒ Iterators made easy! Zero cost abstractions for designing and using C++ iterators.

Actions Status Actions Status Actions Status Actions Status Actions Status codecov

EasyIterator

C++ iterators and range-based loops are incredibly useful, however defining iterators still requires a large amount of boilerplate code. The goal of this library is to find alternative and useful ways to use and create C++17 iterators without impacting performance or compiler optimizations.

Example

Iteration

EasyIterator adds well-known generators and iterator combinators from other languages to C++, such as range, zip and enumerate.

using namespace easy_iterator;

std::vector<int> integers(10);
std::vector<std::string> strings(integers.size());

for (auto i: range(integers.size())) {
  integers[i] = i*i;
}

for (auto [i, v, s]: zip(range(integers.size()), integers, strings)) {
  s = std::to_string(i) + "^2 = " + std::to_string(v);
}

for (auto [i, s]: enumerate(strings)) {
  std::cout << "strings[" << i << "] = \"" << s << "\"" << std::endl;
}

Iterator definition

Most iterator boilerplate code is defined in an easy_iterator::IteratorPrototype base class type. A possible implementation of the range iterable is below.

using namespace easy_iterator;

template <class T> struct RangeIterator: public IteratorPrototype<T, dereference::ByValue> {
  T increment;

  RangeIterator(const T &start):
    IteratorPrototype<T, dereference::ByValue>(start),
    increment(1) {
  }

  RangeIterator &operator++(){ RangeIterator::value += increment; return *this; }
};

template <class T> auto range(T end) {
  return wrap(RangeIterator<T>(begin), RangeIterator<T>(end));
}

Iterable algorithms

Algorithms can be easily wrapped into iterators by defining a class that defines advance() and value() member functions. The code below shows how to define an iterator over Fibonacci numbers.

struct Fibonacci {
  unsigned current = 0;
  unsigned next = 1;

  void advance() {
    auto tmp = next;
    next += current;
    current = tmp;
  }
  
  unsigned value() {
    return current;
  }
};

using namespace easy_iterator;

for (auto [i,v]: enumerate(MakeIterable<Fibonacci>())){
  std::cout << "Fib_" << i << "\t= " << v << std::endl;
  if (i == 10) break;
}

Algorithms that have an end state can also be defined by returning a the state in the advance() method. If the initial state can also be undefined, the iterator should define a bool init() method and inherit from easy_iterator::InitializedIterable. The code below shows an alternative range implementation.

template <class T> struct RangeIterator: public easy_iterator::InitializedIterable {
  T current, max, step;
  RangeIterator(T end): current(0), max(end), step(1) { }
  bool advance(){ current += step; return current != max; }
  bool init(){ return current != max; }
  T value(){ return current; }
};

template <class T> auto range(T end) {
  return easy_iterator::MakeIterable<RangeIterator<T>>(end);
}

Installation and usage

EasyIterator is a single-header library, so you can simply download and copy the header into your project, or use the Cmake script to install it globally. Using the CPM dependency manager, you can also include EasyIterator simply by adding the following to your projects' CMakeLists.txt.

CPMAddPackage(
  NAME EasyIterator
  VERSION 1.4
  GIT_REPOSITORY https://github.com/TheLartians/EasyIterator.git
)

target_link_libraries(myProject EasyIterator)            
set_target_properties(myProject PROPERTIES CXX_STANDARD 17)        

Test suite

You can run the tests suite included in this repo with the following commands.

cmake -Htest -Bbuild/test
cmake --build build/test
cmake --build build/test --target test

Performance

EasyIterator is designed to come with little or no performance impact compared to handwritten code. For example, using for(auto i: range(N)) loops create identical assembly compared to regular for(auto i=0;i<N;++i) loops (using clang++ -O2). The performance of different methods and approaches can be compared with the included benchmark suite. You can build and run the benchmark with the following commands:

cmake -Hbenchmark -Bbuild/bench -DCMAKE_BUILD_TYPE=Release
cmake --build build/bench -j8
./build/bench/EasyIteratorBenchmark

More Repositories

1

ModernCppStarter

๐Ÿš€ Kick-start your C++! A template for modern C++ projects using CMake, CI, code coverage, clang-format, reproducible dependency management and much more.
CMake
4,417
star
2

PEGParser

๐Ÿ’ก Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
C++
240
star
3

modern-wasm-starter

๐Ÿ›ธ Run C++ code on web and create blazingly fast websites! A starter template to easily create WebAssembly packages using type-safe C++ bindings with automatic TypeScript declarations.
TypeScript
239
star
4

Format.cmake

๐Ÿ’… Stylize your code! Automatic clang-format and cmake-format targets for CMake.
Python
159
star
5

Observe

๐Ÿ“ฃ Hey listen! A simple general-purpose event-listener system for C++17.
CMake
126
star
6

PackageProject.cmake

๐Ÿ›๏ธ Help other developers use your project. A CMake script for packaging C/C++ projects for simple project installation while employing best-practices for maximum compatibility.
CMake
103
star
7

MiniCppStarter

๐Ÿงธ A tiny single-file version of the ModernCppStarter project for exploring libraries or playing with C++ code. Reproducible dependency management included, so the code will work the same everywhere.
CMake
75
star
8

StaticTypeInfo

๐Ÿ€ Up your type-game. A small C++ library for compile-time type names and type indices.
CMake
70
star
9

Ccache.cmake

๐Ÿš… Compile faster with Ccache! A Ccache integration for CMake with Xcode support.
CMake
58
star
10

Glue

โ›“๏ธ Bindings that stick. A simple and generic API for C++ to other language bindings supporting bidirectional communication, inheritance and automatic declarations.
C++
56
star
11

TypeScriptXX

๐Ÿงท Stay safe! Type-safe scripting for C++ using TypeScriptToLua and CMake with auto-generated declarations.
CMake
40
star
12

GroupSourcesByFolder.cmake

Automatically group sources by folder structure for Visual Studio/Xcode generators
CMake
32
star
13

BitLens

๐Ÿ”Ž Have your bits and eat them too! A C++17 bit lens container for vector types.
C++
21
star
14

EmGlue

๐Ÿ•ธ๏ธ Glue C++ to your browser! Universal bindings for JavaScript/Wasm using Glue and Embind.
C++
21
star
15

StaticHash

Constexpr hash functions for C++
C++
18
star
16

substitute

๐ŸฟEnjoy the the big screen! Watch and synchronise subtitles externally in any browser. On mobile and desktop.
TypeScript
16
star
17

TypeScript2Python

๐Ÿšƒ Transpile TypeScript types to Python! A TypeScript to Python type transpiler.
TypeScript
15
star
18

Revisited

๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ The visitor pattern revisited. An inheritance-aware acyclic visitor template, any and any-function templates.
C++
15
star
19

react-native-simple-transition

๐ŸŒ  An easy to use transition component for React Native
Java
13
star
20

Generator

A generator class emulating coroutines' yield functionality through std::thread
C++
9
star
21

PyPropagate

A paraxial wave propagation framework for python
Jupyter Notebook
5
star
22

NDArray

[legacy project] A fast n-dimensional array template for C++
C++
5
star
23

LuaGlue

Lua bindings for the Glue library
C++
4
star
24

LHC

[legacy project] Some single-header helper libraries I use in some projects
C++
4
star
25

IndexSet

A class for manipulating large sets of indices with optimal performance and memory use
CMake
3
star
26

denon-cli

A simple command line interface for controlling Denon AVR receivers.
JavaScript
2
star
27

Presentations

https://thelartians.github.io/Presentations
JavaScript
2
star
28

Saphira

Our submission for the #WirVsVirus Hackathron: https://youtu.be/OWfDj2fOfVk | https://thelartians.github.io/Saphira/
TypeScript
2
star
29

NDArrayOld

A fast n-dimensional array template for c++
C++
2
star
30

Lars

A cmake enabled git-submodule collection of my C++ toolbox
C++
1
star
31

advent-of-code-2020

โ˜ƒ๏ธ๐ŸŽ„๐Ÿ‘จโ€๐Ÿ’ป Solving Advent of Code 2020 in Rust ๐Ÿฆ€
Rust
1
star