|
@@ -7,15 +7,6 @@
|
|
|
|
|
|
namespace SCTL_NAMESPACE {
|
|
namespace SCTL_NAMESPACE {
|
|
|
|
|
|
-template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& 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 <class ValueType> Vector<ValueType>::Vector() {
|
|
template <class ValueType> Vector<ValueType>::Vector() {
|
|
dim = 0;
|
|
dim = 0;
|
|
capacity = 0;
|
|
capacity = 0;
|
|
@@ -159,6 +150,29 @@ template <class ValueType> void Vector<ValueType>::PushBack(const ValueType& x)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Element access
|
|
|
|
+
|
|
|
|
+template <class ValueType> inline ValueType& Vector<ValueType>::operator[](Long j) {
|
|
|
|
+ assert(j >= 0 && j < dim);
|
|
|
|
+ return data_ptr[j];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> inline const ValueType& Vector<ValueType>::operator[](Long j) const {
|
|
|
|
+ assert(j >= 0 && j < dim);
|
|
|
|
+ return data_ptr[j];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Vector-Vector operations
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const std::vector<ValueType>& V) {
|
|
|
|
+ {
|
|
|
|
+ if (capacity < V.size()) ReInit(V.size());
|
|
|
|
+ dim = V.size();
|
|
|
|
+ memcopy(data_ptr, Ptr2ConstItr<ValueType>(&V[0], V.size()), dim);
|
|
|
|
+ }
|
|
|
|
+ return *this;
|
|
|
|
+}
|
|
|
|
+
|
|
template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const Vector<ValueType>& V) {
|
|
template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const Vector<ValueType>& V) {
|
|
if (this != &V) {
|
|
if (this != &V) {
|
|
if (capacity < V.dim) ReInit(V.dim);
|
|
if (capacity < V.dim) ReInit(V.dim);
|
|
@@ -168,23 +182,164 @@ template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const
|
|
return *this;
|
|
return *this;
|
|
}
|
|
}
|
|
|
|
|
|
-template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(const std::vector<ValueType>& V) {
|
|
|
|
- {
|
|
|
|
- if (capacity < V.size()) ReInit(V.size());
|
|
|
|
- dim = V.size();
|
|
|
|
- memcopy(data_ptr, Ptr2ConstItr<ValueType>(&V[0], V.size()), dim);
|
|
|
|
- }
|
|
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator+=(const Vector<ValueType>& V) {
|
|
|
|
+ SCTL_ASSERT(V.Dim() == dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) data_ptr[i] += V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
return *this;
|
|
return *this;
|
|
}
|
|
}
|
|
|
|
|
|
-template <class ValueType> inline ValueType& Vector<ValueType>::operator[](Long j) {
|
|
|
|
- assert(j >= 0 && j < dim);
|
|
|
|
- return data_ptr[j];
|
|
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator-=(const Vector<ValueType>& V) {
|
|
|
|
+ SCTL_ASSERT(V.Dim() == dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) data_ptr[i] -= V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return *this;
|
|
}
|
|
}
|
|
|
|
|
|
-template <class ValueType> inline const ValueType& Vector<ValueType>::operator[](Long j) const {
|
|
|
|
- assert(j >= 0 && j < dim);
|
|
|
|
- return data_ptr[j];
|
|
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator*=(const Vector<ValueType>& V) {
|
|
|
|
+ SCTL_ASSERT(V.Dim() == dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) data_ptr[i] *= V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return *this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator/=(const Vector<ValueType>& V) {
|
|
|
|
+ SCTL_ASSERT(V.Dim() == dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) data_ptr[i] /= V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return *this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator+(const Vector<ValueType>& V) const {
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ SCTL_ASSERT(V.Dim() == dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] + V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator-(const Vector<ValueType>& V) const {
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ SCTL_ASSERT(V.Dim() == dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] - V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator*(const Vector<ValueType>& V) const {
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ SCTL_ASSERT(V.Dim() == dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] * V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator/(const Vector<ValueType>& V) const {
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ SCTL_ASSERT(V.Dim() == dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] / V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Vector-Scalar operations
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator=(ValueType s) {
|
|
|
|
+ for (Long i = 0; i < dim; i++) data_ptr[i] = s;
|
|
|
|
+ return *this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator+=(ValueType s) {
|
|
|
|
+ for (Long i = 0; i < dim; i++) data_ptr[i] += s;
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return *this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator-=(ValueType s) {
|
|
|
|
+ for (Long i = 0; i < dim; i++) data_ptr[i] -= s;
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return *this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator*=(ValueType s) {
|
|
|
|
+ for (Long i = 0; i < dim; i++) data_ptr[i] *= s;
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return *this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType>& Vector<ValueType>::operator/=(ValueType s) {
|
|
|
|
+ for (Long i = 0; i < dim; i++) data_ptr[i] /= s;
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return *this;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator+(ValueType s) const {
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] + s;
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator-(ValueType s) const {
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] - s;
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator*(ValueType s) const {
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] * s;
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> Vector<ValueType>::operator/(ValueType s) const {
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = data_ptr[i] / s;
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> operator+(ValueType s, const Vector<ValueType>& V) {
|
|
|
|
+ Long dim = V.Dim();
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = s + V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> operator-(ValueType s, const Vector<ValueType>& V) {
|
|
|
|
+ Long dim = V.Dim();
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = s - V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> operator*(ValueType s, const Vector<ValueType>& V) {
|
|
|
|
+ Long dim = V.Dim();
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = s * V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> Vector<ValueType> operator/(ValueType s, const Vector<ValueType>& V) {
|
|
|
|
+ Long dim = V.Dim();
|
|
|
|
+ Vector<ValueType> Vr(dim);
|
|
|
|
+ for (Long i = 0; i < dim; i++) Vr[i] = s / V[i];
|
|
|
|
+ Profile::Add_FLOP(dim);
|
|
|
|
+ return Vr;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& 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;
|
|
}
|
|
}
|
|
|
|
|
|
} // end namespace
|
|
} // end namespace
|