#pragma once

#include <vector>
#include <cstddef>

template<class It>
class IteratorRange {
 public:
    IteratorRange(It b, It e) : begin_(b), end_(e),
                                size_(std::distance(b, e)) {}

    typedef It iterator;

    It begin() const {
        return begin_;
    }

    It end() const {
        return end_;
    }

    size_t size() const {
        return size_;
    }

 private:
    It begin_, end_;
    size_t size_;
};

template<typename It>
class Slicer {
  public:
    Slicer(It b, It e, int s) {
        It end_ = e;
        It cur = b;
        while (cur != end_) {
            int size = s < end_ - cur ? s : end_ - cur;
            pages_.push_back(IteratorRange<It>(cur, cur + size));
            cur += size;
        }
    }

    std::size_t size() const {
        return pages_.size();
    }

    typename std::vector<IteratorRange<It>>::iterator begin() {
        return pages_.begin();
    }

    typename std::vector<IteratorRange<It>>::iterator end() {
        return pages_.end();
    }

 private:
    std::vector<IteratorRange<It>> pages_;
};

template <typename C>
Slicer<typename C::iterator> Slice(C &c, std::size_t page_size) {
    return Slicer<typename C::iterator>(c.begin(), c.end(), page_size);
}

template <typename C>
Slicer<typename C::const_iterator> Slice(const C &c, std::size_t page_size) {
    return Slicer<typename C::const_iterator>(c.begin(), c.end(), page_size);
}