diff options
Diffstat (limited to 'src/server/shared')
-rw-r--r-- | src/server/shared/Containers.h | 14 | ||||
-rw-r--r-- | src/server/shared/Dynamic/TypeContainer.h | 70 | ||||
-rw-r--r-- | src/server/shared/Dynamic/TypeContainerFunctions.h | 137 | ||||
-rw-r--r-- | src/server/shared/Dynamic/TypeContainerVisitor.h | 39 | ||||
-rw-r--r-- | src/server/shared/Dynamic/TypeList.h | 1 |
5 files changed, 204 insertions, 57 deletions
diff --git a/src/server/shared/Containers.h b/src/server/shared/Containers.h index 58379e1fefa..0e5752ccd68 100644 --- a/src/server/shared/Containers.h +++ b/src/server/shared/Containers.h @@ -92,6 +92,20 @@ namespace Trinity return false; } + + template<class K, class V, template<class, class, class...> class M, class... Rest> + void MultimapErasePair(M<K, V, Rest...>& multimap, K const& key, V const& value) + { + auto range = multimap.equal_range(key); + for (auto itr = range.first; itr != range.second;) + { + if (itr->second == value) + itr = multimap.erase(itr); + else + ++itr; + } + } + } //! namespace Containers } diff --git a/src/server/shared/Dynamic/TypeContainer.h b/src/server/shared/Dynamic/TypeContainer.h index 2165945d119..b68dec00da7 100644 --- a/src/server/shared/Dynamic/TypeContainer.h +++ b/src/server/shared/Dynamic/TypeContainer.h @@ -25,6 +25,7 @@ */ #include <map> +#include <unordered_map> #include <vector> #include "Define.h" #include "Dynamic/TypeList.h" @@ -35,37 +36,40 @@ * By itself its meaningless but collaborate along with TypeContainers, * it become the most powerfully container in the whole system. */ -template<class OBJECT> struct ContainerMapList +template<class OBJECT> +struct ContainerMapList { //std::map<OBJECT_HANDLE, OBJECT *> _element; GridRefManager<OBJECT> _element; }; -template<> struct ContainerMapList<TypeNull> /* nothing is in type null */ +template<> +struct ContainerMapList<TypeNull> /* nothing is in type null */ { }; -template<class H, class T> struct ContainerMapList<TypeList<H, T> > +template<class H, class T> +struct ContainerMapList<TypeList<H, T> > { ContainerMapList<H> _elements; ContainerMapList<T> _TailElements; }; -/* - * @class ContaierArrayList is a multi-type container for - * array of elements. - */ -template<class OBJECT> struct ContainerArrayList +template<class OBJECT, class KEY_TYPE> +struct ContainerUnorderedMap +{ + std::unordered_map<KEY_TYPE, OBJECT*> _element; +}; + +template<class KEY_TYPE> +struct ContainerUnorderedMap<TypeNull, KEY_TYPE> { - std::vector<OBJECT> _element; }; -// termination condition -template<> struct ContainerArrayList<TypeNull> { }; -// recursion -template<class H, class T> struct ContainerArrayList<TypeList<H, T> > +template<class H, class T, class KEY_TYPE> +struct ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE> { - ContainerArrayList<H> _elements; - ContainerArrayList<T> _TailElements; + ContainerUnorderedMap<H, KEY_TYPE> _elements; + ContainerUnorderedMap<T, KEY_TYPE> _TailElements; }; /* @@ -101,14 +105,16 @@ class TypeMapContainer template<class SPECIFIC_TYPE> size_t Count() const { return Trinity::Count(i_elements, (SPECIFIC_TYPE*)NULL); } /// inserts a specific object into the container - template<class SPECIFIC_TYPE> bool insert(SPECIFIC_TYPE *obj) + template<class SPECIFIC_TYPE> + bool insert(SPECIFIC_TYPE *obj) { SPECIFIC_TYPE* t = Trinity::Insert(i_elements, obj); return (t != NULL); } /// Removes the object from the container, and returns the removed object - //template<class SPECIFIC_TYPE> bool remove(SPECIFIC_TYPE* obj) + //template<class SPECIFIC_TYPE> + //bool remove(SPECIFIC_TYPE* obj) //{ // SPECIFIC_TYPE* t = Trinity::Remove(i_elements, obj); // return (t != NULL); @@ -120,5 +126,35 @@ class TypeMapContainer private: ContainerMapList<OBJECT_TYPES> i_elements; }; + +template<class OBJECT_TYPES, class KEY_TYPE> +class TypeUnorderedMapContainer +{ +public: + template<class SPECIFIC_TYPE> + bool Insert(KEY_TYPE const& handle, SPECIFIC_TYPE* obj) + { + return Trinity::Insert(_elements, handle, obj); + } + + template<class SPECIFIC_TYPE> + bool Remove(KEY_TYPE const& handle) + { + return Trinity::Remove(_elements, handle, (SPECIFIC_TYPE*)NULL); + } + + template<class SPECIFIC_TYPE> + SPECIFIC_TYPE* Find(KEY_TYPE const& handle) + { + return Trinity::Find(_elements, handle, (SPECIFIC_TYPE*)NULL); + } + + ContainerUnorderedMap<OBJECT_TYPES, KEY_TYPE>& GetElements() { return _elements; } + ContainerUnorderedMap<OBJECT_TYPES, KEY_TYPE> const& GetElements() const { return _elements; } + +private: + ContainerUnorderedMap<OBJECT_TYPES, KEY_TYPE> _elements; +}; + #endif diff --git a/src/server/shared/Dynamic/TypeContainerFunctions.h b/src/server/shared/Dynamic/TypeContainerFunctions.h index a89f5bd10d4..bd6d4c348ba 100644 --- a/src/server/shared/Dynamic/TypeContainerFunctions.h +++ b/src/server/shared/Dynamic/TypeContainerFunctions.h @@ -28,60 +28,163 @@ #include "Define.h" #include "Dynamic/TypeList.h" #include <map> +#include <unordered_map> namespace Trinity { + // Helpers + // Insert helpers + template<class SPECIFIC_TYPE, class KEY_TYPE> + bool Insert(ContainerUnorderedMap<SPECIFIC_TYPE, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* obj) + { + auto i = elements._element.find(handle); + if (i == elements._element.end()) + { + elements._element[handle] = obj; + return true; + } + else + { + ASSERT(i->second == obj, "Object with certain key already in but objects are different!"); + return false; + } + } + + template<class SPECIFIC_TYPE, class KEY_TYPE> + bool Insert(ContainerUnorderedMap<TypeNull, KEY_TYPE>& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/) + { + return false; + } + + template<class SPECIFIC_TYPE, class KEY_TYPE, class T> + bool Insert(ContainerUnorderedMap<T, KEY_TYPE>& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/) + { + return false; + } + + template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T> + bool Insert(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* obj) + { + bool ret = Insert(elements._elements, handle, obj); + return ret ? ret : Insert(elements._TailElements, handle, obj); + } + + // Find helpers + template<class SPECIFIC_TYPE, class KEY_TYPE> + SPECIFIC_TYPE* Find(ContainerUnorderedMap<SPECIFIC_TYPE, KEY_TYPE> const& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* /*obj*/) + { + auto i = elements._element.find(handle); + if (i == elements._element.end()) + return nullptr; + else + return i->second; + } + + template<class SPECIFIC_TYPE, class KEY_TYPE> + SPECIFIC_TYPE* Find(ContainerUnorderedMap<TypeNull, KEY_TYPE> const& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/) + { + return nullptr; + } + + template<class SPECIFIC_TYPE, class KEY_TYPE, class T> + SPECIFIC_TYPE* Find(ContainerUnorderedMap<T, KEY_TYPE> const& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/) + { + return nullptr; + } + + template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T> + SPECIFIC_TYPE* Find(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE> const& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* /*obj*/) + { + SPECIFIC_TYPE* ret = Find(elements._elements, handle, (SPECIFIC_TYPE*)nullptr); + return ret ? ret : Find(elements._TailElements, handle, (SPECIFIC_TYPE*)nullptr); + } + + // Erase helpers + template<class SPECIFIC_TYPE, class KEY_TYPE> + bool Remove(ContainerUnorderedMap<SPECIFIC_TYPE, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* /*obj*/) + { + elements._element.erase(handle); + return true; + } + + template<class SPECIFIC_TYPE, class KEY_TYPE> + bool Remove(ContainerUnorderedMap<TypeNull, KEY_TYPE>& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/) + { + return false; + } + + template<class SPECIFIC_TYPE, class KEY_TYPE, class T> + bool Remove(ContainerUnorderedMap<T, KEY_TYPE>& /*elements*/, KEY_TYPE const& /*handle*/, SPECIFIC_TYPE* /*obj*/) + { + return false; + } + + template<class SPECIFIC_TYPE, class KEY_TYPE, class H, class T> + bool Remove(ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE>& elements, KEY_TYPE const& handle, SPECIFIC_TYPE* /*obj*/) + { + bool ret = Remove(elements._elements, handle, (SPECIFIC_TYPE*)nullptr); + return ret ? ret : Remove(elements._TailElements, handle, (SPECIFIC_TYPE*)nullptr); + } + /* ContainerMapList Helpers */ // count functions - template<class SPECIFIC_TYPE> size_t Count(const ContainerMapList<SPECIFIC_TYPE> &elements, SPECIFIC_TYPE* /*fake*/) + template<class SPECIFIC_TYPE> + size_t Count(ContainerMapList<SPECIFIC_TYPE> const& elements, SPECIFIC_TYPE* /*fake*/) { return elements._element.getSize(); } - template<class SPECIFIC_TYPE> size_t Count(const ContainerMapList<TypeNull> &/*elements*/, SPECIFIC_TYPE* /*fake*/) + template<class SPECIFIC_TYPE> + size_t Count(ContainerMapList<TypeNull> const& /*elements*/, SPECIFIC_TYPE* /*fake*/) { return 0; } - template<class SPECIFIC_TYPE, class T> size_t Count(const ContainerMapList<T> &/*elements*/, SPECIFIC_TYPE* /*fake*/) + template<class SPECIFIC_TYPE, class T> + size_t Count(ContainerMapList<T> const& /*elements*/, SPECIFIC_TYPE* /*fake*/) { return 0; } - template<class SPECIFIC_TYPE, class T> size_t Count(const ContainerMapList<TypeList<SPECIFIC_TYPE, T> >&elements, SPECIFIC_TYPE* fake) + template<class SPECIFIC_TYPE, class T> + size_t Count(ContainerMapList<TypeList<SPECIFIC_TYPE, T>> const& elements, SPECIFIC_TYPE* fake) { return Count(elements._elements, fake); } - template<class SPECIFIC_TYPE, class H, class T> size_t Count(const ContainerMapList<TypeList<H, T> >&elements, SPECIFIC_TYPE* fake) + template<class SPECIFIC_TYPE, class H, class T> + size_t Count(ContainerMapList<TypeList<H, T>> const& elements, SPECIFIC_TYPE* fake) { return Count(elements._TailElements, fake); } // non-const insert functions - template<class SPECIFIC_TYPE> SPECIFIC_TYPE* Insert(ContainerMapList<SPECIFIC_TYPE> &elements, SPECIFIC_TYPE *obj) - { + template<class SPECIFIC_TYPE> + SPECIFIC_TYPE* Insert(ContainerMapList<SPECIFIC_TYPE>& elements, SPECIFIC_TYPE* obj) { //elements._element[hdl] = obj; obj->AddToGrid(elements._element); return obj; } - template<class SPECIFIC_TYPE> SPECIFIC_TYPE* Insert(ContainerMapList<TypeNull> &/*elements*/, SPECIFIC_TYPE * /*obj*/) + template<class SPECIFIC_TYPE> + SPECIFIC_TYPE* Insert(ContainerMapList<TypeNull>& /*elements*/, SPECIFIC_TYPE* /*obj*/) { - return NULL; + return nullptr; } // this is a missed - template<class SPECIFIC_TYPE, class T> SPECIFIC_TYPE* Insert(ContainerMapList<T> &/*elements*/, SPECIFIC_TYPE * /*obj*/) + template<class SPECIFIC_TYPE, class T> + SPECIFIC_TYPE* Insert(ContainerMapList<T>& /*elements*/, SPECIFIC_TYPE* /*obj*/) { - return NULL; // a missed + return nullptr; // a missed } // Recursion - template<class SPECIFIC_TYPE, class H, class T> SPECIFIC_TYPE* Insert(ContainerMapList<TypeList<H, T> >&elements, SPECIFIC_TYPE *obj) + template<class SPECIFIC_TYPE, class H, class T> + SPECIFIC_TYPE* Insert(ContainerMapList<TypeList<H, T>>& elements, SPECIFIC_TYPE* obj) { - SPECIFIC_TYPE* t= Insert(elements._elements, obj); - return (t != NULL ? t : Insert(elements._TailElements, obj)); + SPECIFIC_TYPE* t = Insert(elements._elements, obj); + return (t != nullptr ? t : Insert(elements._TailElements, obj)); } //// non-const remove method @@ -93,20 +196,20 @@ namespace Trinity //template<class SPECIFIC_TYPE> SPECIFIC_TYPE* Remove(ContainerMapList<TypeNull> &/*elements*/, SPECIFIC_TYPE * /*obj*/) //{ - // return NULL; + // return nullptr; //} //// this is a missed //template<class SPECIFIC_TYPE, class T> SPECIFIC_TYPE* Remove(ContainerMapList<T> &/*elements*/, SPECIFIC_TYPE * /*obj*/) //{ - // return NULL; // a missed + // return nullptr; // a missed //} //template<class SPECIFIC_TYPE, class T, class H> SPECIFIC_TYPE* Remove(ContainerMapList<TypeList<H, T> > &elements, SPECIFIC_TYPE *obj) //{ // // The head element is bad // SPECIFIC_TYPE* t = Remove(elements._elements, obj); - // return ( t != NULL ? t : Remove(elements._TailElements, obj) ); + // return ( t != nullptr ? t : Remove(elements._TailElements, obj) ); //} } #endif diff --git a/src/server/shared/Dynamic/TypeContainerVisitor.h b/src/server/shared/Dynamic/TypeContainerVisitor.h index 514b52d3dfe..e10a2331e25 100644 --- a/src/server/shared/Dynamic/TypeContainerVisitor.h +++ b/src/server/shared/Dynamic/TypeContainerVisitor.h @@ -37,21 +37,6 @@ template<class VISITOR, class TYPE_CONTAINER> void VisitorHelper(VISITOR &v, TYP v.Visit(c); } -// terminate condition for container list -template<class VISITOR> void VisitorHelper(VISITOR &/*v*/, ContainerList<TypeNull> &/*c*/) { } - -template<class VISITOR, class T> void VisitorHelper(VISITOR &v, ContainerList<T> &c) -{ - v.Visit(c._element); -} - -// recursion for container list -template<class VISITOR, class H, class T> void VisitorHelper(VISITOR &v, ContainerList<TypeList<H, T> > &c) -{ - VisitorHelper(v, c._elements); - VisitorHelper(v, c._TailElements); -} - // terminate condition container map list template<class VISITOR> void VisitorHelper(VISITOR &/*v*/, ContainerMapList<TypeNull> &/*c*/) { } @@ -67,23 +52,31 @@ template<class VISITOR, class H, class T> void VisitorHelper(VISITOR &v, Contain VisitorHelper(v, c._TailElements); } -// array list -template<class VISITOR, class T> void VisitorHelper(VISITOR &v, ContainerArrayList<T> &c) +// for TypeMapContainer +template<class VISITOR, class OBJECT_TYPES> void VisitorHelper(VISITOR &v, TypeMapContainer<OBJECT_TYPES> &c) { - v.Visit(c._element); + VisitorHelper(v, c.GetElements()); } -template<class VISITOR> void VisitorHelper(VISITOR &/*v*/, ContainerArrayList<TypeNull> &/*c*/) { } +// TypeUnorderedMapContainer +template<class VISITOR, class KEY_TYPE> +void VisitorHelper(VISITOR& /*v*/, ContainerUnorderedMap<TypeNull, KEY_TYPE>& /*c*/) { } + +template<class VISITOR, class KEY_TYPE, class T> +void VisitorHelper(VISITOR& v, ContainerUnorderedMap<T, KEY_TYPE>& c) +{ + v.Visit(c._element); +} -// recursion -template<class VISITOR, class H, class T> void VisitorHelper(VISITOR &v, ContainerArrayList<TypeList<H, T> > &c) +template<class VISITOR, class KEY_TYPE, class H, class T> +void VisitorHelper(VISITOR& v, ContainerUnorderedMap<TypeList<H, T>, KEY_TYPE>& c) { VisitorHelper(v, c._elements); VisitorHelper(v, c._TailElements); } -// for TypeMapContainer -template<class VISITOR, class OBJECT_TYPES> void VisitorHelper(VISITOR &v, TypeMapContainer<OBJECT_TYPES> &c) +template<class VISITOR, class OBJECT_TYPES, class KEY_TYPE> +void VisitorHelper(VISITOR& v, TypeUnorderedMapContainer<OBJECT_TYPES, KEY_TYPE>& c) { VisitorHelper(v, c.GetElements()); } diff --git a/src/server/shared/Dynamic/TypeList.h b/src/server/shared/Dynamic/TypeList.h index f1ccca9b043..f0355929700 100644 --- a/src/server/shared/Dynamic/TypeList.h +++ b/src/server/shared/Dynamic/TypeList.h @@ -40,5 +40,6 @@ struct TypeList #define TYPELIST_3(T1, T2, T3) TypeList<T1, TYPELIST_2(T2, T3) > #define TYPELIST_4(T1, T2, T3, T4) TypeList<T1, TYPELIST_3(T2, T3, T4) > #define TYPELIST_5(T1, T2, T3, T4, T5) TypeList<T1, TYPELIST_4(T2, T3, T4, T5) > +#define TYPELIST_6(T1, T2, T3, T4, T5, T6) TypeList<T1, TYPELIST_5(T2, T3, T4, T5, T6) > #endif |