Vector2.cxx
1 // Copyright (C) 2010, INRIA
2 // Author(s): Marc Fragu, Vivien Mallet
3 // Copyright (C) 2011, Vivien Mallet
4 //
5 // This file is part of the linear-algebra library Seldon,
6 // http://seldon.sourceforge.net/.
7 //
8 // Seldon is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License as published by the Free
10 // Software Foundation; either version 2.1 of the License, or (at your option)
11 // any later version.
12 //
13 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
14 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16 // more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with Seldon. If not, see http://www.gnu.org/licenses/.
20 
21 
22 #ifndef SELDON_FILE_VECTOR_VECTOR2_CXX
23 
24 
25 #include "Vector2.hxx"
26 
27 
28 namespace Seldon
29 {
30 
31 
33  // VECTOR2 //
35 
36 
37  /***************
38  * CONSTRUCTOR *
39  ***************/
40 
41 
43 
46  template <class T, class Allocator0, class Allocator1>
48  {
49  }
50 
51 
53 
56  template <class T, class Allocator0, class Allocator1>
58  {
59  data_.Reallocate(length);
60  }
61 
62 
64 
68  template <class T, class Allocator0, class Allocator1>
70  {
71  data_.Clear();
72  long m = length.GetSize();
73  data_.Reallocate(m);
74  for(long i = 0; i < m; i++)
75  data_(i).Reallocate(length(i));
76  }
77 
78 
79  /**************
80  * DESTRUCTOR *
81  **************/
82 
83 
85  template <class T, class Allocator0, class Allocator1>
87  {
88  }
89 
90 
91  /*****************************
92  * MANAGEMENT OF THE VECTORS *
93  *****************************/
94 
95 
97 
100  template <class T, class Allocator0, class Allocator1>
102  {
103  for (long i = 0; i < GetLength(); i++)
104  if (GetLength(i) > 0)
105  return false;
106  return true;
107  }
108 
109 
111 
114  template <class T, class Allocator0, class Allocator1>
116  {
117  return data_.GetSize();
118  }
119 
120 
122 
125  template <class T, class Allocator0, class Allocator1>
127  {
128  return data_.GetLength();
129  }
130 
131 
133 
137  template <class T, class Allocator0, class Allocator1>
139  {
140  return data_(i).GetSize();
141  }
142 
143 
145 
149  template <class T, class Allocator0, class Allocator1>
151  {
152  size_t total = sizeof(*this) + sizeof(pointer)*GetLength();
153  for (long i = 0; i < GetLength(); i++)
154  total += data_(i).GetMemorySize();
155 
156  return total;
157  }
158 
159 
161 
165  template <class T, class Allocator0, class Allocator1>
167  {
168  return data_(i).GetLength();
169  }
170 
171 
173 
176  template <class T, class Allocator0, class Allocator1>
178  {
179  long total = 0;
180  for (long i = 0; i < GetLength(); i++)
181  total += GetLength(i);
182  return total;
183  }
184 
185 
187 
194  template <class T, class Allocator0, class Allocator1>
195  long Vector2<T, Allocator0, Allocator1>::GetNelement(long beg, long end) const
196  {
197  if (beg > end)
198  throw WrongArgument("Vector2::GetNelement(long beg, long end)",
199  "The lower bound of the range of inner vectors, ["
200  + to_str(beg) + ", " + to_str(end)
201  + "[, is strictly greater than its upper bound.");
202  if (beg < 0 || end > GetLength())
203  throw WrongArgument("Vector2::GetNelement(long beg, long end)",
204  "The inner-vector indexes should be in [0,"
205  + to_str(GetLength()) + "] but [" + to_str(beg)
206  + ", " + to_str(end) + "[ was provided.");
207 
208  long total = 0;
209  for (long i = beg; i < end; i++)
210  total += GetLength(i);
211  return total;
212  }
213 
214 
216 
219  template <class T, class Allocator0, class Allocator1>
221  {
222  Vector<long> shape(GetLength());
223  for (long i = 0; i < GetLength(); i++)
224  shape(i) = GetLength(i);
225  return shape;
226  }
227 
228 
230 
233  template <class T, class Allocator0, class Allocator1>
235  {
236  shape.Reallocate(GetLength());
237  for (long i = 0; i < GetLength(); i++)
238  shape(i) = GetLength(i);
239  }
240 
241 
243 
246  template <class T, class Allocator0, class Allocator1>
248  {
249  data_.Reallocate(M);
250  }
251 
252 
254 
258  template <class T, class Allocator0, class Allocator1>
260  {
261  data_(i).Reallocate(N);
262  }
263 
264 
266 
270  template <class T, class Allocator0, class Allocator1>
273  {
274  long m = length.GetSize();
275  data_.Reallocate(m);
276  for(long i = 0; i < m; i++)
277  data_(i).Reallocate(length(i));
278  }
279 
280 
282 
287  template <class T, class Allocator0, class Allocator1>
289  {
290  if (beg > end)
291  throw WrongArgument("Vector2::SelectInnerVector(long beg, long end)",
292  "The lower bound of the range of inner vectors, ["
293  + to_str(beg) + ", " + to_str(end)
294  + "[, is strictly greater than its upper bound.");
295  if (beg < 0 || end > GetLength())
296  throw WrongArgument("Vector2::SelectInnerVector(long beg, long end)",
297  "The inner-vector indexes should be in [0,"
298  + to_str(GetLength()) + "] but [" + to_str(beg)
299  + ", " + to_str(end) + "[ was provided.");
300 
301  if (beg > 0)
302  for (long i = 0; i < end - beg; i++)
303  data_(i) = data_(beg + i);
304 
305  data_.Resize(end - beg);
306  }
307 
308 
310 
314  template <class T, class Allocator0, class Allocator1>
317  {
318  Vector<T, VectFull, Allocator0> data(GetNelement());
319  long i, j, n(0);
320  for (i = 0; i < GetLength(); i++)
321  for (j = 0; j < GetLength(i); j++)
322  data(n++) = data_(i)(j);
323  return data;
324  }
325 
326 
328 
332  template <class T, class Allocator0, class Allocator1>
333  template <class Td, class Allocatord>
336  {
337  data.Reallocate(GetNelement());
338  long i, j, n(0);
339  for (i = 0; i < GetLength(); i++)
340  for (j = 0; j < GetLength(i); j++)
341  data(n++) = data_(i)(j);
342  }
343 
344 
346 
354  template <class T, class Allocator0, class Allocator1>
355  template <class Td, class Allocatord>
357  ::Flatten(long beg, long end, Vector<Td, VectFull, Allocatord>& data) const
358  {
359  if (beg > end)
360  throw WrongArgument("Vector2::Flatten(long beg, long end, Vector& data)",
361  "The lower bound of the range of inner vectors, ["
362  + to_str(beg) + ", " + to_str(end)
363  + "[, is strictly greater than its upper bound.");
364  if (beg < 0 || end > GetLength())
365  throw WrongArgument("Vector2::Flatten(long beg, long end, Vector& data)",
366  "The inner-vector indexes should be in [0,"
367  + to_str(GetLength()) + "] but [" + to_str(beg)
368  + ", " + to_str(end) + "[ was provided.");
369 
370  data.Reallocate(GetNelement(beg, end));
371  long i, j, n(0);
372  for (i = beg; i < end; i++)
373  for (j = 0; j < GetLength(i); j++)
374  data(n++) = data_(i)(j);
375  }
376 
377 
379 
383  template <class T, class Allocator0, class Allocator1>
385  {
386  data_(i).PushBack(x);
387  }
388 
389 
391 
394  template <class T, class Allocator0, class Allocator1>
397  {
398  data_.PushBack(X);
399  }
400 
401 
403 
407  template <class T, class Allocator0, class Allocator1>
410  VectFull, Allocator1>& V)
411  {
412  for (long i = 0; i < V.GetM(); i++)
413  data_.PushBack(V(i));
414  }
415 
416 
418 
422  template <class T, class Allocator0, class Allocator1>
425  {
426  PushBack(V.GetVector());
427  }
428 
429 
431  template <class T, class Allocator0, class Allocator1>
433  {
434  data_.Clear();
435  }
436 
437 
439 
442  template <class T, class Allocator0, class Allocator1>
444  {
445  data_(i).Clear();
446  }
447 
448 
450 
453  template <class T, class Allocator0, class Allocator1>
455  {
456  for (long i = 0; i < data_.GetM(); i++)
457  data_(i).Fill(x);
458  }
459 
460 
462 
465  template <class T, class Allocator0, class Allocator1>
468  {
469  return data_;
470  }
471 
472 
474 
477  template <class T, class Allocator0, class Allocator1>
480  {
481  return data_;
482  }
483 
484 
486 
490  template <class T, class Allocator0, class Allocator1>
491  Vector<T, VectFull, Allocator0>&
493  {
494  return data_(i);
495  }
496 
497 
499 
503  template <class T, class Allocator0, class Allocator1>
506  {
507  return data_(i);
508  }
509 
510 
512 
517  template <class T, class Allocator0, class Allocator1>
520  {
521  Clear();
522  Reallocate(V.GetLength());
523  for (long i = 0; i < V.GetLength(); i++)
524  data_(i) = V(i);
525  }
526 
527 
529 
533  template <class T, class Allocator0, class Allocator1>
536  {
538  output.Copy(*this);
539  return output;
540  }
541 
542 
543  /*********************************
544  * ELEMENT ACCESS AND ASSIGNMENT *
545  *********************************/
546 
547 
549 
553  template <class T, class Allocator0, class Allocator1>
556  {
557  return data_(i);
558  }
559 
560 
562 
566  template <class T, class Allocator0, class Allocator1>
569  {
570  return data_(i);
571  }
572 
573 
575 
580  template <class T, class Allocator0, class Allocator1>
583  {
584  return data_(i)(j);
585  }
586 
587 
589 
594  template <class T, class Allocator0, class Allocator1>
595  typename Vector2<T, Allocator0, Allocator1>::reference
597  {
598  return data_(i)(j);
599  }
600 
601 
602  /**********************
603  * CONVENIENT METHODS *
604  **********************/
605 
606 
608 
616  template <class T, class Allocator0, class Allocator1>
617  template <class V2>
619  {
620  if (V.GetLength() != GetLength())
621  return false;
622  for (long i = 0; i < GetLength(); i++)
623  if (V.GetLength(i) != GetLength(i))
624  return false;
625  return true;
626  }
627 
628 
630  template <class T, class Allocator0, class Allocator1>
632  {
633  for (long i = 0; i < data_.GetM(); i++)
634  {
635  cout << "Vector " << i << ": ";
636  data_(i).Print();
637  }
638  }
639 
640 
641  /**************************
642  * INPUT/OUTPUT FUNCTIONS *
643  **************************/
644 
645 
647 
655  template <class T, class Allocator0, class Allocator1>
657  ::Write(string file_name, bool with_size) const
658  {
659  ofstream file_stream;
660  file_stream.open(file_name.c_str());
661 
662 #ifdef SELDON_CHECK_IO
663  // Checks if the file was opened.
664  if (!file_stream.is_open())
665  throw IOError("Vector2::Write(string file_name, bool with_size)",
666  string("Unable to open file \"") + file_name + "\".");
667 #endif
668 
669  this->Write(file_stream, with_size);
670 
671  file_stream.close();
672  }
673 
674 
676 
684  template <class T, class Allocator0, class Allocator1>
686  ::Write(ostream& stream, bool with_size) const
687  {
688 
689 #ifdef SELDON_CHECK_IO
690  // Checks if the stream is ready.
691  if (!stream.good())
692  throw IOError("Vector2::Write(ostream& stream, bool with_size)",
693  "The stream is not ready.");
694 #endif
695 
696  if (with_size)
697  {
698  int m = GetLength();
699  stream.write(reinterpret_cast<char*>(const_cast<int*>(&m)),
700  sizeof(int));
701  }
702 
703  for (long i = 0; i < GetLength(); i++)
704  data_(i).Write(stream, with_size);
705 
706 #ifdef SELDON_CHECK_IO
707  // Checks if data was written.
708  if (!stream.good())
709  throw IOError("Vector2::Write(ostream& stream, bool with_size)",
710  "Output operation failed.");
711 #endif
712 
713  }
714 
715 
717 
727  template <class T, class Allocator0, class Allocator1>
729  ::Read(string file_name, bool with_size)
730  {
731  ifstream file_stream;
732  file_stream.open(file_name.c_str());
733 
734 #ifdef SELDON_CHECK_IO
735  // Checks if the file was opened.
736  if (!file_stream.is_open())
737  throw IOError("Vector2::Read(string file_name, bool with_size)",
738  string("Unable to open file \"") + file_name + "\".");
739 #endif
740 
741  this->Read(file_stream, with_size);
742 
743  file_stream.close();
744  }
745 
746 
748 
758  template <class T, class Allocator0, class Allocator1>
760  ::Read(istream& stream, bool with_size)
761  {
762 
763 #ifdef SELDON_CHECK_IO
764  // Checks if the stream is ready.
765  if (!stream.good())
766  throw IOError("Vector2::Read(istream& stream, bool with_size)",
767  "The stream is not ready.");
768 #endif
769 
770  if (with_size)
771  {
772  int new_size;
773  stream.read(reinterpret_cast<char*>(&new_size), sizeof(int));
774  this->Reallocate(new_size);
775  }
776 
777  for (long i = 0; i < GetLength(); i++)
778  data_(i).Read(stream, with_size);
779 
780 #ifdef SELDON_CHECK_IO
781  // Checks if data was read.
782  if (!stream.good())
783  throw IOError("Vector2::Read(istream& stream, bool with_size)",
784  "Output operation failed.");
785 #endif
786 
787  }
788 
789 
790 } // namespace Seldon.
791 
792 
793 #define SELDON_FILE_VECTOR_VECTOR2_CXX
794 #endif
Seldon::Vector2::Copy
void Copy(const Vector2< T, Allocator0, Allocator1 > &V)
Copies a Vector2 instance.
Definition: Vector2.cxx:519
Seldon::Vector2::HasSameShape
bool HasSameShape(const V2 &V) const
Checks whether another Vector2 instance has the same shape.
Definition: Vector2.cxx:618
Seldon::Vector2::GetLength
long GetLength() const
Returns the size along dimension 1.
Definition: Vector2.cxx:126
Seldon::Vector2::GetSize
long GetSize() const
Returns the size along dimension 1.
Definition: Vector2.cxx:115
Seldon::to_str
std::string to_str(const T &input)
Converts most types to string.
Definition: CommonInline.cxx:137
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::Vector2
Vector of vectors.
Definition: Vector2.hxx:53
Seldon::Vector2::GetMemorySize
size_t GetMemorySize() const
Returns the memory used by the object in bytes.
Definition: Vector2.cxx:150
Seldon::Vector2::GetVector
Vector< Vector< T, VectFull, Allocator0 >, VectFull, Allocator1 > & GetVector()
Returns the vector of vectors.
Definition: Vector2.cxx:467
Seldon::Vector2::GetNelement
long GetNelement() const
Returns the total number of elements in the inner vectors.
Definition: Vector2.cxx:177
Seldon::Vector2::Print
void Print() const
Displays the vector.
Definition: Vector2.cxx:631
Seldon::VectFull
Definition: StorageInline.cxx:74
Seldon::Vector2::IsEmpty
bool IsEmpty() const
Checks whether no elements are contained in the inner vectors.
Definition: Vector2.cxx:101
Seldon::Vector2::PushBack
void PushBack(long i, const T &x)
Appends an element at the end of the inner vector #i.
Definition: Vector2.cxx:384
Seldon::Vector2::Write
void Write(string file_name, bool with_size=true) const
Writes the instance in a binary file.
Definition: Vector2.cxx:657
Seldon::Vector2::~Vector2
~Vector2()
Destructor. The vector of vectors and the inner vectors are deallocated.
Definition: Vector2.cxx:86
Seldon::Vector2::Fill
void Fill(const T &x)
Fills the vector with a given value.
Definition: Vector2.cxx:454
Seldon::Vector2::Read
void Read(string file_name, bool with_size=true)
Reads the Vector2 from a file.
Definition: Vector2.cxx:729
Seldon::Vector2::GetShape
Vector< long > GetShape() const
Returns the shape.
Definition: Vector2.cxx:220
Seldon::Vector2::Select
void Select(long beg, long end)
Selects a range of inner vectors.
Definition: Vector2.cxx:288
Seldon::Vector2::Reallocate
void Reallocate(long M)
Reallocates the vector of vector.
Definition: Vector2.cxx:247
Seldon::Vector2::Clear
void Clear()
Clears the vector.
Definition: Vector2.cxx:432
Seldon::Vector2::Vector2
Vector2()
Default constructor.
Definition: Vector2.cxx:47
Seldon::WrongArgument
Definition: Errors.hxx:76
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::IOError
Definition: Errors.hxx:150
Seldon::Vector2::Flatten
Vector< T, VectFull, Allocator0 > Flatten() const
Returns all values in a vector.
Definition: Vector2.cxx:316
Seldon::Vector2::operator()
const Vector< T, VectFull, Allocator0 > & operator()(long i) const
Returns a given inner vector.
Definition: Vector2.cxx:555
Seldon::Vector2::Copy
Vector2< T, Allocator0, Allocator1 > Copy() const
Copies a Vector2 instance.
Definition: Vector2.cxx:535