ListIterator.hpp #2

  • //
  • guest/
  • ShadauxCat/
  • Sprawl/
  • Mainline/
  • collections/
  • iterator/
  • ListIterator.hpp
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#pragma once
#include <iterator>
#include "../../common/specialized.hpp"

namespace sprawl
{
	namespace collections
	{
		template<typename T>
		class List;

		template<typename T>
		class ForwardList;
	}

	template<typename ValueType, typename WrapperType>
	class ListIterator : public std::iterator<std::forward_iterator_tag, ValueType, std::ptrdiff_t, ValueType*, ValueType&>
	{
	public:
		typedef WrapperType* accessor_type;
		ListIterator(WrapperType* item)
			: m_currentItem(item)
		{
			//
		}

		ValueType& operator*()
		{
			return m_currentItem->m_value;
		}

		ValueType* operator->()
		{
			return &m_currentItem->m_value;
		}


		ValueType const& operator*() const
		{
			return m_currentItem->m_value;
		}

		ValueType const* operator->() const
		{
			return &m_currentItem->m_value;
		}

		ValueType& Value()
		{
			return m_currentItem->m_value;
		}

		ValueType const& Value() const
		{
			return m_currentItem->m_value;
		}

		ListIterator<ValueType, WrapperType>& operator++()
		{
			m_currentItem = m_currentItem ? m_currentItem->next : nullptr;
			return *this;
		}

		ListIterator<ValueType, WrapperType> operator++(int)
		{
			ListIterator<ValueType, WrapperType> tmp(*this);
			++(*this);
			return tmp;
		}

		ListIterator<ValueType, WrapperType> const& operator++() const
		{
			m_currentItem = m_currentItem ? m_currentItem->next : nullptr;
			return *this;
		}

		ListIterator<ValueType, WrapperType> const operator++(int) const
		{
			ListIterator<ValueType, WrapperType> tmp(*this);
			++(*this);
			return tmp;
		}

		ListIterator<ValueType, WrapperType> operator+(int steps)
		{
			WrapperType* item = m_currentItem;
			for(int i = 0; i < steps; ++i)
			{
				if(!item)
				{
					break;
				}
				item = item->next;
			}
			return ListIterator<ValueType, WrapperType>(item);
		}

		ListIterator<ValueType, WrapperType> const operator+(int steps) const
		{
			WrapperType* item = m_currentItem;
			for(int i = 0; i < steps; ++i)
			{
				if(!item)
				{
					break;
				}
				item = item->next;
			}
			return ListIterator<ValueType, WrapperType>(item);
		}

		bool operator==(ListIterator<ValueType, WrapperType> const& rhs) const
		{
			return m_currentItem == rhs.m_currentItem;
		}

		bool operator!=(ListIterator<ValueType, WrapperType> const& rhs) const
		{
			return !this->operator==(rhs);
		}

		operator bool() const
		{
			return m_currentItem != nullptr;
		}

		bool operator!() const
		{
			return m_currentItem != nullptr;
		}

		bool Valid() const
		{
			return m_currentItem != nullptr;
		}

		bool More() const
		{
			return m_currentItem != nullptr && m_currentItem->next != nullptr;
		}

		ListIterator<ValueType, WrapperType> Next()
		{
			return ListIterator<ValueType, WrapperType>(m_currentItem ? m_currentItem->next : nullptr);
		}

		ListIterator<ValueType, WrapperType> const Next() const
		{
			return ListIterator<ValueType, WrapperType>(m_currentItem ? m_currentItem->next : nullptr);
		}

		operator bool()
		{
			return m_currentItem != nullptr;
		}

	protected:
		friend class sprawl::collections::List<ValueType>;
		friend class sprawl::collections::ForwardList<ValueType>;
		mutable WrapperType* m_currentItem;
	};
}
# Change User Description Committed
#3 14091 ShadauxCat -Created Vector class, iterator, and unit test
-Made list iterator a bidirectional iterator when created from a doubly-linked list. (It still has operator-- when created from singly-linked list, but it won't compile if it's used.)
-Changed front() and back() in list classes to Front() and Back()

#review-14083
#2 12508 ShadauxCat -Added threading library.
Currently only functional for Linux; Windows will fail to link. (I will fix this soon.)
-Fixed missing move and copy constructors in List and ForwardList
-Fixed broken move constructor in HashMap
-Fixed missing const get() in HashMap
-Fixed broken operator-> in ListIterator
-Added sprawl::noncopyable
-Added sketch headers for filesystem library
-Made StringLiteral hashable, added special hashes for pointers and integers in murmur3
-Fixed compiler warning in async_network
-Updated memory allocators to use new threading library for mutexes
-Added accessibility to sprawl::StringLiteral to be able toa ccess its pointer and length and perform pointer comparisons

#review-12504
#1 11496 ShadauxCat Initial checkin: Current states for csbuild and libSprawl