ROOT logo
// @(#)root/foam:$Id: TFoamVect.cxx 20882 2007-11-19 11:31:26Z rdm $
// Author: S. Jadach <mailto:Stanislaw.jadach@ifj.edu.pl>, P.Sawicki <mailto:Pawel.Sawicki@ifj.edu.pl>

//_____________________________________________________________________________
//                                                                            //
// Auxiliary class TFoamVect of n-dimensional vector, with dynamic allocation //
// used for the cartesian geometry of the TFoam  cells                        //
//                                                                            //
//_____________________________________________________________________________

#include "Riostream.h"
#include "TSystem.h"
#include "TFoamVect.h"

#define SW2 setprecision(7) << setw(12)

ClassImp(TFoamVect);

//_____________________________________________________________________________
TFoamVect::TFoamVect()
{
// Default constructor for streamer

   fDim    =0;
   fCoords =0;
   fNext   =0;
   fPrev   =0;
}

//______________________________________________________________________________
TFoamVect::TFoamVect(Int_t n)
{
// User constructor creating n-dimensional vector
// and allocating dynamically array of components

   Int_t i;
   fNext=0;
   fPrev=0;
   fDim=n;
   fCoords = 0;
   if (n>0) {
      fCoords = new Double_t[fDim];
      if(gDebug) {
         if(fCoords == 0)
            Error("TFoamVect", "Constructor failed to allocate\n");
      }
      for (i=0; i<n; i++) *(fCoords+i)=0.0;
   }
   if(gDebug) Info("TFoamVect", "USER CONSTRUCTOR TFoamVect(const Int_t)\n ");
}

//___________________________________________________________________________
TFoamVect::TFoamVect(const TFoamVect &Vect): TObject(Vect)
{
// Copy constructor

   fNext=0;
   fPrev=0;
   fDim=Vect.fDim;
   fCoords = 0;
   if(fDim>0)  fCoords = new Double_t[fDim];
   if(gDebug) {
      if(fCoords == 0) {
         Error("TFoamVect", "Constructor failed to allocate fCoords\n");
      }
   }
   for(Int_t i=0; i<fDim; i++)
      fCoords[i] = Vect.fCoords[i];
   Error("TFoamVect","+++++ NEVER USE Copy constructor !!!!!\n ");
}

//___________________________________________________________________________
TFoamVect::~TFoamVect()
{
// Destructor
   if(gDebug) Info("TFoamVect"," DESTRUCTOR TFoamVect~ \n");
   delete [] fCoords; //  free(fCoords)
   fCoords=0;
}


//////////////////////////////////////////////////////////////////////////////
//                     Overloading operators                                //
//////////////////////////////////////////////////////////////////////////////

//____________________________________________________________________________
TFoamVect& TFoamVect::operator =(const TFoamVect& Vect)
{
// substitution operator

   Int_t i;
   if (&Vect == this) return *this;
   if( fDim != Vect.fDim )
      Error("TFoamVect","operator=Dims. are different: %d and %d \n ",fDim,Vect.fDim);
   if( fDim != Vect.fDim ) {  // cleanup
      delete [] fCoords;
      fCoords = new Double_t[fDim];
   }
   fDim=Vect.fDim;
   for(i=0; i<fDim; i++)
      fCoords[i] = Vect.fCoords[i];
   fNext=Vect.fNext;
   fPrev=Vect.fPrev;
   if(gDebug)  Info("TFoamVect", "SUBSITUTE operator =\n ");
   return *this;
}

//______________________________________________________________________
Double_t &TFoamVect::operator[](Int_t n)
{
// [] is for access to elements as in ordinary matrix like a[j]=b[j]
// (Perhaps against some strict rules but rather practical.)
// Range protection is built in, consequently for substitution
// one should use rather use a=b than explicit loop!

   if ((n<0) || (n>=fDim)) {
      Error( "TFoamVect","operator[], out of range \n");
   }
   return fCoords[n];
}

//______________________________________________________________________
TFoamVect& TFoamVect::operator*=(const Double_t &x)
{
// unary multiplication operator *=

   for(Int_t i=0;i<fDim;i++)
      fCoords[i] = fCoords[i]*x;
   return *this;
}

//_______________________________________________________________________
TFoamVect& TFoamVect::operator+=(const TFoamVect& Shift)
{
// unary addition operator +=; adding vector c*=x,
   if( fDim != Shift.fDim){
      Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
   }
   for(Int_t i=0;i<fDim;i++)
      fCoords[i] = fCoords[i]+Shift.fCoords[i];
   return *this;
}

//________________________________________________________________________
TFoamVect& TFoamVect::operator-=(const TFoamVect& Shift)
{
// unary subtraction operator -=
   if( fDim != Shift.fDim) {
      Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
   }
   for(Int_t i=0;i<fDim;i++)
      fCoords[i] = fCoords[i]-Shift.fCoords[i];
   return *this;
}

//_________________________________________________________________________
TFoamVect TFoamVect::operator+(const TFoamVect &p2)
{
// addition operator +; sum of 2 vectors: c=a+b, a=a+b,
// NEVER USE IT, VERY SLOW!!!
   TFoamVect temp(fDim);
   temp  = (*this);
   temp += p2;
   return temp;
}

