Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //array.hpp
- //This header defines the struct "array" which allows for the creation
- //of dynamic arrays with any given dimension or type.
- #pragma once
- #ifndef MULTIDIM_ARRAY
- #define MULTIDIM_ARRAY
- #include <cstdarg>
- template<typename T>
- struct array{
- private:
- unsigned num_dimensions;//number of dimensions
- unsigned data_size;//size of data (in elements)
- unsigned* dimensions;//array of dimension sizes
- T* data;//the data
- //static function to be used if memcpy is not defined
- static void* memcpy(void* dst,const void* src,unsigned len) const{
- unsigned x=0;
- while(x<len){
- src[x]=dst[x];
- ++x;}
- return dst;}
- public:
- //static error variables
- static T aoob;
- static const T caoob;
- //accesor methods
- inline unsigned numdim() const{
- return num_dimensions;}
- inline unsigned dimsize(unsigned x) const{
- return dimensions[x];}
- //use to get data directly from data array
- inline T& operator[](unsigned x){
- return data[x];}
- inline const T& operator[](unsigned x) const{
- return data[x];}
- //constructor
- //no arguments
- array(){
- num_dimensions=0;
- data_size=0;
- dimensions=NULL;
- data=NULL;}
- //array(number of dimensions,<dimension sizes>...)
- array(unsigned ndim,...){
- num_dimensions=ndim;
- if(ndim==0){
- dimensions=NULL;
- data=NULL;
- data_size=0;}
- else{
- dimensions=new unsigned[ndim];
- va_list args;
- va_start(args,ndim);
- unsigned size=1;
- for(int a=0;a<ndim;++a){
- dimensions[a]=va_arg(args,T);
- size*=dimensions[a];}
- data=new T[size];
- data_size=size;
- va_end(args);}}
- //destrctor
- ~array(){
- delete[] dimensions;
- delete[] data;}
- //returns a reference to the element at the given position
- //operator()(x,y,z,...)
- T& operator()(unsigned x,...){
- unsigned index,pos=x,dim=1,a=0;
- va_list args;
- va_start(args,x);
- for(;a<num_dimensions;++a){
- if((index=va_arg(args,unsigned))>dimensions[a]){
- return aoob;}
- dim*=dimensions[a];
- pos+=temp*dim;}
- va_end(args);
- return data[pos];}
- const T& operator()(unsigned x,...) const{
- unsigned index,pos=x,dim=1,a=0;
- va_list args;
- va_start(args,x);
- for(;a<num_dimensions;++a){
- if((index=va_arg(args,unsigned))>dimensions[a]){
- return aoob;}
- dim*=dimensions[a];
- pos+=temp*dim;}
- va_end(args);
- return data[pos];}
- //resizes the array, even with different dimensions
- //resize(number of dimensions (0 for current),<dimension sizes>...)
- //TODO: fix this function so that it keeps old data
- unsigned resize(unsigned ndim,...){
- //fix parameters
- if(ndim==0){
- ndim=num_dimensions;}
- //variables
- unsigned a,b,size=1,w=dimensions[0];
- T* ndata;
- va_list args;
- va_start(args,ndim);
- //change dimensions
- delete[] dimensions;
- dimensions=new unsigned[ndim];
- for(a=0;a<ndim;++a){
- dimensions[a]=va_arg(args,unsigned);
- size*=dimensions[a];}
- ndata=new T[size];
- //copy old data to new buffer
- unsigned mindim=(num_dimensions<ndim?num_dimensions:ndim);
- unsigned mindimsize;
- for(a=0;a<mindim;++a){
- mindimsize=(dimensions[a]<ndim[a]?dimensions[a]:ndim[a]);
- for(b=0;b<mindimsize;++b){
- memcpy(ndata+
- delete[] data;
- data=ndata;
- num_dimensions=ndim;
- return 0;}
- //returns the size in bytes of the contained data
- inline int datasize() const{
- return data_size*sizeof(T);}
- //returns the number of elements contained
- inline int size() const{
- return data_size;}};
- #endif
Advertisement
Add Comment
Please, Sign In to add comment