.uppdev: Dim

git-svn-id: svn://ultimatepp.org/upp/trunk@7759 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2014-10-06 08:12:10 +00:00
parent 4dc1bfa4a3
commit 70810f4345
8 changed files with 558 additions and 0 deletions

130
uppdev/Dim/Dim.cpp Normal file
View file

@ -0,0 +1,130 @@
#include <Core/Core.h>
using namespace Upp;
#include "Dim2.h"
#include "Dim3.h"
#include "Dim4.h"
#include "Dim5.h"
#include "Dim.hpp"
int gi;
int hi;
void Foo() {
gi++;
}
Dim2<double> Box(int n) {
RLOG("---");
Dim2<double> d2;
d2.Create(n, n);
for(int pass = 0; pass < 2; pass++) {
int q = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(pass)
ASSERT(d2[i][j] == ++q);
else
d2[i][j] = ++q;
}
return d2;
}
Dim3<double> Cube(int n) {
RLOG("---");
Dim3<double> d3;
d3.Create(n, n, n);
for(int pass = 0; pass < 2; pass++) {
int q = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
if(pass)
ASSERT(d3[i][k][j] == ++q);
else
d3[i][k][j] = ++q;
}
return d3;
}
Dim4<double> Qube(int n) {
RLOG("---");
Dim4<double> d4;
d4.Create(n, n, n, n);
for(int pass = 0; pass < 2; pass++) {
int q = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
for(int l = 0; l < n; l++)
if(pass)
ASSERT(d4[i][k][j][l] == ++q);
else
d4[i][k][j][l] = ++q;
}
return d4;
}
Dim5<double> Cube5(int n) {
RLOG("---");
Dim5<double> d5;
d5.Create(n, n, n, n, n);
for(int pass = 0; pass < 2; pass++) {
int q = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
for(int l = 0; l < n; l++)
for(int m = 0; m < n; m++)
if(pass)
ASSERT(d5[i][k][j][l][m] == ++q);
else
d5[i][k][j][l][m] = ++q;
}
return d5;
}
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_FILE|LOG_COUT);
for(int i = 0; i < 20; i++) {
RLOG(i);
Box(i);
}
for(int i = 0; i < 20; i++) {
RLOG(i);
Cube(i);
}
for(int i = 0; i < 20; i++) {
RLOG(i);
Qube(i);
}
for(int i = 0; i < 20; i++) {
RLOG(i);
Cube5(i);
}
Dim2<double> b = Box(20);
RDUMP(b[19][19]);
Dim3<double> x = Cube(20);
RDUMP(x[0][0][0]);
RLOG("---");
x = Cube(40);
RDUMP(x[10][10][10]);
const Dim3<double>& y = x;
RDUMP(y[10][10][10]);
Dim4<double> qx = Qube(20);
RDUMP(qx[19][19][19][19]);
const Dim4<double>& qy = qx;
RDUMP(qy[10][10][10][10]);
Dim5<double> fx = Cube5(10);
RDUMP(fx[9][9][9][9][9]);
const Dim5<double>& fy = fx;
RDUMP(fy[9][9][9][9][9]);
}

159
uppdev/Dim/Dim.hpp Normal file
View file

