⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vector.cxx

📁 Multivac 的Level set包
💻 CXX
📖 第 1 页 / 共 2 页
字号:
// Copyright (C) 2001-2004 Vivien Mallet//// This file is part of Seldon library.// Seldon library provides matrices and vectors structures for// linear algebra.// // Seldon is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.// // Seldon is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the// GNU General Public License (file "license") for more details.//// For more information, please see the Seldon home page://     http://spacetown.free.fr/lib/seldon/#ifndef SELDON_FILE_VECTOR_CXX#include "Vector.hxx"namespace Seldon{  ///////////////////  // C_VECTOR_BASE //  ///////////////////  /****************   * CONSTRUCTORS *   ****************/    //! Default constructor.  /*!    Nothing is allocated. Vector length is set to zero.  */  template <class T, class Allocator>  inline Vector_Base<T, Allocator>::Vector_Base()  {    m_ = 0;    data_ = NULL;  }  //! Main constructor.  /*!    \param i length.    \warning Nothing is allocated.  */  template <class T, class Allocator>  inline Vector_Base<T, Allocator>::Vector_Base(int i)  {    m_ = i;  }  //! Copy constructor.  /*!    \param A base vector to be copied.    \warning Only the length is copied.  */  template <class T, class Allocator>  inline Vector_Base<T, Allocator>::Vector_Base(Vector_Base<T, Allocator>& A)  {    m_ = A.GetM();  }  /**************   * DESTRUCTOR *   **************/    //! Destructor.  template <class T, class Allocator>  inline Vector_Base<T, Allocator>::~Vector_Base()  {#ifdef SELDON_CHECK_MEMORY    try      {#endif	if (data_ != NULL)	  {	    vect_allocator_.deallocate(data_, m_);	    m_ = 0;	    data_ = NULL;	  }	#ifdef SELDON_CHECK_MEMORY      }    catch (...)      {	m_ = 0;	data_ = NULL;      }#endif  }  /*******************   * BASIC FUNCTIONS *   *******************/    //! Returns the number of elements.  /*!    \return The length of the vector.  */  template <class T, class Allocator>  int Vector_Base<T, Allocator>::GetM() const  {    return m_;  }  //! Returns the number of elements.  /*!    \return The length of the vector.  */  template <class T, class Allocator>  int Vector_Base<T, Allocator>::GetLength() const  {    return m_;  }  //! Returns the number of elements stored.  /*!    \return The length of the vector stored.  */  template <class T, class Allocator>  int Vector_Base<T, Allocator>::GetSize() const  {    return m_;  }  //! Returns a pointer to data_ (stored data).  /*!    \return A pointer to the data_, i.e. the data array.  */  template <class T, class Allocator>  typename Vector_Base<T, Allocator>::pointer  Vector_Base<T, Allocator>::GetData() const  {    return data_;  }  //! Returns a const pointer to data_ (stored data).  /*!    \return A const pointer to the data_, i.e. the data array.  */  template <class T, class Allocator>  typename Vector_Base<T, Allocator>::const_pointer  Vector_Base<T, Allocator>::GetDataConst() const  {    return reinterpret_cast<typename Vector_Base<T,      Allocator>::const_pointer>(data_);  }  //! Returns a pointer of type "void*" to the data array (data_).  /*!    \return A pointer of type "void*" to the data array.  */  template <class T, class Allocator>  void* Vector_Base<T, Allocator>::GetDataVoid() const  {    return reinterpret_cast<void*>(data_);  }  //! Returns a pointer of type "const void*" to the data array (data_).  /*!    \return A pointer of type "const void*" to the data array.  */  template <class T, class Allocator>  const void* Vector_Base<T, Allocator>::GetDataConstVoid() const  {    return reinterpret_cast<const void*>(data_);  }  ///////////////////////  // VECTOR<VECT_FULL> //  ///////////////////////    /****************   * CONSTRUCTORS *   ****************/  //! Default constructor.  /*!    On exit, the vector is empty.  */  template <class T, class Allocator>  Vector<T, Vect_Full, Allocator>::Vector()  throw():    Vector_Base<T, Allocator>()  {  }  //! Main constructor.  /*! Builds a vector of a given size.    \param i length of the vector.  */  template <class T, class Allocator>  Vector<T, Vect_Full, Allocator>::Vector(int i):    Vector_Base<T, Allocator>(i)  {#ifdef SELDON_CHECK_MEMORY    try      {#endif	  	this->data_ = this->vect_allocator_.allocate(i, this);	  #ifdef SELDON_CHECK_MEMORY      }    catch (...)      {	this->m_ = 0;	this->data_ = NULL;      }    if (this->data_ == NULL)      this->m_ = 0;    if (this->data_ == NULL && i != 0)      throw NoMemory("Vector<Vect_Full>::Vector(int)",		     string("Unable to allocate memory for a vector of size ")		     + to_str(i*sizeof(T)) + " bytes ("		     + to_str(i) + " elements).");#endif        }  //! Copy constructor.  /*! Builds a copy of a vector.    \param V vector to be copied.  */  template <class T, class Allocator>  Vector<T, Vect_Full, Allocator>::Vector(Vector<T, Vect_Full, Allocator>& V):    Vector_Base<T, Allocator>(V)  {      #ifdef SELDON_CHECK_MEMORY    try      {#endif	  	this->data_ = this->vect_allocator_.allocate(V.GetM(), this);#ifdef SELDON_CHECK_MEMORY      }    catch (...)      {	this->m_ = 0;	this->data_ = NULL;      }    if (this->data_ == NULL)      this->m_ = 0;    if (this->data_ == NULL && V.GetM() != 0)      throw NoMemory("Vector<Vect_Full>::Vector(Vector<Vect_Full>&)",		     string("Unable to allocate memory for a vector of size ")		     + to_str(V.GetM()*sizeof(T)) + " bytes ("		     + to_str(V.GetM()) + " elements).");#endif    this->vect_allocator_.memorycpy(this->data_, V.GetData(), V.GetM());	    }    /**************   * DESTRUCTOR *   **************/  //! Destructor.  template <class T, class Allocator>  Vector<T, Vect_Full, Allocator>::~Vector()  {  }  /*********************   * MEMORY MANAGEMENT *   *********************/  //! Clears the vector.  /*!    Destructs the vector.    \warning On exit, the vector is an empty vector.  */  template <class T, class Allocator>  inline void Vector<T, Vect_Full, Allocator>::Clear()  {    this->~Vector();  }  //! Vector reallocation.  /*!    The vector is resized.    \param i new length of the vector.    \warning Depending on your allocator, initial elements of the vector may    be lost.  */  template <class T, class Allocator>  inline void Vector<T, Vect_Full, Allocator>::Reallocate(int i)  {    if (i != this->m_)      {	this->m_ = i;#ifdef SELDON_CHECK_MEMORY	try	  {#endif	    this->data_ =	      reinterpret_cast<pointer>(this->vect_allocator_.reallocate(this->data_,									 i, this) );#ifdef SELDON_CHECK_MEMORY	  }	catch (...)	  {	    this->m_ = 0;	    this->data_ = NULL;	    return;	  }	if (this->data_ == NULL)	  {	    this->m_ = 0;	    return;	  }#endif      }  }  //! Changes the length of the vector and sets its data array  //! (low level method).  /*!    Reallocates a vector and sets the new data array. It is useful to create    a vector from pre-existing data.    \param i new length of the vector.    \param data the new data array. 'data' contains the new elements of the    vector and must therefore contain 'i' elements.    \warning 'data' has to be used carefully outside the object.    Unless you use 'Nullify', 'data' will be freed by the destructor,    which means that 'data' must have been allocated carefully. The vector    allocator should be compatible.    \note This method should only be used by advanced users.  */  template <class T, class Allocator>  inline void Vector<T, Vect_Full, Allocator>  ::SetData(int i, typename Vector<T, Vect_Full, Allocator>::pointer data)  {    this->Clear();    this->m_ = i;    this->data_ = data;  }  //! Clears the vector without releasing memory.  /*!    On exit, the vector is empty and the memory has not been released.    It is useful for low level manipulations on a Vector instance.    \warning Memory is not released.  */  template <class T, class Allocator>  void Vector<T, Vect_Full, Allocator>::Nullify()  {    this->m_ = 0;    this->data_ = NULL;  }  /**********************************   * ELEMENT ACCESS AND AFFECTATION *   **********************************/  //! 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>::reference  Vector<T, Vect_Full, Allocator>::operator() (int i)  {#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)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -