Dynamic Array (one-dimensional high-dimensional array control)

Source: Internet
Author: User

A good coder always has the compile method to use 1D array to perform operations on the column,
However, when using the highly correlated columns, you can make the concept of dynamic regression in the Code clearer.
In the early C or current C ++, the sequence method configured in the dynamic sequence column is not supported,
(This is only available in c99. For example, GCC can be used to modify the dynamic sequence column)
Therefore, we need to implement the following simple practice:

You need to consider the following three points when configuring the design metric column:

1. configuration efficiency: it is best to call malloc or new only once.
2. access efficiency: the access speed must approach the primary array.
3. Meaning: the memory of the array must be divided into two values.
(Therefore, vector <vector> cannot be regarded as a real struct column,
Just an array container ...)

In the n-hour dynamic route table column, configure the dynamic route table, starting from the second round:

(1) Use the C statement:

Void ** new_arr (INT size, int X, int y) // size = element size {int SZ = sizeof (void *); void ** A = (void **) malloc (SZ * x + size * x * Y); If (! A) Return 0; char * P = (char *) A + SZ * X; while (X --) A [x] = P + size * x * Y; return A;} void del_arr (void ** A) {if (a) free (a);} void set_arr (void ** A, int size, int n ...) {va_list P; va_start (p, n); memcpy (A [0], p, size * n);} int main () {typedef struct {float F; char C;} cc; CC [4] = {0.2, 'A', 0.4, 'B', 0.6, 'C', 0.8, 'D '}; int SZ; int ** A; char *** B; CC *** C; A = (INT ***) new_arr (SZ = sizeof (INT), 2, 4 ); set_arr (void **) A, SZ, 2*4, 1, 2, 3, 4, // sort data in sequence 7, 8, 9, 0); printf ("% d ", A [1] [2]); B = (char ***) new_arr (SZ = sizeof (char *), 2, 2); set_arr (void **) B, SZ, 2*2, "comment column", "2D", // sort data in sequence "comment in", "example "); printf ("/n % s", B [0] [1]); C = (CC **) new_arr (SZ = sizeof (CC), 2, 2 ); set_arr (void **) C, SZ, 2*2, CC [0], CC [1], // sort data in sequence CC [2], CC [3]); printf ("/n % F % C", C [1] [0]. f, C [0] [0]. c); del_arr (void **) A); del_arr (void **) B); del_arr (void **) C );}

(2) Use C ++:

