瀏覽代碼

feat(c++): add PointerType, pnew

greatbridf 2 年之前
父節點
當前提交
68a2a9fdd3
共有 3 個文件被更改,包括 67 次插入23 次删除
  1. 28 0
      include/types/allocator.hpp
  2. 3 0
      include/types/cplusplus.hpp
  3. 36 23
      include/types/hash_map.hpp

+ 28 - 0
include/types/allocator.hpp

@@ -64,12 +64,40 @@ constexpr T* kernel_allocator_new(Args&&... args)
     return allocator_traits<kernel_allocator<T>>::allocate_and_construct(forward<Args>(args)...);
 }
 
+template <PointerType T, typename... Args>
+constexpr auto kernel_allocator_pnew(T, Args&&... args)
+{
+    using value_type = typename traits::remove_pointer<T>::type;
+    return kernel_allocator_new<value_type>(forward<Args>(args)...);
+}
+
 template <typename T, typename... Args>
 constexpr T* kernel_ident_allocator_new(Args&&... args)
 {
     return allocator_traits<kernel_ident_allocator<T>>::allocate_and_construct(forward<Args>(args)...);
 }
 
+template <PointerType T, typename... Args>
+constexpr auto kernel_ident_allocator_pnew(T, Args&&... args)
+{
+    using value_type = typename traits::remove_pointer<T>::type;
+    return kernel_ident_allocator_new<value_type>(forward<Args>(args)...);
+}
+
+template <PointerType T>
+constexpr void kernel_allocator_delete(T ptr)
+{
+    using value_type = typename traits::remove_pointer<T>::type;
+    return allocator_traits<kernel_allocator<value_type>>::deconstruct_and_deallocate(ptr);
+}
+
+template <PointerType T>
+constexpr void kernel_ident_allocator_delete(T ptr)
+{
+    using value_type = typename traits::remove_pointer<T>::type;
+    return allocator_traits<kernel_ident_allocator<value_type>>::deconstruct_and_deallocate(ptr);
+}
+
 template <Allocator _allocator>
 class allocator_traits {
 public:

+ 3 - 0
include/types/cplusplus.hpp

@@ -141,6 +141,9 @@ concept convertible_to = (traits::is_pointer<Src>::value && is_same<Dst, uint32_
     { static_cast<Dst>(_src) };
 };
 
+template <typename T>
+concept PointerType = traits::is_pointer<T>::value;
+
 template <typename A, typename B>
 concept same_as = is_same<A, B>::value;
 

+ 36 - 23
include/types/hash_map.hpp

@@ -111,13 +111,13 @@ public:
         const key_type key;
         value_type value;
 
-        pair(void) = delete;
-        pair(const key_type _key, value_type _val)
+        constexpr pair(void) = delete;
+        constexpr pair(const key_type _key, value_type _val)
             : key(_key)
             , value(_val)
         {
         }
-        bool operator==(const pair& p)
+        constexpr bool operator==(const pair& p)
         {
             return key == p.key;
         }
@@ -129,49 +129,51 @@ public:
         using _Value = typename traits::remove_pointer<Pointer>::type;
         using Reference = typename traits::add_reference<_Value>::type;
 
+        friend class hash_map;
+
     public:
-        iterator(const iterator& iter) noexcept
+        constexpr iterator(const iterator& iter) noexcept
             : p(iter.p)
         {
         }
 
-        iterator(iterator&& iter) noexcept
+        constexpr iterator(iterator&& iter) noexcept
             : p(iter.p)
         {
             iter.p = nullptr;
         }
 
-        iterator& operator=(const iterator& iter)
+        constexpr iterator& operator=(const iterator& iter)
         {
             p = iter.p;
             return *this;
         }
 
-        explicit iterator(Pointer p) noexcept
+        explicit constexpr iterator(Pointer p) noexcept
             : p(p)
         {
         }
 
-        bool operator==(const iterator& iter) const noexcept
+        constexpr bool operator==(const iterator& iter) const noexcept
         {
             return this->p == iter.p;
         }
 
-        bool operator!=(const iterator& iter) const noexcept
+        constexpr bool operator!=(const iterator& iter) const noexcept
         {
             return !(*this == iter);
         }
 
-        bool operator!()
+        constexpr operator bool()
         {
-            return !p;
+            return p != nullptr;
         }
 
-        Reference operator*() const noexcept
+        constexpr Reference operator*() const noexcept
         {
             return *p;
         }
-        Pointer operator->() const noexcept
+        constexpr Pointer operator->() const noexcept
         {
             return p;
         }
@@ -202,19 +204,19 @@ protected:
     }
 
 public:
-    explicit hash_map(void)
+    explicit constexpr hash_map(void)
         : buckets(INITIAL_BUCKETS_ALLOCATED)
     {
         for (size_type i = 0; i < INITIAL_BUCKETS_ALLOCATED; ++i)
             buckets.emplace_back();
     }
 
-    hash_map(const hash_map& v)
+    constexpr hash_map(const hash_map& v)
         : buckets(v.buckets)
     {
     }
 
-    hash_map(hash_map&& v)
+    constexpr hash_map(hash_map&& v)
         : buckets(move(v.buckets))
     {
     }
@@ -224,22 +226,22 @@ public:
         buckets.clear();
     }
 
-    void insert(const pair& p)
+    constexpr void insert(const pair& p)
     {
         auto hash_value = _Hasher::hash(p.key, hash_length());
         buckets.at(hash_value).push_back(p);
     }
-    void insert(pair&& p)
+    constexpr void insert(pair&& p)
     {
         auto hash_value = _Hasher::hash(p.key, hash_length());
         buckets.at(hash_value).push_back(move(p));
     }
-    void insert(const key_type& key, const value_type& val)
+    constexpr void insert(const key_type& key, const value_type& val)
     {
         insert(pair { key, val });
     }
 
-    void remove(const key_type& key)
+    constexpr void remove(const key_type& key)
     {
         auto hash_value = _Hasher::hash(key, hash_length());
         auto& bucket = buckets.at(hash_value);
@@ -251,7 +253,18 @@ public:
         }
     }
 
-    iterator_type find(const key_type& key)
+    constexpr void remove(iterator_type iter)
+    {
+        remove(iter->key);
+        iter.p = nullptr;
+    }
+    constexpr void remove(const_iterator_type iter)
+    {
+        remove(iter->key);
+        iter.p = nullptr;
+    }
+
+    constexpr iterator_type find(const key_type& key)
     {
         auto hash_value = _Hasher::hash(key, hash_length());
         auto& bucket = buckets.at(hash_value);
@@ -262,7 +275,7 @@ public:
         return iterator_type(nullptr);
     }
 
-    const_iterator_type find(const key_type& key) const
+    constexpr const_iterator_type find(const key_type& key) const
     {
         auto hash_value = _Hasher::hash(key, hash_length());
         const auto& bucket = buckets.at(hash_value);
@@ -273,7 +286,7 @@ public:
         return const_iterator_type(nullptr);
     }
 
-    void clear(void)
+    constexpr void clear(void)
     {
         for (size_t i = 0; i < buckets.size(); ++i)
             buckets.at(i).clear();