Guest User

array.h

a guest
Dec 23rd, 2010
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. //array.hpp
  2. //This header defines the struct "array" which allows for the creation
  3. //of dynamic arrays with any given dimension or type.
  4. #pragma once
  5. #ifndef MULTIDIM_ARRAY
  6. #define MULTIDIM_ARRAY
  7.  
  8. #include <cstdarg>
  9.  
  10. template<typename T>
  11. struct array{
  12.     private:
  13.         unsigned num_dimensions;//number of dimensions
  14.         unsigned data_size;//size of data (in elements)
  15.         unsigned* dimensions;//array of dimension sizes
  16.         T* data;//the data
  17.         //static function to be used if memcpy is not defined
  18.         static void* memcpy(void* dst,const void* src,unsigned len) const{
  19.             unsigned x=0;
  20.             while(x<len){
  21.                 src[x]=dst[x];
  22.                 ++x;}
  23.             return dst;}
  24.     public:
  25.         //static error variables
  26.         static T aoob;
  27.         static const T caoob;
  28.         //accesor methods
  29.         inline unsigned numdim() const{
  30.             return num_dimensions;}
  31.         inline unsigned dimsize(unsigned x) const{
  32.             return dimensions[x];}
  33.         //use to get data directly from data array
  34.         inline T& operator[](unsigned x){
  35.             return data[x];}
  36.         inline const T& operator[](unsigned x) const{
  37.             return data[x];}
  38.         //constructor
  39.         //no arguments
  40.         array(){
  41.             num_dimensions=0;
  42.             data_size=0;
  43.             dimensions=NULL;
  44.             data=NULL;}
  45.         //array(number of dimensions,<dimension sizes>...)
  46.         array(unsigned ndim,...){
  47.             num_dimensions=ndim;
  48.             if(ndim==0){
  49.                 dimensions=NULL;
  50.                 data=NULL;
  51.                 data_size=0;}
  52.             else{
  53.                 dimensions=new unsigned[ndim];
  54.                 va_list args;
  55.                 va_start(args,ndim);
  56.                 unsigned size=1;
  57.                 for(int a=0;a<ndim;++a){
  58.                     dimensions[a]=va_arg(args,T);
  59.                     size*=dimensions[a];}
  60.                 data=new T[size];
  61.                 data_size=size;
  62.                 va_end(args);}}
  63.         //destrctor
  64.         ~array(){
  65.             delete[] dimensions;
  66.             delete[] data;}
  67.         //returns a reference to the element at the given position
  68.         //operator()(x,y,z,...)
  69.         T& operator()(unsigned x,...){
  70.             unsigned index,pos=x,dim=1,a=0;
  71.             va_list args;
  72.             va_start(args,x);
  73.             for(;a<num_dimensions;++a){
  74.                 if((index=va_arg(args,unsigned))>dimensions[a]){
  75.                     return aoob;}
  76.                 dim*=dimensions[a];
  77.                 pos+=temp*dim;}
  78.             va_end(args);
  79.             return data[pos];}
  80.         const T& operator()(unsigned x,...) const{
  81.             unsigned index,pos=x,dim=1,a=0;
  82.             va_list args;
  83.             va_start(args,x);
  84.             for(;a<num_dimensions;++a){
  85.                 if((index=va_arg(args,unsigned))>dimensions[a]){
  86.                     return aoob;}
  87.                 dim*=dimensions[a];
  88.                 pos+=temp*dim;}
  89.             va_end(args);
  90.             return data[pos];}
  91.         //resizes the array, even with different dimensions
  92.         //resize(number of dimensions (0 for current),<dimension sizes>...)
  93.         //TODO: fix this function so that it keeps old data
  94.         unsigned resize(unsigned ndim,...){
  95.             //fix parameters
  96.             if(ndim==0){
  97.                 ndim=num_dimensions;}
  98.             //variables
  99.             unsigned a,b,size=1,w=dimensions[0];
  100.             T* ndata;
  101.             va_list args;
  102.             va_start(args,ndim);
  103.             //change dimensions
  104.             delete[] dimensions;
  105.             dimensions=new unsigned[ndim];
  106.             for(a=0;a<ndim;++a){
  107.                 dimensions[a]=va_arg(args,unsigned);
  108.                 size*=dimensions[a];}
  109.             ndata=new T[size];
  110.             //copy old data to new buffer
  111.             unsigned mindim=(num_dimensions<ndim?num_dimensions:ndim);
  112.             unsigned mindimsize;
  113.             for(a=0;a<mindim;++a){
  114.                 mindimsize=(dimensions[a]<ndim[a]?dimensions[a]:ndim[a]);
  115.                 for(b=0;b<mindimsize;++b){
  116.                     memcpy(ndata+
  117.             delete[] data;
  118.             data=ndata;
  119.             num_dimensions=ndim;
  120.             return 0;}
  121.         //returns the size in bytes of the contained data
  122.         inline int datasize() const{
  123.             return data_size*sizeof(T);}
  124.         //returns the number of elements contained
  125.         inline int size() const{
  126.             return data_size;}};
  127.  
  128. #endif
Advertisement
Add Comment
Please, Sign In to add comment