//__________________________________________________________________________
TFoamVect TFoamVect::operator-(const TFoamVect &p2)
{
// subtraction operator -; difference of 2 vectors; c=a-b, a=a-b,
// NEVER USE IT, VERY SLOW!!!
   TFoamVect temp(fDim);
   temp  = (*this);
   temp -= p2;
   return temp;
}

//___________________________________________________________________________
TFoamVect& TFoamVect::operator =(Double_t Vect[])
{
// Loading in ordinary double prec. vector, sometimes can be useful
   Int_t i;
   for(i=0; i<fDim; i++)
      fCoords[i] = Vect[i];
   return *this;
}

//____________________________________________________________________________
TFoamVect& TFoamVect::operator =(Double_t x)
{
// Loading in double prec. number, sometimes can be useful
   if(fCoords != 0) {
      for(Int_t i=0; i<fDim; i++)
         fCoords[i] = x;
   }
   return *this;
}
//////////////////////////////////////////////////////////////////////////////
//                          OTHER METHODS                                   //
//////////////////////////////////////////////////////////////////////////////

//_____________________________________________________________________________
void TFoamVect::Print(Option_t *option) const
{
// Printout of all vector components on "cout"
   if(!option) Error("Print ", "No option set \n");
   Int_t i;
   cout << "(";
   for(i=0; i<fDim-1; i++) cout  << SW2 << *(fCoords+i) << ",";
   cout  << SW2 << *(fCoords+fDim-1);
   cout << ")";
}
//______________________________________________________________________________
void TFoamVect::PrintList(void)
{
// Printout of all member vectors in the list starting from "this"
   Long_t i=0;
   if(this == 0) return;
   TFoamVect *current=this;
   while(current != 0) {
      cout<<"vec["<<i<<"]=";
      current->Print("1");
      cout<<endl;
      current = current->fNext;
      i++;
   }
}

