21 #ifndef SELDON_FILE_SPARSE_VECTOR_CXX
23 #include "SparseVector.hxx"
37 template <
class T,
class Allocator>
42 #ifdef SELDON_CHECK_MEMORY
47 this->index_ = AllocatorInt::allocate(i,
this);
49 #ifdef SELDON_CHECK_MEMORY
58 if (this->index_ == NULL)
64 if (this->data_ == NULL && i != 0)
65 throw NoMemory(
"Vector<VectSparse>::Vector(int)",
66 string(
"Unable to allocate memory for a vector of size ")
67 +
to_str(i *
sizeof(T)) +
" bytes ("
68 +
to_str(i) +
" elements).");
79 template <
class T,
class Allocator>
83 #ifdef SELDON_CHECK_MEMORY
87 if (this->data_ != NULL)
89 Allocator::deallocate(this->data_, this->m_);
95 AllocatorInt::deallocate(index_, this->m_);
101 #ifdef SELDON_CHECK_MEMORY
122 template <
class T,
class Allocator>
132 #ifdef SELDON_CHECK_MEMORY
138 reinterpret_cast<pointer
>(Allocator::
139 reallocate(this->data_, i,
this));
142 =
reinterpret_cast<int*
>(AllocatorInt::
143 reallocate(index_, i,
this));
145 #ifdef SELDON_CHECK_MEMORY
154 if (this->data_ == NULL)
172 template <
class T,
class Allocator>
185 template <
class T,
class Allocator>
195 size_t Nmin = min(this->m_, n);
196 for (
size_t i = 0; i < Nmin; i++)
198 new_value(i) = this->data_[i];
199 new_index(i) = index_[i];
202 SetData(new_value, new_index);
222 template <
class T,
class Allocator>
231 this->index_ = index;
246 template <
class T,
class Allocator>
247 template<
class Allocator2>
252 #ifdef SELDON_CHECK_BOUNDS
253 if (data.GetM() != index.GetM())
254 throw WrongDim(
"Vector<VectSparse>::SetData ",
255 string(
"The data vector and the index vector should")
256 +
" have the same size.\n Size of the data vector: "
257 +
to_str(data.GetM()) +
"\n Size of index vector: "
261 SetData(data.GetM(), data.GetData(), index.GetData());
278 template <
class T,
class Allocator>
279 template<
class Allocator2>
283 SetData(V.GetM(), V.GetData(), V.GetIndex());
293 template <
class T,
class Allocator>
312 template <
class T,
class Allocator>
318 SetComplexZero(zero);
320 while (k < this->m_ && index_[k] < i)
323 if (k >= this->m_ || index_[k] != i)
327 return this->data_[k];
338 template <
class T,
class Allocator>
344 SetComplexZero(zero);
346 while (k < this->m_ && index_[k] < i)
349 if (k >= this->m_ || index_[k] != i)
353 return this->data_[k];
364 template <
class T,
class Allocator>
370 SetComplexZero(zero);
372 while (k < this->m_ && index_[k] < i)
375 if (k >= this->m_ || index_[k] != i)
377 AddInteraction(i, zero);
379 return this->data_[k];
390 template <
class T,
class Allocator>
396 while (k < this->m_ && index_[k] < i)
399 if (k >= this->m_ || index_[k] != i)
401 "No reference to element " +
to_str(i)
402 +
" can be returned: it is a zero entry.");
404 return this->data_[k];
414 template <
class T,
class Allocator>
420 while (k < this->m_ && index_[k] < i)
423 if (k >= this->m_ || index_[k] != i)
425 "the entry " +
to_str(i) +
426 " does not belong to the sparsity pattern.");
429 return this->data_[k];
439 template <
class T,
class Allocator>
445 while (k < this->m_ && index_[k] < i)
448 if (k >= this->m_ || index_[k] != i)
450 "the entry " +
to_str(i) +
451 " does not belong to the sparsity pattern.");
453 return this->data_[k];
463 template <
class T,
class Allocator>
template <
class T2,
class Alloc2>
467 this->Reallocate(X.GetLength());
468 AllocatorInt::memorycpy(this->index_, X.GetIndex(), this->m_);
469 for (
size_t i = 0; i < X.GetLength(); i++)
470 this->Value(i) = X.Value(i);
483 template <
class T,
class Allocator>
495 template <
class T,
class Allocator>
498 for (
int i = 0; i < this->GetM(); i++)
499 cout << (Index(i) + 1) <<
' ' << Value(i) <<
'\n';
508 template <
class T,
class Allocator>
511 int new_size = this->m_;
514 for (
int i = 0; i < new_size; i++)
516 values(i) = this->data_[i];
517 index(i) = index_[i];
520 Seldon::Assemble(new_size, index, values);
521 index.Resize(new_size);
523 SetData(values, index);
532 template <
class T,
class Allocator>
template<
class T0>
535 size_t new_size = this->m_;
539 for (
size_t i = 0; i < this->m_; i++)
540 if (abs(this->data_[i]) > epsilon)
542 values(new_size) = this->data_[i];
543 index(new_size) = index_[i];
547 index.Resize(new_size);
549 SetData(values, index);
559 template <
class T,
class Allocator>
564 while (pos < this->m_ && index_[pos] < i)
568 if (pos < this->m_ && index_[pos] == i)
570 this->data_[pos] += val;
577 Resize(this->m_ + 1);
580 for (k = this->m_-1; k > pos; k--)
582 this->data_[k] = this->data_[k-1];
583 this->index_[k] = this->index_[k-1];
587 this->index_[pos] = i;
588 this->data_[pos] = val;
602 template <
class T,
class Allocator>
608 ind.SetData(n, index);
609 val.SetData(n, value);
610 AddInteractionRow(n, ind, val, already_sorted);
626 template <
class T,
class Allocator>
627 template<
class Allocator0>
639 already_sorted =
true;
641 for (
size_t i = 0; i < n-1; i++)
642 if (index2(i+1) <= index2(i))
643 already_sorted =
false;
650 for (
size_t i = 0; i < n; i++)
652 index(i) = index2(i);
653 value(i) = value2(i);
657 int ni = n; Seldon::Assemble(ni, index, value); n = ni;
661 index.SetData(n, index2.GetData());
662 value.SetData(n, value2.GetData());
670 new_index.Fill(
true);
672 for (
size_t j = 0; j < n; j++)
674 while (k < this->m_ && index_[k] < index(j))
677 if (k < this->m_ && index(j) == index_[k])
679 new_index(j) =
false;
680 this->data_[k] += value(j);
693 for (
size_t j = 0; j < n; j++)
696 while (k < this->m_ && index_[k] < index(j))
698 new_ind(nb) = index_[k];
699 new_val(nb) = this->data_[k];
705 new_ind(nb) = index(j);
706 new_val(nb) = value(j);
713 new_ind(nb) = index_[k];
714 new_val(nb) = this->data_[k];
719 SetData(new_val, new_ind);
734 template <
class T,
class Allocator>
735 typename ClassComplexType<T>::Treal
738 typename ClassComplexType<T>::Treal res(0);
739 for (
size_t i = 0; i < this->m_; i++)
740 res = max(res, abs(this->data_[i]));
750 template <
class T,
class Allocator>
754 #ifdef SELDON_CHECK_DIMENSIONS
755 if (this->GetLength() == 0)
756 throw WrongDim(
"Vector<VectSparse>::GetNormInfIndex()",
760 typename ClassComplexType<T>::Treal res(0), temp;
762 for (
size_t i = 0; i < this->GetLength(); i++)
765 res = max(res, abs(this->data_[i]));
766 if (temp != res) j = i;
769 return this->index_[j];
784 template <
class T,
class Allocator>
788 FileStream.open(FileName.c_str(), ofstream::binary);
790 #ifdef SELDON_CHECK_IO
792 if (!FileStream.is_open())
793 throw IOError(
"Vector<VectSparse>::Write(string FileName)",
794 string(
"Unable to open file \"") + FileName +
"\".");
797 this->Write(FileStream);
809 template <
class T,
class Allocator>
813 #ifdef SELDON_CHECK_IO
816 throw IOError(
"Vector<VectSparse>::Write(ostream& stream)",
817 "Stream is not ready.");
821 stream.write(
reinterpret_cast<char*
>(&n),
824 stream.write(
reinterpret_cast<char*
>(this->index_),
825 this->m_ *
sizeof(
int));
827 stream.write(
reinterpret_cast<char*
>(this->data_),
828 this->m_ *
sizeof(value_type));
830 #ifdef SELDON_CHECK_IO
833 throw IOError(
"Vector<VectSparse>::Write(ostream& stream)",
834 "Output operation failed.");
846 template <
class T,
class Allocator>
850 FileStream.precision(cout.precision());
851 FileStream.flags(cout.flags());
852 FileStream.open(FileName.c_str());
854 #ifdef SELDON_CHECK_IO
856 if (!FileStream.is_open())
857 throw IOError(
"Vector<VectSparse>::WriteText(string FileName)",
858 string(
"Unable to open file \"") + FileName +
"\".");
861 this->WriteText(FileStream);
873 template <
class T,
class Allocator>
877 #ifdef SELDON_CHECK_IO
880 throw IOError(
"Vector<VectSparse>::WriteText(ostream& stream)",
881 "Stream is not ready.");
885 for (
size_t i = 0; i < this->m_; i++)
886 stream << (Index(i) + 1) <<
" " << Value(i) <<
'\n';
888 #ifdef SELDON_CHECK_IO
891 throw IOError(
"Vector<VectSparse>::WriteText(ostream& stream)",
892 "Output operation failed.");
903 template <
class T,
class Allocator>
907 FileStream.open(FileName.c_str(), ifstream::binary);
909 #ifdef SELDON_CHECK_IO
911 if (!FileStream.is_open())
912 throw IOError(
"Vector<VectSparse>::Read(string FileName)",
913 string(
"Unable to open file \"") + FileName +
"\".");
916 this->Read(FileStream);
927 template <
class T,
class Allocator>
931 #ifdef SELDON_CHECK_IO
934 throw IOError(
"Vector<VectSparse>::Read(istream& stream)",
935 "Stream is not ready.");
939 stream.read(
reinterpret_cast<char*
>(&m),
sizeof(
int));
942 stream.read(
reinterpret_cast<char*
>(this->index_), m *
sizeof(
int));
944 stream.read(
reinterpret_cast<char*
>(this->data_),
945 m *
sizeof(value_type));
947 #ifdef SELDON_CHECK_IO
950 throw IOError(
"Vector<VectSparse>::Read(istream& stream)",
951 "Input operation failed.");
962 template <
class T,
class Allocator>
966 FileStream.open(FileName.c_str());
968 #ifdef SELDON_CHECK_IO
970 if (!FileStream.is_open())
971 throw IOError(
"Vector<VectSparse>::ReadText(string FileName)",
972 string(
"Unable to open file \"") + FileName +
"\".");
975 this->ReadText(FileStream);
986 template <
class T,
class Allocator>
991 #ifdef SELDON_CHECK_IO
994 throw IOError(
"Vector<VectSparse>::ReadText(istream& stream)",
995 "Stream is not ready.");
1003 while (!stream.eof())
1006 stream >> ind >> entry;
1012 #ifdef SELDON_CHECK_IO
1014 throw IOError(
string(
"Vector<VectSparse>::ReadText") +
1015 "(istream& stream)",
1016 string(
"Index should be greater ")
1017 +
"than 0 but is equal to " +
to_str(ind) +
".");
1024 if (nb_elt > values.
GetM())
1026 values.
Resize(2 * nb_elt);
1027 index.Resize(2 * nb_elt);
1030 values(nb_elt - 1) = entry;
1031 index(nb_elt - 1) = ind;
1038 this->Reallocate(nb_elt);
1039 for (
int i = 0; i < nb_elt; i++)
1041 Index(i) = index(i);
1042 Value(i) = values(i);
1054 template <
class T,
class Allocator>
1058 for (
size_t i = 0; i < V.
GetLength(); i++)
1059 out << (V.
Index(i) + 1) <<
' ' << V.
Value(i) <<
'\n';
1066 #define SELDON_FILE_SPARSE_VECTOR_CXX