Arch of D language (1)

xiaoxiao2021-03-06  41

There are four arrays in arrays:

INT * P; pointing data pointer INT [3] S; static array int [] a; dynamic array int [char []] X; associated array

pointer

INT * P;

This is the easiest pointing to point to data, equivalent to the C pointer. The purpose of providing a pointer is to provide the interface with C and give D to complete a specific system-level work. There is no correlation with the array length, so there is no way to perform a work in the compilation or runtime. Most traditional pointer usage can use dynamic arrays,

OUT and

Inout parameters and reference type replacement.

Static array

Int [3] S;

This is an array of C language. The length of the static array is determined when compiling.

Dynamic array

Int [] a;

The dynamic array consists of an array length and a pointer to the data. Multiple dynamic arrays may share all or part of data in array.

An array declaration has two ways of declaration arrays: prefix and a sub-diagrading method. The prefix method is usually better, especially when declaring non-flat arrays.

The prefix array declaration prefix declaration appears before the declared flag, should be read from right to left, so:

INT [] a; // int "dynamic array

INT [4] [3] b; // contains three elements, each element is an array of four int

INT [] [5] c; // contains two elements of arrays, each element is an INT dynamic array

INT * [] * [3] D; / / contains an array of three elements, each element pointing to a pointer to the dynamic array of INT

INT [] * e; // The element is pointed to the dynamic array

The suffix array declares that the suffix declaration appears after the declared flag and is read from left to right. The declarations within each group below are equivalent:

// int

Int [] a;

Int a [];

// Contains an array of three elements, each element is an array containing 4 int

Int [4] [3] B;

Int [4] b [3];

INT B [3] [4];

/ / Contains an array of 5 elements, each element is an INT dynamic array

int [] [5] C;

Int [] C [5];

INT C [5] [];

/ / Contain three elements of arrays, each element pointing to a pointer to a dynamic array pointed to INT

INT * [] * [3] D;

INT * [] * D [3];

INT * (* D [3]) [];

// The element finger is a pointer to the dynamic array

int [] * e;

Int (* e []);

Principle: The suffix form is exactly the form of C and C , providing this support to provide an easy migration path for programmers that are used to this form.

The operation of the usage to the array can be divided into two major categories, the operation of the array reference, and the operation of the array content. In D, these two operations have.

The number of group names is actually a reference to the array, such as P, s, or A:

INT * P;

Int [3] S;

Int [] a;

INT * Q;

Int [3] T;

int [] b;

P = q; // p and q point to the same thing

P = S; // P Point the first element of array s

P = a; // p pointing to the first element of array a

s = ...; // error, because s is a compile time static array reference

A = p; // error, because P does not have an array length

A = S; // is initialized to point to array s

A = B; // a and b point to the same array

slice

Cutting an array means getting a sub-array of it. Array slices do not copy the data of the array, it is just returns another reference. E.g:

INT [10] a; // Declare an array containing 10 INT

int [] b;

B = a [1..3]; // a [1..3] is an array containing 2 elements, its content is A [1] and A [2] foo (B [1]); // Equivalent of foo (0)

a [2] = 3;

FOO (B [1]); // Isometrical foo (3)

Shorthand [] Represents the entire array. For example, the assignment of these to B:

Int [10] a;

int [] b;

B = a;

B = a [];

B = a [0 .. a.length];

It is equivalent to semantics.

Cut is not just a part of a part in the reference array, and the pointer can also be converted to an array with an off-border check:

INT * P;

int [] b = p [0..8];

Bit array cut is allowed when the lower bound of the slice is aligned in accordance with byte.

Bit [] B;

...

B [0..8]; // ok

B [8..16]; // ok

B [8..17]; // ok

B [1..16]; / / error, the lower bound is not in byte

Unaligned array slices will throw an ArrayboundSerror exception at runtime.

Array Replication When the cutter operator appears as an assignment expression, it means that the assignment target is an array content rather than a reference to the array. When the left value is a slice, the right value is a array or a corresponding type of pointer, replication occurs.

Int [3] S;

Int [3] T;

S [] = t; // t [3] 3 elements are copied to S [3]

S [] = t []; // T [3] 3 elements are copied to S [3]

S [1..2] = T [0..1]; // Isometric S [1] = T [0]

S [0..2] = T [1..3]; // Equivalent to S [0] = T [1], S [1] = T [2]

s [0..4] = T [0..4]; / ​​/ error, only 3 elements in S

S [0..2] = T; // Error, the length of the left value and the right value different overlapping replication will be considered an error:

s [0..2] = s [1..3]; // error, overlapping copy

