TizenRefApp-8728 [Gallery] Implement custom hash functor support into 87/135287/5
authorIgor Nazarov <i.nazarov@samsung.com>
Wed, 21 Jun 2017 13:02:05 +0000 (16:02 +0300)
committerIgor Nazarov <i.nazarov@samsung.com>
Thu, 22 Jun 2017 12:28:48 +0000 (15:28 +0300)
ucl::HashMap

- Added custom hash functor support into ucl::HashMap;
- Implemented hash functor for ucl::Aspect.

Change-Id: Ie087cbf49c46b1393b04d6d13dc84d1ec52df0a7

ucl/inc/ucl/misc/Aspect.h
ucl/inc/ucl/misc/Aspect.hpp
ucl/inc/ucl/misc/HashMap.h

index 082cd91164b46123776f9c1b4193de8c855a1028..6459c55695246db3c5402d95a5a2986c2b88f817 100644 (file)
@@ -30,6 +30,10 @@ namespace ucl {
                explicit constexpr Aspect(const char *name);
 
                constexpr operator const char *() const;
+
+               struct Hash {
+                       size_t operator()(const Aspect &key) const;
+               };
        };
 
        // Non-member functions //
index aac7ddb05b17d5c1363eedf3d95ad4a8fce4460f..989468171e820ff899debd3c8c4427ce9a283845 100644 (file)
 
 namespace ucl {
 
+       // Aspect<CHILD>::Hash //
+
+       template <class CHILD>
+       size_t Aspect<CHILD>::Hash::operator()(const Aspect<CHILD> &key) const
+       {
+               constexpr size_t PRIME = 31;
+               size_t result = 0;
+               for (size_t i = 0; (key.name[i] != '\0'); ++i) {
+                       result *= PRIME;
+                       result += key.name[i];
+               }
+               return result;
+       }
+
+       // Aspect<CHILD> //
+
        template <class CHILD>
        constexpr Aspect<CHILD>::Aspect() :
                name(nullptr)
index e8c078c1ffae453a783e9dd99fb2e1affa91e6bb..f505a4b5ba3a41dfcefdb3809cd40483bf541899 100644 (file)
@@ -50,8 +50,24 @@ namespace ucl {
                        }
                };
 
-               using Hash = typename std::conditional<std::is_enum<KEY>::value,
-                               EnumClassHash<KEY>, std::hash<KEY>>::type;
+               template <class KEY2, class = void>
+               struct GetHash {
+                       using Type = std::hash<KEY2>;
+               };
+
+               template <class KEY2>
+               struct GetHash<KEY2, typename std::enable_if<
+                               std::is_enum<KEY2>::value>::type> {
+                       using Type = EnumClassHash<KEY2>;
+               };
+
+               template <class KEY2>
+               struct GetHash<KEY2, typename std::enable_if<
+                               std::is_class<typename KEY2::Hash>::value>::type> {
+                       using Type = typename KEY2::Hash;
+               };
+
+               using Hash = typename GetHash<KEY>::Type;
 
        private:
                std::unordered_map<KEY, VALUE, Hash> m_map;