diff --git a/uppdev/Dim/Dim.cpp b/uppdev/Dim/Dim.cpp new file mode 100644 index 000000000..9db3d18e3 --- /dev/null +++ b/uppdev/Dim/Dim.cpp @@ -0,0 +1,130 @@ +#include + +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 Box(int n) { + RLOG("---"); + Dim2 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 Cube(int n) { + RLOG("---"); + Dim3 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 Qube(int n) { + RLOG("---"); + Dim4 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 Cube5(int n) { + RLOG("---"); + Dim5 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 b = Box(20); + RDUMP(b[19][19]); + + Dim3 x = Cube(20); + RDUMP(x[0][0][0]); + RLOG("---"); + x = Cube(40); + RDUMP(x[10][10][10]); + + const Dim3& y = x; + RDUMP(y[10][10][10]); + + Dim4 qx = Qube(20); + RDUMP(qx[19][19][19][19]); + const Dim4& qy = qx; + RDUMP(qy[10][10][10][10]); + + Dim5 fx = Cube5(10); + RDUMP(fx[9][9][9][9][9]); + const Dim5& fy = fx; + RDUMP(fy[9][9][9][9][9]); +} diff --git a/uppdev/Dim/Dim.hpp b/uppdev/Dim/Dim.hpp new file mode 100644 index 000000000..19451a3d1 --- /dev/null +++ b/uppdev/Dim/Dim.hpp @@ -0,0 +1,159 @@ +template +void Dim2::Create(int cx_, int cy_) +{ + cx = cx_; cy = cy_; + ASSERT(cx >= 0 && cy >= 0); + if(data) + delete[] data; + data = new T[cx * cy]; +} + +template +T& Dim2::At0(int x, int y) const +{ + ASSERT(x >= 0 && y >= 0 && x < cx && y < cy); + return data[x * cy + y]; +} + +template +void Dim2::Clear() +{ + if(data) + delete[] data; + cx = cy = 0; +} + +template +void Dim2::Set(const Dim2& src) +{ + Create(src.cx, src.cy); + Copy(data, src.data, src.data + cx * cy); +} + +template +Dim2& Dim2::operator=(const Dim2& src) +{ + if(&src == this) return *this; + Set(src); + return *this; +} + +template +void Dim3::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 +T& Dim3::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 +void Dim3::Clear() +{ + if(data) + delete[] data; + cx = cy = cz = 0; +} + +template +void Dim3::Set(const Dim3& src) +{ + Create(src.cx, src.cy, src.cz); + Copy(data, src.data, src.data + cx * cy * cz); +} + +template +Dim3& Dim3::operator=(const Dim3& src) +{ + if(&src == this) return *this; + Set(src); + return *this; +} + +template +void Dim4::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 +T& Dim4::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 +void Dim4::Clear() +{ + if(data) + delete[] data; + cx = cy = cz = cu = 0; +} + +template +void Dim4::Set(const Dim4& src) +{ + Create(src.cx, src.cy, src.cz, src.cu); + Copy(data, src.data, src.data + cx * cy * cz * cu); +} + +template +Dim4& Dim4::operator=(const Dim4& src) +{ + if(&src == this) return *this; + Set(src); + return *this; +} + +template +void Dim5::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 +T& Dim5::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 +void Dim5::Clear() +{ + if(data) + delete[] data; + cx = cy = cz = cu = cv = 0; +} + +template +void Dim5::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 +Dim5& Dim5::operator=(const Dim5& src) +{ + if(&src == this) return *this; + Set(src); + return *this; +} diff --git a/uppdev/Dim/Dim.upp b/uppdev/Dim/Dim.upp new file mode 100644 index 000000000..cc848aa0a --- /dev/null +++ b/uppdev/Dim/Dim.upp @@ -0,0 +1,14 @@ +uses + Core; + +file + Dim2.h, + Dim3.h, + Dim.hpp, + Dim4.h, + Dim5.h, + Dim.cpp; + +mainconfig + "" = "SSE2"; + diff --git a/uppdev/Dim/Dim2.h b/uppdev/Dim/Dim2.h new file mode 100644 index 000000000..0d1ea6909 --- /dev/null +++ b/uppdev/Dim/Dim2.h @@ -0,0 +1,47 @@ +#ifndef _Dim_Dim2_h_ +#define _Dim_Dim2_h_ + +template class Dim2; +template class Dim2y; + +template +class Dim2x { +protected: + typename Dim2 *dim; + int x; + friend typename Dim2; + +public: + R& operator[](int i) { return dim->At(x, i); } +}; + +template +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 operator[](int i) { Dim2x h; h.dim = this; h.x = i; return h; } + Dim2x operator[](int i) const { Dim2x h; h.dim = const_cast *>(this); h.x = i; return h; } +}; + +#endif diff --git a/uppdev/Dim/Dim3.h b/uppdev/Dim/Dim3.h new file mode 100644 index 000000000..1fb6dc571 --- /dev/null +++ b/uppdev/Dim/Dim3.h @@ -0,0 +1,57 @@ +#ifndef _Dim_Dim_h_ +#define _Dim_Dim_h_ + +template class Dim3; +template class Dim3y; + +template +class Dim3x { +protected: + typename Dim3 *dim; + int x; + friend typename Dim3; + +public: + Dim3y operator[](int i) { Dim3y h; (Dim3x&)h = *this; h.y = i; return h; } +}; + +template +class Dim3y : Dim3x { + int y; + friend typename Dim3x; + +public: + R& operator[](int i) { return dim->At(x, y, i); } +}; + +template +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 operator[](int i) { Dim3x h; h.dim = this; h.x = i; return h; } + Dim3x operator[](int i) const { Dim3x h; h.dim = const_cast *>(this); h.x = i; return h; } +}; + + +#endif diff --git a/uppdev/Dim/Dim4.h b/uppdev/Dim/Dim4.h new file mode 100644 index 000000000..d337895e2 --- /dev/null +++ b/uppdev/Dim/Dim4.h @@ -0,0 +1,68 @@ +#ifndef _Dim_Dim4_h_ +#define _Dim_Dim4_h_ + +template class Dim4; +template class Dim4y; +template class Dim4z; + +template +class Dim4x { +protected: + typename Dim4 *dim; + int x; + friend typename Dim4; + friend typename Dim4z; + +public: + Dim4y operator[](int i) { Dim4y h; (Dim4x&)h = *this; h.y = i; return h; } +}; + +template +class Dim4y : protected Dim4x { +protected: + int y; + friend typename Dim4x; + +public: + Dim4z operator[](int i) { Dim4z h; (Dim4y&)h = *this; h.z = i; return h; } +}; + +template +class Dim4z : Dim4y { + int z; + friend typename Dim4y; + +public: + R& operator[](int i) { return dim->At(x, y, z, i); } +}; + +template +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 operator[](int i) { Dim4x h; h.dim = this; h.x = i; return h; } + Dim4x operator[](int i) const { Dim4x h; h.dim = const_cast *>(this); h.x = i; return h; } +}; + +#endif diff --git a/uppdev/Dim/Dim5.h b/uppdev/Dim/Dim5.h new file mode 100644 index 000000000..9db524935 --- /dev/null +++ b/uppdev/Dim/Dim5.h @@ -0,0 +1,79 @@ +#ifndef _Dim_Dim5_h_ +#define _Dim_Dim5_h_ + +template class Dim5; +template class Dim5y; +template class Dim5z; +template class Dim5v; + +template +class Dim5x { +protected: + typename Dim5 *dim; + int x; + friend typename Dim5; + friend typename Dim5v; + +public: + Dim5y operator[](int i) { Dim5y h; (Dim5x&)h = *this; h.y = i; return h; } +}; + +template +class Dim5y : protected Dim5x { +protected: + int y; + friend typename Dim5x; + +public: + Dim5z operator[](int i) { Dim5z h; (Dim5y&)h = *this; h.z = i; return h; } +}; + +template +class Dim5z : protected Dim5y { +protected: + int z; + friend typename Dim5y; + +public: + Dim5v operator[](int i) { Dim5v h; (Dim5z&)h = *this; h.v = i; return h; } +}; + +template +class Dim5v : protected Dim5z { + int v; + friend typename Dim5z; + +public: + R& operator[](int i) { return dim->At(x, y, z, v, i); } +}; + +template +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 operator[](int i) { Dim5x h; h.dim = this; h.x = i; return h; } + Dim5x operator[](int i) const { Dim5x h; h.dim = const_cast *>(this); h.x = i; return h; } +}; + +#endif diff --git a/uppdev/Dim/init b/uppdev/Dim/init new file mode 100644 index 000000000..e511f5456 --- /dev/null +++ b/uppdev/Dim/init @@ -0,0 +1,4 @@ +#ifndef _Dim_icpp_init_stub +#define _Dim_icpp_init_stub +#include "Core/init" +#endif