/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* */ /* This file is part of the class library */ /* SoPlex --- the Sequential object-oriented simPlex. */ /* */ /* Copyright 1996-2022 Zuse Institute Berlin */ /* */ /* Licensed under the Apache License, Version 2.0 (the "License"); */ /* you may not use this file except in compliance with the License. */ /* You may obtain a copy of the License at */ /* */ /* http://www.apache.org/licenses/LICENSE-2.0 */ /* */ /* Unless required by applicable law or agreed to in writing, software */ /* distributed under the License is distributed on an "AS IS" BASIS, */ /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ /* See the License for the specific language governing permissions and */ /* limitations under the License. */ /* */ /* You should have received a copy of the Apache-2.0 license */ /* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */ /* */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /**@file dsvectorbase.h * @brief Dynamic sparse vectors. */ #ifndef _DSVECTORBASE_H_ #define _DSVECTORBASE_H_ #include #include "soplex/svectorbase.h" namespace soplex { template < class R > class VectorBase; template < class S > class SSVectorBase; template < class R > class SLinSolver; /**@brief Dynamic sparse vectors. * @ingroup Algebra * * Class DSVectorBase implements dynamic sparse vectors, i.e. #SVectorBase%s with an automatic memory management. This * allows the user to freely add() as many nonzeros to a DSVectorBase as desired, without any precautions. For saving * memory method setMax() allows to reduce memory consumption to the amount really required. * * @todo Both DSVectorBase and SVectorBase have a member variable that points to allocated memory. This does not seem to * make too much sense. Why doesn't DSVectorBase use the element of its base class? */ template < class R > class DSVectorBase : public SVectorBase { friend class SLinSolver; private: // ------------------------------------------------------------------------------------------------------------------ /**@name Data */ ///@{ /// Memory. Nonzero* theelem; ///@} // ------------------------------------------------------------------------------------------------------------------ /**@name Private helpers */ ///@{ /// Allocate memory for \p n nonzeros. void allocMem(int n) { spx_alloc(theelem, n); for(int i = 0; i < n; i++) new(&(theelem[i])) Nonzero(); SVectorBase::setMem(n, theelem); } /// Ensure there is room for \p n new nonzeros. void makeMem(int n) { assert(n >= 0); if(SVectorBase::max() - SVectorBase::size() < n) { assert(SVectorBase::size() + n > 0); setMax(SVectorBase::size() + n); } } ///@} public: // ------------------------------------------------------------------------------------------------------------------ /**@name Construction, assignment, and destruction */ ///@{ /// Default constructor. /** Creates a DSVectorBase ready to hold \p n nonzeros. However, the memory is automatically enlarged, if more * nonzeros are added to the DSVectorBase. */ explicit DSVectorBase(int n = 8) : theelem(0) { allocMem((n < 1) ? 2 : n); assert(isConsistent()); } /// Copy constructor. template < class S > explicit DSVectorBase(const SVectorBase& old) : theelem(0) { allocMem(old.size()); SVectorBase::operator=(old); assert(isConsistent()); } /// Copy constructor. /** The redundancy with the copy constructor below is necessary since otherwise the compiler doesn't realize that it * could use the more general one with S = R and generates a shallow copy constructor. */ DSVectorBase(const DSVectorBase& old) : SVectorBase() , theelem(0) { allocMem(old.size()); SVectorBase::operator=(old); assert(isConsistent()); } /// Copy constructor. template < class S > DSVectorBase(const DSVectorBase& old) : SVectorBase() , theelem(0) { allocMem(old.size()); SVectorBase::operator=(old); assert(isConsistent()); } /// Copy constructor. template < class S > explicit DSVectorBase(const VectorBase& vec); /// Copy constructor. template < class S > explicit DSVectorBase(const SSVectorBase& old); /// Assignment operator. template < class S > DSVectorBase& operator=(const SVectorBase& vec) { if(this != &vec) { SVectorBase::clear(); makeMem(vec.size()); SVectorBase::operator=(vec); } return *this; } /// Assignment operator. DSVectorBase& operator=(const DSVectorBase& vec) { if(this != &vec) { SVectorBase::clear(); makeMem(vec.size()); SVectorBase::operator=(vec); } return *this; } /// Assignment operator. template < class S > DSVectorBase& operator=(const DSVectorBase& vec) { if(this != (DSVectorBase*)(&vec)) { SVectorBase::clear(); makeMem(vec.size()); SVectorBase::operator=(vec); } return *this; } /// Assignment operator. template < class S > DSVectorBase& operator=(const VectorBase& vec); /// Assignment operator. template < class S > DSVectorBase& operator=(const SSVectorBase& vec); /// Destructor. virtual ~DSVectorBase() { if(theelem) { for(int i = SVectorBase::max() - 1; i >= 0; i--) theelem[i].~Nonzero(); spx_free(theelem); } } ///@} // ------------------------------------------------------------------------------------------------------------------ /**@name Modification */ ///@{ /// Append nonzeros of \p sv. template < class S > void add(const SVectorBase& vec) { SVectorBase::clear(); makeMem(vec.size()); SVectorBase::add(vec); } /// Append one nonzero \p (i,v). void add(int i, const R& v) { makeMem(1); SVectorBase::add(i, v); } /// Append one uninitialized nonzero. void add(int i) { makeMem(1); SVectorBase::add(i); } /// Append \p n nonzeros. void add(int n, const int i[], const R v[]) { makeMem(n); SVectorBase::add(n, i, v); } /// Reset nonzero memory to >= \p newmax. /** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is * reset to size() only. */ void setMax(int newmax = 1) { int siz = SVectorBase::size(); int len = (newmax < siz) ? siz : newmax; if(len == SVectorBase::max()) return; Nonzero* newmem = 0; /* allocate new memory */ spx_alloc(newmem, len); /* call copy constructor for first elements */ int i; for(i = 0; i < siz; i++) new((&newmem[i])) Nonzero(theelem[i]); /* call default constructor for remaining elements */ for(; i < len; i++) new((&newmem[i])) Nonzero(); /* free old memory */ for(i = SVectorBase::max() - 1; i >= 0; i--) theelem[i].~Nonzero(); if(theelem != 0) spx_free(theelem); /* assign new memory */ theelem = newmem; SVectorBase::setMem(len, theelem); SVectorBase::set_size(siz); } ///@} // ------------------------------------------------------------------------------------------------------------------ /**@name Utilities */ ///@{ /// Consistency check. bool isConsistent() const { #ifdef ENABLE_CONSISTENCY_CHECKS if(theelem != 0 && SVectorBase::mem() != theelem) return MSGinconsistent("DSVectorBase"); #endif return true; } ///@} }; /// Allocate memory for \p n nonzeros (specialization for Real). template<> inline void DSVectorBase::allocMem(int n) { spx_alloc(theelem, n); SVectorBase::setMem(n, theelem); } /// Destructor (specialization for Real). template<> inline DSVectorBase::~DSVectorBase() { if(theelem) spx_free(theelem); } /// Reset nonzero memory to >= \p newmax. /** This methods resets the memory consumption to \p newmax. However, if \p newmax < size(), it is * reset to size() only (specialization for Real). */ template<> inline void DSVectorBase::setMax(int newmax) { int siz = size(); int len = (newmax < siz) ? siz : newmax; spx_realloc(theelem, len); setMem(len, theelem); // reset 'size' to old size since the above call to setMem() sets 'size' to 0 set_size(siz); } } // namespace soplex #endif // _DSVECTORBASE_H_