Dhairya Malhotra 7 年之前
父節點
當前提交
93037a2aa6
共有 6 個文件被更改,包括 32 次插入29 次删除
  1. 3 3
      include/sctl/matrix.hpp
  2. 5 5
      include/sctl/matrix.txx
  3. 8 6
      include/sctl/mem_mgr.hpp
  4. 7 7
      include/sctl/mem_mgr.txx
  5. 4 3
      include/sctl/vector.hpp
  6. 5 5
      include/sctl/vector.txx

+ 3 - 3
include/sctl/matrix.hpp

@@ -24,7 +24,7 @@ template <class ValueType> class Matrix {
 
   Matrix();
 
-  Matrix(Long dim1, Long dim2, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
+  Matrix(Long dim1, Long dim2, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
 
   Matrix(const Matrix<ValueType>& M);
 
@@ -32,7 +32,7 @@ template <class ValueType> class Matrix {
 
   void Swap(Matrix<ValueType>& M);
 
-  void ReInit(Long dim1, Long dim2, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
+  void ReInit(Long dim1, Long dim2, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
 
   void Write(const char* fname) const;
 
@@ -117,7 +117,7 @@ template <class ValueType> class Matrix {
   Matrix<ValueType> pinv(ValueType eps = -1);
 
  private:
-  void Init(Long dim1, Long dim2, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
+  void Init(Long dim1, Long dim2, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
 
   StaticArray<Long, 2> dim;
   Iterator<ValueType> data_ptr;

+ 5 - 5
include/sctl/matrix.txx

@@ -33,11 +33,11 @@ template <class ValueType> void Matrix<ValueType>::Init(Long dim1, Long dim2, It
   if (own_data) {
     if (dim[0] * dim[1] > 0) {
       data_ptr = aligned_new<ValueType>(dim[0] * dim[1]);
-      if (data_ != nullptr) {
+      if (data_ != NullIterator<ValueType>()) {
         memcopy(data_ptr, data_, dim[0] * dim[1]);
       }
     } else
-      data_ptr = nullptr;
+      data_ptr = NullIterator<ValueType>();
   } else
     data_ptr = data_;
 }
@@ -56,11 +56,11 @@ template <class ValueType> Matrix<ValueType>::Matrix(const Matrix<ValueType>& M)
 
 template <class ValueType> Matrix<ValueType>::~Matrix() {
   if (own_data) {
-    if (data_ptr != nullptr) {
+    if (data_ptr != NullIterator<ValueType>()) {
       aligned_delete(data_ptr);
     }
   }
-  data_ptr = nullptr;
+  data_ptr = NullIterator<ValueType>();
   dim[0] = 0;
   dim[1] = 0;
 }
@@ -87,7 +87,7 @@ template <class ValueType> void Matrix<ValueType>::ReInit(Long dim1, Long dim2,
   if (own_data_ && own_data && dim[0] * dim[1] >= dim1 * dim2) {
     dim[0] = dim1;
     dim[1] = dim2;
-    if (data_ != nullptr) {
+    if (data_ != NullIterator<ValueType>()) {
       memcopy(data_ptr, data_, dim[0] * dim[1]);
     }
   } else {

+ 8 - 6
include/sctl/mem_mgr.hpp

@@ -43,15 +43,15 @@ template <class ValueType> class ConstIterator {
   static const Long ValueSize = sizeof(ValueType);
 
  public:
-  ConstIterator(void* base_ = nullptr) {
-    base = (char*)base_;
+  ConstIterator() {
+    base = nullptr;
     len = 0;
     offset = 0;
     alloc_ctr = 0;
     mem_head = nullptr;
   }
 
-  // template <size_t LENGTH> ConstIterator(ValueType (&base_)[LENGTH]) {  // DEPRECATED
+  // template <size_t LENGTH> ConstIterator(ValueType (&base_)[LENGTH]) {  // DEPRECATED (because mem_head cannot be stored)
   //   SCTL_ASSERT(false);
   // }
 
@@ -169,7 +169,7 @@ template <class ValueType> class Iterator : public ConstIterator<ValueType> {
   typedef std::random_access_iterator_tag iterator_category;
 
  public:
-  Iterator(void* base_ = nullptr) : ConstIterator<ValueType>(base_) {}
+  Iterator() : ConstIterator<ValueType>() {}
 
   template <size_t LENGTH> Iterator(ValueType (&base_)[LENGTH]) : ConstIterator<ValueType>(base_) {}
 
@@ -263,11 +263,13 @@ template <class ValueType> ConstIterator<ValueType> Ptr2ConstItr(const void* ptr
 
 #else
 
-template <class ValueType> ValueType* Ptr2Itr(void* ptr, Long len) { return (ValueType*) ptr; }
-template <class ValueType> const ValueType* Ptr2ConstItr(const void* ptr, Long len) { return (const ValueType*) ptr; }
+template <class ValueType> Iterator<ValueType> Ptr2Itr(void* ptr, Long len) { return (Iterator<ValueType>) ptr; }
+template <class ValueType> ConstIterator<ValueType> Ptr2ConstItr(const void* ptr, Long len) { return (ConstIterator<ValueType>) ptr; }
 
 #endif
 
+template <class ValueType> Iterator<ValueType> NullIterator() { return Ptr2Itr<ValueType>(nullptr, 0); }
+
 /**
  * \brief MemoryManager class declaration.
  */

+ 7 - 7
include/sctl/mem_mgr.txx

@@ -30,7 +30,7 @@ template <class ValueType> inline void ConstIterator<ValueType>::IteratorAssertC
   const auto& mem_head = this->mem_head;
   const auto& alloc_ctr = this->alloc_ctr;
 
-  if (*this == nullptr) SCTL_WARN("dereferencing a nullptr is undefined.");
+  if (*this == NullIterator<ValueType>()) SCTL_WARN("dereferencing a nullptr is undefined.");
   SCTL_ASSERT_MSG(offset >= 0 && offset + (Long)sizeof(ValueType) <= len, "access to pointer [B" << (offset < 0 ? "" : "+") << offset << ",B" << (offset + (Long)sizeof(ValueType) < 0 ? "" : "+") << offset + (Long)sizeof(ValueType) << ") is outside of the range [B,B+" << len << ").");
   if (mem_head) {
     MemoryManager::MemHead& mh = *(MemoryManager::MemHead*)(mem_head);
@@ -168,7 +168,7 @@ inline void MemoryManager::CheckMemHead(const MemHead& mem_head) {  // Verify he
 }
 
 inline Iterator<char> MemoryManager::malloc(const Long n_elem, const Long type_size, const MemHead::TypeID type_id) const {
-  if (!n_elem) return nullptr;
+  if (!n_elem) return NullIterator<char>();
   static uintptr_t alignment = SCTL_MEM_ALIGN - 1;
   static uintptr_t header_size = (uintptr_t)(sizeof(MemHead) + alignment) & ~(uintptr_t)alignment;
 
@@ -273,7 +273,7 @@ inline Iterator<char> MemoryManager::malloc(const Long n_elem, const Long type_s
 }
 
 inline void MemoryManager::free(Iterator<char> p) const {
-  if (p == nullptr) return;
+  if (p == NullIterator<char>()) return;
   static uintptr_t alignment = SCTL_MEM_ALIGN - 1;
   static uintptr_t header_size = (uintptr_t)(sizeof(MemHead) + alignment) & ~(uintptr_t)alignment;
   SCTL_UNUSED(header_size);
@@ -393,7 +393,7 @@ inline void MemoryManager::test() {
 
     for (Integer j = 0; j < 3; j++) {
       tmp = (Iterator<double>)memgr.malloc(M * sizeof(double));
-      SCTL_ASSERT(tmp != nullptr);
+      SCTL_ASSERT(tmp != NullIterator<double>());
       tt = omp_get_wtime();
 #pragma omp parallel for
       for (Long i = 0; i < M; i += 64) tmp[i] = (double)i;
@@ -466,12 +466,12 @@ inline void MemoryManager::delete_node(Long indx) const {
 }
 
 template <class ValueType> inline Iterator<ValueType> aligned_new(Long n_elem, const MemoryManager* mem_mgr) {
-  if (!n_elem) return nullptr;
+  if (!n_elem) return NullIterator<ValueType>();
 
   static MemoryManager def_mem_mgr(0);
   if (!mem_mgr) mem_mgr = &def_mem_mgr;
   Iterator<ValueType> A = (Iterator<ValueType>)mem_mgr->malloc(n_elem, sizeof(ValueType), typeid(ValueType).hash_code());
-  SCTL_ASSERT_MSG(A != nullptr, "memory allocation failed.");
+  SCTL_ASSERT_MSG(A != NullIterator<ValueType>(), "memory allocation failed.");
 
   if (!std::is_trivial<ValueType>::value) {  // Call constructors
                                           // printf("%s\n", __PRETTY_FUNCTION__);
@@ -497,7 +497,7 @@ template <class ValueType> inline Iterator<ValueType> aligned_new(Long n_elem, c
 }
 
 template <class ValueType> inline void aligned_delete(Iterator<ValueType> A, const MemoryManager* mem_mgr) {
-  if (A == nullptr) return;
+  if (A == NullIterator<ValueType>()) return;
 
   if (!std::is_trivial<ValueType>::value) {  // Call destructors
     // printf("%s\n", __PRETTY_FUNCTION__);

+ 4 - 3
include/sctl/vector.hpp

@@ -1,6 +1,7 @@
 #ifndef _SCTL_VECTOR_HPP_
 #define _SCTL_VECTOR_HPP_
 
+#include SCTL_INCLUDE(mem_mgr.hpp)
 #include SCTL_INCLUDE(common.hpp)
 
 #include <vector>
@@ -21,7 +22,7 @@ template <class ValueType> class Vector {
 
   Vector();
 
-  Vector(Long dim_, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
+  Vector(Long dim_, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
 
   Vector(const Vector& V);
 
@@ -31,7 +32,7 @@ template <class ValueType> class Vector {
 
   void Swap(Vector<ValueType>& v1);
 
-  void ReInit(Long dim_, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
+  void ReInit(Long dim_, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
 
   void Write(const char* fname) const;
 
@@ -102,7 +103,7 @@ template <class ValueType> class Vector {
   Vector operator/(ValueType s) const;
 
  private:
-  void Init(Long dim_, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
+  void Init(Long dim_, Iterator<ValueType> data_ = NullIterator<ValueType>(), bool own_data_ = true);
 
   Long dim;
   Long capacity;

+ 5 - 5
include/sctl/vector.txx

@@ -14,11 +14,11 @@ template <class ValueType> void Vector<ValueType>::Init(Long dim_, Iterator<Valu
   if (own_data) {
     if (dim > 0) {
       data_ptr = aligned_new<ValueType>(capacity);
-      if (data_ != nullptr) {
+      if (data_ != NullIterator<ValueType>()) {
         memcopy(data_ptr, data_, dim);
       }
     } else
-      data_ptr = nullptr;
+      data_ptr = NullIterator<ValueType>();
   } else
     data_ptr = data_;
 }
@@ -41,11 +41,11 @@ template <class ValueType> Vector<ValueType>::Vector(const std::vector<ValueType
 
 template <class ValueType> Vector<ValueType>::~Vector() {
   if (own_data) {
-    if (data_ptr != nullptr) {
+    if (data_ptr != NullIterator<ValueType>()) {
       aligned_delete(data_ptr);
     }
   }
-  data_ptr = nullptr;
+  data_ptr = NullIterator<ValueType>();
   capacity = 0;
   dim = 0;
 }
@@ -70,7 +70,7 @@ template <class ValueType> void Vector<ValueType>::Swap(Vector<ValueType>& v1) {
 template <class ValueType> void Vector<ValueType>::ReInit(Long dim_, Iterator<ValueType> data_, bool own_data_) {
   if (own_data_ && own_data && dim_ <= capacity) {
     dim = dim_;
-    if (data_ != nullptr) {
+    if (data_ != NullIterator<ValueType>()) {
       memcopy(data_ptr, data_, dim);
     }
   } else {