1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
//array.hpp
//This header defines the struct "array" which allows for the creation
//of dynamic arrays with any given dimension or type.
#pragma once
//#include <algorithm>
#include <cstdarg>
#include <cstring>
#include <cstdio>
#define MIN(x,y) (x>y?y:x)
enum array_error{
none,
no_dimensions,
out_of_bounds};
template<typename T>
struct array{
private:
unsigned num_dimensions;//number of dimensions
unsigned data_size;//size of data (in elements)
int* dimensions;//array of dimension sizes
T* data;//the data
T* change_dim(unsigned ndim,va_list args){
int size=1;
unsigned a;
int* ndata;
delete[] dimensions;
dimensions=new int[ndim];
for(a=0;a<ndim;++a){
dimensions[a]=va_arg(args,int);
size*=dimensions[a];}
ndata=new T[size];
data_size=size;
return ndata;}
public:
array_error error;//any errors in element accessing or allocation
//accesor methods
inline int num_dim(){
return num_dimensions;}
inline const int* dim_size(int x){
return dimensions[x];}
//use to get data directly from data array
inline T& operator[](int x){
return data[x];}
//constructor
//array(number of dimensions,<dimension sizes>...)
array(int ndim,...){
num_dimensions=ndim;
error=none;
if(ndim==0){
error=no_dimensions;
dimensions=NULL;
data=NULL;
data_size=0;}
else{
dimensions=new int[ndim];
va_list args;
va_start(args,ndim);
int 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()(int x,...){
int temp,pos=x,dim=1;
unsigned a=0;
va_list args;
va_start(args,x);
for(;a<num_dimensions;++a){
if((temp=va_arg(args,int))>dimensions[a]){
error=out_of_bounds;
return data[0];}
dim*=dimensions[a];
pos+=temp*dim;}
va_end(args);
return data[pos];}
//resizes the array, even with different dimensions
//resize(number of dimensions (-1 for current),<dimension sizes>...)
int resize(int ndim,...){
//fix parameters
if(ndim==0){
error=no_dimensions;
return 1;}
if(ndim==-1){
ndim=num_dimensions;}
//variables
int a,w=dimensions[0];
unsigned size=1;
T* ndata;
va_list args;
va_start(args,ndim);
//change dimensions
delete[] dimensions;
dimensions=new int[ndim];
for(a=0;a<ndim;++a){
dimensions[a]=va_arg(args,int);
size*=dimensions[a];}
ndata=new T[size];
data_size=size;
//copy old data to new buffer
for(a=0;a<MIN((signed)num_dimensions,ndim);++a){
memcpy(ndata+dimensions[0],data+w,MIN(w,dimensions[0]));}
delete[] data;
data=ndata;
num_dimensions=ndim;
return 0;}
//save the array to a file
void save(char* filename){
FILE* file=fopen(filename,"w");
int size=datasize;
fwrite(&size,4,1,file);
fwrite(&num_dimensions,4,1,file);
fwrite(dimensions,4,num_dimensions,file);
fwrite(data,sizeof(T),size,file);
fclose(file);}
//load an array from the file (deletes all old data)
void load(char* filename){
FILE* file=fopen(filename,"r");
int size;
fread(&size,4,1,file);
fread(&num_dimensions,4,1,file);
delete[] dimensions;
dimensions=new int[num_dimensions];
fread(dimensions,4,num_dimensions,file);
delete[] data;
data=new T[size];
fread(data,sizeof(T),size,file);
fclose(file);}
//returns the size in bytes of the contained data
inline int datasize(){
return data_size*sizeof(T);}
//returns the number of elements contained
inline int size(){
return data_size;}};
| |