VectorInline.cxx
1 // Copyright (C) 2001-2011 Vivien Mallet
2 // Copyright (C) 2003-2015 Marc DuruflĂ©
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_VECTOR_INLINE_CXX
22 
23 #include "Vector.hxx"
24 #include "VectorExpressionInline.cxx"
25 
26 namespace Seldon
27 {
28 
29 
31  // C_VECTOR_BASE //
33 
34 
35  /****************
36  * CONSTRUCTORS *
37  ****************/
38 
39 
41 
44  template <class T, class Allocator>
46  {
47  m_ = 0;
48  data_ = NULL;
49  }
50 
51 
53 
57  template <class T, class Allocator>
59  {
60  m_ = i;
61  data_ = NULL;
62  }
63 
64 
66 
70  template <class T, class Allocator>
73  {
74  m_ = A.GetM();
75  data_ = NULL;
76  }
77 
78 
79  /**************
80  * DESTRUCTOR *
81  **************/
82 
83 
85  template <class T, class Allocator>
87  {
88  this->Clear();
89  }
90 
91 
93  template <class T, class Allocator>
95  {
96 
97 #ifdef SELDON_CHECK_MEMORY
98  try
99  {
100 #endif
101 
102  if (data_ != NULL)
103  {
104  Allocator::deallocate(data_, m_);
105  m_ = 0;
106  data_ = NULL;
107  }
108 
109 #ifdef SELDON_CHECK_MEMORY
110  }
111  catch (...)
112  {
113  m_ = 0;
114  data_ = NULL;
115  }
116 #endif
117 
118  }
119 
120 
121  /*******************
122  * BASIC FUNCTIONS *
123  *******************/
124 
125 
127 
130  template <class T, class Allocator>
132  {
133  return m_;
134  }
135 
136 
138 
141  template <class T, class Allocator>
143  {
144  return m_;
145  }
146 
147 
149 
152  template <class T, class Allocator>
154  {
155  return m_;
156  }
157 
158 
160 
164  template <class T, class Allocator>
166  {
167  return sizeof(*this) + sizeof(T)*size_t(GetSize());
168  }
169 
170 
172 
175  template <class T, class Allocator>
176  inline typename Vector_Base<T, Allocator>::pointer
178  {
179  return data_;
180  }
181 
182 
184 
187  template <class T, class Allocator>
188  inline typename Vector_Base<T, Allocator>::const_pointer
190  {
191  return reinterpret_cast<typename Vector_Base<T,
192  Allocator>::const_pointer>(data_);
193  }
194 
195 
197 
200  template <class T, class Allocator>
202  {
203  return reinterpret_cast<void*>(data_);
204  }
205 
206 
208 
211  template <class T, class Allocator>
213  {
214  return reinterpret_cast<const void*>(data_);
215  }
216 
217 
219  // VECTOR<VECT_FULL> //
221 
222 
223  /****************
224  * CONSTRUCTORS *
225  ****************/
226 
227 
229 
232  template <class T, class Allocator>
234  Vector_Base<T, Allocator>()
235  {
236  }
237 
238 
240 
243  template <class T, class Allocator>
245  Vector_Base<T, Allocator>(i)
246  {
247 
248 #ifdef SELDON_CHECK_MEMORY
249  try
250  {
251 #endif
252 
253  this->data_ = Allocator::allocate(i, this);
254 
255 #ifdef SELDON_CHECK_MEMORY
256  }
257  catch (...)
258  {
259  this->m_ = 0;
260  this->data_ = NULL;
261  }
262  if (this->data_ == NULL)
263  this->m_ = 0;
264  if (this->data_ == NULL && i != 0)
265  throw NoMemory("Vector<VectFull>::Vector(int)",
266  string("Unable to allocate memory for a vector of size ")
267  + to_str(i*sizeof(T)) + " bytes ("
268  + to_str(i) + " elements).");
269 #endif
270 
271  }
272 
273 
275 
285  template <class T, class Allocator>
287  ::Vector(size_t i, typename Vector<T, VectFull, Allocator>::pointer data):
288  Vector_Base<T, Allocator>()
289  {
290  SetData(i, data);
291  }
292 
293 
295 
298  template <class T, class Allocator>
299  inline Vector<T, VectFull, Allocator>::
301  Vector_Base<T, Allocator>(V)
302  {
303 
304 #ifdef SELDON_CHECK_MEMORY
305  try
306  {
307 #endif
308 
309  this->data_ = Allocator::allocate(V.GetM(), this);
310 
311 #ifdef SELDON_CHECK_MEMORY
312  }
313  catch (...)
314  {
315  this->m_ = 0;
316  this->data_ = NULL;
317  }
318  if (this->data_ == NULL)
319  this->m_ = 0;
320  if (this->data_ == NULL && V.GetM() != 0)
321  throw NoMemory("Vector<VectFull>::Vector(Vector<VectFull>&)",
322  string("Unable to allocate memory for a vector of size ")
323  + to_str(V.GetM()*sizeof(T)) + " bytes ("
324  + to_str(V.GetM()) + " elements).");
325 #endif
326 
327  Allocator::memorycpy(this->data_, V.GetData(), V.GetM());
328 
329  }
330 
331 
332  /**************
333  * DESTRUCTOR *
334  **************/
335 
336 
338  template <class T, class Allocator>
340  {
341  }
342 
343 
344  /*********************
345  * MEMORY MANAGEMENT *
346  *********************/
347 
348 
350 
354  template <class T, class Allocator>
356  {
358  }
359 
360 
362 
368  template <class T, class Allocator>
370  {
371  ReallocateVector(i);
372  }
373 
374 
376 
382  template <class T, class Allocator>
384  {
385  // function implemented in the aim that explicit specialization
386  // of Reallocate can call ReallocateVector
387  if (i != this->m_)
388  {
389 
390  this->m_ = i;
391 
392 #ifdef SELDON_CHECK_MEMORY
393  try
394  {
395 #endif
396 
397  this->data_ =
398  reinterpret_cast<pointer>(Allocator::
399  reallocate(this->data_, i, this));
400 
401 #ifdef SELDON_CHECK_MEMORY
402  }
403  catch (...)
404  {
405  this->m_ = 0;
406  this->data_ = NULL;
407  return;
408  }
409  if (this->data_ == NULL)
410  {
411  this->m_ = 0;
412  return;
413  }
414 #endif
415 
416  }
417  }
418 
419 
435  template <class T, class Allocator>
438  {
439  this->Clear();
440 
441  this->m_ = i;
442 
443  this->data_ = data;
444  }
445 
446 
448 
460  template <class T, class Allocator>
461  template <class Allocator0>
462  inline void Vector<T, VectFull, Allocator>
464  {
465  SetData(V.GetLength(), V.GetData());
466  }
467 
468 
470 
475  template <class T, class Allocator>
477  {
478  this->m_ = 0;
479  this->data_ = NULL;
480  }
481 
482 
483  /**********************************
484  * ELEMENT ACCESS AND AFFECTATION *
485  **********************************/
486 
487 
489 
493  template <class T, class Allocator>
496  {
497 
498 #ifdef SELDON_CHECK_BOUNDS
499  CheckBounds(i, this->m_, "Vector<VectFull>");
500 #endif
501 
502  return this->data_[i];
503  }
504 
505 
507 
511  template <class T, class Allocator>
514  {
515 
516 #ifdef SELDON_CHECK_BOUNDS
517  CheckBounds(i, this->m_, "Vector<VectFull>");
518 #endif
519 
520  return this->data_[i];
521  }
522 
523 
525 
529  template <class T, class Allocator>
532  {
533 
534 #ifdef SELDON_CHECK_BOUNDS
535  CheckBounds(i, this->m_, "Vector<VectFull>");
536 #endif
537 
538  return this->data_[i];
539  }
540 
541 
543 
547  template <class T, class Allocator>
550  {
551 
552 #ifdef SELDON_CHECK_BOUNDS
553  CheckBounds(i, this->m_, "Vector<VectFull>");
554 #endif
555 
556  return this->data_[i];
557  }
558 
559 
561 
566  template <class T, class Allocator>
569  {
570  this->Copy(X);
571 
572  return *this;
573  }
574 
575 
577 
582  template <class T, class Allocator>
585  {
586  this->Reallocate(X.GetLength());
587 
588  Allocator::memorycpy(this->data_, X.GetData(), this->m_);
589  }
590 
591 
593 
598  template <class T, class Allocator>
601  {
602  return Vector<T, VectFull, Allocator>(*this);
603  }
604 
605 
607 
610  template <class T, class Allocator> template<class T0>
612  ::operator*= (const T0& alpha)
613  {
614  for (size_t i = 0; i < this->m_; i++)
615  this->data_[i] *= alpha;
616 
617  return *this;
618  }
619 
620 
622  template<class T, class Allocator> template<class E>
625  {
626  this->Reallocate(X.GetSize());
627  for (size_t i = 0; i < this->m_; i++)
628  this->data_[i] = X(i);
629 
630  return *this;
631  }
632 
633 
635  template<class T, class Allocator> template<class E>
638  {
639  for (size_t i = 0; i < this->m_; i++)
640  this->data_[i] += X(i);
641 
642  return *this;
643  }
644 
645 
647  template<class T, class Allocator> template<class E>
650  {
651  for (size_t i = 0; i < this->m_; i++)
652  this->data_[i] -= X(i);
653 
654  return *this;
655  }
656 
657 
659 
664  template <class T, class Allocator>
666  {
667  size_t i = this->GetLength();
668  this->Reallocate(i + 1);
669  this->data_[i] = x;
670  }
671 
672 
674 
677  template <class T, class Allocator> template<class T0>
679  {
680  Resize(this->m_+1);
681  this->data_[this->m_-1] = x;
682  }
683 
684 
686 
689  template <class T, class Allocator> template<class Allocator0>
692  {
693  size_t Nold = this->m_;
694  Resize(this->m_ + X.GetSize());
695  for (size_t i = 0; i < X.GetSize(); i++)
696  this->data_[Nold+i] = X(i);
697  }
698 
699 
700  /*******************
701  * BASIC FUNCTIONS *
702  *******************/
703 
704 
706 
709  template <class T, class Allocator>
711  {
712  return this->m_;
713  }
714 
715 
716  /************************
717  * CONVENIENT FUNCTIONS *
718  ************************/
719 
720 
722 
726  template <class T, class Allocator>
728  {
729  Allocator::memoryset(this->data_, char(0),
730  this->GetDataSize() * sizeof(value_type));
731  }
732 
733 
735  template <class T, class Allocator>
737  {
738  for (size_t i = 0; i < this->m_; i++)
739  SetComplexReal(i, this->data_[i]);
740  }
741 
742 
744 
747  template <class T, class Allocator>
748  template <class T0>
749  inline void Vector<T, VectFull, Allocator>::Fill(const T0& x)
750  {
751  T x_;
752  SetComplexReal(x, x_);
753  for (size_t i = 0; i < this->m_; i++)
754  this->data_[i] = x_;
755  }
756 
757 
759 
762  template <class T, class Allocator>
765  {
766  this->Fill(x);
767 
768  return *this;
769  }
770 
771 
773 
776  template <class T, class Allocator>
778  {
779 #ifndef SELDON_WITHOUT_REINIT_RANDOM
780  srand(time(NULL));
781 #endif
782  for (size_t i = 0; i < this->m_; i++)
783  SetComplexReal(rand(), this->data_[i]);
784  }
785 
786 
788  template <class T, class Allocator>
790  {
791  for (size_t i = 0; i < this->GetLength(); i++)
792  cout << (*this)(i) << "\t";
793  cout << endl;
794  }
795 
796 
797 } // namespace Seldon.
798 
799 #define SELDON_FILE_VECTOR_INLINE_CXX
800 #endif
Seldon::VectorExpression::GetSize
long GetSize() const
returns the size of the associated vector
Definition: VectorExpressionInline.cxx:8
Seldon::to_str
std::string to_str(const T &input)
Converts most types to string.
Definition: CommonInline.cxx:137
Seldon::Vector_Base::GetM
long GetM() const
Returns the number of elements.
Definition: VectorInline.cxx:131
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::VectorExpression
Expression between vectors.
Definition: VectorExpression.hxx:8
Seldon::Vector_Base::GetSize
size_t GetSize() const
Returns the number of elements stored.
Definition: VectorInline.cxx:153
Seldon::Vector_Base::GetDataConstVoid
const void * GetDataConstVoid() const
Returns a pointer of type "const void*" to the data array (data_).
Definition: VectorInline.cxx:212
Seldon::Vector_Base
Base structure for all vectors.
Definition: SeldonHeader.hxx:201
Seldon::Vector_Base::Clear
void Clear()
Releases memory used by the vector.
Definition: VectorInline.cxx:94
Seldon::Vector_Base::GetMemorySize
size_t GetMemorySize() const
Returns the memory used by the object in bytes.
Definition: VectorInline.cxx:165
Seldon::Vector< T, VectFull, Allocator >
Full vector class.
Definition: Vector.hxx:88
Seldon::Vector_Base::GetLength
size_t GetLength() const
Returns the number of elements.
Definition: VectorInline.cxx:142
Seldon::Vector_Base::GetDataVoid
void * GetDataVoid() const
Returns a pointer of type "void*" to the data array (data_).
Definition: VectorInline.cxx:201
Seldon::Vector_Base::~Vector_Base
~Vector_Base()
Destructor.
Definition: VectorInline.cxx:86
Seldon::Vector_Base::Vector_Base
Vector_Base()
Default constructor.
Definition: VectorInline.cxx:45
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::NoMemory
Definition: Errors.hxx:90
Seldon::Vector_Base::GetDataConst
const_pointer GetDataConst() const
Returns a const pointer to data_ (stored data).
Definition: VectorInline.cxx:189
Seldon::SetComplexReal
void SetComplexReal(int n, std::complex< T > &number)
Sets a complex number to (n, 0).
Definition: CommonInline.cxx:234
Seldon::Vector_Base::GetData
pointer GetData() const
Returns a pointer to data_ (stored data).
Definition: VectorInline.cxx:177