All Projects → TheLartians → Easyiterator

TheLartians / Easyiterator

Licence: mit
🏃 Iterators made easy! Zero cost abstractions for designing and using C++ iterators.

Programming Languages

cpp
1120 projects
cplusplus
227 projects

Projects that are alternatives of or similar to Easyiterator

Mir Algorithm
Dlang Core Library
Stars: ✭ 143 (+33.64%)
Mutual labels:  algorithms, iterator, range
Styled React Boilerplate
Minimal & Modern boilerplate for building apps with React & styled-components
Stars: ✭ 198 (+85.05%)
Mutual labels:  simple, boilerplate
Range V3
Range library for C++14/17/20, basis for C++20's std::ranges
Stars: ✭ 3,231 (+2919.63%)
Mutual labels:  iterator, range
Range Slider
The simplest JavaScript custom range slider ever!
Stars: ✭ 41 (-61.68%)
Mutual labels:  simple, range
staticstep
Provides truly zero-cost alternatives to Iterator::step_by for both incrementing and decrementing any type that satisfies RangeBounds<T: Copy + Default + Step>.
Stars: ✭ 13 (-87.85%)
Mutual labels:  iterator, range
Functionalplus
Functional Programming Library for C++. Write concise and readable C++ code.
Stars: ✭ 1,286 (+1101.87%)
Mutual labels:  algorithms, range
Hapi Starter Kit
Hapi.js based REST boilerplate which uses latest ES7/ES8 features (async/await) with code coverage and follows best pratices
Stars: ✭ 103 (-3.74%)
Mutual labels:  boilerplate
2018 Qwb Ctf
2018强网杯CTF___题目整理
Stars: ✭ 106 (-0.93%)
Mutual labels:  reverse
Frontbook
📖 FrontBook is a small and modern frontend boilerplate, enabling you to write ES201* today in production-ready projects.
Stars: ✭ 102 (-4.67%)
Mutual labels:  boilerplate
Starterkit
Kirby's sample site – the easiest way to get started with Kirby
Stars: ✭ 102 (-4.67%)
Mutual labels:  boilerplate
Code With Love
Open source programming algorithms
Stars: ✭ 107 (+0%)
Mutual labels:  algorithms
Element Ui Admin
基于 element-ui 的单页面后台管理项目模版
Stars: ✭ 106 (-0.93%)
Mutual labels:  boilerplate
Coursera Data Structures Algorithms
Coursera: Data Structures and Algorithms Specialization
Stars: ✭ 105 (-1.87%)
Mutual labels:  algorithms
Aqua
💡 A website and user system starter
Stars: ✭ 1,391 (+1200%)
Mutual labels:  boilerplate
Simple aspnet auth
Simple ASP.NET Authorisation boilerplate project. No EF, no database, no IdentityServer4 just a basic logging in system for both cookies and JWT and a controller with a set of examples.
Stars: ✭ 105 (-1.87%)
Mutual labels:  simple
Bootstrap 4 Sass Gulp 4 Boilerplate
A Bootstrap 4.5.2 boilerplate with font-awesome, sass, gulp 4 tasks
Stars: ✭ 103 (-3.74%)
Mutual labels:  boilerplate
Uva
800 UVa Online Judge solutions in Go
Stars: ✭ 106 (-0.93%)
Mutual labels:  algorithms
Wild Next
Our next.js boilerplate with sane base configuration.
Stars: ✭ 101 (-5.61%)
Mutual labels:  boilerplate
Node Flowtype Boilerplate
This boilerplate repository is outdated and no longer maintained. Instead, I strongly recommend to use TypeScript.
Stars: ✭ 104 (-2.8%)
Mutual labels:  boilerplate
Kaggle Dogs Vs Cats Caffe
Kaggle dogs vs cats solution in Caffe
Stars: ✭ 105 (-1.87%)
Mutual labels:  benchmark

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].