#include #include #include #include #include namespace pvfmm { template std::ostream& operator<<(std::ostream& output, const Vector& V) { std::ios::fmtflags f(std::cout.flags()); output << std::fixed << std::setprecision(4) << std::setiosflags(std::ios::left); for (Long i = 0; i < V.Dim(); i++) output << std::setw(10) << V[i] << ' '; output << ";\n"; std::cout.flags(f); return output; } template Vector::Vector() { dim = 0; capacity = 0; own_data = true; data_ptr = NULL; } template Vector::Vector(Long dim_, Iterator data_, bool own_data_) { dim = dim_; capacity = dim; own_data = own_data_; if (own_data) { if (dim > 0) { data_ptr = aligned_new(capacity); if (data_ != NULL) { memcopy(data_ptr, data_, dim); } } else data_ptr = NULL; } else data_ptr = data_; } template Vector::Vector(const Vector& V) { dim = V.dim; capacity = dim; own_data = true; if (dim > 0) { data_ptr = aligned_new(capacity); memcopy(data_ptr, V.data_ptr, dim); } else data_ptr = NULL; } template Vector::Vector(const std::vector& V) { dim = V.size(); capacity = dim; own_data = true; if (dim > 0) { data_ptr = aligned_new(capacity); memcopy(data_ptr, PVFMM_PTR2CONSTITR(ValueType, &V[0], V.size()), dim); } else data_ptr = NULL; } template Vector::~Vector() { if (own_data) { if (data_ptr != NULL) { aligned_delete(data_ptr); } } data_ptr = NULL; capacity = 0; dim = 0; } template void Vector::Swap(Vector& v1) { Long dim_ = dim; Long capacity_ = capacity; Iterator data_ptr_ = data_ptr; bool own_data_ = own_data; dim = v1.dim; capacity = v1.capacity; data_ptr = v1.data_ptr; own_data = v1.own_data; v1.dim = dim_; v1.capacity = capacity_; v1.data_ptr = data_ptr_; v1.own_data = own_data_; } template void Vector::ReInit(Long dim_, Iterator data_, bool own_data_) { if (own_data_ && own_data && dim_ <= capacity) { dim = dim_; if (data_ != NULL) { memcopy(data_ptr, data_, dim); } } else { Vector tmp(dim_, data_, own_data_); this->Swap(tmp); } } template void Vector::Write(const char* fname) const { FILE* f1 = fopen(fname, "wb+"); if (f1 == NULL) { std::cout << "Unable to open file for writing:" << fname << '\n'; return; } StaticArray dim_; dim_[0] = (uint64_t)dim; dim_[1] = 0; fwrite(&dim_[0], sizeof(uint64_t), 2, f1); fwrite(data_ptr, sizeof(ValueType), dim, f1); fclose(f1); } template inline Long Vector::Dim() const { return dim; } template inline Long Vector::Capacity() const { return capacity; } template void Vector::SetZero() { if (dim > 0) pvfmm::memset(data_ptr, 0, dim); } template Iterator Vector::Begin() { return data_ptr; } template ConstIterator Vector::Begin() const { return ConstIterator(data_ptr); } template void Vector::PushBack(const ValueType& x) { if (capacity > dim) { data_ptr[dim] = x; dim++; } else { Vector v(capacity * 1.6 + 1); memcopy(v.data_ptr, data_ptr, dim); v.dim = dim; Swap(v); assert(capacity > dim); data_ptr[dim] = x; dim++; } } template Vector& Vector::operator=(const Vector& V) { if (this != &V) { if (capacity < V.dim) ReInit(V.dim); dim = V.dim; memcopy(data_ptr, V.data_ptr, dim); } return *this; } template Vector& Vector::operator=(const std::vector& V) { { if (capacity < V.size()) ReInit(V.size()); dim = V.size(); memcopy(data_ptr, PVFMM_PTR2CONSTITR(ValueType, &V[0], V.size()), dim); } return *this; } template inline ValueType& Vector::operator[](Long j) { assert(j >= 0 && j < dim); return data_ptr[j]; } template inline const ValueType& Vector::operator[](Long j) const { assert(j >= 0 && j < dim); return data_ptr[j]; } } // end namespace