Template <class T> struct Array {array (int x, int y = 1): Y (Y), x (x), P (New T [x * Y]) {}~ Array () {for (x * = y; X --; P [X]. ~ T (); Delete [] P;} t * operator [] (int I) {return P + I * Y;} array <t> & operator <(t o) {P [n = 0] = O; return * This;} array <t> & operator, (t o) {P [++ N] = O; return * This;} PRIVATE: int X, Y, N; T * P ;}; int main () {struct CC {float F; char C ;} CC [4] = {0.2, 'A', 0.4, 'B', 0.6, 'C', 0.8, 'D'}; array <int> A (2, 4 ); A <, // sort data in sequence, 8, 9, 0; cout <A [1] [2]; array <char *> B ); B <"comment column", "template", // sort data in sequence "comment in", "example "; cout <Endl <B [0] [1]; array <CC> C (2, 2); C <CC [0], CC [1], // sort data in sequence CC [2], CC [3]; cout <Endl <C [1] [0]. F <"" <C [0] [0]. C ;}

According to the same concept, Zookeeper extends to N equal:

C statement Syntax: (this Code was modified by a program that was once ridiculed)

# Include <stdarg. h> void * new_arr (INT size, int Ndim, int n ...) {static int d [32] = {0}; // a maximum of 32 bytes int I, j, nptr, nelem, SZ = sizeof (void *); // It refers to void * a, ** B, ** C, ** t; char * P; // A = starting address of the dynamic sequence column va_list V; va_start (v, Ndim); for (I = nDim-1; I> = 0; I --) // retrieve the number of shards d [I] = va_arg (v, INT); If (0 = Ndim) return 0; // disable the configuration of 0 release partition column D [nDim-1] = N; // The release positive release version does not remove n nptr = d [1]; // calculate the table size of the biter. nelem = d [0] * D [1]; // The total space between (I = 2; I <Ndim; I ++) ++ nptr * = d [I], nelem * = d [I]; n = nptr * SZ + nelem * size; A = malloc (n); // configure an empty if (! A) {printf ("memory configuration loss: % d bytes", n); _ getch (); Return 0; // If the configuration fails, return 0} p = (char *) A + nptr * SZ; // P indicates the starting address of the actual primary storage point B = (void **) P-(nelem/= d [0]); // B points to C = B; I = nelem; // set the C-times cursor to the while (I --) {* C ++ = P; P + = d [0] * size;} // set the index address of the 1D table. For (I = 1; I <nDim-1; I ++) {// set 2 ~ The index T = B for the n-digit table; // T points to the device element C = (B-= (nelem/= d [I]) to be indexed. // make C times faster 2 ~ N struct refers to the biter table for (j = 0; j <nelem; j ++) * C ++ = T, T + = d [I];} return ;} program example: int main () {typedef int ***** I4D; int I; I4D A = (I4D) new_arr (sizeof (I4D), 4, 2, 3, 4, 5 ); for (I = 0; I <2*3*4*5; I ++) A [0] [0] [0] [I] = I; printf ("/n % d", a [1] [1] [1] [2]); // 87 printf ("/n % d ", A [1] [0] [3] [1]); // 76 del_arr ();}

C ++ coding method:

At present, the most efficient practice of the C ++ Dynamic Route multi-sector pricing column is... violence law ....
That is, the template is used to convert from 1 worker to N worker,
This method is equivalent to build-in array and also boost: multi_array 4 ~ 5 times ..
Let's take another example, and make the form of an dynamic column,
Use array (D1, D2,...) to access the sequence column element:

Template <class T, int n> struct Array {array (INT d ...) // The degree of parallelism of each shard is {dim [1] = dim [N] = 1; // guaranteed () can positively access memcpy (dim, & D, N * sizeof (INT); // degrees of parallelism for each shard of a pair (n = dim [N-1], I = N-2; I> = 0; I --) N * = dim [I], dim [I] = N; // calculates the cumulative degree if (0 = N) cout <"arr size = 0"; // configure strongswan else P = new T [dim [0]; // configure the actual space} T & operator () (Int J ...) {va_start (v, J); For (n = J * dim [1], I = 2; I <= N; ++ I) N + = dim [I] * va_arg (v, INT); // calculate Index offset return P [N];} ~ Array () {for (I = dim [0]; I --; P [I]. ~ T (); // Delete [] P;} PRIVATE: int I, n, dim [n + 1]; // dim [.] = degree of attention of each worker va_list V; T * P; // response column reality}; int main (/* David Dr 080705 */) {array <int, 4> A (,); For (INT I = 0; I <2*3*4*5; I ++) A (, 0, I) = I; cout <Endl <A (,); // 87 cout <Endl <A (,); // 76}

In order to use the modulo analyze column partitioning method, we had to add some long and ugly samples,
Now, arr (D1, D2,... DN) and ARR [D1] [D2]... [DN] can be used.
The two methods are used to access the token column ~

Template <typename T, int n = 1> struct Array {array (INT d ...) // The degree of parallelism of each shard is {dim [1] = dim [N] = 1; // guaranteed () can positively access memcpy (dim, & D, N * sizeof (INT); // degrees of parallelism for each shard of a pair (n = dim [N-1], I = N-2; I> = 0; I --) N * = dim [I], dim [I] = N; // calculates the cumulative degree if (0 = N) cout <"arr size = 0"; // configure strongswan else P = new T [dim [0]; // configure the actual space} T & operator () (Int J ...) {va_start (v, J); For (n = J * dim [1], I = 2; I <= N; ++ I) N + = dim [I] * va_arg (v, INT ); // Calculate the index offset return P [N];} ~ Array () {for (I = dim [0]; I --; P [I]. ~ T (); // The Delete [] P;} template <typename t, int n> struct PTR // specifies the struct object {PTR <t, n> (T * t, int * D): p (t), D (d) {} PTR <t, N-1> operator [] (int n) {return PTR <t, N-1> (p + N * (* D), d + 1 );} operator T * () {return P;} PRIVATE: T * P; int * D; // used to calculate the number of bytes during Operation }; template <typename T> struct PTR <t, 1> // define the meaning of {PTR (T * t, int *): p (t) {} T & operator [] (int n) {return P [N];} operator T * () {return P;} PRIVATE: T * P;}; PTR <t, N-1> operator [] (int n) {return PTR <t, N-1> (p + N * dim [1], dim + 2);} operator T * () {return P;} PRIVATE: int I, n, dim [n + 1]; // dim [.] = degree of attention for each vertex va_list V; T * P; // entity column}; Template <class T> struct array <t, 1> {array (int d ): D (D), P (New T [d]) {} ~ Array () {While (d --) P [D]. ~ T (); Delete [] P;} T & operator [] (int n) {return P [N];} T & operator () (int n) {return P [N];} PRIVATE: T * P; int D ;}; int main (/* David Dr 080705 */) {int I; array <int, 4> A (,); for (I = 0; I <2*3*4*5; I ++) A (, 0, I) = I; cout <Endl <A (1, 1, 2) // 87 <Endl <A [1] [0] [3] [1] // 76 <Endl <* A [1] [0] [3]; // 75 struct Java {char * tool; float version;} Java [3] = {"netbeans", 3.2, "Eclipse", 3.5, "jcreator", 5.1 }; array <Java> B (3); for (I = 0; I <3; I ++) B (I) = Java [I]; cout <Endl <B [1]. tool <Endl <B (2 ). version ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.