casa
$Rev:20696$
|
00001 #ifndef __casac_array_h__ 00002 #define __casac_array_h__ 00003 00004 #include <vector> 00005 00006 namespace casac { 00007 00008 template<class t> class array { 00009 public: 00010 typedef std::vector<t> vtype; 00011 typedef std::vector<int> stype; 00012 00013 array( ) : value_(0), shape_(0) { } 00014 array( const vtype &v, const stype &s ) : value_( new vtype(v) ), 00015 shape_( new stype(s) ) { } 00016 array( vtype *v, const stype &s ) : value_(v), shape_(new stype(s)) { } 00017 vtype &vec( ) { init( ); return *value_; } 00018 const vtype &vec( ) const { init(); return *value_; } 00019 stype &shape( ) { init(); return *shape_; } 00020 const stype &shape( ) const { init(); return *shape_; } 00021 00022 void resize( const stype &shp ) { init(); if ( shape_ ) delete shape_; 00023 shape_ = new stype(shp); 00024 unsigned int size = 1; 00025 for (stype::const_iterator it=shape_->begin(); it != shape_->end(); ++it) size *= *it; 00026 value_->resize(size); } 00027 00028 void set( const vtype &v, const stype &s ) 00029 { if ( value_ ) delete value_; 00030 if ( shape_ ) delete shape_; 00031 value_ = new vtype(v); shape_ = new stype(s); } 00032 void set( vtype *v, const stype &s ) 00033 { if ( value_ ) delete value_; 00034 if ( shape_ ) delete shape_; 00035 value_ = v; shape_ = new stype(s); } 00036 00037 ~array( ) { if ( value_ ) delete value_; 00038 if ( shape_ ) delete shape_; 00039 value_ = 0; shape_ = 0; } 00040 private: 00041 void init( ) const { if ( ! value_ ) ((array<t>*)this)->value_ = new vtype( ); 00042 if ( ! shape_ ) ((array<t>*)this)->shape_ = new stype( ); } 00043 vtype *value_; 00044 stype *shape_; 00045 }; 00046 00047 } 00048 00049 #endif