21 #ifndef SELDON_FILE_VECTOR_HETEROGENEOUSCOLLECTION_CXX
24 #include "HeterogeneousCollection.hxx"
45 template <
class T,
template <
class U>
class Allocator >
48 float_dense_c_.Clear();
49 float_sparse_c_.Clear();
50 double_dense_c_.Clear();
51 double_sparse_c_.Clear();
59 label_vector_.clear();
67 template <
class T,
template <
class U>
class Allocator >
71 float_dense_c_.Clear();
72 float_sparse_c_.Clear();
73 double_dense_c_.Clear();
74 double_sparse_c_.Clear();
82 label_vector_.clear();
90 template <
class T,
template <
class U>
class Allocator >
94 float_dense_c_.Deallocate();
95 float_sparse_c_.Deallocate();
96 double_dense_c_.Deallocate();
97 double_sparse_c_.Deallocate();
105 label_vector_.clear();
118 template <
class T,
template <
class U>
class Allocator >
123 int m = vector.GetM();
126 length_sum_.PushBack(this->m_);
127 collection_.PushBack(0);
128 subvector_.PushBack(float_dense_c_.GetNvector());
129 float_dense_c_.AddVector(vector);
137 template <
class T,
template <
class U>
class Allocator >
142 int m = vector.GetM();
145 length_sum_.PushBack(this->m_);
146 collection_.PushBack(1);
147 subvector_.PushBack(float_sparse_c_.GetNvector());
148 float_sparse_c_.AddVector(vector);
156 template <
class T,
template <
class U>
class Allocator >
161 int m = vector.GetM();
164 length_sum_.PushBack(this->m_);
165 collection_.PushBack(2);
166 subvector_.PushBack(double_dense_c_.GetNvector());
167 double_dense_c_.AddVector(vector);
175 template <
class T,
template <
class U>
class Allocator >
180 int m = vector.GetM();
183 length_sum_.PushBack(this->m_);
184 collection_.PushBack(3);
185 subvector_.PushBack(double_sparse_c_.GetNvector());
186 double_sparse_c_.AddVector(vector);
195 template <
class T,
template <
class U>
class Allocator >
196 template <
class T0,
class Storage0,
class Allocator0>
201 SetName(Nvector_ - 1, name);
210 template <
class T,
template <
class U>
class Allocator >
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, ")
221 +
"], but is equal to " +
to_str(i) +
".");
224 if (collection_(i) != 0)
226 "::SetVector(int i, Vector<float, VectFull>)",
227 string(
"The ") +
to_str(i) +
"-th inner vector "
228 "should be of type " + GetType(i) +
".");
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);
245 template <
class T,
template <
class U>
class Allocator >
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, ")
257 +
"], but is equal to " +
to_str(i) +
".");
260 if (collection_(i) != 1)
262 "::SetVector(int i, Vector<float, VectSparse>)",
263 string(
"The ") +
to_str(i) +
"-th inner vector "
264 "should be of type " + GetType(i) +
".");
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);
281 template <
class T,
template <
class U>
class Allocator >
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, ")
293 +
"], but is equal to " +
to_str(i) +
".");
296 if (collection_(i) != 2)
298 "::SetVector(int i, Vector<double, VectFull>)",
299 string(
"The ") +
to_str(i) +
"-th inner vector "
300 "should be of type " + GetType(i) +
".");
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);
317 template <
class T,
template <
class U>
class Allocator >
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, ")
329 +
"], but is equal to " +
to_str(i) +
".");
332 if (collection_(i) != 3)
334 "::SetVector(int i, Vector<double, VectSparse>)",
335 string(
"The ") +
to_str(i) +
"-th inner vector "
336 "should be of type " + GetType(i) +
".");
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);
354 template <
class T,
template <
class U>
class Allocator >
355 template <
class T0,
class Storage0,
class Allocator0>
360 SetVector(i, vector);
370 template <
class T,
template <
class U>
class Allocator >
371 template <
class T0,
class Storage0,
class Allocator0>
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);
390 template <
class T,
template <
class U>
class Allocator >
392 ::SetName(
int i,
string name)
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, ")
400 +
"], but is equal to " +
to_str(i) +
".");
403 if (i >=
int(label_vector_.size()))
404 label_vector_.resize(Nvector_,
"");
406 if (label_vector_[i] !=
"")
407 label_map_.erase(label_vector_[i]);
409 label_vector_[i] = name;
410 label_map_[name] = i;
415 template <
class T,
template <
class U>
class Allocator >
427 template <
class T,
template <
class U>
class Allocator >
429 ::GetVector(
int i,
typename
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, ")
439 +
"], but is equal to " +
to_str(i) +
".");
442 if (collection_(i) != 0)
444 "::GetVector(int i, Vector<float, VectFull>&)",
445 string(
"The ") +
to_str(i) +
"-th inner vector "
446 "is of type " + GetType(i) +
".");
448 vector.SetData(float_dense_c_.GetVector(subvector_(i)));
457 template <
class T,
template <
class U>
class Allocator >
459 ::GetVector(
int i,
typename
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, ")
469 +
"], but is equal to " +
to_str(i) +
".");
472 if (collection_(i) != 1)
474 "::GetVector(int i, Vector<float, VectSparse>&)",
475 string(
"The ") +
to_str(i) +
"-th inner vector "
476 "is of type " + GetType(i) +
".");
478 vector.SetData(float_sparse_c_.GetVector(subvector_(i)));
487 template <
class T,
template <
class U>
class Allocator >
489 ::GetVector(
int i,
typename
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, ")
499 +
"], but is equal to " +
to_str(i) +
".");
502 if (collection_(i) != 2)
504 "::GetVector(int i, Vector<double, VectDense>&)",
505 string(
"The ") +
to_str(i) +
"-th inner vector "
506 "is of type " + GetType(i) +
".");
508 vector.SetData(double_dense_c_.GetVector(subvector_(i)));
517 template <
class T,
template <
class U>
class Allocator >
519 ::GetVector(
int i,
typename
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, ")
529 +
"], but is equal to " +
to_str(i) +
".");
532 if (collection_(i) != 3)
534 "::GetVector(int i, Vector<double, VectSparse>&)",
535 string(
"The ") +
to_str(i) +
"-th inner vector "
536 "is of type " + GetType(i) +
".");
538 vector.SetData(double_sparse_c_.GetVector(subvector_(i)));
547 template <
class T,
template <
class U>
class Allocator >
548 template <
class T0,
class Storage0,
class Allocator0>
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);
572 template <
class T,
template <
class U>
class Allocator >
575 ::operator() (
int i)
const
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, ")
582 +
"], but is equal to " +
to_str(i) +
".");
586 while (i >= length_sum_(j))
589 switch (collection_(j))
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)));
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)));
600 return j == 0 ? double_dense_c_.GetVector(subvector_(j))(i) :
601 double_dense_c_.GetVector(subvector_(j))(i - length_sum_(j - 1));
603 return j == 0 ? double_sparse_c_.GetVector(subvector_(j))(i) :
604 double_sparse_c_.GetVector(subvector_(j))(i - length_sum_(j - 1));
617 template <
class T,
template <
class U>
class Allocator >
623 collection_.Copy(X.collection_);
624 subvector_.Copy(X.subvector_);
625 length_.Copy(X.length_);
626 length_sum_.Copy(X.length_sum_);
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_);
633 label_map_.insert(X.label_map_.begin(), X.label_map_.end());
634 label_vector_.assign(X.label_vector_.begin(), X.label_vector_.end());
642 template <
class T,
template <
class U>
class Allocator >
646 ::operator*= (
const T0& alpha)
648 float_dense_c_ *= alpha;
649 float_sparse_c_ *= alpha;
650 double_dense_c_ *= alpha;
651 double_sparse_c_ *= alpha;
662 template <
class T,
template <
class U>
class Allocator >
666 for (
int i = 0; i < Nvector_; i++)
668 if (i <
int(label_vector_.size()) && label_vector_[i] !=
"")
669 cout << label_vector_[i] <<
":" << endl;
671 cout <<
"(noname):" << endl;
673 switch(collection_(i))
676 float_dense_c_.GetVector(subvector_(i)).Print();
679 float_sparse_c_.GetVector(subvector_(i)).Print();
682 double_dense_c_.GetVector(subvector_(i)).Print();
685 double_sparse_c_.GetVector(subvector_(i)).Print();
699 template <
class T,
template <
class U>
class Allocator >
701 ::Write(
string FileName,
bool with_size =
true)
const
704 FileStream.open(FileName.c_str(), ofstream::binary);
706 #ifdef SELDON_CHECK_IO
708 if (!FileStream.is_open())
709 throw IOError(
"Vector<FloatDouble, DenseSparseCollection>"
710 "::Write(string FileName)",
711 string(
"Unable to open file \"") + FileName +
"\".");
714 this->Write(FileStream, with_size);
727 template <
class T,
template <
class U>
class Allocator >
729 ::Write(ostream& FileStream,
bool with_size =
true)
const
732 #ifdef SELDON_CHECK_IO
734 if (!FileStream.good())
735 throw IOError(
"Vector<FloatDouble, DenseSparseCollection>"
736 "::Write(ostream& FileStream)",
737 "The stream is not ready.");
741 .write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->Nvector_)),
744 collection_.Write(FileStream, with_size);
746 for (
int i = 0; i < Nvector_; i++)
748 switch (collection_(i))
751 float_dense_c_.GetVector(subvector_(i)).Write(FileStream);
754 float_sparse_c_.GetVector(subvector_(i)).Write(FileStream);
757 double_dense_c_.GetVector(subvector_(i)).Write(FileStream);
760 double_sparse_c_.GetVector(subvector_(i)).Write(FileStream);
765 #ifdef SELDON_CHECK_IO
767 if (!FileStream.good())
768 throw IOError(
"Vector<FloatDouble, DenseSparseCollection>"
769 "::Write(ostream& FileStream)",
770 "Output operation failed.");
781 template <
class T,
template <
class U>
class Allocator >
783 ::WriteText(
string FileName)
const
786 FileStream.precision(cout.precision());
787 FileStream.flags(cout.flags());
788 FileStream.open(FileName.c_str());
790 #ifdef SELDON_CHECK_IO
792 if (!FileStream.is_open())
793 throw IOError(
"Vector<FloatDouble, DenseSparseCollection>"
794 "::WriteText(string FileName)",
795 string(
"Unable to open file \"") + FileName +
"\".");
798 this->WriteText(FileStream);
810 template <
class T,
template <
class U>
class Allocator >
812 ::WriteText(ostream& FileStream)
const
815 #ifdef SELDON_CHECK_IO
817 if (!FileStream.good())
818 throw IOError(
"Vector<FloatDouble, DenseSparseCollection>"
819 "::Write(ostream& FileStream)",
820 "The stream is not ready.");
823 for (
int i = 0; i < Nvector_; i++)
825 switch (collection_(i))
828 float_dense_c_.GetVector(subvector_(i)).WriteText(FileStream);
831 float_sparse_c_.GetVector(subvector_(i)).WriteText(FileStream);
834 double_dense_c_.GetVector(subvector_(i)).WriteText(FileStream);
837 double_sparse_c_.GetVector(subvector_(i)).WriteText(FileStream);
842 #ifdef SELDON_CHECK_IO
844 if (!FileStream.good())
845 throw IOError(
"Vector<FloatDouble, DenseSparseCollection>"
846 "::Write(ostream& FileStream)",
847 "Output operation failed.");
858 template <
class T,
template <
class U>
class Allocator >
860 ::Read(
string FileName)
863 FileStream.open(FileName.c_str(), ifstream::binary);
865 #ifdef SELDON_CHECK_IO
867 if (!FileStream.is_open())
868 throw IOError(
"Vector<FloatDouble, DenseSparseCollection>"
869 "::Read(string FileName)",
870 string(
"Unable to open file \"") + FileName +
"\".");
873 this->Read(FileStream);
885 template <
class T,
template <
class U>
class Allocator >
887 ::Read(istream& FileStream)
890 #ifdef SELDON_CHECK_IO
892 if (!FileStream.good())
893 throw IOError(
"Vector<FloatDouble, DenseSparseCollection>"
894 "::Read(istream& FileStream)",
895 "The stream is not ready.");
898 int* Nvector =
new int;
899 FileStream.read(
reinterpret_cast<char*
>(Nvector),
sizeof(
int));
902 collection.Read(FileStream);
911 for (
int i = 0; i < *Nvector; i++)
913 switch (collection(i))
940 #ifdef SELDON_CHECK_IO
942 if (!FileStream.good())
943 throw IOError(
"Vector<FloatDouble, DenseSparseCollection>"
944 "::Read(istream& FileStream)",
945 "Input operation failed.");
957 template <
class T,
template <
class U>
class Allocator >
966 for (
int i = 0; i < V.GetNvector(); i++)
968 switch (V.GetCollectionIndex()(i))
1007 template <
class T,
template <
class U>
class Allocator >
1008 string Vector<FloatDouble, DenseSparseCollection, Allocator<T> >
1009 ::GetType(
int i)
const
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>";
1018 return "Vector<double, VectSparse>";
1025 #define SELDON_FILE_VECTOR_HETEROGENEOUSCOLLECTION_CXX