Vector3.cxx
1 // Copyright (C) 2010-2012, INRIA
2 // Author(s): Marc Fragu, Vivien Mallet
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_VECTOR_3_CXX
22 
23 
24 #include "Vector3.hxx"
25 
26 
27 namespace Seldon
28 {
29 
30 
32  // VECTOR3 //
34 
35 
36  /***************
37  * CONSTRUCTOR *
38  ***************/
39 
40 
42 
45  template <class T, class Allocator0, class Allocator1, class Allocator2>
47  {
48  }
49 
50 
52 
56  template <class T, class Allocator0, class Allocator1, class Allocator2>
58  {
59  data_.Reallocate(length);
60  }
61 
62 
64 
70  template <class T, class Allocator0, class Allocator1, class Allocator2>
73  {
74  data_.Clear();
75  int n, m = length.GetSize();
76  data_.Reallocate(m);
77  for (int i = 0; i < m; i++)
78  {
79  n = length(i);
80  data_(i).Reallocate(n);
81  }
82  }
83 
84 
86 
92  template <class T, class Allocator0, class Allocator1, class Allocator2>
93  template <class Allocator>
95  ::Vector3(Vector<Vector<int>, Vect_Full, Allocator>& length)
96  {
97  data_.Clear();
98  int n, m = length.GetSize();
99  data_.Reallocate(m);
100  for (int i = 0; i < m; i++)
101  {
102  n = length(i).GetSize();
103  data_(i).Reallocate(n);
104  for (int j = 0; j < n; j++)
105  data_(i)(j).Reallocate(length(i)(j));
106  }
107  }
108 
109 
110  /**************
111  * DESTRUCTOR *
112  **************/
113 
114 
116 
119  template <class T, class Allocator0, class Allocator1, class Allocator2>
121  {
122  }
123 
124 
125  /**********************
126  * VECTORS MANAGEMENT *
127  **********************/
128 
129 
131 
134  template <class T, class Allocator0, class Allocator1, class Allocator2>
136  {
137  return data_.GetLength();
138  }
139 
140 
142 
145  template <class T, class Allocator0, class Allocator1, class Allocator2>
147  {
148  return data_.GetSize();
149  }
150 
151 
153 
157  template <class T, class Allocator0, class Allocator1, class Allocator2>
159  {
160  return data_(i).GetLength();
161  }
162 
163 
165 
169  template <class T, class Allocator0, class Allocator1, class Allocator2>
171  {
172  return data_(i).GetSize();
173  }
174 
175 
177 
182  template <class T, class Allocator0, class Allocator1, class Allocator2>
184  ::GetLength(int i, int j) const
185  {
186  return data_(i)(j).GetLength();
187  }
188 
189 
191 
196  template <class T, class Allocator0, class Allocator1, class Allocator2>
198  ::GetSize(int i, int j) const
199  {
200  return data_(i)(j).GetSize();
201  }
202 
203 
205 
209  template <class T, class Allocator0, class Allocator1, class Allocator2>
211  {
212  size_t total = sizeof(*this) + sizeof(pointer)*GetLength();
213  for (int i = 0; i < GetLength(); i++)
214  {
215  total += sizeof(int)+sizeof(pointer)+sizeof(pointer)*GetLength(i);
216  for (int j = 0; j < GetLength(i); j++)
217  total += data_(i)(j).GetMemorySize();
218  }
219 
220  return total;
221  }
222 
223 
225 
228  template <class T, class Allocator0, class Allocator1, class Allocator2>
230  {
231  int total = 0;
232  for (int i = 0; i < GetLength(); i++)
233  for (int j = 0; j < GetLength(i); j++ )
234  total += GetLength(i, j);
235  return total;
236  }
237 
238 
240 
247  template <class T, class Allocator0, class Allocator1, class Allocator2>
249  ::GetNelement(int beg, int end) const
250  {
251  if (beg > end)
252  throw WrongArgument("Vector3::GetNelement(int beg, int end)",
253  "The lower bound of the range of inner vectors "
254  "of vectors, [" + to_str(beg) + ", " + to_str(end)
255  + "[, is strictly greater than its upper bound.");
256  if (beg < 0 || end > GetLength())
257  throw WrongArgument("Vector3::GetNelement(int beg, int end)",
258  "The inner-vector of vectors indexes should be in "
259  "[0," + to_str(GetLength()) + "] but ["
260  + to_str(beg)
261  + ", " + to_str(end) + "[ was provided.");
262 
263  int total = 0;
264  for (int i = beg; i < end; i++)
265  for (int j = 0; j < GetLength(i); j++ )
266  total += GetLength(i, j);
267  return total;
268  }
269 
270 
272 
286  template <class T, class Allocator0, class Allocator1, class Allocator2>
288  ::GetNelement(int beg0, int end0, int beg1, int end1) const
289  {
290  if (beg0 > end0)
291  throw WrongArgument("Vector3:::GetNelement(int beg0, int end0, "
292  "int beg1, int end1)",
293  "The lower bound of the range of inner vectors "
294  "of vectors, [" + to_str(beg0) + ", " + to_str(end0)
295  + "[, is strictly greater than its upper bound.");
296  if (beg1 > end1)
297  throw WrongArgument("Vector3:::GetNelement(int beg0, int end0, "
298  "int beg1, int end1)",
299  "The lower bound of the range of "
300  "of vectors, [" + to_str(beg1) + ", " + to_str(end1)
301  + "[, is strictly greater than its upper bound.");
302  if (beg0 < 0 || end0 > GetLength())
303  throw WrongArgument("Vector3:::GetNelement(int beg0, int end0, "
304  "int beg1, int end1)",
305  "The inner-vector of vectors indexes should be in "
306  "[0," + to_str(GetLength()) + "] but ["
307  + to_str(beg0)
308  + ", " + to_str(end0) + "[ was provided.");
309 
310  int total = 0;
311  for (int i = beg0; i < end0; i++)
312  {
313  if (beg1 < 0 || end1 > GetLength(i))
314  throw WrongArgument("Vector3:::GetNelement(int beg0, int end0, "
315  "int beg1, int end1)",
316  "For the inner vector of vectors "
317  + to_str(i) + ", the vectors indexes should be "
318  "in [0," + to_str(GetLength(i)) + "] but ["
319  + to_str(beg1)
320  + ", " + to_str(end1) + "[ was provided.");
321  for (int j = beg1; j < end1; j++)
322  total += GetLength(i, j);
323  }
324  return total;
325  }
326 
327 
329 
334  template <class T, class Allocator0, class Allocator1, class Allocator2>
336  ::GetShape(int i) const
337  {
338  Vector<int> shape(GetLength(i));
339  for (int j = 0; j < GetLength(i); j++)
340  shape(j) = GetLength(i, j);
341  return shape;
342  }
343 
344 
346 
351  template <class T, class Allocator0, class Allocator1, class Allocator2>
353  ::GetShape(int i, Vector<int>& shape) const
354  {
355  shape.Reallocate(GetLength(i));
356  for (int j = 0; j < GetLength(i); j++)
357  shape(j) = GetLength(i, j);
358  }
359 
360 
362 
365  template <class T, class Allocator0, class Allocator1, class Allocator2>
367  {
368  data_.Reallocate(N);
369  }
370 
371 
373 
377  template <class T, class Allocator0, class Allocator1, class Allocator2>
379  ::Reallocate(int i, int N)
380  {
381  data_(i).Reallocate(N);
382  }
383 
384 
386 
391  template <class T, class Allocator0, class Allocator1, class Allocator2>
393  ::Reallocate(int i, int j, int N)
394  {
395  data_(i)(j).Reallocate(N);
396  }
397 
398 
400 
404  template <class T, class Allocator0, class Allocator1, class Allocator2>
405  template <class Td, class Allocatord>
408  {
409  data.Reallocate(GetNelement());
410  int i, j, k, n(0);
411  for (i = 0; i < GetLength(); i++)
412  for (j = 0; j < GetLength(i); j++)
413  for (k = 0; k < GetLength(i, j); k++)
414  data(n++) = data_(i)(j)(k);
415  }
416 
417 
419 
427  template <class T, class Allocator0, class Allocator1, class Allocator2>
428  template <class Td, class Allocatord>
430  ::Flatten(int beg, int end, Vector<Td, VectFull, Allocatord>& data) const
431  {
432  if (beg > end)
433  throw WrongArgument("Vector3:::Flatten(int beg, int end, Vector& data)",
434  "The lower bound of the range of inner vectors "
435  "of vectors, [" + to_str(beg) + ", " + to_str(end)
436  + "[, is strictly greater than its upper bound.");
437  if (beg < 0 || end > GetLength())
438  throw WrongArgument("Vector3:::Flatten(int beg, int end, Vector& data)",
439  "The inner-vector of vectors indexes should be in "
440  "[0," + to_str(GetLength()) + "] but ["
441  + to_str(beg)
442  + ", " + to_str(end) + "[ was provided.");
443 
444  data.Reallocate(GetNelement(beg, end));
445  int i, j, k, n(0);
446  for (i = beg; i < end; i++)
447  for (j = 0; j < GetLength(i); j++)
448  for (k = 0; k < GetLength(i, j); k++)
449  data(n++) = data_(i)(j)(k);
450  }
451 
452 
470  template <class T, class Allocator0, class Allocator1, class Allocator2>
471  template <class Td, class Allocatord>
473  ::Flatten(int beg0, int end0, int beg1, int end1,
475  {
476  if (beg0 > end0)
477  throw WrongArgument("Vector3:::Flatten(int beg0, int end0, int beg1, "
478  "int end1, Vector& data)",
479  "The lower bound of the range of inner vectors "
480  "of vectors, [" + to_str(beg0) + ", " + to_str(end0)
481  + "[, is strictly greater than its upper bound.");
482  if (beg1 > end1)
483  throw WrongArgument("Vector3:::Flatten(int beg0, int end0, int beg1, "
484  "int end1, Vector& data)",
485  "The lower bound of the range of "
486  "of vectors, [" + to_str(beg1) + ", " + to_str(end1)
487  + "[, is strictly greater than its upper bound.");
488  if (beg0 < 0 || end0 > GetLength())
489  throw WrongArgument("Vector3:::Flatten(int beg0, int end0, int beg1, "
490  "int end1, Vector& data)",
491  "The inner-vector of vectors indexes should be in "
492  "[0," + to_str(GetLength()) + "] but ["
493  + to_str(beg0)
494  + ", " + to_str(end0) + "[ was provided.");
495 
496  data.Reallocate(GetNelement(beg0, end0, beg1, end1));
497  int i, j, k, n(0);
498  for (i = beg0; i < end0; i++)
499  {
500  if (beg1 < 0 || end1 > GetLength(i))
501  throw WrongArgument("Vector3:::Flatten(int beg0, int end0, int beg1, "
502  "int end1, Vector& data)",
503  "For the inner vector of vectors "
504  + to_str(i) + ", the vectors indexes should be "
505  "in [0," + to_str(GetLength(i)) + "] but ["
506  + to_str(beg1)
507  + ", " + to_str(end1) + "[ was provided.");
508  for (j = beg1; j < end1; j++)
509  for (k = 0; k < GetLength(i, j); k++)
510  data(n++) = data_(i)(j)(k);
511  }
512  }
513 
514 
516 
521  template <class T, class Allocator0, class Allocator1, class Allocator2>
522  void Vector3<T, Allocator0,
523  Allocator1, Allocator2>::PushBack(int i, int j, const T& x)
524  {
525  data_(i)(j).PushBack(x);
526  }
527 
528 
530 
534  template <class T, class Allocator0, class Allocator1, class Allocator2>
537  {
538  data_(i).PushBack(X);
539  }
540 
541 
543 
546  template <class T, class Allocator0, class Allocator1, class Allocator2>
549  Vect_Full, Allocator1>& X)
550  {
551  data_.PushBack(X);
552  }
553 
554 
556 
560  template <class T, class Allocator0, class Allocator1, class Allocator2>
563  Vect_Full, Allocator1>, Vect_Full, Allocator2>& X)
564  {
565  for (int i = 0; i < X.GetM(); i++)
566  data_.PushBack(X(i));
567  }
568 
569 
571 
575  template <class T, class Allocator0, class Allocator1, class Allocator2>
578  {
579  for (int i = 0; i < X.GetLength(); i++)
580  data_.PushBack(X.GetVector());
581  }
582 
583 
585  template <class T, class Allocator0, class Allocator1, class Allocator2>
587  {
588  data_.Clear();
589  }
590 
591 
593 
596  template <class T, class Allocator0, class Allocator1, class Allocator2>
597  void Vector3<T, Allocator0,
598  Allocator1, Allocator2>::Clear(int i)
599  {
600  data_(i).Clear();
601  }
602 
603 
605 
609  template <class T, class Allocator0, class Allocator1, class Allocator2>
611  {
612  data_(i)(j).Clear();
613  }
614 
615 
617 
620  template <class T, class Allocator0, class Allocator1, class Allocator2>
622  {
623  for (int i = 0; i < data_.GetM(); i++)
624  for (int j = 0; j < data_(i).GetM(); j++)
625  data_(i)(j).Fill(x);
626  }
627 
628 
630 
633  template <class T, class Allocator0, class Allocator1, class Allocator2>
634  Vector<Vector<Vector<T, Vect_Full, Allocator0>, Vect_Full, Allocator1>,
635  Vect_Full, Allocator2>&
637  {
638  return data_;
639  }
640 
641 
643 
646  template <class T, class Allocator0, class Allocator1, class Allocator2>
648  Vect_Full, Allocator1>, Vect_Full, Allocator2>&
650  {
651  return data_;
652  }
653 
654 
656 
660  template <class T, class Allocator0, class Allocator1, class Allocator2>
661  Vector<Vector<T, Vect_Full, Allocator0>, VectFull, Allocator1>&
663  {
664  return data_(i);
665  }
666 
667 
669 
673  template <class T, class Allocator0, class Allocator1, class Allocator2>
676  {
677  return data_(i);
678  }
679 
680 
682 
687  template <class T, class Allocator0, class Allocator1, class Allocator2>
690  {
691  return data_(i)(j);
692  }
693 
694 
696 
701  template <class T, class Allocator0, class Allocator1, class Allocator2>
704  const
705  {
706  return data_(i)(j);
707  }
708 
709 
710  /*********************************
711  * ELEMENT ACCESS AND ASSIGNMENT *
712  *********************************/
713 
714 
716 
720  template <class T, class Allocator0, class Allocator1, class Allocator2>
722  Vector3<T, Allocator0,
723  Allocator1, Allocator2>::operator() (int i) const
724  {
725  return data_(i);
726  }
727 
728 
730 
734  template <class T, class Allocator0, class Allocator1, class Allocator2>
737  {
738  return data_(i);
739  }
740 
741 
743 
748  template <class T, class Allocator0, class Allocator1, class Allocator2>
750  Vector3<T, Allocator0,
751  Allocator1, Allocator2>::operator() (int i, int j) const
752  {
753  return data_(i)(j);
754  }
755 
756 
758 
763  template <class T, class Allocator0, class Allocator1, class Allocator2>
766  {
767  return data_(i)(j);
768  }
769 
770 
772 
778  template <class T, class Allocator0, class Allocator1, class Allocator2>
780  Vector3<T, Allocator0,
781  Allocator1, Allocator2>::operator() (int i, int j, int k) const
782  {
783  return data_(i)(j)(k);
784  }
785 
786 
788 
794  template <class T, class Allocator0, class Allocator1, class Allocator2>
795  typename Vector3<T, Allocator0, Allocator1, Allocator2>::reference
797  (int i, int j, int k)
798  {
799  return data_(i)(j)(k);
800  }
801 
802 
803  /**********************
804  * CONVENIENT METHODS *
805  *********************/
806 
807 
809  template <class T, class Allocator0, class Allocator1, class Allocator2>
811  {
812  for (int i = 0; i < data_.GetM(); i++)
813  for(int j = 0; j < data_(i).GetM(); j++)
814  {
815  cout << "Vector " << i << ", " << j << ": ";
816  data_(i)(j).Print();
817  }
818  }
819 
820 
821  /**************************
822  * INPUT/OUTPUT FUNCTIONS *
823  **************************/
824 
825 
827 
832  template <class T, class Allocator0, class Allocator1, class Allocator2>
834  ::Write(string file_name, bool with_size) const
835  {
836  ofstream file_stream;
837  file_stream.open(file_name.c_str());
838 
839 #ifdef SELDON_CHECK_IO
840  // Checks if the file was opened.
841  if (!file_stream.is_open())
842  throw IOError("Vector3::Write(string file_name, bool with_size)",
843  string("Unable to open file \"") + file_name + "\".");
844 #endif
845 
846  this->Write(file_stream, with_size);
847 
848  file_stream.close();
849  }
850 
851 
853 
858  template <class T, class Allocator0, class Allocator1, class Allocator2>
860  ::Write(ostream& stream, bool with_size) const
861  {
862 
863 #ifdef SELDON_CHECK_IO
864  // Checks if the stream is ready.
865  if (!stream.good())
866  throw IOError("Vector3::Write(ostream& stream, bool with_size)",
867  "The stream is not ready.");
868 #endif
869 
870  if (with_size)
871  {
872  int m = GetLength();
873  stream.write(reinterpret_cast<char*>(const_cast<int*>(&m)),
874  sizeof(int));
875  }
876 
877  for (int i = 0; i < GetLength(); i++)
878  {
879  if (with_size)
880  {
881  int m = GetLength(i);
882  stream.write(reinterpret_cast<char*>(const_cast<int*>(&m)),
883  sizeof(int));
884  }
885  for (int j = 0; j < GetLength(i); j++)
886  data_(i)(j).Write(stream, with_size);
887  }
888 
889 #ifdef SELDON_CHECK_IO
890  // Checks if data was written.
891  if (!stream.good())
892  throw IOError("Vector3::Write(ostream& stream, bool with_size)",
893  "Output operation failed.");
894 #endif
895 
896  }
897 
898 
900 
907  template <class T, class Allocator0, class Allocator1, class Allocator2>
909  ::Read(string file_name, bool with_size)
910  {
911  ifstream file_stream;
912  file_stream.open(file_name.c_str());
913 
914 #ifdef SELDON_CHECK_IO
915  // Checks if the file was opened.
916  if (!file_stream.is_open())
917  throw IOError("Vector3::Read(string file_name, bool with_size)",
918  string("Unable to open file \"") + file_name + "\".");
919 #endif
920 
921  this->Read(file_stream, with_size);
922 
923  file_stream.close();
924  }
925 
926 
928 
935  template <class T, class Allocator0, class Allocator1, class Allocator2>
937  ::Read(istream& stream, bool with_size)
938  {
939 
940 #ifdef SELDON_CHECK_IO
941  // Checks if the stream is ready.
942  if (!stream.good())
943  throw IOError("Vector3::Read(istream& stream, bool with_size)",
944  "The stream is not ready.");
945 #endif
946 
947  if (with_size)
948  {
949  int new_size;
950  stream.read(reinterpret_cast<char*>(&new_size), sizeof(int));
951  this->Reallocate(new_size);
952  }
953 
954  for (int i = 0; i < GetLength(); i++)
955  {
956  if (with_size)
957  {
958  int new_size;
959  stream.read(reinterpret_cast<char*>(&new_size), sizeof(int));
960  this->Reallocate(i, new_size);
961  }
962  for (int j = 0; j < GetLength(i); j++)
963  data_(i)(j).Read(stream, with_size);
964  }
965 
966 #ifdef SELDON_CHECK_IO
967  // Checks if data was read.
968  if (!stream.good())
969  throw IOError("Vector3::Read(istream& stream, bool with_size)",
970  "Output operation failed.");
971 #endif
972 
973  }
974 
975 
976 } // namespace Seldon.
977 
978 
979 #define SELDON_FILE_VECTOR_VECTOR_3_CXX
980 #endif
Seldon::Vector3::Reallocate
void Reallocate(int N)
Reallocates the vector of vectors of vectors.
Definition: Vector3.cxx:366
Seldon::Vector3::Vector3
Vector3()
Default constructor.
Definition: Vector3.cxx:46
Seldon::to_str
std::string to_str(const T &input)
Converts most types to string.
Definition: CommonInline.cxx:137
Seldon::Vector< int >
Seldon::Vector3::Read
void Read(string file_name, bool with_size=true)
Reads the Vector3 from a file.
Definition: Vector3.cxx:909
Seldon::Vector3::Clear
void Clear()
Clears the vector.
Definition: Vector3.cxx:586
Seldon::VectFull
Definition: StorageInline.cxx:74
Seldon::Vector3::GetShape
Vector< int > GetShape(int i) const
Returns the shape of a vector of vectors.
Definition: Vector3.cxx:336
Seldon::Vector3
Vector of vectors of vectors.
Definition: Vector3.hxx:65
Seldon::Vector3::GetSize
int GetSize() const
Returns size along dimension 1.
Definition: Vector3.cxx:146
Seldon::Vector3::Fill
void Fill(const T &x)
Fills the vector with a given value.
Definition: Vector3.cxx:621
Seldon::Vector3::GetMemorySize
size_t GetMemorySize() const
Returns the memory used by the object in bytes.
Definition: Vector3.cxx:210
Seldon::Vector3::Flatten
void Flatten(Vector< Td, VectFull, Allocatord > &data) const
Returns all values in a vector.
Definition: Vector3.cxx:407
Seldon::WrongArgument
Definition: Errors.hxx:76
Seldon::Vector3::~Vector3
~Vector3()
Destructor.
Definition: Vector3.cxx:120
Seldon::Vector3::PushBack
void PushBack(int i, int j, const T &x)
Appends an element at the end of the inner vector #i #j.
Definition: Vector3.cxx:523
Seldon::Vector3::GetVector
Vector< Vector< Vector< T, Vect_Full, Allocator0 >, Vect_Full, Allocator1 >, Vect_Full, Allocator2 > & GetVector()
Returns the vector of vectors of vectors.
Definition: Vector3.cxx:636
Seldon::Vector3::Print
void Print() const
Displays the vector.
Definition: Vector3.cxx:810
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::IOError
Definition: Errors.hxx:150
Seldon::Vector3::operator()
const Vector< Vector< T, Vect_Full, Allocator0 >, VectFull, Allocator1 > & operator()(int i) const
Returns a given inner vector of vectors.
Definition: Vector3.cxx:723
Seldon::Vector3::GetLength
int GetLength() const
Returns size along dimension 1.
Definition: Vector3.cxx:135
Seldon::Vector3::GetNelement
int GetNelement() const
Returns the total number of elements in the inner vectors.
Definition: Vector3.cxx:229
Seldon::Vector3::Write
void Write(string file_name, bool with_size=true) const
Writes the instance in a binary file.
Definition: Vector3.cxx:834