GithubHelp home page GithubHelp logo

rangeunion's Introduction

Introduction

CMake
Imagine, you have several ranges and you want to iterate over each element defined by the rangers just one time. The ranges may be not disjunct.

For this problem, the class 'range_union' is provided. You can add non disjunct ranges to the union and request iterators which will iterate over all elements.

Complexity

Inserting and deleting new ranges into range_union is very cost efficient. Inserting costs at max one upper and one lower bound which means, only 'O(log2(n))' where n is the number of ranges inside the union. Delete operations performs one 'equal_range' operation which is again only 'O(log2(n))' where n is the number of ranges inside the union.

Both operations performs also a 'erase' operation which complexity depends on the container which is used. For this reason, the 'range_union' template provides a parameter for the container type. Suggested containers are 'std::vector' and 'std::list'.

When you have more insert/delete operations, 'std::list' is your choice, but the iteration over all the elements costs more. The container type 'std::vector' provides faster iteration, but the performance of the insert/delete operation is slower than 'std::list' container provides.

Dependencies

  • C++98

Example

More examples can be found in the test files.

#include <numeric>
#include <vector>
#include <iostream>
#include <range_union.hpp>

using namespace tyti;

int main()
{
    using range_t = pair_range<std::vector<int>::iterator>;
    range_t range;
    range_union< range_t > rUnion;
    std::vector<int> numbers(25);
    std::iota(numbers.begin(), numbers.end(), 0);

    range.first = numbers.begin() + 5;
    range.second = numbers.begin() + 10;

    rUnion += range;
    for (const auto& obj : rUnion)
        std::cout << *obj << " ";
        //output: 5 6 7 8 9
    std::cout << std::endl;

    range.first = numbers.begin() + 7;
    range.second = numbers.begin() + 15;
    
    rUnion += range;
    for (const auto& obj : rUnion)
        std::cout << *obj << " ";
        //output: 5 6 7 8 9 10 11 12 13 14 
    std::cout << std::endl;

    range.first = numbers.begin() + 6;
    range.second = numbers.begin() + 12;

    rUnion -= range;
    for (const auto& obj : rUnion)
        std::cout << *obj << " ";
        //output: 5 12 13 14
    std::cout << std::endl;

    range.first = numbers.begin() + 3;
    range.second = numbers.begin() + 18;

    rUnion += range;
    for (const auto& obj : rUnion)
        std::cout << *obj << " ";
        //output: 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
    std::cout << std::endl;
}

License

MIT License © Matthias Möller. Made with ♥ in Germany.

rangeunion's People

Contributors

tinytinni avatar

Watchers

 avatar  avatar

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.