casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
array.h
Go to the documentation of this file.
1 #ifndef __casac_array_h__
2 #define __casac_array_h__
3 
4 #include <vector>
5 
6 namespace casac {
7 
8 template<class t> class array {
9  public:
10  typedef std::vector<t> vtype;
11  typedef std::vector<int> stype;
12 
13  array( ) : value_(0), shape_(0) { }
14  array( const vtype &v, const stype &s ) : value_( new vtype(v) ),
15  shape_( new stype(s) ) { }
16  array( vtype *v, const stype &s ) : value_(v), shape_(new stype(s)) { }
17  vtype &vec( ) { init( ); return *value_; }
18  const vtype &vec( ) const { init(); return *value_; }
19  stype &shape( ) { init(); return *shape_; }
20  const stype &shape( ) const { init(); return *shape_; }
21 
22  void resize( const stype &shp ) { init(); if ( shape_ ) delete shape_;
23  shape_ = new stype(shp);
24  unsigned int size = 1;
25  for (stype::const_iterator it=shape_->begin(); it != shape_->end(); ++it) size *= *it;
26  value_->resize(size); }
27 
28  void set( const vtype &v, const stype &s )
29  { if ( value_ ) delete value_;
30  if ( shape_ ) delete shape_;
31  value_ = new vtype(v); shape_ = new stype(s); }
32  void set( vtype *v, const stype &s )
33  { if ( value_ ) delete value_;
34  if ( shape_ ) delete shape_;
35  value_ = v; shape_ = new stype(s); }
36 
37  ~array( ) { if ( value_ ) delete value_;
38  if ( shape_ ) delete shape_;
39  value_ = 0; shape_ = 0; }
40  private:
41  void init( ) const { if ( ! value_ ) ((array<t>*)this)->value_ = new vtype( );
42  if ( ! shape_ ) ((array<t>*)this)->shape_ = new stype( ); }
45 };
46 
47 }
48 
49 #endif
void set(const vtype &v, const stype &s)
Definition: array.h:28
Elements::const_iterator const_iterator
void set(vtype *v, const stype &s)
Definition: array.h:32
array(vtype *v, const stype &s)
Definition: array.h:16
std::vector< int > stype
Definition: array.h:11
void init() const
Definition: array.h:41
size_t size() const
stype & shape()
Definition: array.h:19
vtype * value_
Definition: array.h:43
stype * shape_
Definition: array.h:44
void resize(const stype &shp)
Definition: array.h:22
~array()
Definition: array.h:37
array(const vtype &v, const stype &s)
Definition: array.h:14
vtype & vec()
Definition: array.h:17
std::vector< t > vtype
Definition: array.h:10
const vtype & vec() const
Definition: array.h:18
const stype & shape() const
Definition: array.h:20