P
Ja klar. Hier ist der Code (habe die Klasse ohne spezielle Zusatzfunktionen kopiert, sonst wäre es doch etwas zu lang...). Unten ist noch eine Beispiel .cpp File angegeben, das erklärt wie man sie benutzt... thy & have fun!
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
//---------------------------------------------------------------------
// LATTICE: by psi2@freesurf.ch
//---------------------------------------------------------------------
template <class T> class lattice{
private:
vector<T> L; //the lattice
long N_SITES; //number of sites of the lattice
public:
const int D; //dimensions
const int N; //size of the lattice
lattice(const int,const int); //constructor
void store(const vector<int>&, T); //store a value of type T at position (pos definded by vector, i,j,k,...)
T get(const vector<int>&); //get a value of type T at position (pos defined by vector, i,j,k,..)
long index(const vector<int>&); //returns the index of the lattice vector in function of i,j,k,...
void run_tl(const vector<T>&, lattice<T>&, const T (*func)(const vector<T>&, const vector<T>&, lattice<T>&));
//run through lattice with pointer to function to do
};
//---------------------------------------------------------------------
// CONSTRUCTOR: build of the lattice (vector)
//---------------------------------------------------------------------
template <class T> lattice<T>::lattice(const int dim, const int size) : N(size), D(dim){
int resize_L=int(pow(double(N),double(D)));
L.resize(L.size()+resize_L);
N_SITES=L.size();
}
//---------------------------------------------------------------------
// STORE: stores a value at the chosen point of type T
//---------------------------------------------------------------------
template <class T> inline void lattice<T>::store(const vector<int>& indvec, const T value){
long int_index= 0;
int_index= index(indvec);
L[int_index]=value;
}
//---------------------------------------------------------------------
// GET: returns a value at the chosen point
//---------------------------------------------------------------------
template <class T > inline T lattice<T>::get(const vector<int>& indvec){
long int_index= 0;
int_index= index(indvec);
return L[int_index];
}
//---------------------------------------------------------------------
// INDEX: returns the vector index (long int) due to matrix indizes i,j,k,..
//---------------------------------------------------------------------
template <class T> inline long lattice<T>::index(const vector<int>& indvec){
long int_index=0;
int N_pow=1; // for generating 1,N,N^2, ...
for(int i=0;i<D;i++){
int_index+=indvec[i]*N_pow; // a(i,j,k,l,...)=i + N*j + N^2*k + N^3*l
N_pow*=N; // generates: 1, N, N^2, ...
}
return int_index;
}
//---------------------------------------------------------------------
// RUN_TL: run through the lattice
//---------------------------------------------------------------------
template <class T> inline void lattice<T>::run_tl(const vector<T>& pars, lattice<T>& l, const T (*func)(const vector<T>&, const vector<T>&, lattice<T>&)){
vector<int> ind(D);;
for(long cnt=0;cnt<N_SITES;++cnt){
if(ind[0]!=0 && ind[0]%(N)==0){
ind[0]=0;
for(int t=1;t<D;t++){
if(ind[t]!=0 && ind[t]%(N-1)==0){
ind[t]=0;
}
else {ind[t]++; break;}
}
}
// statement starts here
func(pars,ind,l);
// statement stops here
ind[0]=ind[0]+1;
}
}
// -------------------------------------------------------
// AN EASY EXAMPLE HOW TO USE THE HEADER
// #include</home/dani/data/ethz/st/landau/p_lattice.h>
// const int fill(const vector<int>& pars, const vector<int>& ind, lattice<int>& lat){
// lat.store(ind,pars[0]+pars[1]+pars[2]); // store the sum over all parameters...
// }
//
//
// int main(){
// int size=20;
// int dim=5;
// // Initializes the Lattice
// lattice<int> lat(dim,size);
// // Build a Index Vector for 5 Dim
// vector<int> index(dim); // i,j,k,l,m, for 5 dimensions
// index[0]=3; // i=3
// index[1]=13; // j=13
// index[2]=5; // k=5 etc...
// index[3]=17;
// index[4]=8;
//
// lat.store(index,25); // stores the value 25 at the point 3,13,4,5,17,8
// cout << lat.get(index) << '\n'; // gets the value
//
//
// // ADVANCED ;-)
// // this is a run through the whole lattice (run_tl) applying the function fill which is defined above... fill
// // In other words apply at each point of the lattice the function fill!
// // the number of parameters you need can be defined by the size of the vector pars. see below...
//
// const int (*ptfun)(const vector<int>&, const vector<int>&, lattice<int>& );
// ptfun = &fill;
// vector<int> pars(3);
// pars[0]=3; pars[1]=2; pars[2]=5; // arbitrary parameters
// lat.run_tl(pars,lat,ptfun);
//
// cout << lat.get(index) << '\n';
// cout << "Total Number of Sites = " << "N^D= " << lat.N << "^" << lat.D << "= " << int(pow(double(lat.N),double(lat.D))) << "!!! ;-)" << '\n';
//
//
//
// }