21 #ifndef SELDON_FILE_VECTOR_VECTORCOLLECTION_CXX
24 #include "VectorCollection.hxx"
45 template <
class T,
class Allocator >
48 for (
int i = 0; i < Nvector_; i++)
51 label_vector_.clear();
59 template <
class T,
class Allocator >
62 for (
int i = 0; i < Nvector_; i++)
70 label_vector_.clear();
80 template <
class T,
class Allocator >
95 template <
class T,
class Allocator >
98 for (
int i = 0; i < Nvector_; i++)
106 label_vector_.clear();
120 template <
class T,
class Allocator >
124 map<string,int>::const_iterator label_iterator = label_map_.find(name);
125 if (label_iterator == label_map_.end())
126 throw WrongArgument(
"VectorCollection::GetVectorIndex(string name)",
127 "Unknown vector name: \"" + name +
"\".");
128 return label_iterator->second;
138 template <
class T,
class Allocator >
141 map<string,int>::const_iterator label_iterator = label_map_.find(name);
142 if (label_iterator == label_map_.end())
143 throw WrongArgument(
"VectorCollection::GetIndex(string name)",
144 string(
"Unknown vector name: \"") + name +
"\".");
145 return (label_iterator->second == 0) ?
146 0 : length_sum_(label_iterator->second - 1);
155 template <
class T,
class Allocator >
160 map<string,int>::const_iterator label_iterator;
161 label_iterator = label_map_.find(name);
162 if (label_iterator == label_map_.end())
163 throw WrongArgument(
"VectorCollection::SetVector(string name)",
164 string(
"Unknown vector name: \"") + name +
"\".");
165 return GetVector(label_iterator->second);
174 template <
class T,
class Allocator >
178 map<string,int>::iterator label_iterator;
179 label_iterator = label_map_.find(name);
180 if (label_iterator == label_map_.end())
181 throw WrongArgument(
"VectorCollection::SetVector(string name)",
182 string(
"Unknown vector name: \"") + name +
"\".");
183 return GetVector(label_iterator->second);
197 template <
class T,
class Allocator >
201 #ifdef SELDON_CHECK_BOUNDS
202 if (i < 0 || i >= this->m_)
203 throw WrongIndex(
"VectorCollection::operator()",
204 string(
"Index should be in [0, ")
206 +
"], but is equal to " +
to_str(i) +
".");
210 while (i >= length_sum_(j))
212 return (j == 0) ? vector_(j)(i) : vector_(j)(i - length_sum_(j - 1));
221 template <
class T,
class Allocator >
225 #ifdef SELDON_CHECK_BOUNDS
226 if (i < 0 || i >= this->m_)
227 throw WrongIndex(
"VectorCollection::operator()",
228 string(
"Index should be in [0, ")
230 +
"], but is equal to " +
to_str(i) +
".");
234 while (i >= length_sum_(j))
236 return (j == 0) ? vector_(j)(i) : vector_(j)(i - length_sum_(j - 1));
246 template <
class T,
class Allocator >
262 template <
class T,
class Allocator >
280 template <
class T,
class Allocator >
281 template <
class T0,
class Allocator0>
285 #ifdef SELDON_CHECK_BOUNDS
286 if (this->m_ != X.GetM())
288 string(
"The size of X should be equal to ")
290 +
", but is equal to " +
to_str(X.GetM()) +
".");
293 for (
int i = 0; i < X.GetM(); i++)
302 template <
class T,
class Allocator >
303 template <
class T0,
class Storage0,
class Allocator0>
306 Storage0, Allocator0>& vector)
310 length_sum_.PushBack(this->m_);
314 for (
int i = 0; i < Nvector_ - 1; i++)
316 new_vector(i).SetData(vector_(i));
317 vector_(i).Nullify();
320 vector_.SetData(new_vector);
321 new_vector.Nullify();
324 SetVector(Nvector_ - 1, vector);
333 template <
class T,
class Allocator >
334 template <
class T0,
class Storage0,
class Allocator0>
337 Storage0, Allocator0>& vector,
341 SetName(Nvector_ - 1, name);
350 template <
class T,
class Allocator >
351 template <
class T0,
class Storage0,
class Allocator0>
354 Storage0, Allocator0>& vector)
357 size_difference = vector.GetM() - vector_(i).GetM();
358 this->m_ += size_difference;
359 length_(i) = vector.GetM();
360 for (
int k = i; k < Nvector_; k++)
361 length_sum_(k) += size_difference;
363 vector_(i).Nullify();
364 vector_(i).SetData(vector);
374 template <
class T,
class Allocator >
375 template <
class T0,
class Storage0,
class Allocator0>
380 SetVector(i, vector);
390 template <
class T,
class Allocator >
391 template <
class T0,
class Storage0,
class Allocator0>
394 Storage0, Allocator0>& vector)
396 map<string,int>::iterator label_iterator;
397 label_iterator = label_map_.find(name);
398 if (label_iterator == label_map_.end())
399 throw WrongArgument(
"VectorCollection::SetVector(string name, Vector)",
400 string(
"Unknown vector name: \"") + name +
"\".");
401 SetVector(label_iterator->second, vector);
410 template <
class T,
class Allocator >
415 #ifdef SELDON_CHECK_BOUNDS
416 if (i < 0 || i >= Nvector_)
417 throw WrongIndex(
"VectorCollection::SetName(int i, string name)",
418 string(
"Index should be in [0, ")
420 +
"], but is equal to " +
to_str(i) +
".");
423 if (i >=
int(label_vector_.size()))
424 label_vector_.resize(Nvector_,
"");
426 if (label_vector_[i] !=
"")
427 label_map_.erase(label_vector_[i]);
429 label_vector_[i] = name;
430 label_map_[name] = i;
439 template <
class T,
class Allocator >
452 template <
class T,
class Allocator >
455 for (
int i = 0; i < Nvector_; i++)
456 vector_(i).Nullify();
457 for (
int i = 0; i < Nvector_; i++)
458 length_(i) = length_sum_(i) = 0;
471 template <
class T,
class Allocator>
475 for (
int i = 0; i < Nvector_; i++)
481 template <
class T,
class Allocator >
484 for (
int i = 0; i < GetNvector(); i++)
486 if (i <
int(label_vector_.size()) && label_vector_[i] !=
"")
487 cout << label_vector_[i] <<
":" << endl;
489 cout <<
"(noname):" << endl;
503 template <
class T,
class Allocator >
505 ::Write(
string FileName,
bool with_size)
const
508 FileStream.open(FileName.c_str(), ofstream::binary);
510 #ifdef SELDON_CHECK_IO
512 if (!FileStream.is_open())
513 throw IOError(
"Vector<Collection>::Write(string FileName)",
514 string(
"Unable to open file \"") + FileName +
"\".");
517 this->Write(FileStream, with_size);
530 template <
class T,
class Allocator >
532 ::Write(ostream& FileStream,
bool with_size)
const
535 #ifdef SELDON_CHECK_IO
537 if (!FileStream.good())
538 throw IOError(
"Vector<Collection>::Write(ostream& FileStream)",
539 "The stream is not ready.");
543 .write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->m_)),
546 for (
int i = 0; i < GetNvector(); i++)
547 vector_(i).Write(FileStream,
false);
549 #ifdef SELDON_CHECK_IO
551 if (!FileStream.good())
552 throw IOError(
"Vector<Collection>::Write(ostream& FileStream)",
553 "Output operation failed.");
564 template <
class T,
class Allocator >
569 FileStream.precision(cout.precision());
570 FileStream.flags(cout.flags());
571 FileStream.open(FileName.c_str());
573 #ifdef SELDON_CHECK_IO
575 if (!FileStream.is_open())
576 throw IOError(
"Vector<Collection>::WriteText(string FileName)",
577 string(
"Unable to open file \"") + FileName +
"\".");
580 this->WriteText(FileStream);
592 template <
class T,
class Allocator >
597 #ifdef SELDON_CHECK_IO
599 if (!FileStream.good())
600 throw IOError(
"Vector<Collection>::Write(ostream& FileStream)",
601 "The stream is not ready.");
604 for (
int i = 0; i < GetNvector(); i++)
605 vector_(i).WriteText(FileStream);
607 #ifdef SELDON_CHECK_IO
609 if (!FileStream.good())
610 throw IOError(
"Vector<Collection>::Write(ostream& FileStream)",
611 "Output operation failed.");
622 template <
class T,
class Allocator >
626 FileStream.open(FileName.c_str(), ifstream::binary);
628 #ifdef SELDON_CHECK_IO
630 if (!FileStream.is_open())
631 throw IOError(
"Vector<Collection>::Read(string FileName)",
632 string(
"Unable to open file \"") + FileName +
"\".");
636 length.Read(FileStream);
638 this->Read(FileStream, length);
650 template <
class T,
class Allocator >
655 FileStream.open(FileName.c_str(), ifstream::binary);
657 #ifdef SELDON_CHECK_IO
659 if (!FileStream.is_open())
660 throw IOError(
"Vector<Collection>::Read(string FileName)",
661 string(
"Unable to open file \"") + FileName +
"\".");
664 this->Read(FileStream, length);
676 template <
class T,
class Allocator >
682 #ifdef SELDON_CHECK_IO
684 if (!FileStream.good())
685 throw IOError(
"Vector<Collection>::Read(istream& FileStream)",
686 "The stream is not ready.");
690 working_vector.Read(FileStream);
696 Nvector = length.GetSize();
697 length_sum.Reallocate(Nvector);
698 length_sum(0) = length(0);
699 for (
int i = 1; i < Nvector; i++)
700 length_sum(i) = length_sum(i - 1) + length(i);
703 U.SetData(length(0), &working_vector.GetData()[0]);
708 for (
int i = 1; i < Nvector; i++)
710 U.SetData(length(i), &working_vector.GetData()[length_sum(i - 1)]);
717 #ifdef SELDON_CHECK_IO
719 if (!FileStream.good())
720 throw IOError(
"Vector<Collection>::Read(istream& FileStream)",
721 "Input operation failed.");
733 template <
class T,
class Allocator>
737 for (
int i = 0; i < V.GetNvector() - 1; i++)
738 out << V.GetVector(i) <<
'\t';
739 if (V.GetNvector() != 0)
740 out << V.GetVector(V.GetNvector() - 1);
748 #define SELDON_FILE_VECTOR_VECTORCOLLECTION_CXX