瀏覽代碼

add clear and destructor

ykiko 2 年之前
父節點
當前提交
2368c361e3
共有 1 個文件被更改,包括 12 次插入0 次删除
  1. 12 0
      include/pocketpy/vector.h

+ 12 - 0
include/pocketpy/vector.h

@@ -362,6 +362,13 @@ namespace pkpy {
             return *this;
         }
 
+        ~small_vector() {
+            std::destroy_n(data(), m_size);
+            if (!is_small()) {
+                std::free(m_internal.begin);
+            }
+        }
+
         template <typename... Args> void emplace_back(Args &&...args) noexcept {
             if (m_size == m_capacity) {
                 auto new_capacity = m_capacity * 2;
@@ -396,5 +403,10 @@ namespace pkpy {
                 (data() + m_size)->~T();
             }
         }
+
+        void clear() {
+            std::destroy_n(data(), m_size);
+            m_size = 0;
+        }
     };
 } // namespace pkpy