vector.txx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include <cassert>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <pvfmm/mem_mgr.hpp>
  5. #include <pvfmm/profile.hpp>
  6. namespace pvfmm {
  7. template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& V) {
  8. std::ios::fmtflags f(std::cout.flags());
  9. output << std::fixed << std::setprecision(4) << std::setiosflags(std::ios::left);
  10. for (Long i = 0; i < V.Dim(); i++) output << std::setw(10) << V[i] << ' ';
  11. output << ";\n";
  12. std::cout.flags(f);
  13. return output;
  14. }
  15. template <class ValueType> Vector<ValueType>::Vector() {
  16. dim = 0;
  17. capacity = 0;
  18. own_data = true;
  19. data_ptr = NULL;
  20. }
  21. template <class ValueType> Vector<ValueType>::Vector(Long dim_, Iterator<ValueType> data_, bool own_data_) {
  22. dim = dim_;
  23. capacity = dim;
  24. own_data = own_data_;
  25. if (own_data) {
  26. if (dim > 0) {
  27. data_ptr = aligned_new<ValueType>(capacity);
  28. if (data_ != NULL) {
  29. memcopy(data_ptr, data_, dim);
  30. }
  31. } else
  32. data_ptr = NULL;
  33. } else
  34. data_ptr = data_;
  35. }
  36. template <class ValueType> Vector<ValueType>::Vector(const Vector<ValueType>& V) {
  37. dim = V.dim;
  38. capacity = dim;
  39. own_data = true;
  40. if (dim > 0) {
  41. data_ptr = aligned_new<ValueType>(capacity);
  42. memcopy(data_ptr, V.data_ptr, dim);
  43. } else
  44. data_ptr = NULL;
  45. }
  46. template <class ValueType> Vector<ValueType>::Vector(const std::vector<ValueType>& V) {
  47. dim = V.size();
  48. capacity = dim;
  49. own_data = true;
  50. if (dim > 0) {
  51. data_ptr = aligned_new<ValueType>(capacity);
  52. memcopy(data_ptr, PVFMM_PTR2CONSTITR(ValueType, &V[0], V.size()), dim);
  53. } else
  54. data_ptr = NULL;
  55. }
  56. template <class ValueType> Vector<ValueType>::~Vector() {
  57. if (own_data) {
  58. if (data_ptr != NULL) {
  59. aligned_delete(data_ptr);
  60. }
  61. }
  62. data_ptr = NULL;
  63. capacity = 0;
  64. dim = 0;
  65. }
  66. template <class ValueType> void Vector<ValueType>::Swap(Vector<ValueType>& v1) {
  67. Long dim_ = dim;
  68. Long capacity_ = capacity;
  69. Iterator<ValueType> data_ptr_ = data_ptr;
  70. bool own_data_ = own_data;
  71. dim = v1.dim;
  72. capacity = v1.capacity;
  73. data_ptr = v1.data_ptr;
  74. own_data = v1.own_data;
  75. v1.dim = dim_;
  76. v1.capacity = capacity_;
  77. v1.data_ptr = data_ptr_;
  78. v1.own_data = own_data_;
  79. }
  80. template <class ValueType> void Vector<ValueType>::ReInit(Long dim_, Iterator<ValueType> data_, bool own_data_) {
  81. if (own_data_ && own_data && dim_ <= capacity) {
  82. dim = dim_;
  83. if (data_ != NULL) {
  84. memcopy(data_ptr, data_, dim);
  85. }
  86. } else {
  87. Vector<ValueType> tmp(dim_, data_, own_data_);
  88. this->Swap(tmp);
  89. }
  90. }
  91. template <class ValueType> void Vector<ValueType>::Write(const char* fname) const {
  92. FILE* f1 = fopen(fname, "wb+");
  93. if (f1 == NULL) {
  94. std::cout << "Unable to open file for writing:" << fname << '\n';
  95. return;
  96. }
  97. StaticArray<uint64_t, 2> dim_;
  98. dim_[0] = (uint64_t)dim;
  99. dim_[1] = 0;
  100. fwrite(&dim_[0], sizeof(uint64_t), 2, f1);
  101. fwrite(data_ptr, sizeof(ValueType), dim, f1);
  102. fclose(f1);
  103. }
  104. template <class ValueType> inline Long Vector<ValueType>::Dim() const { return dim; }
  105. template <class ValueType> inline Long Vector<ValueType>::Capacity() const { return capacity; }
  106. template <class ValueType> void Vector<ValueType>::SetZero() {
  107. if (dim > 0) pvfmm::memset(data_ptr, 0, dim);
  108. }
  109. template <class ValueType> Iterator<ValueType> Vector<ValueType>::Begin() { return data_ptr; }
  110. template <class ValueType> ConstIterator<ValueType> Vector<ValueType>::Begin() const { return ConstIterator<ValueType>(data_ptr); }
  111. template <class ValueType> void Vector<ValueType>::PushBack(const ValueType& x) {
  112. if (capacity > dim) {
  113. data_ptr[dim] = x;
  114. dim++;
  115. } else {
  116. Vector<ValueType> v(capacity * 1.6 + 1);
  117. memcopy(v.data_ptr, data_ptr, dim);
  118. v.dim = dim;
  119. Swap(v);
  120. assert(capacity > dim);
  121. data_ptr[dim] = x;
  122. dim++;
  123. }
  124. }
  125. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const Vector<ValueType>& V) {
  126. if (this != &V) {
  127. if (capacity < V.dim) ReInit(V.dim);
  128. dim = V.dim;
  129. memcopy(data_ptr, V.data_ptr, dim);
  130. }
  131. return *this;
  132. }
  133. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const std::vector<ValueType>& V) {
  134. {
  135. if (capacity < V.size()) ReInit(V.size());
  136. dim = V.size();
  137. memcopy(data_ptr, PVFMM_PTR2CONSTITR(ValueType, &V[0], V.size()), dim);
  138. }
  139. return *this;
  140. }
  141. template <class ValueType> inline ValueType& Vector<ValueType>::operator[](Long j) {
  142. assert(j >= 0 && j < dim);
  143. return data_ptr[j];
  144. }
  145. template <class ValueType> inline const ValueType& Vector<ValueType>::operator[](Long j) const {
  146. assert(j >= 0 && j < dim);
  147. return data_ptr[j];
  148. }
  149. } // end namespace