📄 vector.cxx
字号:
+ "], but is equal to " + to_str(i) + ".");#endif return this->data_[i]; } //! Access operator. /*! \param i index. \return The value of the vector at 'i'. */ template <class T, class Allocator> inline typename Vector<T, Vect_Full, Allocator>::const_reference Vector<T, Vect_Full, Allocator>::operator() (int i) const {#ifdef SELDON_CHECK_BOUNDARIES if (i < 0 || i >= this->m_) throw WrongIndex("Vector<Vect_Full>::operator()", string("Index should be in [0, ") + to_str(this->m_-1) + "], but is equal to " + to_str(i) + ".");#endif return this->data_[i]; } //! Duplicates a vector (assignment operator). /*! \param X vector to be copied. \note Memory is duplicated: 'X' is therefore independent from the current instance after the copy. */ template <class T, class Allocator> inline Vector<T, Vect_Full, Allocator>& Vector<T, Vect_Full, Allocator> ::operator= (const Vector<T, Vect_Full, Allocator>& X) { this->Copy(X); return *this; } //! Duplicates a vector. /*! \param X vector to be copied. \note Memory is duplicated: 'X' is therefore independent from the current instance after the copy. */ template <class T, class Allocator> inline void Vector<T, Vect_Full, Allocator> ::Copy(const Vector<T, Vect_Full, Allocator>& X) { this->Reallocate(X.GetLength()); this->vect_allocator_.memorycpy(this->data_, X.GetData(), this->m_); } /******************* * BASIC FUNCTIONS * *******************/ //! Returns the number of elements stored. /*! \return The number of elements stored in memory. */ template <class T, class Allocator> int Vector<T, Vect_Full, Allocator>::GetDataSize() { return this->m_; } /************************ * CONVENIENT FUNCTIONS * ************************/ //! Sets all elements to zero. /*! \warning It fills the memory with zeros. If the vector stores complex structures, use 'Fill' instead. */ template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::Zero() { this->vect_allocator_.memoryset(this->data_, char(0), this->GetDataSize() * sizeof(value_type)); } //! Fills the vector with 0, 1, 2, ... template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::Fill() { for (int i = 0; i < this->m_; i++) this->data_[i] = i; } //! Fills the vector with a given value. /*! \param x value to fill the vector with. */ template <class T, class Allocator> template <class T0> void Vector<T, Vect_Full, Allocator>::Fill(const T0& x) { for (int i = 0; i < this->m_; i++) this->data_[i] = x; } //! Fills the vector with a given value. /*! \param x value to fill the vector with. */ template <class T, class Allocator> template <class T0> Vector<T, Vect_Full, Allocator>& Vector<T, Vect_Full, Allocator>::operator= (const T0& x) { this->Fill(x); return *this; } //! Fills the vector randomly. /*! \note The random generator is very basic. */ template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::FillRand() { srand(time(NULL)); for (int i = 0; i < this->m_; i++) this->data_[i] = rand(); } //! Displays the vector. template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::Print() const { for (int i = 0; i < this->GetLength(); i++) cout << (*this)(i) << "\t"; cout << endl; } /********* * NORMS * *********/ //! Returns the infinite norm. /*! \return The infinite norm. */ template <class T, class Allocator> typename Vector<T, Vect_Full, Allocator>::value_type Vector<T, Vect_Full, Allocator>::GetNormInf() const { value_type res = value_type(0); for (int i = 0; i < this->GetLength(); i++) { res = max(res, this->data_[i]); res = max(res, -(this->data_[i])); } return res; } //! Returns the index of the highest absolute value. /*! \return The index of the element that has the highest absolute value. */ template <class T, class Allocator> int Vector<T, Vect_Full, Allocator>::GetNormInfIndex() const {#ifdef SELDON_CHECK_DIMENSIONS if (this->GetLength() == 0) throw WrongDim("Vector<Vect_Full>::GetNormInfIndex()", "Vector is null.");#endif value_type res = value_type(0), temp; int j = 0; for (int i = 0; i < this->GetLength(); i++) { temp = res; res = max(res, this->data_[i]); res = max(res, -(this->data_[i])); if (temp != res) j = i; } return j; } /************************** * OUTPUT/INPUT FUNCTIONS * **************************/ //! Writes the vector in a file. /*! The length of the vector (integer) and all elements of the vector are stored in binary format. \param FileName file name. */ template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::Write(string FileName) const { ofstream FileStream; FileStream.open(FileName.c_str());#ifdef SELDON_CHECK_IO // Checks if the file was opened. if (!FileStream.is_open()) throw IOError("Vector<Vect_Full>::Write(string FileName)", string("Unable to open file \"") + FileName + "\".");#endif this->Write(FileStream); FileStream.close(); } //! Writes the vector in a file stream. /*! The length of the vector (integer) and all elements of the vector are stored in binary format. \param FileStream file stream. */ template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::Write(ofstream& FileStream) const {#ifdef SELDON_CHECK_IO // Checks if the stream is ready. if (!FileStream.good()) throw IOError("Vector<Vect_Full>::Write(ofstream& FileStream)", "Stream is not ready.");#endif FileStream.write(reinterpret_cast<char*>(const_cast<int*>(&this->m_)), sizeof(int)); FileStream.write(reinterpret_cast<char*>(this->data_), this->m_ * sizeof(value_type));#ifdef SELDON_CHECK_IO // Checks if data was written. if (!FileStream.good()) throw IOError("Vector<Vect_Full>::Write(ofstream& FileStream)", string("Output operation failed.") + string(" The output file may have been removed") + "or there is no space left on device.");#endif } //! Writes the vector in a file. /*! All elements of the vector are stored in text format. The length is not stored. \param FileName file name. */ template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::WriteText(string FileName) const { ofstream FileStream; FileStream.open(FileName.c_str());#ifdef SELDON_CHECK_IO // Checks if the file was opened. if (!FileStream.is_open()) throw IOError("Vector<Vect_Full>::WriteText(string FileName)", string("Unable to open file \"") + FileName + "\".");#endif this->WriteText(FileStream); FileStream.close(); } //! Writes the vector in a file stream. /*! All elements of the vector are stored in text format. The length is not stored. \param FileStream file stream. */ template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::WriteText(ofstream& FileStream) const {#ifdef SELDON_CHECK_IO // Checks if the stream is ready. if (!FileStream.good()) throw IOError("Vector<Vect_Full>::WriteText(ofstream& FileStream)", "Stream is not ready.");#endif if (this->GetLength() != 0) FileStream << (*this)(0); for (int i = 1; i < this->GetLength(); i++) FileStream << "\t" << (*this)(i);#ifdef SELDON_CHECK_IO // Checks if data was written. if (!FileStream.good()) throw IOError("Vector<Vect_Full>::WriteText(ofstream& FileStream)", string("Output operation failed.") + string(" The output file may have been removed") + "or there is no space left on device.");#endif } //! Sets the vector from a file. /*! Sets the vector according to a binary file that stores the length of the vector (integer) and all elements. \param FileName file name. */ template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::Read(string FileName) { ifstream FileStream; FileStream.open(FileName.c_str());#ifdef SELDON_CHECK_IO // Checks if the file was opened. if (!FileStream.is_open()) throw IOError("Vector<Vect_Full>::Read(string FileName)", string("Unable to open file \"") + FileName + "\".");#endif this->Read(FileStream); FileStream.close(); } //! Sets the vector from a file stream. /*! Sets the vector according to a binary file stream that stores the length of the vector (integer) and all elements. \param FileStream file stream. */ template <class T, class Allocator> void Vector<T, Vect_Full, Allocator>::Read(ifstream& FileStream) {#ifdef SELDON_CHECK_IO // Checks if the strem is ready. if (!FileStream.good()) throw IOError("Vector<Vect_Full>::Read(ifstream& FileStream)", "Stream is not ready.");#endif int new_size; FileStream.read(reinterpret_cast<char*>(&new_size), sizeof(int)); this->Reallocate(new_size); FileStream.read(reinterpret_cast<char*>(this->data_), new_size * sizeof(value_type));#ifdef SELDON_CHECK_IO // Checks if data was read. if (!FileStream.good()) throw IOError("Vector<Vect_Full>::Read(ifstream& FileStream)", string("Output operation failed.") + string(" The intput file may have been removed") + " or may not contain enough data.");#endif } //! operator<< overloaded for vectors. /*! \param out output stream. \param V vector to be put in the stream. \return The updated stream. */ template <class T, class Storage, class Allocator> ostream& operator << (ostream& out, const Vector<T, Storage, Allocator>& V) { for (int i = 0; i < V.GetLength() - 1; i++) out << V(i) << '\t'; if (V.GetLength() != 0) out << V(V.GetLength() - 1); return out; }} // namespace Seldon.#define SELDON_FILE_VECTOR_CXX#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -