vector/Vector2.cxx

00001 // Copyright (C) 2010, INRIA
00002 // Author(s): Marc Fragu, Vivien Mallet
00003 //
00004 // This file is part of the linear-algebra library Seldon,
00005 // http://seldon.sourceforge.net/.
00006 //
00007 // Seldon is free software; you can redistribute it and/or modify it under the
00008 // terms of the GNU Lesser General Public License as published by the Free
00009 // Software Foundation; either version 2.1 of the License, or (at your option)
00010 // any later version.
00011 //
00012 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
00015 // more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public License
00018 // along with Seldon. If not, see http://www.gnu.org/licenses/.
00019 
00020 
00021 #ifndef SELDON_FILE_VECTOR_VECTOR2_CXX
00022 
00023 
00024 #include "Vector2.hxx"
00025 
00026 
00027 namespace Seldon
00028 {
00029 
00030 
00032   // VECTOR2 //
00034 
00035 
00036   /***************
00037    * CONSTRUCTOR *
00038    ***************/
00039 
00040 
00042 
00045   template <class T, class Allocator0, class Allocator1>
00046   Vector2<T, Allocator0, Allocator1>::Vector2()
00047   {
00048   }
00049 
00050 
00052 
00055   template <class T, class Allocator0, class Allocator1>
00056   Vector2<T, Allocator0, Allocator1>::Vector2(int length)
00057   {
00058     data_.Reallocate(length);
00059   }
00060 
00061 
00063 
00067   template <class T, class Allocator0, class Allocator1>
00068   Vector2<T, Allocator0, Allocator1>::Vector2(const Vector<int>& length)
00069   {
00070     data_.Clear();
00071     int m = length.GetSize();
00072     data_.Reallocate(m);
00073     for(int i = 0; i < m; i++)
00074       data_(i).Reallocate(length(i));
00075   }
00076 
00077 
00078   /**************
00079    * DESTRUCTOR *
00080    **************/
00081 
00082 
00084   template <class T, class Allocator0, class Allocator1>
00085   Vector2<T, Allocator0, Allocator1>::~Vector2()
00086   {
00087   }
00088 
00089 
00090   /*****************************
00091    * MANAGEMENT OF THE VECTORS *
00092    *****************************/
00093 
00094 
00096 
00099   template <class T, class Allocator0, class Allocator1>
00100   bool Vector2<T, Allocator0, Allocator1>::IsEmpty() const
00101   {
00102     for (int i = 0; i < GetLength(); i++)
00103       if (GetLength(i) > 0)
00104         return false;
00105     return true;
00106   }
00107 
00108 
00110 
00113   template <class T, class Allocator0, class Allocator1>
00114   int Vector2<T, Allocator0, Allocator1>::GetSize() const
00115   {
00116     return data_.GetSize();
00117   }
00118 
00119 
00121 
00124   template <class T, class Allocator0, class Allocator1>
00125   int Vector2<T, Allocator0, Allocator1>::GetLength() const
00126   {
00127     return data_.GetLength();
00128   }
00129 
00130 
00132 
00136   template <class T, class Allocator0, class Allocator1>
00137   int Vector2<T, Allocator0, Allocator1>::GetSize(int i) const
00138   {
00139     return data_(i).GetSize();
00140   }
00141 
00142 
00144 
00148   template <class T, class Allocator0, class Allocator1>
00149   int Vector2<T, Allocator0, Allocator1>::GetLength(int i) const
00150   {
00151     return data_(i).GetLength();
00152   }
00153 
00154 
00156 
00159   template <class T, class Allocator0, class Allocator1>
00160   int Vector2<T, Allocator0, Allocator1>::GetNelement() const
00161   {
00162     int total = 0;
00163     for (int i = 0; i < GetLength(); i++)
00164       total += GetLength(i);
00165     return total;
00166   }
00167 
00168 
00170 
00177   template <class T, class Allocator0, class Allocator1>
00178   int Vector2<T, Allocator0, Allocator1>::GetNelement(int beg, int end) const
00179   {
00180     if (beg > end)
00181       throw WrongArgument("Vector2::GetNelement(int beg, int end)",
00182                           "The lower bound of the range of inner vectors, ["
00183                           + to_str(beg) + ", " + to_str(end)
00184                           + "[, is strictly greater than its upper bound.");
00185     if (beg < 0 || end > GetLength())
00186       throw WrongArgument("Vector2::GetNelement(int beg, int end)",
00187                           "The inner-vector indexes should be in [0,"
00188                           + to_str(GetLength()) + "] but [" + to_str(beg)
00189                           + ", " + to_str(end) + "[ was provided.");
00190 
00191     int total = 0;
00192     for (int i = beg; i < end; i++)
00193       total += GetLength(i);
00194     return total;
00195   }
00196 
00197 
00199 
00202   template <class T, class Allocator0, class Allocator1>
00203   Vector<int> Vector2<T, Allocator0, Allocator1>::GetShape() const
00204   {
00205     Vector<int> shape(GetLength());
00206     for (int i = 0; i < GetLength(); i++)
00207       shape(i) = GetLength(i);
00208     return shape;
00209   }
00210 
00211 
00213 
00216   template <class T, class Allocator0, class Allocator1>
00217   void Vector2<T, Allocator0, Allocator1>::GetShape(Vector<int>& shape) const
00218   {
00219     shape.Reallocate(GetLength());
00220     for (int i = 0; i < GetLength(); i++)
00221       shape(i) = GetLength(i);
00222   }
00223 
00224 
00226 
00229   template <class T, class Allocator0, class Allocator1>
00230   void Vector2<T, Allocator0, Allocator1>::Reallocate(int M)
00231   {
00232     data_.Reallocate(M);
00233   }
00234 
00235 
00237 
00241   template <class T, class Allocator0, class Allocator1>
00242   void Vector2<T, Allocator0, Allocator1>::Reallocate(int i, int N)
00243   {
00244     data_(i).Reallocate(N);
00245   }
00246 
00247 
00249 
00253   template <class T, class Allocator0, class Allocator1>
00254   void Vector2<T, Allocator0, Allocator1>
00255   ::Reallocate(const Vector<int>& length)
00256   {
00257     int m = length.GetSize();
00258     data_.Reallocate(m);
00259     for(int i = 0; i < m; i++)
00260       data_(i).Reallocate(length(i));
00261   }
00262 
00263 
00265 
00270   template <class T, class Allocator0, class Allocator1>
00271   void Vector2<T, Allocator0, Allocator1>::Select(int beg, int end)
00272   {
00273     if (beg > end)
00274       throw WrongArgument("Vector2::SelectInnerVector(int beg, int end)",
00275                           "The lower bound of the range of inner vectors, ["
00276                           + to_str(beg) + ", " + to_str(end)
00277                           + "[, is strictly greater than its upper bound.");
00278     if (beg < 0 || end > GetLength())
00279       throw WrongArgument("Vector2::SelectInnerVector(int beg, int end)",
00280                           "The inner-vector indexes should be in [0,"
00281                           + to_str(GetLength()) + "] but [" + to_str(beg)
00282                           + ", " + to_str(end) + "[ was provided.");
00283 
00284     if (beg > 0)
00285       for (int i = 0; i < end - beg; i++)
00286         data_(i) = data_(beg + i);
00287     data_.Reallocate(end - beg);
00288   }
00289 
00290 
00292 
00296   template <class T, class Allocator0, class Allocator1>
00297   template <class Td, class Allocatord>
00298   void Vector2<T, Allocator0, Allocator1>
00299   ::Flatten(Vector<Td, VectFull, Allocatord>& data) const
00300   {
00301     data.Reallocate(GetNelement());
00302     int i, j, n(0);
00303     for (i = 0; i < GetLength(); i++)
00304       for (j = 0; j < GetLength(i); j++)
00305         data(n++) = data_(i)(j);
00306   }
00307 
00308 
00310 
00318   template <class T, class Allocator0, class Allocator1>
00319   template <class Td, class Allocatord>
00320   void Vector2<T, Allocator0, Allocator1>
00321   ::Flatten(int beg, int end, Vector<Td, VectFull, Allocatord>& data) const
00322   {
00323     if (beg > end)
00324       throw WrongArgument("Vector2::Flatten(int beg, int end, Vector& data)",
00325                           "The lower bound of the range of inner vectors, ["
00326                           + to_str(beg) + ", " + to_str(end)
00327                           + "[, is strictly greater than its upper bound.");
00328     if (beg < 0 || end > GetLength())
00329       throw WrongArgument("Vector2::Flatten(int beg, int end, Vector& data)",
00330                           "The inner-vector indexes should be in [0,"
00331                           + to_str(GetLength()) + "] but [" + to_str(beg)
00332                           + ", " + to_str(end) + "[ was provided.");
00333 
00334     data.Reallocate(GetNelement(beg, end));
00335     int i, j, n(0);
00336     for (i = beg; i < end; i++)
00337       for (j = 0; j < GetLength(i); j++)
00338         data(n++) = data_(i)(j);
00339   }
00340 
00341 
00343 
00347   template <class T, class Allocator0, class Allocator1>
00348   void Vector2<T, Allocator0, Allocator1>::PushBack(int i, const T& x)
00349   {
00350     data_(i).PushBack(x);
00351   }
00352 
00353 
00355 
00358   template <class T, class Allocator0, class Allocator1>
00359   void Vector2<T, Allocator0, Allocator1>
00360   ::PushBack(const Vector<T, VectFull, Allocator0>& X)
00361   {
00362     data_.PushBack(X);
00363   }
00364 
00365 
00367 
00371   template <class T, class Allocator0, class Allocator1>
00372   void Vector2<T, Allocator0, Allocator1>
00373   ::PushBack(const Vector<Vector<T, VectFull, Allocator0>,
00374              VectFull, Allocator1>& V)
00375   {
00376     for (int i = 0; i < V.GetLength(); i++)
00377       data_.PushBack(V(i));
00378   }
00379 
00380 
00382 
00386   template <class T, class Allocator0, class Allocator1>
00387   void Vector2<T, Allocator0, Allocator1>
00388   ::PushBack(const Vector2<T, Allocator0, Allocator1>& V)
00389   {
00390     PushBack(V.GetVector());
00391   }
00392 
00393 
00395   template <class T, class Allocator0, class Allocator1>
00396   void Vector2<T, Allocator0, Allocator1>::Clear()
00397   {
00398     data_.Clear();
00399   }
00400 
00401 
00403 
00406   template <class T, class Allocator0, class Allocator1>
00407   void Vector2<T, Allocator0, Allocator1>::Clear(int i)
00408   {
00409     data_(i).Clear();
00410   }
00411 
00412 
00414 
00417   template <class T, class Allocator0, class Allocator1>
00418   void Vector2<T, Allocator0, Allocator1>::Fill(const T& x)
00419   {
00420     for (int i = 0; i < data_.GetLength(); i++)
00421       data_(i).Fill(x);
00422   }
00423 
00424 
00426 
00429   template <class T, class Allocator0, class Allocator1>
00430   Vector<Vector<T, VectFull, Allocator0>, VectFull, Allocator1>&
00431   Vector2<T, Allocator0, Allocator1>::GetVector()
00432   {
00433     return data_;
00434   }
00435 
00436 
00438 
00441   template <class T, class Allocator0, class Allocator1>
00442   const Vector<Vector<T, VectFull, Allocator0>, VectFull, Allocator1>
00443   Vector2<T, Allocator0, Allocator1>::GetVector() const
00444   {
00445     return data_;
00446   }
00447 
00448 
00450 
00454   template <class T, class Allocator0, class Allocator1>
00455   Vector<T, VectFull, Allocator0>&
00456   Vector2<T, Allocator0, Allocator1>::GetVector(int i)
00457   {
00458     return data_(i);
00459   }
00460 
00461 
00463 
00467   template <class T, class Allocator0, class Allocator1>
00468   const Vector<T, VectFull, Allocator0>&
00469   Vector2<T, Allocator0, Allocator1>::GetVector(int i) const
00470   {
00471     return data_(i);
00472   }
00473 
00474 
00476 
00481   template <class T, class Allocator0, class Allocator1>
00482   void Vector2<T, Allocator0, Allocator1>
00483   ::Copy(const Vector2<T, Allocator0, Allocator1>& V)
00484   {
00485     Clear();
00486     Reallocate(V.GetLength());
00487     for (int i = 0; i < V.GetLength(); i++)
00488       data_(i) = V(i);
00489   }
00490 
00491 
00492   /*********************************
00493    * ELEMENT ACCESS AND ASSIGNMENT *
00494    *********************************/
00495 
00496 
00498 
00502   template <class T, class Allocator0, class Allocator1>
00503   const Vector<T, VectFull, Allocator0>&
00504   Vector2<T, Allocator0, Allocator1>::operator() (int i) const
00505   {
00506     return data_(i);
00507   }
00508 
00509 
00511 
00515   template <class T, class Allocator0, class Allocator1>
00516   Vector<T, VectFull, Allocator0>&
00517   Vector2<T, Allocator0, Allocator1>::operator() (int i)
00518   {
00519     return data_(i);
00520   }
00521 
00522 
00524 
00529   template <class T, class Allocator0, class Allocator1>
00530   typename Vector2<T, Allocator0, Allocator1>::const_reference
00531   Vector2<T, Allocator0, Allocator1>::operator() (int i, int j) const
00532   {
00533     return data_(i)(j);
00534   }
00535 
00536 
00538 
00543   template <class T, class Allocator0, class Allocator1>
00544   typename Vector2<T, Allocator0, Allocator1>::reference
00545   Vector2<T, Allocator0, Allocator1>::operator() (int i, int j)
00546   {
00547     return data_(i)(j);
00548   }
00549 
00550 
00551   /**********************
00552    * CONVENIENT METHODS *
00553    **********************/
00554 
00555 
00557 
00565   template <class T, class Allocator0, class Allocator1>
00566   template <class V2>
00567   bool Vector2<T, Allocator0, Allocator1>::HasSameShape(const V2& V) const
00568   {
00569     if (V.GetLength() != GetLength())
00570       return false;
00571     for (int i = 0; i < GetLength(); i++)
00572       if (V.GetLength(i) != GetLength(i))
00573         return false;
00574     return true;
00575   }
00576 
00577 
00579   template <class T, class Allocator0, class Allocator1>
00580   void Vector2<T, Allocator0, Allocator1>::Print() const
00581   {
00582     for (int i = 0; i < data_.GetLength(); i++)
00583       {
00584         cout << "Vector " << i << ": ";
00585         data_(i).Print();
00586       }
00587   }
00588 
00589 
00590 } // namespace Seldon.
00591 
00592 
00593 #define SELDON_FILE_VECTOR_VECTOR2_CXX
00594 #endif