Newer
Older
libj / include / j / util / iterator.hpp
/**
 * @file iterator.hpp
 * @brief J Library Iterator ヘッダファイル。
 * @copyright  2001 - 2024  Nomura Kei
 * @depends
 *   j.hpp
 */
#ifndef J_UTIL_ITERATOR_HPP
#define J_UTIL_ITERATOR_HPP

#include <vector>
#include <algorithm>

#include <j.hpp>

namespace j
{
    namespace util
    {

        template <typename T>
        class Iterator
        {
        private:
            typename std::vector<T>::const_iterator current;
            typename std::vector<T>::const_iterator end;

        public:
            Iterator(typename std::vector<T>::const_iterator start, typename std::vector<T>::const_iterator end) : current(start), end(end) {}
            virtual ~Iterator() = default;

            /**
             * 反復処理でさらに要素がある場合に true を返します。
             *
             * @return 反復処理でさらに要素がある場合は true
             */
            virtual bool hasNext() const
            {
                return (current != end);
            }

            /**
             * 反復処理で次の要素を返します。
             *
             * @return 反復処理での次の要素
             */
            virtual T &next()
            {
                if (!hasNext())
                {
                    // TODO
                }
                return const_cast<T &>(*current++);
            }

            T &operator*() const
            {
                return const_cast<T &>(*current);
            }

            Iterator &operator++()
            {
                current++;
                return *this;
            }

            Iterator operator++(int)
            {
                Iterator tmp = *this;
                current++;
                return tmp;
            }

            bool operator==(const Iterator &ite) const
            {
                return (current == ite.current);
            }

            bool operator!=(const Iterator &ite) const
            {
                return (current != ite.current);
            }

            using iterator_category = std::input_iterator_tag;
            using value_type = T;
            using difference_type = std::ptrdiff_t;
            using pointer = T *;
            using reference = T &;
        };

    } // namespace util
} // namespace j

#endif // J_UTIL_COLLECTION_HPP