HeterogeneousCollection.cxx
1 // Copyright (C) 2001-2009 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_HETEROGENEOUSCOLLECTION_CXX
22 
23 
24 #include "HeterogeneousCollection.hxx"
25 
26 
27 namespace Seldon
28 {
29 
30 
32  // VECTOR HETEROGENEOUSCOLLECTION //
34 
35 
36  /**************
37  * DESTRUCTOR *
38  **************/
39 
40 
42 
45  template <class T, template <class U> class Allocator >
47  {
48  float_dense_c_.Clear();
49  float_sparse_c_.Clear();
50  double_dense_c_.Clear();
51  double_sparse_c_.Clear();
52  collection_.Clear();
53  subvector_.Clear();
54  length_sum_.Clear();
55  length_.Clear();
56  Nvector_ = 0;
57  this->m_ = 0;
58  label_map_.clear();
59  label_vector_.clear();
60  }
61 
62 
64 
67  template <class T, template <class U> class Allocator >
69  ::Clear()
70  {
71  float_dense_c_.Clear();
72  float_sparse_c_.Clear();
73  double_dense_c_.Clear();
74  double_sparse_c_.Clear();
75  collection_.Clear();
76  subvector_.Clear();
77  length_sum_.Clear();
78  length_.Clear();
79  Nvector_ = 0;
80  this->m_ = 0;
81  label_map_.clear();
82  label_vector_.clear();
83  }
84 
85 
87 
90  template <class T, template <class U> class Allocator >
92  ::Deallocate()
93  {
94  float_dense_c_.Deallocate();
95  float_sparse_c_.Deallocate();
96  double_dense_c_.Deallocate();
97  double_sparse_c_.Deallocate();
98  collection_.Clear();
99  subvector_.Clear();
100  length_sum_.Clear();
101  length_.Clear();
102  Nvector_ = 0;
103  this->m_ = 0;
104  label_map_.clear();
105  label_vector_.clear();
106  }
107 
108 
109  /**********************
110  * VECTORS MANAGEMENT *
111  **********************/
112 
113 
115 
118  template <class T, template <class U> class Allocator >
120  ::AddVector(const Vector<float, VectFull, Allocator<float> >& vector)
121  {
122  Nvector_++;
123  int m = vector.GetM();
124  this->m_+= m;
125  length_.PushBack(m);
126  length_sum_.PushBack(this->m_);
127  collection_.PushBack(0);
128  subvector_.PushBack(float_dense_c_.GetNvector());
129  float_dense_c_.AddVector(vector);
130  }
131 
132 
134 
137  template <class T, template <class U> class Allocator >
139  ::AddVector(const Vector<float, VectSparse, Allocator<float> >& vector)
140  {
141  Nvector_++;
142  int m = vector.GetM();
143  this->m_+= m;
144  length_.PushBack(m);
145  length_sum_.PushBack(this->m_);
146  collection_.PushBack(1);
147  subvector_.PushBack(float_sparse_c_.GetNvector());
148  float_sparse_c_.AddVector(vector);
149  }
150 
151 
153 
156  template <class T, template <class U> class Allocator >
158  ::AddVector(const Vector<double, VectFull, Allocator<double> >& vector)
159  {
160  Nvector_++;
161  int m = vector.GetM();
162  this->m_+= m;
163  length_.PushBack(m);
164  length_sum_.PushBack(this->m_);
165  collection_.PushBack(2);
166  subvector_.PushBack(double_dense_c_.GetNvector());
167  double_dense_c_.AddVector(vector);
168  }
169 
170 
172 
175  template <class T, template <class U> class Allocator >
177  ::AddVector(const Vector<double, VectSparse, Allocator<double> >& vector)
178  {
179  Nvector_++;
180  int m = vector.GetM();
181  this->m_+= m;
182  length_.PushBack(m);
183  length_sum_.PushBack(this->m_);
184  collection_.PushBack(3);
185  subvector_.PushBack(double_sparse_c_.GetNvector());
186  double_sparse_c_.AddVector(vector);
187  }
188 
189 
191 
195  template <class T, template <class U> class Allocator >
196  template <class T0, class Storage0, class Allocator0>
198  ::AddVector(const Vector<T0, Storage0, Allocator0>& vector, string name)
199  {
200  AddVector(vector);
201  SetName(Nvector_ - 1, name);
202  }
203 
204 
206 
210  template <class T, template <class U> class Allocator >
212  ::SetVector(int i, const Vector<float, VectFull, Allocator<float> >& vector)
213  {
214 
215 #ifdef SELDON_CHECK_BOUNDS
216  if (i < 0 || i >= Nvector_)
217  throw WrongIndex("Vector<FloatDouble, DenseSparseCollection>"
218  "::SetVector(int i, Vector<float, VectFull>)",
219  string("Index should be in [0, ")
220  + to_str(Nvector_ - 1)
221  + "], but is equal to " + to_str(i) + ".");
222 #endif
223 
224  if (collection_(i) != 0)
225  WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
226  "::SetVector(int i, Vector<float, VectFull>)",
227  string("The ") + to_str(i) + "-th inner vector "
228  "should be of type " + GetType(i) + ".");
229 
230  int size_difference;
231  size_difference = vector.GetM() - length_(i);
232  this->m_ += size_difference;
233  length_(i) = vector.GetM();
234  for (int k = i; k < Nvector_; k++)
235  length_sum_(k) += size_difference;
236  float_dense_c_.SetVector(subvector_(i), vector);
237  }
238 
239 
241 
245  template <class T, template <class U> class Allocator >
247  ::SetVector(int i,
248  const Vector<float, VectSparse, Allocator<float> >& vector)
249  {
250 
251 #ifdef SELDON_CHECK_BOUNDS
252  if (i < 0 || i >= Nvector_)
253  throw WrongIndex("Vector<FloatDouble, DenseSparseCollection>"
254  "::SetVector(int i, Vector<float, VectSparse>)",
255  string("Index should be in [0, ")
256  + to_str(Nvector_ - 1)
257  + "], but is equal to " + to_str(i) + ".");
258 #endif
259 
260  if (collection_(i) != 1)
261  WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
262  "::SetVector(int i, Vector<float, VectSparse>)",
263  string("The ") + to_str(i) + "-th inner vector "
264  "should be of type " + GetType(i) + ".");
265 
266  int size_difference;
267  size_difference = vector.GetM() - length_(i);
268  this->m_ += size_difference;
269  length_(i) = vector.GetM();
270  for (int k = i; k < Nvector_; k++)
271  length_sum_(k) += size_difference;
272  float_sparse_c_.SetVector(subvector_(i), vector);
273  }
274 
275 
277 
281  template <class T, template <class U> class Allocator >
283  ::SetVector(int i,
284  const Vector<double, VectFull, Allocator<double> >& vector)
285  {
286 
287 #ifdef SELDON_CHECK_BOUNDS
288  if (i < 0 || i >= Nvector_)
289  throw WrongIndex("Vector<FloatDouble, DenseSparseCollection>"
290  "::SetVector(int i, Vector<double, VectFull>)",
291  string("Index should be in [0, ")
292  + to_str(Nvector_ - 1)
293  + "], but is equal to " + to_str(i) + ".");
294 #endif
295 
296  if (collection_(i) != 2)
297  WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
298  "::SetVector(int i, Vector<double, VectFull>)",
299  string("The ") + to_str(i) + "-th inner vector "
300  "should be of type " + GetType(i) + ".");
301 
302  int size_difference;
303  size_difference = vector.GetM() - length_(i);
304  this->m_ += size_difference;
305  length_(i) = vector.GetM();
306  for (int k = i; k < Nvector_; k++)
307  length_sum_(k) += size_difference;
308  double_dense_c_.SetVector(subvector_(i), vector);
309  }
310 
311 
313 
317  template <class T, template <class U> class Allocator >
319  ::SetVector(int i,
320  const Vector<double, VectSparse, Allocator<double> >& vector)
321  {
322 
323 #ifdef SELDON_CHECK_BOUNDS
324  if (i < 0 || i >= Nvector_)
325  throw WrongIndex("Vector<FloatDouble, DenseSparseCollection>"
326  "::SetVector(int i, Vector<double, VectSparse>)",
327  string("Index should be in [0, ")
328  + to_str(Nvector_ - 1)
329  + "], but is equal to " + to_str(i) + ".");
330 #endif
331 
332  if (collection_(i) != 3)
333  WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
334  "::SetVector(int i, Vector<double, VectSparse>)",
335  string("The ") + to_str(i) + "-th inner vector "
336  "should be of type " + GetType(i) + ".");
337 
338  int size_difference;
339  size_difference = vector.GetM() - length_(i);
340  this->m_ += size_difference;
341  length_(i) = vector.GetM();
342  for (int k = i; k < Nvector_; k++)
343  length_sum_(k) += size_difference;
344  double_sparse_c_.SetVector(subvector_(i), vector);
345  }
346 
347 
349 
354  template <class T, template <class U> class Allocator >
355  template <class T0, class Storage0, class Allocator0>
357  ::SetVector(int i, const Vector<T0, Storage0, Allocator0>& vector,
358  string name)
359  {
360  SetVector(i, vector);
361  SetName(i, name);
362  }
363 
364 
366 
370  template <class T, template <class U> class Allocator >
371  template <class T0, class Storage0, class Allocator0>
373  ::SetVector(string name, const Vector<T0, Storage0, Allocator0>& vector)
374  {
375  map<string,int>::iterator label_iterator;
376  label_iterator = label_map_.find(name);
377  if (label_iterator == label_map_.end())
378  throw WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
379  "::SetVector(string name, Vector)",
380  string("Unknown vector name: \"") + name + "\".");
381  SetVector(label_iterator->second, vector);
382  }
383 
384 
386 
390  template <class T, template <class U> class Allocator >
392  ::SetName(int i, string name)
393  {
394 #ifdef SELDON_CHECK_BOUNDS
395  if (i < 0 || i >= Nvector_)
396  throw WrongIndex("Vector<FloatDouble, DenseSparseCollection>"
397  "::SetVector(int i, string name)",
398  string("Index should be in [0, ")
399  + to_str(Nvector_ - 1)
400  + "], but is equal to " + to_str(i) + ".");
401 #endif
402 
403  if (i >= int(label_vector_.size()))
404  label_vector_.resize(Nvector_, "");
405 
406  if (label_vector_[i] != "")
407  label_map_.erase(label_vector_[i]);
408 
409  label_vector_[i] = name;
410  label_map_[name] = i;
411  }
412 
413 
415  template <class T, template <class U> class Allocator >
417  {
418  Clear();
419  }
420 
421 
423 
427  template <class T, template <class U> class Allocator >
429  ::GetVector(int i, typename
430  Vector<FloatDouble, DenseSparseCollection, Allocator<T> >
431  ::float_dense_v& vector) const
432  {
433 #ifdef SELDON_CHECK_BOUNDS
434  if (i < 0 || i >= Nvector_)
435  throw WrongIndex("Vector<FloatDouble, DenseSparseCollection>"
436  "::GetVector(int i, Vector<float, VectFull>&)",
437  string("Index should be in [0, ")
438  + to_str(Nvector_ - 1)
439  + "], but is equal to " + to_str(i) + ".");
440 #endif
441 
442  if (collection_(i) != 0)
443  WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
444  "::GetVector(int i, Vector<float, VectFull>&)",
445  string("The ") + to_str(i) + "-th inner vector "
446  "is of type " + GetType(i) + ".");
447 
448  vector.SetData(float_dense_c_.GetVector(subvector_(i)));
449  }
450 
451 
453 
457  template <class T, template <class U> class Allocator >
459  ::GetVector(int i, typename
460  Vector<FloatDouble, DenseSparseCollection, Allocator<T> >
461  ::float_sparse_v& vector) const
462  {
463 #ifdef SELDON_CHECK_BOUNDS
464  if (i < 0 || i >= Nvector_)
465  throw WrongIndex("Vector<FloatDouble, DenseSparseCollection>"
466  "::GetVector(int i, Vector<float, VectSparse>&)",
467  string("Index should be in [0, ")
468  + to_str(Nvector_ - 1)
469  + "], but is equal to " + to_str(i) + ".");
470 #endif
471 
472  if (collection_(i) != 1)
473  WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
474  "::GetVector(int i, Vector<float, VectSparse>&)",
475  string("The ") + to_str(i) + "-th inner vector "
476  "is of type " + GetType(i) + ".");
477 
478  vector.SetData(float_sparse_c_.GetVector(subvector_(i)));
479  }
480 
481 
483 
487  template <class T, template <class U> class Allocator >
489  ::GetVector(int i, typename
490  Vector<FloatDouble, DenseSparseCollection, Allocator<T> >
491  ::double_dense_v& vector) const
492  {
493 #ifdef SELDON_CHECK_BOUNDS
494  if (i < 0 || i >= Nvector_)
495  throw WrongIndex("Vector<FloatDouble, DenseSparseCollection>"
496  "::GetVector(int i, Vector<double, VectDense>&)",
497  string("Index should be in [0, ")
498  + to_str(Nvector_ - 1)
499  + "], but is equal to " + to_str(i) + ".");
500 #endif
501 
502  if (collection_(i) != 2)
503  WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
504  "::GetVector(int i, Vector<double, VectDense>&)",
505  string("The ") + to_str(i) + "-th inner vector "
506  "is of type " + GetType(i) + ".");
507 
508  vector.SetData(double_dense_c_.GetVector(subvector_(i)));
509  }
510 
511 
513 
517  template <class T, template <class U> class Allocator >
519  ::GetVector(int i, typename
520  Vector<FloatDouble, DenseSparseCollection, Allocator<T> >
521  ::double_sparse_v& vector) const
522  {
523 #ifdef SELDON_CHECK_BOUNDS
524  if (i < 0 || i >= Nvector_)
525  throw WrongIndex("Vector<FloatDouble, DenseSparseCollection>"
526  "::GetVector(int i, Vector<double, VectSparse>&)",
527  string("Index should be in [0, ")
528  + to_str(Nvector_ - 1)
529  + "], but is equal to " + to_str(i) + ".");
530 #endif
531 
532  if (collection_(i) != 3)
533  WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
534  "::GetVector(int i, Vector<double, VectSparse>&)",
535  string("The ") + to_str(i) + "-th inner vector "
536  "is of type " + GetType(i) + ".");
537 
538  vector.SetData(double_sparse_c_.GetVector(subvector_(i)));
539  }
540 
541 
543 
547  template <class T, template <class U> class Allocator >
548  template <class T0, class Storage0, class Allocator0>
550  ::GetVector(string name,Vector<T0, Storage0, Allocator0>& vector) const
551  {
552  map<string,int>::const_iterator label_iterator;
553  label_iterator = label_map_.find(name);
554  if (label_iterator == label_map_.end())
555  throw WrongArgument("Vector<FloatDouble, DenseSparseCollection>"
556  "::SetVector(string name)",
557  string("Unknown vector name ") + name + ".");
558  GetVector(label_iterator->second, vector);
559  }
560 
561 
562  /*********************************
563  * ELEMENT ACCESS AND ASSIGNMENT *
564  *********************************/
565 
566 
568 
572  template <class T, template <class U> class Allocator >
573  double
575  ::operator() (int i) const
576  {
577 #ifdef SELDON_CHECK_BOUNDS
578  if (i < 0 || i >= this->m_)
579  throw WrongIndex("Vector<FloatDouble, DenseSparse>::operator()",
580  string("Index should be in [0, ")
581  + to_str(this->m_ - 1)
582  + "], but is equal to " + to_str(i) + ".");
583 #endif
584 
585  int j = 0;
586  while (i >= length_sum_(j))
587  j++;
588 
589  switch (collection_(j))
590  {
591  case 0:
592  return (j == 0) ? double(float_dense_c_.GetVector(subvector_(j))(i)) :
593  double(float_dense_c_.
594  GetVector(subvector_(j))(i - length_sum_(j - 1)));
595  case 1:
596  return (j == 0) ? double(float_sparse_c_.GetVector(subvector_(j))(i)):
597  double(float_sparse_c_.
598  GetVector(subvector_(j))(i - length_sum_(j - 1)));
599  case 2:
600  return j == 0 ? double_dense_c_.GetVector(subvector_(j))(i) :
601  double_dense_c_.GetVector(subvector_(j))(i - length_sum_(j - 1));
602  case 3:
603  return j == 0 ? double_sparse_c_.GetVector(subvector_(j))(i) :
604  double_sparse_c_.GetVector(subvector_(j))(i - length_sum_(j - 1));
605  default:
606  return 0.;
607  }
608  }
609 
610 
612 
617  template <class T, template <class U> class Allocator >
619  ::Copy(const Vector<FloatDouble, DenseSparseCollection, Allocator<T> >& X)
620  {
621  Clear();
622  this->m_ = X.GetM();
623  collection_.Copy(X.collection_);
624  subvector_.Copy(X.subvector_);
625  length_.Copy(X.length_);
626  length_sum_.Copy(X.length_sum_);
627 
628  float_dense_c_.Copy(X.float_dense_c_);
629  float_sparse_c_.Copy(X.float_sparse_c_);
630  double_dense_c_.Copy(X.double_dense_c_);
631  double_sparse_c_.Copy(X.double_sparse_c_);
632 
633  label_map_.insert(X.label_map_.begin(), X.label_map_.end());
634  label_vector_.assign(X.label_vector_.begin(), X.label_vector_.end());
635  }
636 
637 
639 
642  template <class T, template <class U> class Allocator >
643  template<class T0>
646  ::operator*= (const T0& alpha)
647  {
648  float_dense_c_ *= alpha;
649  float_sparse_c_ *= alpha;
650  double_dense_c_ *= alpha;
651  double_sparse_c_ *= alpha;
652  return *this;
653  }
654 
655 
657  // CONVENIENT METHODS //
659 
660 
662  template <class T, template <class U> class Allocator >
664  ::Print() const
665  {
666  for (int i = 0; i < Nvector_; i++)
667  {
668  if (i < int(label_vector_.size()) && label_vector_[i] != "")
669  cout << label_vector_[i] << ":" << endl;
670  else
671  cout << "(noname):" << endl;
672 
673  switch(collection_(i))
674  {
675  case 0:
676  float_dense_c_.GetVector(subvector_(i)).Print();
677  break;
678  case 1:
679  float_sparse_c_.GetVector(subvector_(i)).Print();
680  break;
681  case 2:
682  double_dense_c_.GetVector(subvector_(i)).Print();
683  break;
684  case 3:
685  double_sparse_c_.GetVector(subvector_(i)).Print();
686  break;
687  }
688  }
689  }
690 
691 
693 
699  template <class T, template <class U> class Allocator >
701  ::Write(string FileName, bool with_size = true) const
702  {
703  ofstream FileStream;
704  FileStream.open(FileName.c_str(), ofstream::binary);
705 
706 #ifdef SELDON_CHECK_IO
707  // Checks if the file was opened.
708  if (!FileStream.is_open())
709  throw IOError("Vector<FloatDouble, DenseSparseCollection>"
710  "::Write(string FileName)",
711  string("Unable to open file \"") + FileName + "\".");
712 #endif
713 
714  this->Write(FileStream, with_size);
715 
716  FileStream.close();
717  }
718 
719 
721 
727  template <class T, template <class U> class Allocator >
729  ::Write(ostream& FileStream, bool with_size = true) const
730  {
731 
732 #ifdef SELDON_CHECK_IO
733  // Checks if the stream is ready.
734  if (!FileStream.good())
735  throw IOError("Vector<FloatDouble, DenseSparseCollection>"
736  "::Write(ostream& FileStream)",
737  "The stream is not ready.");
738 #endif
739  if (with_size)
740  FileStream
741  .write(reinterpret_cast<char*>(const_cast<int*>(&this->Nvector_)),
742  sizeof(int));
743 
744  collection_.Write(FileStream, with_size);
745 
746  for (int i = 0; i < Nvector_; i++)
747  {
748  switch (collection_(i))
749  {
750  case 0:
751  float_dense_c_.GetVector(subvector_(i)).Write(FileStream);
752  break;
753  case 1:
754  float_sparse_c_.GetVector(subvector_(i)).Write(FileStream);
755  break;
756  case 2:
757  double_dense_c_.GetVector(subvector_(i)).Write(FileStream);
758  break;
759  case 3:
760  double_sparse_c_.GetVector(subvector_(i)).Write(FileStream);
761  break;
762  }
763  }
764 
765 #ifdef SELDON_CHECK_IO
766  // Checks if data was written.
767  if (!FileStream.good())
768  throw IOError("Vector<FloatDouble, DenseSparseCollection>"
769  "::Write(ostream& FileStream)",
770  "Output operation failed.");
771 #endif
772  }
773 
774 
776 
781  template <class T, template <class U> class Allocator >
783  ::WriteText(string FileName) const
784  {
785  ofstream FileStream;
786  FileStream.precision(cout.precision());
787  FileStream.flags(cout.flags());
788  FileStream.open(FileName.c_str());
789 
790 #ifdef SELDON_CHECK_IO
791  // Checks if the file was opened.
792  if (!FileStream.is_open())
793  throw IOError("Vector<FloatDouble, DenseSparseCollection>"
794  "::WriteText(string FileName)",
795  string("Unable to open file \"") + FileName + "\".");
796 #endif
797 
798  this->WriteText(FileStream);
799 
800  FileStream.close();
801  }
802 
803 
805 
810  template <class T, template <class U> class Allocator >
812  ::WriteText(ostream& FileStream) const
813  {
814 
815 #ifdef SELDON_CHECK_IO
816  // Checks if the stream is ready.
817  if (!FileStream.good())
818  throw IOError("Vector<FloatDouble, DenseSparseCollection>"
819  "::Write(ostream& FileStream)",
820  "The stream is not ready.");
821 #endif
822 
823  for (int i = 0; i < Nvector_; i++)
824  {
825  switch (collection_(i))
826  {
827  case 0:
828  float_dense_c_.GetVector(subvector_(i)).WriteText(FileStream);
829  break;
830  case 1:
831  float_sparse_c_.GetVector(subvector_(i)).WriteText(FileStream);
832  break;
833  case 2:
834  double_dense_c_.GetVector(subvector_(i)).WriteText(FileStream);
835  break;
836  case 3:
837  double_sparse_c_.GetVector(subvector_(i)).WriteText(FileStream);
838  break;
839  }
840  }
841 
842 #ifdef SELDON_CHECK_IO
843  // Checks if data was written.
844  if (!FileStream.good())
845  throw IOError("Vector<FloatDouble, DenseSparseCollection>"
846  "::Write(ostream& FileStream)",
847  "Output operation failed.");
848 #endif
849  }
850 
851 
853 
858  template <class T, template <class U> class Allocator >
860  ::Read(string FileName)
861  {
862  ifstream FileStream;
863  FileStream.open(FileName.c_str(), ifstream::binary);
864 
865 #ifdef SELDON_CHECK_IO
866  // Checks if the file was opened.
867  if (!FileStream.is_open())
868  throw IOError("Vector<FloatDouble, DenseSparseCollection>"
869  "::Read(string FileName)",
870  string("Unable to open file \"") + FileName + "\".");
871 #endif
872 
873  this->Read(FileStream);
874 
875  FileStream.close();
876  }
877 
878 
880 
885  template <class T, template <class U> class Allocator >
887  ::Read(istream& FileStream)
888  {
889 
890 #ifdef SELDON_CHECK_IO
891  // Checks if the stream is ready.
892  if (!FileStream.good())
893  throw IOError("Vector<FloatDouble, DenseSparseCollection>"
894  "::Read(istream& FileStream)",
895  "The stream is not ready.");
896 #endif
897 
898  int* Nvector = new int;
899  FileStream.read(reinterpret_cast<char*>(Nvector), sizeof(int));
900 
901  Vector<int, VectFull, MallocAlloc<int> > collection(*Nvector);
902  collection.Read(FileStream);
903 
904  Clear();
905 
906  float_dense_v v0;
907  float_sparse_v v1;
908  double_dense_v v2;
909  double_sparse_v v3;
910 
911  for (int i = 0; i < *Nvector; i++)
912  {
913  switch (collection(i))
914  {
915  case 0:
916  v0.Read(FileStream);
917  AddVector(v0);
918  v0.Nullify();
919  break;
920  case 1:
921  v1.Read(FileStream);
922  AddVector(v1);
923  v1.Nullify();
924  break;
925  case 2:
926  v2.Read(FileStream);
927  AddVector(v2);
928  v2.Nullify();
929  break;
930  case 3:
931  v3.Read(FileStream);
932  AddVector(v3);
933  v3.Nullify();
934  break;
935  }
936  }
937 
938  delete Nvector;
939 
940 #ifdef SELDON_CHECK_IO
941  // Checks if data was read.
942  if (!FileStream.good())
943  throw IOError("Vector<FloatDouble, DenseSparseCollection>"
944  "::Read(istream& FileStream)",
945  "Input operation failed.");
946 #endif
947 
948  }
949 
950 
952 
957  template <class T, template <class U> class Allocator >
958  ostream& operator <<
959  (ostream& out,
961  {
966  for (int i = 0; i < V.GetNvector(); i++)
967  {
968  switch (V.GetCollectionIndex()(i))
969  {
970  case 0:
971  V.GetVector(i, v0);
972  out << v0 << '\t';
973  v0.Nullify();
974  break;
975  case 1:
976  V.GetVector(i, v1);
977  out << v1 << '\t';
978  v1.Nullify();
979  break;
980  case 2:
981  V.GetVector(i, v2);
982  out << v2 << '\t';
983  v2.Nullify();
984  break;
985  case 3:
986  V.GetVector(i, v3);
987  out << v3 << '\t';
988  v3.Nullify();
989  break;
990  }
991  }
992  return out;
993  }
994 
995 
997  // PROTECTED METHOD //
999 
1000 
1002 
1007  template <class T, template <class U> class Allocator >
1008  string Vector<FloatDouble, DenseSparseCollection, Allocator<T> >
1009  ::GetType(int i) const
1010  {
1011  if (collection_(i) == 0)
1012  return "Vector<float, VectDense>";
1013  else if (collection_(i) == 1)
1014  return "Vector<float, VectSparse>";
1015  else if (collection_(i) == 2)
1016  return "Vector<double, VectDense>";
1017  else
1018  return "Vector<double, VectSparse>";
1019  }
1020 
1021 
1022 } // namespace Seldon.
1023 
1024 
1025 #define SELDON_FILE_VECTOR_HETEROGENEOUSCOLLECTION_CXX
1026 #endif
Seldon::FloatDouble
Definition: Storage.hxx:365
Seldon::to_str
std::string to_str(const T &input)
Converts most types to string.
Definition: CommonInline.cxx:137
Seldon::DenseSparseCollection
Definition: StorageInline.cxx:89
Seldon::Vector
Definition: SeldonHeader.hxx:207
Seldon::WrongIndex
Definition: Errors.hxx:114
Seldon::VectFull
Definition: StorageInline.cxx:74
Seldon::WrongArgument
Definition: Errors.hxx:76
Seldon::VectSparse
Definition: StorageInline.cxx:79
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::IOError
Definition: Errors.hxx:150
Seldon::AddVector
void AddVector(const T0 &alpha, const Vector< T1, Storage1, Allocator1 > &X, Vector< T2, Storage2, Allocator2 > &Y)
Adds two vectors Y = Y + alpha X.
Definition: Functions_Vector.cxx:94