#pragma once #include "../../common/specialized.hpp" #include "../../common/compat.hpp" namespace sprawl { template class TreeIterator; namespace collections { template< typename A, typename... B > class BinaryTree; namespace detail { enum class RedBlackColor : bool { Red, Black, }; template< typename A, typename B, size_t C, typename... D > class BinaryTree_Impl; template class TreeAccessorGroup_Impl; template class TreeAccessorGroup_Impl { public: ValueType& Value() { return m_value; } ValueType const& Value() const { return m_value; } ValueType& operator*() { return m_value; } ValueType* operator->() { return &m_value; } ValueType const& operator*() const { return m_value; } ValueType const* operator->() const { return &m_value; } inline NullAccessor& Accessor(Specialized) { static NullAccessor accessor; return accessor; } inline NullAccessor const& Accessor(Specialized) const { static NullAccessor accessor; return accessor; } protected: template< typename A, typename... B > friend class sprawl::collections::BinaryTree; template< typename A, typename B, size_t C, typename... D > friend class sprawl::collections::detail::BinaryTree_Impl; template friend class sprawl::TreeIterator; TreeAccessorGroup_Impl(ValueType const& value) : m_value(value) { // } TreeAccessorGroup_Impl(ValueType&& value) : m_value(std::move(value)) { // } TreeAccessorGroup_Impl(TreeAccessorGroup_Impl const& other) : m_value(other.m_value) { // } inline MostInheritedType* Left(Specialized) { return nullptr; } inline MostInheritedType const* Left(Specialized) const { return nullptr; } inline MostInheritedType* Right(Specialized) { return nullptr; } inline MostInheritedType const* Right(Specialized) const { return nullptr; } inline MostInheritedType* Parent(Specialized) { return nullptr; } inline MostInheritedType const* Parent(Specialized) const { return nullptr; } inline RedBlackColor Color(Specialized) { return RedBlackColor::Red; } inline void SetLeft(Specialized, MostInheritedType*) { // } inline void SetRight(Specialized, MostInheritedType*) { // } inline void SetParent(Specialized, MostInheritedType*) { // } inline void SetColor(Specialized, RedBlackColor) { // } ValueType m_value; }; template class TreeAccessorGroup_Impl : public TreeAccessorGroup_Impl { public: typedef TreeAccessorGroup_Impl Base; using Base::Accessor; inline AccessorType& Accessor(Specialized) { return m_thisAccessor; } inline AccessorType const& Accessor(Specialized) const { return m_thisAccessor; } protected: template< typename A, typename... B > friend class sprawl::collections::BinaryTree; template< typename A, typename B, size_t C, typename... D > friend class sprawl::collections::detail::BinaryTree_Impl; template friend class sprawl::TreeIterator; TreeAccessorGroup_Impl(ValueType const& value) : Base(value) , m_thisAccessor(this->m_value) , m_leftThisAccessor(nullptr) , m_rightThisAccessor(nullptr) , m_parentThisAccessor(nullptr) , m_colorThisAccessor(RedBlackColor::Red) { // } TreeAccessorGroup_Impl(ValueType&& value) : Base(std::move(value)) , m_thisAccessor(this->m_value) , m_leftThisAccessor(nullptr) , m_rightThisAccessor(nullptr) , m_parentThisAccessor(nullptr) , m_colorThisAccessor(RedBlackColor::Red) { // } TreeAccessorGroup_Impl(TreeAccessorGroup_Impl const& other) : Base(other) , m_thisAccessor(other.m_thisAccessor) , m_leftThisAccessor(nullptr) , m_rightThisAccessor(nullptr) , m_parentThisAccessor(nullptr) , m_colorThisAccessor(RedBlackColor::Red) { // } //Note: In all of these constructors, this->m_value isn't initialized yet. //But accessors hold a reference to it, and don't do anything with it during initialization. //So it's safe to give them the memory address even though we haven't initialized it yet. //Case one: Exactly two parameters, first one is key type, second one is value type. //Initialize with key, pass value up. template::value>::type> TreeAccessorGroup_Impl(Param1 const& key, ValueType const& value) : Base(value) , m_thisAccessor(this->m_value, key) , m_leftThisAccessor(nullptr) , m_rightThisAccessor(nullptr) , m_parentThisAccessor(nullptr) , m_colorThisAccessor(RedBlackColor::Red) { // } //Case two: Three or more parameters, first one is key type. Second one can't be value because value is last, so if we have a third parameter, second isn't value. //Initialize with key, pass remaining parameters up. template::value>::type> TreeAccessorGroup_Impl(Param1 const& key, Param2&& leftParam, Params&&... moreParams) : Base(std::forward(leftParam), std::forward(moreParams)...) , m_thisAccessor(this->m_value, key) , m_leftThisAccessor(nullptr) , m_rightThisAccessor(nullptr) , m_parentThisAccessor(nullptr) , m_colorThisAccessor(RedBlackColor::Red) { // } //Case three: Exactly two parameters, first one is not key type, second one's type doesn't matter. //Pass both parameters up. template::value>::type> TreeAccessorGroup_Impl(Param1&& firstParam, Param2&& leftParam) : Base(std::forward(firstParam), std::forward(leftParam)) , m_thisAccessor(this->m_value) , m_leftThisAccessor(nullptr) , m_rightThisAccessor(nullptr) , m_parentThisAccessor(nullptr) , m_colorThisAccessor(RedBlackColor::Red) { // } //Case four: More than two parameters, first one is not key type. //Pass all parameters up. template::value>::type> TreeAccessorGroup_Impl(Param1&& firstParam, Param2&& secondParam, Params&&... moreKeys) : Base(std::forward(firstParam), std::forward(secondParam), std::forward(moreKeys)...) , m_thisAccessor(this->m_value) , m_leftThisAccessor(nullptr) , m_rightThisAccessor(nullptr) , m_parentThisAccessor(nullptr) , m_colorThisAccessor(RedBlackColor::Red) { // } using Base::Left; inline MostInheritedType* Left(Specialized) { return m_leftThisAccessor; } inline MostInheritedType const* Left(Specialized) const { return m_leftThisAccessor; } using Base::Right; inline MostInheritedType* Right(Specialized) { return m_rightThisAccessor; } inline MostInheritedType const* Right(Specialized) const { return m_rightThisAccessor; } using Base::Parent; inline MostInheritedType* Parent(Specialized) { return m_parentThisAccessor; } inline MostInheritedType const* Parent(Specialized) const { return m_parentThisAccessor; } using Base::Color; inline RedBlackColor Color(Specialized) const { return m_colorThisAccessor; } using Base::SetLeft; inline void SetLeft(Specialized, MostInheritedType* left) { m_leftThisAccessor = left; } using Base::SetRight; inline void SetRight(Specialized, MostInheritedType* right) { m_rightThisAccessor = right; } using Base::SetParent; inline void SetParent(Specialized, MostInheritedType* right) { m_parentThisAccessor = right; } using Base::SetColor; inline void SetColor(Specialized, RedBlackColor color) { m_colorThisAccessor = color; } AccessorType m_thisAccessor; MostInheritedType* m_leftThisAccessor; MostInheritedType* m_rightThisAccessor; MostInheritedType* m_parentThisAccessor; RedBlackColor m_colorThisAccessor; }; template class TreeAccessorGroup : public TreeAccessorGroup_Impl, 0, Accessors...> { public: typedef TreeAccessorGroup_Impl, 0, Accessors...> Base; typedef Base* BasePtr; template TreeAccessorGroup(Param1&& firstParam, Param2&& secondParam, Params&&... params) : Base(std::forward(firstParam), std::forward(secondParam), std::forward(params)...) { // } TreeAccessorGroup(ValueType const& value) : Base(value) { // } TreeAccessorGroup(ValueType&& value) : Base(std::move(value)) { // } TreeAccessorGroup(TreeAccessorGroup const& other) : Base(other) { // } template auto Key() const -> decltype(BasePtr(nullptr)->Accessor(Specialized()).Key()) { return this->Accessor(Specialized()).Key(); } auto Key() const -> decltype(BasePtr(nullptr)->Accessor(Specialized<0>()).Key()) { return this->Accessor(Specialized<0>()).Key(); } }; } } }