{"id":14957355,"url":"https://github.com/thelartians/easyiterator","last_synced_at":"2025-10-11T04:02:24.323Z","repository":{"id":88218117,"uuid":"185423881","full_name":"TheLartians/EasyIterator","owner":"TheLartians","description":"🏃 Iterators made easy! Zero cost abstractions for designing and using C++ iterators.","archived":false,"fork":false,"pushed_at":"2024-10-14T15:33:27.000Z","size":87,"stargazers_count":143,"open_issues_count":1,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-18T07:03:31.430Z","etag":null,"topics":["advance","algorithms","benchmark","boilerplate","cplusplus","cpp","enumerate","iterator","iterators","loops","next","pythonic","range","reverse","simple"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TheLartians.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-05-07T14:56:12.000Z","updated_at":"2025-03-29T14:52:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"b2ea3fa5-3090-46f2-8113-87fdb7a1e447","html_url":"https://github.com/TheLartians/EasyIterator","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/TheLartians/EasyIterator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheLartians%2FEasyIterator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheLartians%2FEasyIterator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheLartians%2FEasyIterator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheLartians%2FEasyIterator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TheLartians","download_url":"https://codeload.github.com/TheLartians/EasyIterator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheLartians%2FEasyIterator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279006112,"owners_count":26084027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["advance","algorithms","benchmark","boilerplate","cplusplus","cpp","enumerate","iterator","iterators","loops","next","pythonic","range","reverse","simple"],"created_at":"2024-09-24T13:14:47.184Z","updated_at":"2025-10-11T04:02:24.312Z","avatar_url":"https://github.com/TheLartians.png","language":"C++","readme":"[![Actions Status](https://github.com/TheLartians/EasyIterator/workflows/MacOS/badge.svg)](https://github.com/TheLartians/EasyIterator/actions)\n[![Actions Status](https://github.com/TheLartians/EasyIterator/workflows/Windows/badge.svg)](https://github.com/TheLartians/EasyIterator/actions)\n[![Actions Status](https://github.com/TheLartians/EasyIterator/workflows/Ubuntu/badge.svg)](https://github.com/TheLartians/EasyIterator/actions)\n[![Actions Status](https://github.com/TheLartians/EasyIterator/workflows/Style/badge.svg)](https://github.com/TheLartians/EasyIterator/actions)\n[![Actions Status](https://github.com/TheLartians/EasyIterator/workflows/Install/badge.svg)](https://github.com/TheLartians/EasyIterator/actions)\n[![codecov](https://codecov.io/gh/TheLartians/EasyIterator/branch/master/graph/badge.svg)](https://codecov.io/gh/TheLartians/EasyIterator)\n\n# EasyIterator\n\nC++ iterators and range-based loops are incredibly useful, however defining iterators still requires a large amount of boilerplate code.\nThe goal of this library is to find alternative and useful ways to use and create C++17 iterators without impacting performance or compiler optimizations.\n\n## Example\n\n### Iteration\n\nEasyIterator adds well-known generators and iterator combinators from other languages to C++, such as `range`, `zip` and `enumerate`. \n\n```cpp\nusing namespace easy_iterator;\n\nstd::vector\u003cint\u003e integers(10);\nstd::vector\u003cstd::string\u003e strings(integers.size());\n\nfor (auto i: range(integers.size())) {\n  integers[i] = i*i;\n}\n\nfor (auto [i, v, s]: zip(range(integers.size()), integers, strings)) {\n  s = std::to_string(i) + \"^2 = \" + std::to_string(v);\n}\n\nfor (auto [i, s]: enumerate(strings)) {\n  std::cout \u003c\u003c \"strings[\" \u003c\u003c i \u003c\u003c \"] = \\\"\" \u003c\u003c s \u003c\u003c \"\\\"\" \u003c\u003c std::endl;\n}\n```\n\n### Iterator definition\n\nMost iterator boilerplate code is defined in an `easy_iterator::IteratorPrototype` base class type.\nA possible implementation of the `range` iterable is below.\n\n```cpp\nusing namespace easy_iterator;\n\ntemplate \u003cclass T\u003e struct RangeIterator: public IteratorPrototype\u003cT, dereference::ByValue\u003e {\n  T increment;\n\n  RangeIterator(const T \u0026start):\n    IteratorPrototype\u003cT, dereference::ByValue\u003e(start),\n    increment(1) {\n  }\n\n  RangeIterator \u0026operator++(){ RangeIterator::value += increment; return *this; }\n};\n\ntemplate \u003cclass T\u003e auto range(T end) {\n  return wrap(RangeIterator\u003cT\u003e(begin), RangeIterator\u003cT\u003e(end));\n}\n```\n\n### Iterable algorithms\n\nAlgorithms 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.\n\n```cpp\nstruct Fibonacci {\n  unsigned current = 0;\n  unsigned next = 1;\n\n  void advance() {\n    auto tmp = next;\n    next += current;\n    current = tmp;\n  }\n  \n  unsigned value() {\n    return current;\n  }\n};\n\nusing namespace easy_iterator;\n\nfor (auto [i,v]: enumerate(MakeIterable\u003cFibonacci\u003e())){\n  std::cout \u003c\u003c \"Fib_\" \u003c\u003c i \u003c\u003c \"\\t= \" \u003c\u003c v \u003c\u003c std::endl;\n  if (i == 10) break;\n}\n```\n\nAlgorithms 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.\n\n```cpp\ntemplate \u003cclass T\u003e struct RangeIterator: public easy_iterator::InitializedIterable {\n  T current, max, step;\n  RangeIterator(T end): current(0), max(end), step(1) { }\n  bool advance(){ current += step; return current != max; }\n  bool init(){ return current != max; }\n  T value(){ return current; }\n};\n\ntemplate \u003cclass T\u003e auto range(T end) {\n  return easy_iterator::MakeIterable\u003cRangeIterator\u003cT\u003e\u003e(end);\n}\n```\n\n## Installation and usage\n\nEasyIterator 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.\nUsing the [CPM](https://github.com/cpm-cmake/CPM.cmake) dependency manager, you can also include EasyIterator simply by adding the following to your projects' `CMakeLists.txt`.\n\n```cmake\nCPMAddPackage(\"gh:thelartians/easyiterator@1.5\")\n\ntarget_link_libraries(myProject EasyIterator)            \nset_target_properties(myProject PROPERTIES CXX_STANDARD 17)        \n```\n\n## Test suite\n\nYou can run the tests suite included in this repo with the following commands.\n\n```bash\ncmake -Htest -Bbuild/test\ncmake --build build/test\ncmake --build build/test --target test\n```\n\n## Performance\n\nEasyIterator 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\u003cN;++i)` loops (using `clang++ -O2`).\nThe performance of different methods and approaches can be compared with the included benchmark suite. \nYou can build and run the benchmark with the following commands:\n\n```bash\ncmake -Hbenchmark -Bbuild/bench -DCMAKE_BUILD_TYPE=Release\ncmake --build build/bench -j8\n./build/bench/EasyIteratorBenchmark\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthelartians%2Feasyiterator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthelartians%2Feasyiterator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthelartians%2Feasyiterator/lists"}