libcruft-util/iterator/indices.hpp

74 lines
1.8 KiB
C++
Raw Normal View History

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2010-2018 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
namespace cruft::iterator {
///////////////////////////////////////////////////////////////////////////
template <typename ContainerT>
class indices {
public:
using value_type = std::size_t;
indices (const ContainerT &_container):
m_container (_container)
{ ; }
class iterator {
public:
using iterator_category = std::forward_iterator_tag;
iterator (value_type _index):
m_index (_index)
{ ; }
bool
operator!= (const iterator &rhs) const
{
return m_index != rhs.m_index;
}
bool
operator== (const iterator &rhs) const
{
return m_index == rhs.m_index;
}
iterator&
operator++ (void) &
{
++m_index;
return *this;
};
const value_type&
operator* (void) const&
{
return m_index;
}
private:
value_type m_index;
};
iterator begin (void) const { return iterator { value_type {0} }; }
iterator end (void) const { return iterator { m_container.size () }; }
constexpr auto size (void) const noexcept
{
return std::size (m_container);
}
private:
const ContainerT &m_container;
};
template <typename ContainerT>
indices (ContainerT const&) -> indices<ContainerT>;
}