00001
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00025 #ifndef TAO_CPPTYPE_H
00026 #define TAO_CPPTYPE_H
00027
00028 #include<iostream>
00029
00030 using namespace std;
00031
00032 enum TaoCppTypes{
00033 TC_BASE=0,
00034 TC_NUMBER=1,
00035 TC_COMPLEX=2,
00036 TC_STRING=3,
00037 TC_ARRAY=4,
00038 TC_HASH=5,
00039 TAO_PLUGIN=6,
00040
00041 TC_NUM_ARRAY=10,
00042 TC_ARRAY_BYTE=11,
00043 TC_ARRAY_SHORT=12,
00044 TC_ARRAY_INT=13,
00045 TC_ARRAY_FLOAT=14,
00046 TC_ARRAY_DOUBLE=15,
00047 TC_ARRAY_COMPLEX=16,
00048 };
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00064 class TcBase
00065 {
00066 public:
00067 TcBase(){ reserved=0; }
00068 virtual ~TcBase(){}
00069
00072 bool visited;
00073
00076 void *reserved;
00077
00079 virtual short type()const{ return TC_BASE; }
00082 virtual short rtti()const{ return TC_BASE; }
00083
00085 virtual double getValue(){ return 0; }
00087 virtual char* getChars(){ return 0; }
00089 virtual TcBase*getElement(int i){ return this; }
00090
00092 virtual void print( ostream *out=0, bool reserved=0 ){};
00093 };
00094
00095
00096
00098 class TcNumber : public TcBase
00099 {
00100 double value;
00101
00102 public:
00103 TcNumber( double val=0 ){ value=val; }
00104 ~TcNumber(){}
00105
00107 void setValue(double val){ value=val; }
00109 double getValue(){ return value; }
00110
00111 short type()const{ return TC_NUMBER; }
00112 short rtti()const{ return TC_NUMBER; }
00113 void print( ostream *out=0, bool reserved=0 ){
00114 if(!out) out=&cout;
00115 *out<<value;
00116 }
00117 };
00118
00119
00121 class TcComplex : public TcBase
00122 {
00123 double real,imag;
00124
00125 public:
00126 TcComplex( double rl=0, double ig=0 ){ real=rl; imag=ig; }
00127 ~TcComplex(){}
00128
00130 void setValue( double rl, double ig ){ real=rl; imag=ig; }
00132 void setReal( double rl ){ real=rl; }
00134 void setImag( double ig ){ imag=ig; }
00136 double getReal(){ return real; }
00138 double getImag(){ return imag; }
00139
00140 short type()const{ return TC_COMPLEX; }
00141 short rtti()const{ return TC_COMPLEX; }
00142 void print( ostream *out=0, bool reserved=0 ){
00143 if(!out) out=&cout;
00144 *out<<real<<"+"<<imag<<"$";
00145 }
00146 };
00147
00148
00149
00151 class TcString : public TcBase
00152 {
00153 char *chars;
00154
00155 public:
00156 TcString(){ chars=(char*)calloc(0,sizeof(char)); }
00157 ~TcString(){ free(chars); }
00158
00160 void setChars(const char *ch){
00161 chars=(char*)realloc( chars, (strlen(ch)+1)*sizeof(char) );
00162 memcpy( chars, ch, (strlen(ch)+1)*sizeof(char) );
00163 }
00165 char* getChars(){ return chars; }
00166
00167 short type()const{ return TC_STRING; }
00168 short rtti()const{ return TC_STRING; }
00169 void print( ostream *out=0, bool reserved=0 ){
00170 if(!out) out=&cout;
00171 if(reserved) *out<<"\"";
00172 *out<<chars;
00173 if(reserved) *out<<"\"";
00174 }
00175 };
00176
00177
00178
00180 class TcArray : public TcBase
00181 {
00182 bool printed;
00183 int lenArray;
00184
00185 int numElem;
00186 TcBase **elements;
00187
00188 public:
00189 TcArray(){ lenArray=numElem=0; elements=0; printed=0; }
00194 ~TcArray(){ free( elements ); }
00195
00196 short type()const{ return TC_ARRAY; }
00197 short rtti()const{ return TC_ARRAY; }
00198
00200 int size()const{ return numElem; }
00201
00203 TcBase* getElement(int i){
00204 if( i<0 || i>=numElem ) return 0;
00205 return elements[i];
00206 }
00207
00209 void insert( TcBase *tbase, int id=-1 ){
00210 if( numElem+2 > lenArray ){
00211 lenArray += 100;
00212 elements=(TcBase**)realloc( elements, lenArray*sizeof(TcBase*) );
00213 }
00214 if( id<0 )
00215 elements[ numElem ]=tbase;
00216 else{
00217 for(int i=numElem; i>id; i-- )
00218 elements[i]=elements[i-1];
00219 elements[id]=tbase;
00220 }
00221 numElem++;
00222 }
00226 TcBase* replace( TcBase *tbase, int id ){
00227 if( id<0 || id>=numElem ) return 0;
00228 TcBase* tmp=elements[id];
00229 elements[id]=tbase;
00230 return tmp;
00231 }
00232 void print( ostream *out=0, bool reserved=0 ){
00233 if(!out) out=&cout;
00234 if( printed ){
00235
00236 *out<<"{...}";
00237 return;
00238 }
00239 printed=1;
00240 *out<<"{ ";
00241 for(int i=0; i<numElem; i++ ){
00242 elements[i]->print(out,1);
00243 if(i+1<numElem) *out<<", ";
00244 }
00245 *out<<" }";
00246 printed=0;
00247 }
00248 };
00249
00250
00251
00253 class TcHash : public TcBase
00254 {
00255 bool printed;
00256 int lenArray;
00257
00258 int numElem;
00259
00260 char **keys;
00261 TcBase **elements;
00262
00266 bool keyToIndex( const char *key, int &id ){
00267 int first=0;
00268 int last=numElem-1;
00269 id=0;
00270 while( first<=last ){
00271 int mid=(first+last)/2;
00272 int cmp=strcmp( keys[mid], key );
00273 if( cmp==0 ){
00274 id=mid;
00275 return 1;
00276 }else if( cmp<0 ){
00277 first=mid+1;
00278 id=first;
00279 }else{
00280 last=mid-1;
00281 }
00282 }
00283 return 0;
00284 }
00285
00286 public:
00287 TcHash(){ lenArray=numElem=0; printed=0; }
00292 ~TcHash(){
00293 for(int i=0; i<numElem; i++) free( keys[i] );
00294 free( elements );
00295 }
00296
00297 short type()const{ return TC_HASH; }
00298 short rtti()const{ return TC_HASH; }
00299
00301 int size()const{ return numElem; }
00302
00304 char* getKey( int i ){ return keys[i]; }
00306 TcBase* getValue( int i ){ return elements[i]; }
00308 TcBase* getValue( char *key ){
00309 int id;
00310 if( keyToIndex( key, id ) ) return elements[id];
00311 return 0;
00312 }
00313
00317 TcBase* insert( const char *key, TcBase *value ){
00318 int id;
00319 bool exists=keyToIndex( key, id );
00320 if( exists ){
00321 TcBase *p=elements[id];
00322 elements[id]=value;
00323 return p;
00324 }else{
00325 if( numElem+2 > lenArray ){
00326 lenArray += 100;
00327 keys=(char**)realloc( keys, lenArray*sizeof(char*) );
00328 elements=(TcBase**)realloc( elements, lenArray*sizeof(TcBase*) );
00329 }
00330 numElem++;
00331 for( int i=id+1; i<numElem; i++ ){
00332 keys[i]=keys[i-1];
00333 elements[i]=elements[i-1];
00334 }
00335 char *tmp=(char*)calloc( strlen(key)+1, sizeof(char) );
00336 memcpy( tmp, key, (strlen(key)+1)*sizeof(char) );
00337 keys[ id ]=tmp;
00338 elements[ id ]=value;
00339 }
00340 return 0;
00341 }
00342 void print( ostream *out=0, bool reserved=0 ){
00343 if(!out) out=&cout;
00344 if( printed ){
00345 *out<<"{...}";
00346 return;
00347 }
00348 printed=1;
00349 *out<<"{ ";
00350 for(int i=0;i<numElem;i++){
00351 *out<<"\""<<keys[i]<<"\"=>";
00352 elements[i]->print(out,1);
00353 if(i+1<numElem) *out<<", ";
00354 }
00355 *out<<" }";
00356 printed=0;
00357 }
00358 };
00359
00360
00361
00364
00366 class TcNumArray : public TcBase
00367 {
00368 protected:
00369
00371 int ndim;
00372
00374 int *dims;
00375
00377 int *dimAccum;
00378
00380 void updateDimAccum(){
00381 dimAccum=(int*)realloc( dimAccum, ndim*sizeof(int) );
00382 for(int j=0; j<ndim; j++) dimAccum[j]=1;
00383 for(int i=ndim-2; i>=0; i--)
00384 dimAccum[i]=dimAccum[i+1]*dims[i+1];
00385 }
00387 void setShape( int *ds, int nd ){
00388 free(dims);
00389 dims=ds;
00390 ndim=nd;
00391 updateDimAccum();
00392 }
00393
00395 virtual void resizeVector(int nnew,int nold){};
00396
00397 virtual void printElement( int i, ostream *out ){};
00398
00399 public:
00400 TcNumArray(){
00401 ndim=1;
00402 dims=(int*)malloc(sizeof(int));
00403 dimAccum=(int*)malloc(sizeof(int));
00404 dims[0]=0;
00405 dimAccum[0]=1;
00406 }
00407 virtual ~TcNumArray(){
00408 free(dims);
00409 free(dimAccum);
00410 }
00411
00413 int size()const{ return dimAccum[0]*dims[0]; }
00415 int dim()const{ return ndim; }
00417 int dim(int i)const{ return dims[i]; }
00419 int* getDims(){ return dims; }
00420
00423 int getFlatIndex( int *index ){
00424 int id=0;
00425 for(int i=0;i<ndim;i++) id +=index[i] * dimAccum[i];
00426 return id;
00427 }
00430 void getMulDimIndex( int id, int *index ){
00431 for(int i=ndim-1; i>=0; i-- ){
00432 index[i] = id % dims[i];
00433 id /= dims[i];
00434 }
00435 }
00436
00438 void resize(int nnew){
00439 int nold=size();
00440 dims=(int*)realloc( dims, 2*sizeof(int) );
00441 dims[0]=1;
00442 dims[1]=nnew;
00443 ndim=2;
00444 updateDimAccum();
00445 if( nnew==nold ) return;
00446 resizeVector( nnew, nold );
00447 }
00450 void resize(int *ds, int nd){
00451 if( nd==1 ){
00452 resize( ds[0] );
00453 return;
00454 }
00455 int nold=size();
00456 if( ndim != nd ){
00457 ndim = nd;
00458 dims = (int*)realloc( dims, ndim*sizeof(int) );
00459 }
00460 for(int i=0;i<ndim;i++) dims[i]=ds[i];
00461 updateDimAccum();
00462 if( size()==nold ) return;
00463 resizeVector( size(), nold );
00464 }
00466 void reshape( int *ds, int nd ){
00467 int nnew=1;
00468 for(int i=0;i<nd;i++) nnew*=ds[i];
00469 if( nnew != size() ){
00470 cout<<"Warning: improper reshaping\n";
00471 return;
00472 }
00473
00474 if( ndim != nd ){
00475 ndim = nd;
00476 dims = (int*)realloc( dims, ndim*sizeof(int) );
00477 }
00478 for(int j=0;j<ndim;j++) dims[j]=ds[j];
00479 updateDimAccum();
00480 }
00481
00482 short type()const{ return TC_NUM_ARRAY; }
00483 virtual short rtti()const{ return TC_NUM_ARRAY; }
00484
00485 void print( ostream *out=0, bool reserved=0 ){
00486 if( ndim<2 ) return;
00487 int i;
00488 if(!out) out=&cout;
00489 if( ndim==2 && (dims[0]==1 || dims[1]==1) ){
00490
00491 *out<<"[ ";
00492 for( i=0; i<size(); i++ ){
00493 printElement(i,out);
00494 if( i+1<size() ) *out<<", ";
00495 }
00496 if( dims[1]==1 )
00497 *out<<" (T)]";
00498 else
00499 *out<<" ]";
00500 return;
00501 }
00502 int *tmp=(int*)calloc( ndim, sizeof(int) );
00503 for( i=0; i<size(); i++ ){
00504 int mod=i;
00505 for(int j=(int)ndim-1; j>=0; j-- ){
00506 int res = mod % dims[j];
00507 mod /= dims[j];
00508 tmp[ j ]=res;
00509 }
00510 if( tmp[ndim-1]==0 ){
00511 *out<<"row(";
00512 for(int j=0; j+1<ndim; j++ ) *out<<tmp[j]<<",";
00513 *out<<":):\t";
00514 }
00515 printElement(i,out);
00516 if( i+1<size() ) *out<<"\t";
00517 if( tmp[ndim-1]==(dims[ndim-1]-1) ) *out<<"\n";
00518 }
00519 free(tmp);
00520 }
00521 };
00522
00523
00524
00525 typedef unsigned char Byte;
00527 class TcByteArray : public TcNumArray
00528 {
00529 Byte *values;
00530 Byte **ppters;
00531
00532 void resizeVector(int nnew,int nold){
00533 values=(Byte*)realloc( values, nnew*sizeof(Byte) );
00534 for(int i=nold;i<nnew;i++) values[i]=0;
00535 }
00536
00537 void printElement( int i, ostream *out ){ *out<<(short)values[i]; }
00538
00539 public:
00540 TcByteArray(){ values=NULL; ppters=NULL; };
00545 ~TcByteArray(){ free(ppters); }
00546
00548 Byte& element( int id ){ return values[id]; }
00550 Byte& element( int *index ){ return values[ getFlatIndex(index) ]; }
00551
00553 Byte* getAsVector(){ return values; }
00555 Byte**getAsMatrix( int nR=-1 ){
00556 if( nR<=0 || size()%nR!=0 ) nR = dims[0];
00557 int nC=size()/nR;
00558 ppters=(Byte**)realloc( ppters, nR *sizeof(Byte*) );
00559 for(int i=0;i<nR;i++) ppters[i] = &values[ i*nC ];
00560 return ppters;
00561 }
00562
00567 void setByteArray(Byte *vals, int *dm, int nd){
00568 free( values );
00569 values=vals;
00570 setShape( dm, nd );
00571 }
00572
00573 short rtti()const{ return TC_ARRAY_BYTE; }
00574 };
00576 class TcShortArray : public TcNumArray
00577 {
00578 short *values;
00579 short **ppters;
00580
00581 void resizeVector(int nnew,int nold){
00582 values=(short*)realloc( values, nnew*sizeof(short) );
00583 for(int i=nold;i<nnew;i++) values[i]=0;
00584 }
00585
00586 void printElement( int i, ostream *out ){ *out<<values[i]; }
00587
00588 public:
00589 TcShortArray(){ values=NULL; ppters=NULL; };
00592 ~TcShortArray(){ free(ppters); }
00593
00595 short& element( int id ){ return values[id]; }
00597 short& element( int *index ){ return values[ getFlatIndex(index) ]; }
00598
00600 short* getAsVector(){ return values; }
00602 short**getAsMatrix( int nR=-1 ){
00603 if( nR<=0 || size()%nR!=0 ) nR = dims[0];
00604 int nC=size()/nR;
00605 ppters=(short**)realloc( ppters, nR *sizeof(short*) );
00606 for(int i=0;i<nR;i++) ppters[i] = &values[ i*nC ];
00607 return ppters;
00608 }
00609
00611 void setShortArray(short *vals, int *dm, int nd){
00612 free( values );
00613 values=vals;
00614 setShape( dm, nd );
00615 }
00616
00617 short rtti()const{ return TC_ARRAY_SHORT; }
00618 };
00620 class TcIntArray : public TcNumArray
00621 {
00622 int *values;
00623 int **ppters;
00624
00625 void resizeVector(int nnew,int nold){
00626 values=(int*)realloc( values, nnew*sizeof(int) );
00627 for(int i=nold;i<nnew;i++) values[i]=0;
00628 }
00629
00630 void printElement( int i, ostream *out ){ *out<<values[i]; }
00631
00632 public:
00633 TcIntArray(){ values=NULL; ppters=NULL; };
00636 ~TcIntArray(){ free(ppters); }
00637
00639 int& element( int id ){ return values[id]; }
00641 int& element( int *index ){ return values[ getFlatIndex(index) ]; }
00642
00644 int* getAsVector(){ return values; }
00646 int**getAsMatrix( int nR=-1 ){
00647 if( nR<=0 || size()%nR!=0 ) nR = dims[0];
00648 int nC=size()/nR;
00649 ppters=(int**)realloc( ppters, nR *sizeof(int*) );
00650 for(int i=0;i<nR;i++) ppters[i] = &values[ i*nC ];
00651 return ppters;
00652 }
00653
00655 void setIntArray(int *vals, int *dm, int nd){
00656 free( values );
00657 values=vals;
00658 setShape( dm, nd );
00659 }
00660
00661 short rtti()const{ return TC_ARRAY_INT; }
00662 };
00664 class TcFloatArray : public TcNumArray
00665 {
00666 float *values;
00667 float **ppters;
00668
00669 void resizeVector(int nnew,int nold){
00670 values=(float*)realloc( values, nnew*sizeof(float) );
00671 for(int i=nold;i<nnew;i++) values[i]=0;
00672 }
00673
00674 void printElement( int i, ostream *out=0 ){ *out<<values[i]; }
00675
00676 public:
00677 TcFloatArray(){ values=NULL; ppters=NULL; };
00680 ~TcFloatArray(){ free(ppters); }
00681
00683 float& element( int id ){ return values[id]; }
00685 float& element( int *index ){ return values[ getFlatIndex(index) ]; }
00686
00688 float* getAsVector(){ return values; }
00690 float**getAsMatrix( int nR=-1 ){
00691 if( nR<=0 || size()%nR!=0 ) nR = dims[0];
00692 int nC=size()/nR;
00693 ppters=(float**)realloc( ppters, nR *sizeof(float*) );
00694 for(int i=0;i<nR;i++) ppters[i] = &values[ i*nC ];
00695 return ppters;
00696 }
00697
00699 void setFloatArray(float *vals, int *dm, int nd){
00700 free( values );
00701 values=vals;
00702 setShape( dm, nd );
00703 }
00704
00705 short rtti()const{ return TC_ARRAY_FLOAT; }
00706 };
00708 class TcDoubleArray : public TcNumArray
00709 {
00710 double *values;
00711 double **ppters;
00712
00713 void resizeVector(int nnew,int nold){
00714 values=(double*)realloc( values, nnew*sizeof(double) );
00715 for(int i=nold;i<nnew;i++) values[i]=0;
00716 }
00717
00718 void printElement( int i, ostream *out=0 ){ *out<<values[i]; }
00719
00720 public:
00721 TcDoubleArray(){ values=NULL; ppters=NULL; };
00724 ~TcDoubleArray(){ free(ppters); }
00725
00727 double& element( int id ){ return values[id]; }
00729 double& element( int *index ){ return values[ getFlatIndex(index) ]; }
00730
00732 double* getAsVector(){ return values; }
00734 double**getAsMatrix( int nR=-1 ){
00735 if( nR<=0 || size()%nR!=0 ) nR = dims[0];
00736 int nC=size()/nR;
00737 ppters=(double**)realloc( ppters, nR *sizeof(double*) );
00738 for(int i=0;i<nR;i++) ppters[i] = &values[ i*nC ];
00739 return ppters;
00740 }
00741
00743 void setDoubleArray(double *vals, int *dm, int nd){
00744 free( values );
00745 values=vals;
00746 setShape( dm, nd );
00747 }
00748
00749 short rtti()const{ return TC_ARRAY_DOUBLE; }
00750 };
00751
00753 class TcComplexArray : public TcNumArray
00754 {
00755 double *reals;
00756 double *imags;
00757 double **ppters;
00758
00759 void resizeVector(int nnew,int nold){
00760 reals=(double*)realloc( reals, nnew*sizeof(double) );
00761 imags=(double*)realloc( imags, nnew*sizeof(double) );
00762 for(int i=nold;i<nnew;i++) reals[i]=0;
00763 for(int i=nold;i<nnew;i++) imags[i]=0;
00764 }
00765
00766 void printElement(int i, ostream *out=0){*out<<reals[i]<<"+"<<imags[i]<<"$"; }
00767
00768 public:
00769 TcComplexArray(){ reals=imags=NULL; ppters=NULL; };
00770 ~TcComplexArray(){ free(reals); free(imags); free(ppters); }
00771
00772 double& real( int id ){ return reals[id]; }
00773 double& imag( int id ){ return imags[id]; }
00774 double& real( int *index ){ return reals[ getFlatIndex(index) ]; }
00775 double& imag( int *index ){ return imags[ getFlatIndex(index) ]; }
00776
00778 double* getAsVector( bool r=1 ){ if(r) return reals; else return imags; }
00780 double**getAsMatrix( int nR=-1, bool getreal=1 ){
00781 if( nR<=0 || size()%nR!=0 ) nR = dims[0];
00782 int nC=size()/nR;
00783 ppters=(double**)realloc( ppters, nR *sizeof(double*) );
00784 if( getreal )
00785 for(int i=0;i<nR;i++) ppters[i] = &reals[ i*nC ];
00786 else
00787 for(int i=0;i<nR;i++) ppters[i] = &imags[ i*nC ];
00788 return ppters;
00789 }
00790
00791 short rtti()const{ return TC_ARRAY_COMPLEX; }
00792 };
00793
00794 #endif