S [1..3] = s [0..2]; // Error, overlapping replication relative to the serial semantics of C, prohibiting overlapping copies that make the compiler can perform more radical parallel code optimization.

The array assignment If a cutter operator appears as the left value of the assignment expression, and the type of the right value is the same as the type of array element, the content as the array of the left value will be set to the value of the right value.

Int [3] S;

INT * P;

S [] = 3; // Equivalent to S [0] = 3, S [1] = 3, S [2] = 3

P [0..2] = 3; // Isometrical P [0] = 3, P [1] = 3

An array connection binary operator is a connection operator. It is used to connect arrays:

Int [] a;

int [] b;

int [] C;

A = b ~ c; // Punch two array connections to a new array

Many languages ​​are overloaded with the operator to complete the connection operation. This will produce unprolisurized results:

"10" 3

Its value is equal to 13 or string "103"? This is not a look, the language designer has to shake the spirit, carefully write rules to avoid the ambiguity of two - and these rules are difficult to implement properly, easy to neglect, forget. If you use on behalf of the addition, use another operator to represent the array connection.

Similarly, ~ = operator represents append, such as:

A ~ = b; // a result of the connection of A and B

The connection operation always creates a copy of the operation, even if the length is 0, it is true when the length is 0: a = b // a point B

A = b ~ c [0..0] // a copy of the B

Array operation

Note: The array operator has not been implemented.

In general, (a [n..m] OP E) is defined as:

For (i = n; i

A [I] OP E;

So, for expressions:

a [] = b [] 3;

Result equivalent:

For (i = 0; i

a [i] = b [i] 3;

When more than one [] operator appears in the expression, the scope of its representative must match.

A [1..3] = b [] 3; // error, 2 elements of array cannot be the same as the array of three elements

Example:

INT [3] abc; // contains 3 int static arrays

Int [] DEF = [1, 2, 3]; // contains three INT dynamic arrays

Void Dibb (Int * Array)

{

Array [2]; // Isometrical * (Array 2)

* (array 2); // Get the elements of the subscript 2

}

Void Diss (int [] array)

{

Array [2]; // ok

* (Array 2); // Error, array is not a pointer

}

Void Ditt (int [3] array)

{

Array [2]; // ok

* (Array 2); // Error, array is not a pointer

}

Rectangular array experienced FORTRAN numerical calculation programmers know that multidimensional "rectangular" arrays is much faster than something similar to matrix operations (just like "Array" storage array pointer ") . For example, the syntax of D:

Double [] [] Matrix;

Declare that Matrix is ​​an array of elements to point to array pointers (dynamic array is implemented as a pointer to array data). Since the arrays can have different sizes (which can be dynamically set), sometimes it is called "alternating array." For code optimization, it is even more bad, and the lines of arrays sometimes point each other! Fortunately, D's static array, using the same syntax, is implemented as a fixed rectangular distribution:

Double [3] [3] Matrix;

It declares a rectangular matrix of 3 lines and continuously distributed in memory. In other languages, it is called a multi-dimensional array and declares similar to:

Double Matrix [3, 3];

Array length in [] in static or dynamic arrays, variables

Length is implicitly declared and set to the length of the array.

Int [4] foo;

int [] bar = foo;

INT * P = & foo [0];

// These expressions are equivalent:

Bar []

Bar [0 .. 4]

Bar [0 .. length]

Bar [0 .. bar.length]

p [0 .. length] // 'length' is undefined because P is not an array

Bar [0] length // 'Length' is undefined because it is not in []

Bar [Length-1] // Get the last element within the array

The properties of the array attribute static array are:

SIZEOF returns the size of an array in bytes. Length returns the number of elements in an array. This is a fixed value for static arrays. DUP Creates a dynamic array of the same size and copy the contents of the original array to the new array. Reverse will arrange the elements in the array in reverse order. Returns an array. SORT sorts the elements in the array in accordance with the appropriate method. Returns an array. The properties of the dynamic array are:

SIZEOF returns the size of the dynamic array reference, 8 on the 32-bit platform. The number of elements in the Length get / set array. DUP Creates a dynamic array of the same size and copy the contents of the original array to the new array. Reverse will arrange the elements in the array in reverse order. Returns an array. SORT sorts the elements in the array in accordance with the appropriate method. Returns an array.

Example:

p.Length // error, information without array size for pointers

S.Length // Compile time 3

A.Length // runtime value

P. DUP / / error, length unknown, unable to copy

S. DUP // Create an array containing 3 elements, copy the elements to it

A. DUP // Create an array containing a.length element, copy the element to it

转载请注明原文地址:https://www.9cbs.com/read-55566.html

New Post(0)