00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef EOSLIB_VAR_H
00024 #define EOSLIB_VAR_H
00025
00026 #include <list>
00027 #include <string>
00028 #include <vector>
00029 #include <map>
00030 #include "eoslib_defs.h"
00031 #include "eoslib_rc.h"
00032 #include "eoslib_err.h"
00033
00034 namespace eoslib
00035 {
00036
00037 class dim;
00038
00040
00043 class var: public renamable
00044 {
00045 public:
00047 var(group *g, const std::string& name);
00048 var(group *g, const std::string& name, const std::string& orig_name);
00050 virtual ~var();
00051
00053 std::list<dim*>& get_dims() {return m_dims;}
00055 const std::list<dim*>& get_dims_c() const {return m_dims;}
00057 bool has_dim_of(dim *d) const;
00058
00059 std::vector<int32> get_dim_sizes() const;
00060
00062 std::list<attr*>& get_attrs() {return m_attrs;}
00064 const std::list<attr*>& get_attrs_c() const {return m_attrs;}
00065
00067 attr* get_attr_by_name(const std::string& name);
00068
00070
00079 void set_cv();
00080
00082 group* get_group() const {return m_group;}
00083
00085 virtual value_type_t get_type() const = 0;
00086
00088 virtual void get_value(int32 start[], int32 stride[], int32 edge[], void *buf) const = 0;
00089
00091 void get_all_values(std::vector<char> *pbuf) const;
00092
00094 unsigned int get_tot_num_elements() const;
00095
00097 unsigned int get_tot_num_bytes() const;
00098
00100 unsigned int get_num_bytes(int32) const;
00101
00106 void get_all(void **pbuf);
00107
00109 std::string get_origname() const {return var_orig_name;}
00110
00112
00116 template<typename T> T get_item_at(const void *buf, const std::vector<int32>& pos)
00117 {
00118 const T *b = (const T*)buf;
00119 int32 m = 1, next_m = 1, p = 0;
00120 int i;
00121
00122 const std::list<dim*>& dims = get_dims_c();
00123 std::list<dim*>::const_reverse_iterator it = dims.rbegin();
00124
00125 if(pos.size() != dims.size())
00126 throw std::runtime_error("Dimension of pos is not compatible with the current variable. " "(" __FILE__ ":" TOSTRING(__LINE__)")" );
00127
00128 for(i=pos.size()-1; i>=0; i--)
00129 {
00130 m = next_m;
00131 next_m = next_m * (*it)->get_size();
00132 p += m * pos[i];
00133 it++;
00134 }
00135 return b[p];
00136 }
00137
00139 void dump_r(int sp, bool bDumpValues=false) const;
00140
00142 void add_attr(attr* a);
00144 void remove_attr(attr *a);
00145
00147 bool is_cv(group* g);
00148
00160 var *clone_r(group *g, const std::map<dim*, dim*>& dimmap) const;
00161
00173 bool is_equivalent_to(var *v) const;
00174
00182 virtual bool same_obj_test(var* v) const = 0;
00183
00185 void replace_all_dims(const std::map<dim*, dim*>& dimmap);
00186
00188
00190
00192 static std::string s_get_name(const var*v);
00194 static bool s_same_obj_test(var *v1, var *v2);
00196 static bool s_same_objlist_test(const std::list<var *>& l1, const std::list<var *>& l2);
00198 static int32 INDEX_nD_TO_1D(const std::vector<int32>& dims, const std::vector<int32>& pos);
00199 static int32 INDEX_nD_TO_1D(int32 rank, const int32 dim[], const int32 pos[]);
00200
00202
00211 template<typename T> static int subset(
00212 const T input[],
00213 int32 rank,
00214 const int32 dim[],
00215 const int32 start[],
00216 const int32 stride[],
00217 const int32 edge[],
00218 std::vector<T> *poutput)
00219 {
00220 std::vector<int32> pos(&start[0], &start[rank]);
00221 while(1)
00222 {
00223 poutput->push_back(input[INDEX_nD_TO_1D(rank, dim, &pos[0])]);
00224
00225
00226 ssize_t j = -1;
00227 for(ssize_t i=0; i<rank; i++)
00228 {
00229 if(pos[i] + stride[i] < edge[i])
00230 j = i;
00231 }
00232 if(j == -1)
00233 break;
00234
00235 pos[j] += stride[j];
00236 for(ssize_t i = j+1; i<rank; i++)
00237 pos[i] = start[i];
00238 }
00239 return 0;
00240 }
00241
00243
00253 template<typename T> static int subset2(
00254 const T input[],
00255 int32 rank,
00256 int32 dim[],
00257 int32 start[],
00258 int32 stride[],
00259 int32 edge[],
00260 std::vector<T> *poutput,
00261 int32 pos[],
00262 int index)
00263 {
00264 for(int k=0; k<edge[index]; k++)
00265 {
00266 pos[index] = start[index] + k*stride[index];
00267 if(index+1<rank)
00268 subset2(input, rank, dim, start, stride, edge, poutput,pos,index+1);
00269 if(index==rank-1)
00270 {
00271 poutput->push_back(input[INDEX_nD_TO_1D(rank, dim, &pos[0])]);
00272 }
00273 }
00274 return 0;
00275 }
00276
00277 protected:
00278 var(const var& r);
00284 void _clone_attrs();
00285
00294 virtual var *_clone() const = 0;
00295
00296 group *m_group;
00297
00298
00299 std::list<dim*> m_dims;
00300
00301 std::list<attr*> m_attrs;
00302
00303 std::string var_orig_name;
00304
00305 private:
00306 var();
00307 };
00308 }
00309 #endif
00310