///////////////////////////////////////////////////////////////////////////////
//                End of Class TFoamVect                                        //
///////////////////////////////////////////////////////////////////////////////
 TFoamVect.cxx:1
 TFoamVect.cxx:2
 TFoamVect.cxx:3
 TFoamVect.cxx:4
 TFoamVect.cxx:5
 TFoamVect.cxx:6
 TFoamVect.cxx:7
 TFoamVect.cxx:8
 TFoamVect.cxx:9
 TFoamVect.cxx:10
 TFoamVect.cxx:11
 TFoamVect.cxx:12
 TFoamVect.cxx:13
 TFoamVect.cxx:14
 TFoamVect.cxx:15
 TFoamVect.cxx:16
 TFoamVect.cxx:17
 TFoamVect.cxx:18
 TFoamVect.cxx:19
 TFoamVect.cxx:20
 TFoamVect.cxx:21
 TFoamVect.cxx:22
 TFoamVect.cxx:23
 TFoamVect.cxx:24
 TFoamVect.cxx:25
 TFoamVect.cxx:26
 TFoamVect.cxx:27
 TFoamVect.cxx:28
 TFoamVect.cxx:29
 TFoamVect.cxx:30
 TFoamVect.cxx:31
 TFoamVect.cxx:32
 TFoamVect.cxx:33
 TFoamVect.cxx:34
 TFoamVect.cxx:35
 TFoamVect.cxx:36
 TFoamVect.cxx:37
 TFoamVect.cxx:38
 TFoamVect.cxx:39
 TFoamVect.cxx:40
 TFoamVect.cxx:41
 TFoamVect.cxx:42
 TFoamVect.cxx:43
 TFoamVect.cxx:44
 TFoamVect.cxx:45
 TFoamVect.cxx:46
 TFoamVect.cxx:47
 TFoamVect.cxx:48
 TFoamVect.cxx:49
 TFoamVect.cxx:50
 TFoamVect.cxx:51
 TFoamVect.cxx:52
 TFoamVect.cxx:53
 TFoamVect.cxx:54
 TFoamVect.cxx:55
 TFoamVect.cxx:56
 TFoamVect.cxx:57
 TFoamVect.cxx:58
 TFoamVect.cxx:59
 TFoamVect.cxx:60
 TFoamVect.cxx:61
 TFoamVect.cxx:62
 TFoamVect.cxx:63
 TFoamVect.cxx:64
 TFoamVect.cxx:65
 TFoamVect.cxx:66
 TFoamVect.cxx:67
 TFoamVect.cxx:68
 TFoamVect.cxx:69
 TFoamVect.cxx:70
 TFoamVect.cxx:71
 TFoamVect.cxx:72
 TFoamVect.cxx:73
 TFoamVect.cxx:74
 TFoamVect.cxx:75
 TFoamVect.cxx:76
 TFoamVect.cxx:77
 TFoamVect.cxx:78
 TFoamVect.cxx:79
 TFoamVect.cxx:80
 TFoamVect.cxx:81
 TFoamVect.cxx:82
 TFoamVect.cxx:83
 TFoamVect.cxx:84
 TFoamVect.cxx:85
 TFoamVect.cxx:86
 TFoamVect.cxx:87
 TFoamVect.cxx:88
 TFoamVect.cxx:89
 TFoamVect.cxx:90
 TFoamVect.cxx:91
 TFoamVect.cxx:92
 TFoamVect.cxx:93
 TFoamVect.cxx:94
 TFoamVect.cxx:95
 TFoamVect.cxx:96
 TFoamVect.cxx:97
 TFoamVect.cxx:98
 TFoamVect.cxx:99
 TFoamVect.cxx:100
 TFoamVect.cxx:101
 TFoamVect.cxx:102
 TFoamVect.cxx:103
 TFoamVect.cxx:104
 TFoamVect.cxx:105
 TFoamVect.cxx:106
 TFoamVect.cxx:107
 TFoamVect.cxx:108
 TFoamVect.cxx:109
 TFoamVect.cxx:110
 TFoamVect.cxx:111
 TFoamVect.cxx:112
 TFoamVect.cxx:113
 TFoamVect.cxx:114
 TFoamVect.cxx:115
 TFoamVect.cxx:116
 TFoamVect.cxx:117
 TFoamVect.cxx:118
 TFoamVect.cxx:119
 TFoamVect.cxx:120
 TFoamVect.cxx:121
 TFoamVect.cxx:122
 TFoamVect.cxx:123
 TFoamVect.cxx:124
 TFoamVect.cxx:125
 TFoamVect.cxx:126
 TFoamVect.cxx:127
 TFoamVect.cxx:128
 TFoamVect.cxx:129
 TFoamVect.cxx:130
 TFoamVect.cxx:131
 TFoamVect.cxx:132
 TFoamVect.cxx:133
 TFoamVect.cxx:134
 TFoamVect.cxx:135
 TFoamVect.cxx:136
 TFoamVect.cxx:137
 TFoamVect.cxx:138
 TFoamVect.cxx:139
 TFoamVect.cxx:140
 TFoamVect.cxx:141
 TFoamVect.cxx:142
 TFoamVect.cxx:143
 TFoamVect.cxx:144
 TFoamVect.cxx:145
 TFoamVect.cxx:146
 TFoamVect.cxx:147
 TFoamVect.cxx:148
 TFoamVect.cxx:149
 TFoamVect.cxx:150
 TFoamVect.cxx:151
 TFoamVect.cxx:152
 TFoamVect.cxx:153
 TFoamVect.cxx:154
 TFoamVect.cxx:155
 TFoamVect.cxx:156
 TFoamVect.cxx:157
 TFoamVect.cxx:158
 TFoamVect.cxx:159
 TFoamVect.cxx:160
 TFoamVect.cxx:161
 TFoamVect.cxx:162
 TFoamVect.cxx:163
 TFoamVect.cxx:164
 TFoamVect.cxx:165
 TFoamVect.cxx:166
 TFoamVect.cxx:167
 TFoamVect.cxx:168
 TFoamVect.cxx:169
 TFoamVect.cxx:170
 TFoamVect.cxx:171
 TFoamVect.cxx:172
 TFoamVect.cxx:173
 TFoamVect.cxx:174
 TFoamVect.cxx:175
 TFoamVect.cxx:176
 TFoamVect.cxx:177
 TFoamVect.cxx:178
 TFoamVect.cxx:179
 TFoamVect.cxx:180
 TFoamVect.cxx:181
 TFoamVect.cxx:182
 TFoamVect.cxx:183
 TFoamVect.cxx:184
 TFoamVect.cxx:185
 TFoamVect.cxx:186
 TFoamVect.cxx:187
 TFoamVect.cxx:188
 TFoamVect.cxx:189
 TFoamVect.cxx:190
 TFoamVect.cxx:191
 TFoamVect.cxx:192
 TFoamVect.cxx:193
 TFoamVect.cxx:194
 TFoamVect.cxx:195
 TFoamVect.cxx:196
 TFoamVect.cxx:197
 TFoamVect.cxx:198
 TFoamVect.cxx:199
 TFoamVect.cxx:200
 TFoamVect.cxx:201
 TFoamVect.cxx:202
 TFoamVect.cxx:203
 TFoamVect.cxx:204
 TFoamVect.cxx:205
 TFoamVect.cxx:206
 TFoamVect.cxx:207
 TFoamVect.cxx:208
 TFoamVect.cxx:209
 TFoamVect.cxx:210
 TFoamVect.cxx:211
 TFoamVect.cxx:212
 TFoamVect.cxx:213
 TFoamVect.cxx:214
 TFoamVect.cxx:215
 TFoamVect.cxx:216
 TFoamVect.cxx:217
 TFoamVect.cxx:218
 TFoamVect.cxx:219
 TFoamVect.cxx:220
 TFoamVect.cxx:221
 TFoamVect.cxx:222
 TFoamVect.cxx:223
 TFoamVect.cxx:224
 TFoamVect.cxx:225
 TFoamVect.cxx:226
 TFoamVect.cxx:227
 TFoamVect.cxx:228
 TFoamVect.cxx:229
 TFoamVect.cxx:230
 TFoamVect.cxx:231