@ -0,0 +1,159 @@
template <class T>
void Dim2<T>::Create(int cx_, int cy_)
{
cx = cx_; cy = cy_;
ASSERT(cx >= 0 && cy >= 0);
if(data)
delete[] data;
data = new T[cx * cy];
}
template <class T>
T& Dim2<T>::At0(int x, int y) const
{
ASSERT(x >= 0 && y >= 0 && x < cx && y < cy);
return data[x * cy + y];
}
template <class T>
void Dim2<T>::Clear()
{
if(data)
delete[] data;
cx = cy = 0;
}
template <class T>
void Dim2<T>::Set(const Dim2& src)
{
Create(src.cx, src.cy);
Copy(data, src.data, src.data + cx * cy);
}
template <class T>
Dim2<T>& Dim2<T>::operator=(const Dim2& src)
{
if(&src == this) return *this;
Set(src);
return *this;
}
template <class T>
void Dim3<T>::Create(int cx_, int cy_, int cz_)
{
cx = cx_; cy = cy_; cz = cz_;
ASSERT(cx >= 0 && cy >= 0 && cz >= 0);
if(data)
delete[] data;
data = new T[cx * cy * cz];
}
template <class T>
T& Dim3<T>::At0(int x, int y, int z) const
{
ASSERT(x >= 0 && y >= 0 && z >= 0 && x < cx && y < cy && z < cz);
return data[cz * (x * cy + y) + z];
}
template <class T>
void Dim3<T>::Clear()
{
if(data)
delete[] data;
cx = cy = cz = 0;
}
template <class T>
void Dim3<T>::Set(const Dim3& src)
{
Create(src.cx, src.cy, src.cz);
Copy(data, src.data, src.data + cx * cy * cz);
}
template <class T>
Dim3<T>& Dim3<T>::operator=(const Dim3& src)
{
if(&src == this) return *this;
Set(src);
return *this;
}
template <class T>
void Dim4<T>::Create(int cx_, int cy_, int cz_, int cu_)
{
cx = cx_; cy = cy_; cz = cz_; cu = cu_;
ASSERT(cx >= 0 && cy >= 0 && cz >= 0 && cy >= 0);
if(data)
delete[] data;
data = new T[cx * cy * cz * cu];
}
template <class T>
T& Dim4<T>::At0(int x, int y, int z, int u) const
{
ASSERT(x >= 0 && y >= 0 && z >= 0 && u >= 0 && x < cx && y < cy && z < cz && u < cu);
return data[cu * (cz * (x * cy + y) + z) + u];
}
template <class T>
void Dim4<T>::Clear()
{
if(data)
delete[] data;
cx = cy = cz = cu = 0;
}
template <class T>
void Dim4<T>::Set(const Dim4& src)
{
Create(src.cx, src.cy, src.cz, src.cu);
Copy(data, src.data, src.data + cx * cy * cz * cu);
}
template <class T>
Dim4<T>& Dim4<T>::operator=(const Dim4& src)
{
if(&src == this) return *this;
Set(src);
return *this;
}
template <class T>
void Dim5<T>::Create(int cx_, int cy_, int cz_, int cu_, int cv_)
{
cx = cx_; cy = cy_; cz = cz_; cu = cu_; cv = cv_;
ASSERT(cx >= 0 && cy >= 0 && cz >= 0 && cu >= 0 && cv >= 0);
if(data)
delete[] data;
data = new T[cx * cy * cz * cu * cv];
}
template <class T>
T& Dim5<T>::At0(int x, int y, int z, int u, int v) const
{
ASSERT(x >= 0 && y >= 0 && z >= 0 && u >= 0 && v >= 0 && x < cx && y < cy && z < cz && u < cu && v < cv);
return data[cv * (cu * (cz * (x * cy + y) + z) + u) + v];
}
template <class T>
void Dim5<T>::Clear()
{
if(data)
delete[] data;
cx = cy = cz = cu = cv = 0;
}
template <class T>
void Dim5<T>::Set(const Dim5& src)
{
Create(src.cx, src.cy, src.cz, src.cu, src.cv);
Copy(data, src.data, src.data + cx * cy * cz * cu * cv);
}
template <class T>
Dim5<T>& Dim5<T>::operator=(const Dim5& src)
{
if(&src == this) return *this;
Set(src);
return *this;
}

14
uppdev/Dim/Dim.upp Normal file
View file

@ -0,0 +1,14 @@
uses
Core;
file
Dim2.h,
Dim3.h,
Dim.hpp,
Dim4.h,
Dim5.h,
Dim.cpp;
mainconfig
"" = "SSE2";

47
uppdev/Dim/Dim2.h Normal file
View file

