vector.txx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #include <cassert>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include SCTL_INCLUDE(mem_mgr.hpp)
  5. #include SCTL_INCLUDE(profile.hpp)
  6. namespace SCTL_NAMESPACE {
  7. template <class ValueType> void Vector<ValueType>::Init(Long dim_, Iterator<ValueType> data_, bool own_data_) {
  8. dim = dim_;
  9. capacity = dim;
  10. own_data = own_data_;
  11. if (own_data) {
  12. if (dim > 0) {
  13. data_ptr = aligned_new<ValueType>(capacity);
  14. if (data_ != nullptr) {
  15. memcopy(data_ptr, data_, dim);
  16. }
  17. } else
  18. data_ptr = nullptr;
  19. } else
  20. data_ptr = data_;
  21. }
  22. template <class ValueType> Vector<ValueType>::Vector() {
  23. Init(0);
  24. }
  25. template <class ValueType> Vector<ValueType>::Vector(Long dim_, Iterator<ValueType> data_, bool own_data_) {
  26. Init(dim_, data_, own_data_);
  27. }
  28. template <class ValueType> Vector<ValueType>::Vector(const Vector<ValueType>& V) {
  29. Init(V.Dim(), (Iterator<ValueType>)V.begin());
  30. }
  31. template <class ValueType> Vector<ValueType>::Vector(const std::vector<ValueType>& V) {
  32. Init(V.Dim(), Ptr2ConstItr<ValueType>(&V[0], V.size()));
  33. }
  34. template <class ValueType> Vector<ValueType>::~Vector() {
  35. if (own_data) {
  36. if (data_ptr != nullptr) {
  37. aligned_delete(data_ptr);
  38. }
  39. }
  40. data_ptr = nullptr;
  41. capacity = 0;
  42. dim = 0;
  43. }
  44. template <class ValueType> void Vector<ValueType>::Swap(Vector<ValueType>& v1) {
  45. Long dim_ = dim;
  46. Long capacity_ = capacity;
  47. Iterator<ValueType> data_ptr_ = data_ptr;
  48. bool own_data_ = own_data;
  49. dim = v1.dim;
  50. capacity = v1.capacity;
  51. data_ptr = v1.data_ptr;
  52. own_data = v1.own_data;
  53. v1.dim = dim_;
  54. v1.capacity = capacity_;
  55. v1.data_ptr = data_ptr_;
  56. v1.own_data = own_data_;
  57. }
  58. template <class ValueType> void Vector<ValueType>::ReInit(Long dim_, Iterator<ValueType> data_, bool own_data_) {
  59. if (own_data_ && own_data && dim_ <= capacity) {
  60. dim = dim_;
  61. if (data_ != nullptr) {
  62. memcopy(data_ptr, data_, dim);
  63. }
  64. } else {
  65. Vector<ValueType> tmp(dim_, data_, own_data_);
  66. this->Swap(tmp);
  67. }
  68. }
  69. template <class ValueType> void Vector<ValueType>::Write(const char* fname) const {
  70. FILE* f1 = fopen(fname, "wb+");
  71. if (f1 == nullptr) {
  72. std::cout << "Unable to open file for writing:" << fname << '\n';
  73. return;
  74. }
  75. StaticArray<uint64_t, 2> dim_;
  76. dim_[0] = (uint64_t)Dim();
  77. dim_[1] = 1;
  78. fwrite(&dim_[0], sizeof(uint64_t), 2, f1);
  79. if (dim_[0] * dim_[1]) fwrite(&data_ptr[0], sizeof(ValueType), dim_[0] * dim_[1], f1);
  80. fclose(f1);
  81. }
  82. template <class ValueType> void Vector<ValueType>::Read(const char* fname) {
  83. FILE* f1 = fopen(fname, "r");
  84. if (f1 == nullptr) {
  85. std::cout << "Unable to open file for reading:" << fname << '\n';
  86. return;
  87. }
  88. StaticArray<uint64_t, 2> dim_;
  89. Long readlen = fread(&dim_[0], sizeof(uint64_t), 2, f1);
  90. assert(readlen == 2);
  91. if (Dim() != dim_[0] * dim_[1]) ReInit(dim_[0] * dim_[1]);
  92. if (dim_[0] * dim_[1]) readlen = fread(&data_ptr[0], sizeof(ValueType), dim_[0] * dim_[1], f1);
  93. assert(readlen == dim_[0] * dim_[1]);
  94. fclose(f1);
  95. }
  96. template <class ValueType> inline Long Vector<ValueType>::Dim() const { return dim; }
  97. template <class ValueType> inline Long Vector<ValueType>::Capacity() const { return capacity; }
  98. template <class ValueType> void Vector<ValueType>::SetZero() {
  99. if (dim > 0) memset<ValueType>(data_ptr, 0, dim);
  100. }
  101. template <class ValueType> Iterator<ValueType> Vector<ValueType>::begin() { return data_ptr; }
  102. template <class ValueType> ConstIterator<ValueType> Vector<ValueType>::begin() const { return data_ptr; }
  103. template <class ValueType> Iterator<ValueType> Vector<ValueType>::end() { return data_ptr + dim; }
  104. template <class ValueType> ConstIterator<ValueType> Vector<ValueType>::end() const { return data_ptr + dim; }
  105. template <class ValueType> void Vector<ValueType>::PushBack(const ValueType& x) {
  106. if (capacity > dim) {
  107. data_ptr[dim] = x;
  108. dim++;
  109. } else {
  110. Vector<ValueType> v((Long)(capacity * 1.6) + 1);
  111. memcopy(v.data_ptr, data_ptr, dim);
  112. v.dim = dim;
  113. Swap(v);
  114. assert(capacity > dim);
  115. data_ptr[dim] = x;
  116. dim++;
  117. }
  118. }
  119. // Element access
  120. template <class ValueType> inline ValueType& Vector<ValueType>::operator[](Long j) {
  121. assert(j >= 0 && j < dim);
  122. return data_ptr[j];
  123. }
  124. template <class ValueType> inline const ValueType& Vector<ValueType>::operator[](Long j) const {
  125. assert(j >= 0 && j < dim);
  126. return data_ptr[j];
  127. }
  128. // Vector-Vector operations
  129. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const std::vector<ValueType>& V) {
  130. {
  131. if (capacity < V.size()) ReInit(V.size());
  132. dim = V.size();
  133. memcopy(data_ptr, Ptr2ConstItr<ValueType>(&V[0], V.size()), dim);
  134. }
  135. return *this;
  136. }
  137. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const Vector<ValueType>& V) {
  138. if (this != &V) {
  139. if (capacity < V.dim) ReInit(V.dim);
  140. dim = V.dim;
  141. memcopy(data_ptr, V.data_ptr, dim);
  142. }
  143. return *this;
  144. }
  145. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator+=(const Vector<ValueType>& V) {
  146. SCTL_ASSERT(V.Dim() == dim);
  147. for (Long i = 0; i < dim; i++) data_ptr[i] += V[i];
  148. Profile::Add_FLOP(dim);
  149. return *this;
  150. }
  151. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator-=(const Vector<ValueType>& V) {
  152. SCTL_ASSERT(V.Dim() == dim);
  153. for (Long i = 0; i < dim; i++) data_ptr[i] -= V[i];
  154. Profile::Add_FLOP(dim);
  155. return *this;
  156. }
  157. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator*=(const Vector<ValueType>& V) {
  158. SCTL_ASSERT(V.Dim() == dim);
  159. for (Long i = 0; i < dim; i++) data_ptr[i] *= V[i];
  160. Profile::Add_FLOP(dim);
  161. return *this;
  162. }
  163. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator/=(const Vector<ValueType>& V) {
  164. SCTL_ASSERT(V.Dim() == dim);
  165. for (Long i = 0; i < dim; i++) data_ptr[i] /= V[i];
  166. Profile::Add_FLOP(dim);
  167. return *this;
  168. }
  169. template <class ValueType> Vector<ValueType> Vector<ValueType>::operator+(const Vector<ValueType>& V) const {
  170. Vector<ValueType> Vr(dim);
  171. SCTL_ASSERT(V.Dim() == dim);
  172. for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] + V[i];
  173. Profile::Add_FLOP(dim);
  174. return Vr;
  175. }
  176. template <class ValueType> Vector<ValueType> Vector<ValueType>::operator-(const Vector<ValueType>& V) const {
  177. Vector<ValueType> Vr(dim);
  178. SCTL_ASSERT(V.Dim() == dim);
  179. for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] - V[i];
  180. Profile::Add_FLOP(dim);
  181. return Vr;
  182. }
  183. template <class ValueType> Vector<ValueType> Vector<ValueType>::operator*(const Vector<ValueType>& V) const {
  184. Vector<ValueType> Vr(dim);
  185. SCTL_ASSERT(V.Dim() == dim);
  186. for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] * V[i];
  187. Profile::Add_FLOP(dim);
  188. return Vr;
  189. }
  190. template <class ValueType> Vector<ValueType> Vector<ValueType>::operator/(const Vector<ValueType>& V) const {
  191. Vector<ValueType> Vr(dim);
  192. SCTL_ASSERT(V.Dim() == dim);
  193. for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] / V[i];
  194. Profile::Add_FLOP(dim);
  195. return Vr;
  196. }
  197. // Vector-Scalar operations
  198. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(ValueType s) {
  199. for (Long i = 0; i < dim; i++) data_ptr[i] = s;
  200. return *this;
  201. }
  202. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator+=(ValueType s) {
  203. for (Long i = 0; i < dim; i++) data_ptr[i] += s;
  204. Profile::Add_FLOP(dim);
  205. return *this;
  206. }
  207. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator-=(ValueType s) {
  208. for (Long i = 0; i < dim; i++) data_ptr[i] -= s;
  209. Profile::Add_FLOP(dim);
  210. return *this;
  211. }
  212. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator*=(ValueType s) {
  213. for (Long i = 0; i < dim; i++) data_ptr[i] *= s;
  214. Profile::Add_FLOP(dim);
  215. return *this;
  216. }
  217. template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator/=(ValueType s) {
  218. for (Long i = 0; i < dim; i++) data_ptr[i] /= s;
  219. Profile::Add_FLOP(dim);
  220. return *this;
  221. }
  222. template <class ValueType> Vector<ValueType> Vector<ValueType>::operator+(ValueType s) const {
  223. Vector<ValueType> Vr(dim);
  224. for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] + s;
  225. Profile::Add_FLOP(dim);
  226. return Vr;
  227. }
  228. template <class ValueType> Vector<ValueType> Vector<ValueType>::operator-(ValueType s) const {
  229. Vector<ValueType> Vr(dim);
  230. for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] - s;
  231. Profile::Add_FLOP(dim);
  232. return Vr;
  233. }
  234. template <class ValueType> Vector<ValueType> Vector<ValueType>::operator*(ValueType s) const {
  235. Vector<ValueType> Vr(dim);
  236. for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] * s;
  237. Profile::Add_FLOP(dim);
  238. return Vr;
  239. }
  240. template <class ValueType> Vector<ValueType> Vector<ValueType>::operator/(ValueType s) const {
  241. Vector<ValueType> Vr(dim);
  242. for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] / s;
  243. Profile::Add_FLOP(dim);
  244. return Vr;
  245. }
  246. template <class ValueType> Vector<ValueType> operator+(ValueType s, const Vector<ValueType>& V) {
  247. Long dim = V.Dim();
  248. Vector<ValueType> Vr(dim);
  249. for (Long i = 0; i < dim; i++) Vr[i] = s + V[i];
  250. Profile::Add_FLOP(dim);
  251. return Vr;
  252. }
  253. template <class ValueType> Vector<ValueType> operator-(ValueType s, const Vector<ValueType>& V) {
  254. Long dim = V.Dim();
  255. Vector<ValueType> Vr(dim);
  256. for (Long i = 0; i < dim; i++) Vr[i] = s - V[i];
  257. Profile::Add_FLOP(dim);
  258. return Vr;
  259. }
  260. template <class ValueType> Vector<ValueType> operator*(ValueType s, const Vector<ValueType>& V) {
  261. Long dim = V.Dim();
  262. Vector<ValueType> Vr(dim);
  263. for (Long i = 0; i < dim; i++) Vr[i] = s * V[i];
  264. Profile::Add_FLOP(dim);
  265. return Vr;
  266. }
  267. template <class ValueType> Vector<ValueType> operator/(ValueType s, const Vector<ValueType>& V) {
  268. Long dim = V.Dim();
  269. Vector<ValueType> Vr(dim);
  270. for (Long i = 0; i < dim; i++) Vr[i] = s / V[i];
  271. Profile::Add_FLOP(dim);
  272. return Vr;
  273. }
  274. template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& V) {
  275. std::ios::fmtflags f(std::cout.flags());
  276. output << std::fixed << std::setprecision(4) << std::setiosflags(std::ios::left);
  277. for (Long i = 0; i < V.Dim(); i++) output << std::setw(10) << V[i] << ' ';
  278. output << ";\n";
  279. std::cout.flags(f);
  280. return output;
  281. }
  282. } // end namespace