21 #ifndef SELDON_FILE_MATRIX_POINTERS_CXX
23 #include "Matrix_Pointers.hxx"
34 template <
class T,
class Prop,
class Storage,
class Allocator>
39 #ifdef SELDON_CHECK_MEMORY
44 me_ =
reinterpret_cast<pointer*
>( calloc(Storage::GetFirst(i, j),
47 #ifdef SELDON_CHECK_MEMORY
56 if (me_ == NULL && i != 0 && j != 0)
57 throw NoMemory(
"Matrix_Pointers::Matrix_Pointers(int, int)",
58 string(
"Unable to allocate memory for a matrix of size ")
59 +
to_str(
static_cast<long int>(i)
60 *
static_cast<long int>(j)
61 *
static_cast<long int>(
sizeof(T)))
66 #ifdef SELDON_CHECK_MEMORY
71 this->data_ = Allocator::allocate(
long(i) *
long(j),
this);
73 #ifdef SELDON_CHECK_MEMORY
83 if (this->data_ == NULL && i != 0 && j != 0)
84 throw NoMemory(
"Matrix_Pointers::Matrix_Pointers(int, int)",
85 string(
"Unable to allocate memory for a matrix of size ")
86 +
to_str(
static_cast<long int>(i)
87 *
static_cast<long int>(j)
88 *
static_cast<long int>(
sizeof(T)))
93 pointer ptr = this->data_;
94 long lgth = Storage::GetSecond(i, j);
95 for (
int k = 0; k < Storage::GetFirst(i, j); k++, ptr += lgth)
102 template <
class T,
class Prop,
class Storage,
class Allocator>
121 template <
class T,
class Prop,
class Storage,
class Allocator>
124 #ifdef SELDON_CHECK_MEMORY
129 if (this->data_ != NULL)
131 Allocator::deallocate(this->data_,
long(this->m_) *
long(this->n_));
135 #ifdef SELDON_CHECK_MEMORY
143 #ifdef SELDON_CHECK_MEMORY
154 #ifdef SELDON_CHECK_MEMORY
181 template <
class T,
class Prop,
class Storage,
class Allocator>
186 if (i != this->m_ || j != this->n_)
191 #ifdef SELDON_CHECK_MEMORY
196 me_ =
reinterpret_cast<pointer*
>( realloc(me_,
197 Storage::GetFirst(i, j)
198 *
sizeof(pointer)) );
200 #ifdef SELDON_CHECK_MEMORY
209 if (me_ == NULL && i != 0 && j != 0)
210 throw NoMemory(
"Matrix_Pointers::Reallocate(int, int)",
211 string(
"Unable to reallocate memory for")
212 +
" a matrix of size "
213 +
to_str(
static_cast<long int>(i)
214 *
static_cast<long int>(j)
215 *
static_cast<long int>(
sizeof(T)))
220 #ifdef SELDON_CHECK_MEMORY
224 long taille = long(i)*long(j);
226 reinterpret_cast<pointer
>(Allocator::
227 reallocate(this->data_, taille,
230 #ifdef SELDON_CHECK_MEMORY
240 if (this->data_ == NULL && i != 0 && j != 0)
241 throw NoMemory(
"Matrix_Pointers::Reallocate(int, int)",
242 string(
"Unable to reallocate memory")
243 +
" for a matrix of size "
244 +
to_str(
static_cast<long int>(i)
245 *
static_cast<long int>(j)
246 *
static_cast<long int>(
sizeof(T)))
251 pointer ptr = this->data_;
252 long lgth = Storage::GetSecond(i, j);
253 for (
int k = 0; k < Storage::GetFirst(i, j); k++, ptr += lgth)
275 template <
class T,
class Prop,
class Storage,
class Allocator>
286 #ifdef SELDON_CHECK_MEMORY
291 me_ =
reinterpret_cast<pointer*
>( calloc(Storage::GetFirst(i, j),
294 #ifdef SELDON_CHECK_MEMORY
315 pointer ptr = this->data_;
316 long lgth = Storage::GetSecond(i, j);
317 for (
int k = 0; k < Storage::GetFirst(i, j); k++, ptr += lgth)
328 template <
class T,
class Prop,
class Storage,
class Allocator>
334 #ifdef SELDON_CHECK_MEMORY
345 #ifdef SELDON_CHECK_MEMORY
367 template <
class T,
class Prop,
class Storage,
class Allocator>
372 if (i == this->m_ && j == this->n_)
376 int iold = Storage::GetFirst(this->m_, this->n_);
377 int jold = Storage::GetSecond(this->m_, this->n_);
379 for (
long k = 0; k < this->GetDataSize(); k++)
380 xold(k) = this->data_[k];
383 int inew = Storage::GetFirst(i, j);
384 int jnew = Storage::GetSecond(i, j);
385 this->Reallocate(i,j);
388 int imin = min(iold, inew), jmin = min(jold, jnew);
389 for (
int k = 0; k < imin; k++)
390 for (
int l = 0; l < jmin; l++)
391 this->data_[k*jnew+l] = xold(l+jold*k);
405 template <
class T,
class Prop,
class Storage,
class Allocator>
408 Allocator::memoryset(this->data_,
char(0),
409 this->GetDataSize() *
sizeof(value_type));
414 template <
class T,
class Prop,
class Storage,
class Allocator>
418 SetComplexZero(zero);
423 for (
int i = 0; i < min(this->m_, this->n_); i++)
433 template <
class T,
class Prop,
class Storage,
class Allocator>
436 for (
long i = 0; i < this->GetDataSize(); i++)
445 template <
class T,
class Prop,
class Storage,
class Allocator>
450 for (
long i = 0; i < this->GetDataSize(); i++)
458 template <
class T,
class Prop,
class Storage,
class Allocator>
473 template <
class T,
class Prop,
class Storage,
class Allocator>
476 #ifndef SELDON_WITHOUT_REINIT_RANDOM
479 for (
long i = 0; i < this->GetDataSize(); i++)
490 template <
class T,
class Prop,
class Storage,
class Allocator>
493 for (
int i = 0; i < this->m_; i++)
495 for (
int j = 0; j < this->n_; j++)
496 cout << (*
this)(i, j) <<
"\t";
514 template <
class T,
class Prop,
class Storage,
class Allocator>
518 for (
int i = a; i < min(this->m_, a+m); i++)
520 for (
int j = b; j < min(this->n_, b+n); j++)
521 cout << (*
this)(i, j) <<
"\t";
536 template <
class T,
class Prop,
class Storage,
class Allocator>
547 #ifdef SELDON_WITH_HDF5
556 template <
class T,
class Prop,
class Storage,
class Allocator>
558 ::WriteHDF5(
string FileName,
string group_name,
string dataset_name)
const
560 throw IOError(
"Matrix_Pointers::WriteHDF5(string FileName)",
561 string(
"Unable to write matrix in \"") + FileName +
"\".");
572 template <
class T,
class Prop,
class Storage,
class Allocator>
577 FileStream.open(FileName.c_str(), ofstream::binary | ios::app);
579 #ifdef SELDON_CHECK_IO
581 if (!FileStream.is_open())
582 throw IOError(
"Matrix_Pointers::Write(string FileName)",
583 string(
"Unable to open file \"") + FileName +
"\".");
586 this->Write(FileStream,
false);
602 template <
class T,
class Prop,
class Storage,
class Allocator>
604 ::Write(
string FileName,
bool with_size)
const
607 FileStream.open(FileName.c_str(), ofstream::binary);
609 #ifdef SELDON_CHECK_IO
611 if (!FileStream.is_open())
612 throw IOError(
"Matrix_Pointers::Write(string FileName)",
613 string(
"Unable to open file \"") + FileName +
"\".");
616 this->Write(FileStream, with_size);
632 template <
class T,
class Prop,
class Storage,
class Allocator>
634 ::Write(ostream& FileStream,
bool with_size)
const
637 #ifdef SELDON_CHECK_IO
639 if (!FileStream.good())
640 throw IOError(
"Matrix_Pointers::Write(ostream& FileStream)",
641 "The stream is not ready.");
646 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->m_)),
648 FileStream.write(
reinterpret_cast<char*
>(
const_cast<int*
>(&this->n_)),
652 FileStream.write(
reinterpret_cast<char*
>(this->data_),
653 long(this->m_) *
long(this->n_) *
sizeof(value_type));
655 #ifdef SELDON_CHECK_IO
657 if (!FileStream.good())
658 throw IOError(
"Matrix_Pointers::Write(ostream& FileStream)",
659 "Output operation failed.");
673 template <
class T,
class Prop,
class Storage,
class Allocator>
678 FileStream.precision(cout.precision());
679 FileStream.flags(cout.flags());
680 FileStream.open(FileName.c_str());
682 #ifdef SELDON_CHECK_IO
684 if (!FileStream.is_open())
685 throw IOError(
"Matrix_Pointers::WriteText(string FileName)",
686 string(
"Unable to open file \"") + FileName +
"\".");
689 this->WriteText(FileStream);
703 template <
class T,
class Prop,
class Storage,
class Allocator>
708 #ifdef SELDON_CHECK_IO
710 if (!FileStream.good())
711 throw IOError(
"Matrix_Pointers::WriteText(ostream& FileStream)",
712 "The stream is not ready.");
716 for (i = 0; i < this->GetM(); i++)
718 for (j = 0; j < this->GetN(); j++)
719 FileStream << (*
this)(i, j) <<
'\t';
723 #ifdef SELDON_CHECK_IO
725 if (!FileStream.good())
726 throw IOError(
"Matrix_Pointers::WriteText(ostream& FileStream)",
727 "Output operation failed.");
744 template <
class T,
class Prop,
class Storage,
class Allocator>
749 FileStream.open(FileName.c_str(), ifstream::binary);
751 #ifdef SELDON_CHECK_IO
753 if (!FileStream.is_open())
754 throw IOError(
"Matrix_Pointers::Read(string FileName)",
755 string(
"Unable to open file \"") + FileName +
"\".");
758 this->Read(FileStream, with_size);
775 template <
class T,
class Prop,
class Storage,
class Allocator>
777 ::Read(istream& FileStream,
bool with_size)
780 #ifdef SELDON_CHECK_IO
782 if (!FileStream.good())
783 throw IOError(
"Matrix_Pointers::Read(istream& FileStream)",
784 "The stream is not ready.");
790 FileStream.read(
reinterpret_cast<char*
>(&new_m),
sizeof(
int));
791 FileStream.read(
reinterpret_cast<char*
>(&new_n),
sizeof(
int));
792 this->Reallocate(new_m, new_n);
795 FileStream.read(
reinterpret_cast<char*
>(this->data_),
796 long(this->GetM()) *
long(this->GetN())
797 *
sizeof(value_type));
799 #ifdef SELDON_CHECK_IO
801 if (!FileStream.good())
802 throw IOError(
"Matrix_Pointers::Read(istream& FileStream)",
803 "Input operation failed.");
814 template <
class T,
class Prop,
class Storage,
class Allocator>
818 FileStream.open(FileName.c_str());
820 #ifdef SELDON_CHECK_IO
822 if (!FileStream.is_open())
823 throw IOError(
"Matrix_Pointers::ReadText(string FileName)",
824 string(
"Unable to open file \"") + FileName +
"\".");
827 this->ReadText(FileStream);
838 template <
class T,
class Prop,
class Storage,
class Allocator>
845 #ifdef SELDON_CHECK_IO
847 if (!FileStream.good())
848 throw IOError(
"Matrix_Pointers::ReadText(istream& FileStream)",
849 "The stream is not ready.");
854 getline(FileStream, line);
855 if (FileStream.fail())
860 istringstream line_stream(line);
862 first_row.ReadText(line_stream);
866 other_row.ReadText(FileStream);
869 int n = first_row.GetM();
870 int m = 1 + other_row.GetM() / n;
872 #ifdef SELDON_CHECK_IO
874 if (other_row.GetM() != (m - 1) * n)
875 throw IOError(
"Matrix_Pointers::ReadText(istream& FileStream)",
876 "Not all rows have the same number of columns.");
879 this->Reallocate(m, n);
881 for (
int j = 0; j < n; j++)
882 this->Val(0, j) = first_row(j);
885 for (
int i = 1; i < m; i++)
886 for (
int j = 0; j < n; j++)
887 this->Val(i, j) = other_row(k++);
908 template <
class T,
class Prop,
class Allocator>
913 FileStream.open(FileName.c_str());
915 #ifdef SELDON_CHECK_IO
917 if (!FileStream.is_open())
918 throw IOError(
"Matrix::WriteColumn(string FileName)",
919 string(
"Unable to open file \"") + FileName +
"\".");
922 this->WriteColumn(FileStream, col);
935 template <
class T,
class Prop,
class Allocator>
939 #ifdef SELDON_CHECK_BOUNDS
940 if (col < 0 || col >= this->n_)
941 throw WrongCol(
"Matrix::WriteColumn(ostream& FileStream, int col)",
942 string(
"Index should be in [0, ")
943 +
to_str(this->n_-1) +
"], but is equal to "
947 #ifdef SELDON_CHECK_IO
949 if (!FileStream.good())
950 throw IOError(
"Matrix::WriteColumn(ostream& FileStream, int col)",
951 "The stream is not ready.");
954 FileStream.write(
reinterpret_cast<char*
>(this->me_[col]),
955 this->m_ *
sizeof(value_type));
957 #ifdef SELDON_CHECK_IO
959 if (!FileStream.good())
960 throw IOError(
"Matrix::WriteColumn(ostream& FileStream, int col)",
961 "Output operation failed.");
984 template <
class T,
class Prop,
class Allocator>
989 FileStream.open(FileName.c_str());
991 #ifdef SELDON_CHECK_IO
993 if (!FileStream.is_open())
994 throw IOError(
"Matrix::WriteRow(string FileName, int row)",
995 string(
"Unable to open file \"") + FileName +
"\".");
998 this->WriteRow(FileStream, row);
1011 template <
class T,
class Prop,
class Allocator>
1015 #ifdef SELDON_CHECK_BOUNDS
1016 if (row < 0 || row >= this->m_)
1017 throw WrongRow(
"Matrix::WriteRow(ostream& FileStream, int row)",
1018 string(
"Index should be in [0, ")
1019 +
to_str(this->m_-1) +
"], but is equal to "
1023 #ifdef SELDON_CHECK_IO
1025 if (!FileStream.good())
1026 throw IOError(
"Matrix::WriteRow(ostream& FileStream, int rowm)",
1027 "The stream is not ready.");
1030 FileStream.write(
reinterpret_cast<char*
>(this->me_[row]),
1031 this->n_ *
sizeof(value_type));
1033 #ifdef SELDON_CHECK_IO
1035 if (!FileStream.good())
1036 throw IOError(
"Matrix::WriteRow(ostream& FileStream, int row)",
1037 "Output operation failed.");
1045 #define SELDON_FILE_MATRIX_POINTERS_CXX