/* Copyright 2022, Contributors To LensorOS.
* All rights reserved.
*
* This file is part of LensorOS.
*
* LensorOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LensorOS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LensorOS. If not, see <https://www.gnu.org/licenses
 */

#ifndef LENSOROS_ITERATOR_
#define LENSOROS_ITERATOR_

#include <type_traits>
#include <stddef.h>

namespace std {

template <typename _T>
class reverse_iterator {
    _T __it;

public:

    reverse_iterator() = default;
    constexpr explicit reverse_iterator(_T __it) : __it(__it) {}

    /// Returns the underlying iterator.
    constexpr _T base() const { return __it; }

    /// Post-increment.
    constexpr reverse_iterator operator++(int) {
        reverse_iterator __tmp = *this;
        ++(*this);
        return __tmp;
    }

    /// Pre-increment.
    constexpr reverse_iterator operator--(int) {
        reverse_iterator __tmp = *this;
        ++(*this);
        return __tmp;
    }

    constexpr auto operator++() -> reverse_iterator& { --__it; return *this; }
    constexpr auto operator--() -> reverse_iterator& { ++__it; return *this; }
    constexpr auto operator+(size_t __n) -> reverse_iterator { return reverse_iterator(__it - __n); }
    constexpr auto operator-(size_t __n) -> reverse_iterator { return reverse_iterator(__it + __n); }
    constexpr auto operator+=(size_t __n) -> reverse_iterator& { __it -= __n; return *this; }
    constexpr auto operator-=(size_t __n) -> reverse_iterator& { __it += __n; return *this; }
    constexpr bool operator==(const reverse_iterator& __other) const { return __it == __other.__it; }
    constexpr bool operator!=(const reverse_iterator& __other) const { return __it != __other.__it; }
    constexpr auto operator->() -> _T { return __it - 1; }
    constexpr auto operator*() const -> decltype(auto) { return *(__it - 1); }
};

/// Helper for creating iterators. This is an empty type that does nothing.
struct default_sentinel_t {};
inline constexpr default_sentinel_t default_sentinel{};

}

#endif // LENSOROS_ITERATOR_