@ -0,0 +1,47 @@
#ifndef _Dim_Dim2_h_
#define _Dim_Dim2_h_
template <class T> class Dim2;
template <class T, class R> class Dim2y;
template <class T, class R>
class Dim2x {
protected:
typename Dim2<T> *dim;
int x;
friend typename Dim2<T>;
public:
R& operator[](int i) { return dim->At(x, i); }
};
template <class T>
class Dim2 {
T *data;
int cx, cy;
T& At0(int x, int y) const;
public:
void Create(int cx, int cy);
T& At(int x, int y) { return At0(x, y); }
const T& At(int x, int y) const { return At0(x, y); }
T& operator()(int x, int y) { return At0(x, y); }
const T& operator()(int x, int y) const { return At0(x, y); }
void Clear();
void Set(const Dim2& src);
Dim2& operator=(const Dim2& src);
Dim2(const Dim2& src) { data = NULL; Set(src); }
Dim2() { data = NULL; }
Dim2(int cx, int cy, int cz) { data = NULL; Create(cx, cy, cz); }
~Dim2() { Clear(); }
Dim2x<T, T> operator[](int i) { Dim2x<T, T> h; h.dim = this; h.x = i; return h; }
Dim2x<T, const T> operator[](int i) const { Dim2x<T, const T> h; h.dim = const_cast<Dim2<T> *>(this); h.x = i; return h; }
};
#endif

57
uppdev/Dim/Dim3.h Normal file
View file

@ -0,0 +1,57 @@
#ifndef _Dim_Dim_h_
#define _Dim_Dim_h_
template <class T> class Dim3;
template <class T, class R> class Dim3y;
template <class T, class R>
class Dim3x {
protected:
typename Dim3<T> *dim;
int x;
friend typename Dim3<T>;
public:
Dim3y<T, R> operator[](int i) { Dim3y<T, R> h; (Dim3x<T, R>&)h = *this; h.y = i; return h; }
};
template <class T, class R>
class Dim3y : Dim3x<T, R> {
int y;
friend typename Dim3x<T, R>;
public:
R& operator[](int i) { return dim->At(x, y, i); }
};
template <class T>
class Dim3 {
T *data;
int cx, cy, cz;
T& At0(int x, int y, int z) const;
public:
void Create(int cx, int cy, int cz);
T& At(int x, int y, int z) { return At0(x, y, z); }
const T& At(int x, int y, int z) const { return At0(x, y, z); }
T& operator()(int x, int y, int z) { return At0(x, y, z); }
const T& operator()(int x, int y, int z) const { return At0(x, y, z); }
void Clear();
void Set(const Dim3& src);
Dim3& operator=(const Dim3& src);
Dim3(const Dim3& src) { data = NULL; Set(src); }
Dim3() { data = NULL; }
Dim3(int cx, int cy, int cz) { data = NULL; Create(cx, cy, cz); }
~Dim3() { Clear(); }
Dim3x<T, T> operator[](int i) { Dim3x<T, T> h; h.dim = this; h.x = i; return h; }
Dim3x<T, const T> operator[](int i) const { Dim3x<T, const T> h; h.dim = const_cast<Dim3<T> *>(this); h.x = i; return h; }
};
#endif

68
uppdev/Dim/Dim4.h Normal file
View file

