Git Product home page Git Product logo

any_iterator's Introduction

Type Erased Iterators for modern C++

This repository contains a proposal to add any_iterator and some helper alias templates to the C++ standard library.

Where is the Proposal?

The proposal paper can be found as "any_iterator.md" in this respository (link here).

Is there a sample implementation?

Yes, there is a sample implementation of that proposal within the "implementation" folder.

To set-up and build the sample implementation and tests, perform the following steps:

git clone [email protected]:Shaktal/any_iterator.git any_iterator
cd any_iterator/implementation && cmake . && make AnyIteratorTests && ./AnyIteratorTests

This will build the test suite for the sample implementation. Please feel free to add test cases as desired and submit them in a PR, or alternatively, raise any issues that you discover.

Comments on the proposal paper are also welcomed: please raise them as issues on the github page.

Example Usage

An example program using the any_iterator sample implementation might look something like the following:

using namespace sample;
void capitalize(any_input_iterator<std::string> first,
                any_input_iterator<std::string> last,
                any_output_iterator<std::string> d_outputs)
{
    std::transform(first, last, d_outputs, [](auto str) {
        // Ignore the `::toupper` potential UB ;)
        std::transform(begin(str), end(str), begin(str), ::toupper);
        return str;
    });
}

int main()
{
    capitalize(std::istream_iterator<std::string>(std::cin),
               std::istream_iterator<std::string>(),
               std::ostream_iterator<std::string>(std::cout, "\n"));
}

Benchmarks

There are some simple microbenchmarks included within the implementation/benchmarks folder, using Google Benchmark. These are provided to give some perspective on the relative performance of using the iterator directly or using an any_iterator.

These can be built by cloning the repository as above and initializing CMake (preferably using the -DCMAKE_BUILD_TYPE=Release flag) and then calling make AnyIteratorBenchmarks.

On my machine (Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8, 7.7GiB DDR4 RAM) I get the following results:

---------------------------------------------------------------------------------------------------------
Benchmark                                                                  Time           CPU Iterations
---------------------------------------------------------------------------------------------------------
BM_IteratorCreation<sample::any_input_iterator<int>>/200                   6 ns          6 ns  111440708
BM_IteratorCreation<ContainerType::iterator>/200                           1 ns          1 ns  819729215
BM_IteratorCopy<sample::any_input_iterator<int>>/200                       6 ns          6 ns  112859931
BM_IteratorCopy<ContainerType::iterator>/200                               0 ns          0 ns 1000000000
BM_IteratorCopyToOutput<sample::any_input_iterator<int>>/200            1267 ns       1266 ns     527775
BM_IteratorCopyToOutput<ContainerType::iterator>/200                      23 ns         23 ns   30847233
BM_IteratorOutputIt<sample::any_output_iterator<int>>/200               1356 ns       1355 ns     571798
BM_IteratorOutputIt<std::back_insert_iterator<ContainerType>>/200        761 ns        760 ns     905092

any_iterator's People

Contributors

ajbennieston avatar shaktal avatar

Watchers

 avatar

Forkers

ajbennieston

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.