@ -0,0 +1,68 @@
#ifndef _Dim_Dim4_h_
#define _Dim_Dim4_h_
template <class T> class Dim4;
template <class T, class R> class Dim4y;
template <class T, class R> class Dim4z;
template <class T, class R>
class Dim4x {
protected:
typename Dim4<T> *dim;
int x;
friend typename Dim4<T>;
friend typename Dim4z<T, R>;
public:
Dim4y<T, R> operator[](int i) { Dim4y<T, R> h; (Dim4x<T, R>&)h = *this; h.y = i; return h; }
};
template <class T, class R>
class Dim4y : protected Dim4x<T, R> {
protected:
int y;
friend typename Dim4x<T, R>;
public:
Dim4z<T, R> operator[](int i) { Dim4z<T, R> h; (Dim4y<T, R>&)h = *this; h.z = i; return h; }
};
template <class T, class R>
class Dim4z : Dim4y<T, R> {
int z;
friend typename Dim4y<T, R>;
public:
R& operator[](int i) { return dim->At(x, y, z, i); }
};
template <class T>
class Dim4 {
T *data;
int cx, cy, cz, cu;
T& At0(int x, int y, int z, int u) const;
public:
void Create(int cx, int cy, int cz, int cu);
T& At(int x, int y, int z, int u) { return At0(x, y, z, u); }
const T& At(int x, int y, int z, int u) const { return At0(x, y, z, u); }
T& operator()(int x, int y, int z, int u) { return At0(x, y, z, u); }
const T& operator()(int x, int y, int z, int u) const { return At0(x, y, z, u); }
void Clear();
void Set(const Dim4& src);
Dim4& operator=(const Dim4& src);
Dim4(const Dim4& src) { data = NULL; Set(src); }
Dim4() { data = NULL; }
Dim4(int cx, int cy, int cz, int cu) { data = NULL; Create(cx, cy, cz, cu); }
~Dim4() { Clear(); }
Dim4x<T, T> operator[](int i) { Dim4x<T, T> h; h.dim = this; h.x = i; return h; }
Dim4x<T, const T> operator[](int i) const { Dim4x<T, const T> h; h.dim = const_cast<Dim4<T> *>(this); h.x = i; return h; }
};
#endif

79
uppdev/Dim/Dim5.h Normal file
View file

@ -0,0 +1,79 @@
#ifndef _Dim_Dim5_h_
#define _Dim_Dim5_h_
template <class T> class Dim5;
template <class T, class R> class Dim5y;
template <class T, class R> class Dim5z;
template <class T, class R> class Dim5v;
template <class T, class R>
class Dim5x {
protected:
typename Dim5<T> *dim;
int x;
friend typename Dim5<T>;
friend typename Dim5v<T, R>;
public:
Dim5y<T, R> operator[](int i) { Dim5y<T, R> h; (Dim5x<T, R>&)h = *this; h.y = i; return h; }
};
template <class T, class R>
class Dim5y : protected Dim5x<T, R> {
protected:
int y;
friend typename Dim5x<T, R>;
public:
Dim5z<T, R> operator[](int i) { Dim5z<T, R> h; (Dim5y<T, R>&)h = *this; h.z = i; return h; }
};
template <class T, class R>
class Dim5z : protected Dim5y<T, R> {
protected:
int z;
friend typename Dim5y<T, R>;
public:
Dim5v<T, R> operator[](int i) { Dim5v<T, R> h; (Dim5z<T, R>&)h = *this; h.v = i; return h; }
};
template <class T, class R>
class Dim5v : protected Dim5z<T, R> {
int v;
friend typename Dim5z<T, R>;
public:
R& operator[](int i) { return dim->At(x, y, z, v, i); }
};
template <class T>
class Dim5 {
T *data;
int cx, cy, cz, cu, cv;
T& At0(int x, int y, int z, int u, int v) const;
public:
void Create(int cx, int cy, int cz, int cu, int cv);
T& At(int x, int y, int z, int u, int v) { return At0(x, y, z, u, v); }
const T& At(int x, int y, int z, int u, int v) const { return At0(x, y, z, u, v); }
T& operator()(int x, int y, int z, int u, int v) { return At0(x, y, z, u, v); }
const T& operator()(int x, int y, int z, int u, int v) const { return At0(x, y, z, u, v); }
void Clear();
void Set(const Dim5& src);
Dim5& operator=(const Dim5& src);
Dim5(const Dim5& src) { data = NULL; Set(src); }
Dim5() { data = NULL; }
Dim5(int cx, int cy, int cz, int cu, int cv) { data = NULL; Create(cx, cy, cz, cu, cv); }
~Dim5() { Clear(); }
Dim5x<T, T> operator[](int i) { Dim5x<T, T> h; h.dim = this; h.x = i; return h; }
Dim5x<T, const T> operator[](int i) const { Dim5x<T, const T> h; h.dim = const_cast<Dim5<T> *>(this); h.x = i; return h; }
};
#endif

4
uppdev/Dim/init Normal file
View file

@ -0,0 +1,4 @@
#ifndef _Dim_icpp_init_stub
#define _Dim_icpp_init_stub
#include "Core/init"
#endif