Changeset 1810

Show
Ignore:
Timestamp:
08/05/10 18:34:02 (2 years ago)
Author:
schveiguy
Message:

bugs 4551 and 4590 array documentation
also added anchors for all the headers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/docsrc/arrays.dd

    r1663 r1810  
    135135    ) 
    136136 
    137 <h2>Usage</h2> 
     137<h2>$(LNAME2 usage, Usage)</h2> 
    138138 
    139139    $(P There are two broad kinds of operations to do on an array - 
     
    171171--------- 
    172172 
    173 <h2><a name="slicing">Slicing</a></h2> 
     173<h2>$(LNAME2 slicing, Slicing)</h2> 
    174174 
    175175    $(P $(I Slicing) an array means to specify a subarray of it. 
     
    216216--------- 
    217217 
    218 <h2>Array Copying</h2> 
     218<h2>$(LNAME2 array-copying, Array Copying)</a></h2> 
    219219 
    220220    $(P When the slice operator appears as the lvalue of an assignment 
     
    249249    ) 
    250250 
    251 <h2>Array Setting</h2> 
     251<h2>$(LNAME2 array-setting, Array Setting)</h2> 
    252252 
    253253    $(P If a slice operator appears as the lvalue of an assignment 
     
    265265--------- 
    266266 
    267 <h2>Array Concatenation</h2> 
     267<h2>$(LNAME2 array-concatenation, Array Concatenation)</a></h2> 
    268268 
    269269    $(P The binary operator ~ is the $(I cat) operator. It is used 
     
    285285 
    286286--------- 
    287 "10" + 3 
    288 --------- 
    289  
    290     $(P produce the number 13 or the string "103" as the result? It isn't 
    291     obvious, and the language designers wind up carefully writing rules 
    292     to disambiguate it - rules that get incorrectly implemented, 
    293     overlooked, forgotten, and ignored. It's much better to have + mean 
    294     addition, and a separate operator to be array concatenation. 
     287"10" + 3 + 4 
     288--------- 
     289 
     290        $(P produce the number 17, the string "1034" or the string "107" as the 
     291        result? It isn't obvious, and the language designers wind up carefully 
     292        writing rules to disambiguate it - rules that get incorrectly 
     293        implemented, overlooked, forgotten, and ignored. It's much better to 
     294        have + mean addition, and a separate operator to be array 
     295        concatenation. 
    295296    ) 
    296297 
     
    311312--------- 
    312313 
    313  
    314 $(COMMENT 
    315 <h2>Array Operations</h2> 
    316  
    317     $(P $(B Note): Array operations are not implemented. 
    318     ) 
    319  
    320     $(P In general, (a[n..m] $(I op) e) is defined as: 
    321     ) 
    322  
    323 --------- 
    324 for (i = n; i < m; i++) 
    325     a[i] $(I op) e; 
    326 --------- 
    327  
    328     $(P So, for the expression: 
    329     ) 
    330  
    331 --------- 
    332 a[] = b[] + 3; 
    333 --------- 
    334  
    335     $(P the result is equivalent to:) 
    336  
    337 --------- 
    338 for (i = 0; i < a.length; i++) 
    339     a[i] = b[i] + 3;  
    340 --------- 
    341  
    342     $(P When more than one [] operator appears in an expression, the range 
    343     represented by all must match. 
    344     ) 
    345  
    346 --------- 
    347 a[1..3] = b[] + 3;  // error, 2 elements not same as 3 elements 
    348 --------- 
    349 
    350  
     314        $(P Appending does not always create a copy, see $(LINK2 #resize, 
     315        setting dynamic array length) for details. 
     316    ) 
    351317 
    352318<h2>$(LNAME2 array-operations, Array Operations)</h2> 
     
    402368    ) 
    403369 
    404 <h2>Pointer Arithmetic</h2> 
     370<h2>$(LNAME2 pointer-arithmetic, Pointer Arithmetic)</h2> 
    405371 
    406372--------- 
     
    427393--------- 
    428394 
    429 <h2>Rectangular Arrays</h2> 
     395<h2>$(LNAME2 rectangular-arrays, Rectangular Arrays)</h2> 
    430396 
    431397    $(P Experienced FORTRAN numerics programmers know that multidimensional  
     
    457423--------- 
    458424 
    459 <h2>Array Length</h2> 
     425<h2>$(LNAME2 array-length, Array Length)</h2> 
    460426 
    461427$(V1 
     
    497463--------- 
    498464 
    499 <h2>Array Properties</h2> 
     465<h2>$(LNAME2 array-properties, Array Properties)</h2> 
    500466 
    501467    $(P Static array properties are:) 
     
    647613 
    648614--------- 
    649 p.length    // error, length not known for pointer 
    650 s.length    // compile time constant 3 
    651 a.length    // runtime value 
    652  
    653 p.dup       // error, length not known 
    654 s.dup       // creates an array of 3 elements, copies 
     615int* p; 
     616int[3] s; 
     617int[] a; 
     618 
     619p.length;   // error, length not known for pointer 
     620s.length;   // compile time constant 3 
     621a.length;   // runtime value 
     622 
     623p.dup;      // error, length not known 
     624s.dup;      // creates an array of 3 elements, copies 
    655625        // elements s into it 
    656 a.dup     // creates an array of a.length elements, copies 
     626a.dup;        // creates an array of a.length elements, copies 
    657627        // elements of a into it 
    658628--------- 
    659629 
    660 <h3><a name="resize">Setting Dynamic Array Length</a></h3> 
     630<h3>$(LNAME2 resize, Setting Dynamic Array Length)</h3> 
    661631 
    662632    $(P The $(B $(TT .length)) property of a dynamic array can be set 
     
    670640    $(P This causes the array to be reallocated in place, and the existing 
    671641    contents copied over to the new array. If the new array length is 
    672     shorter, 
    673     only enough are copied to fill the new array. If the new array length 
    674     is longer, the remainder is filled out with the default initializer. 
     642        shorter, the array is not reallocated, and no data is copied.  It is 
     643        equivalent to slicing the array: 
     644 
     645--------- 
     646array = array[0..7]; 
     647--------- 
     648 
     649        If the new array length is longer, the remainder is filled out with the 
     650        default initializer. 
    675651    ) 
    676652 
    677653    $(P To maximize efficiency, the runtime always tries to resize the 
    678     array in place to avoid extra copying. It will always do a copy 
    679     if the new size is larger and the array was not allocated via the 
    680     new operator or a previous 
    681     resize operation. 
    682     ) 
    683  
    684     $(P This means that if there is an array slice immediately following the 
     654    array in place to avoid extra copying. 
     655        $(V1 It will always do a copy if the new size is larger and the array 
     656        was not allocated via the new operator or a previous resize operation.) 
     657        $(V2 It will always do a copy if the new size is larger and the array 
     658        was not allocated via the new operator or resizing in place would 
     659        overwrite valid data in the array.) 
     660    ) 
     661 
     662$(V1    $(P This means that if there is an array slice immediately following the 
    685663    array being resized, the resized array could overlap the slice; i.e.: 
    686664    ) 
     
    706684        // the old a[3] 
    707685--------- 
     686) 
     687 
     688$(V2 
     689        For example: 
     690 
     691--------- 
     692char[] a = new char[20]; 
     693char[] b = a[0..10]; 
     694char[] c = a[10..20]; 
     695char[] d = a; 
     696 
     697b.length = 15;    // always reallocates because extending in place would 
     698                  // overwrite other data in a. 
     699b[11] = 'x';      // a[11] and c[1] are not affected 
     700 
     701d.length = 1; 
     702d.length = 20;    // also reallocates, because doing this will overwrite a and 
     703                  // c 
     704 
     705c.length = 12;    // may reallocate in place if space allows, because nothing 
     706                  // was allocated after c. 
     707c[5] = 'y';       // may affect contents of a, but not b or d because those 
     708                  // were reallocated.  
     709 
     710a.length = 25;    // This always reallocates because if c extended in place, 
     711                  // then extending a would overwrite c.  If c didn't 
     712                  // reallocate in place, it means there was not enough space, 
     713                  // which will still be true for a. 
     714a[15] = 'z';      // does not affect c, because either a or c has reallocated. 
     715--------- 
     716) 
    708717 
    709718    $(P To guarantee copying behavior, use the .dup property to ensure 
    710     a unique array that can be resized. 
    711     ) 
    712  
    713     $(P These issues also apply to concatenating arrays with the ~ and ~= 
    714     operators. 
     719        a unique array that can be resized. $(V2 Also, one may use the phobos 
     720        $(TT .capacity) property to determine how many elements can be appended 
     721        to the array without reallocating.) 
     722    ) 
     723 
     724        $(P These issues also apply to appending arrays with the ~= operator. 
     725        Concatenation using the ~ operator is not affected since it always 
     726        reallocates. 
    715727    ) 
    716728 
     
    754766    ) 
    755767 
    756 <h3>Functions as Array Properties</h3> 
     768        $(V2 $(P Also, you may wish to utilize the phobos $(TT reserve) 
     769        function to pre-allocate array data to use with the append operator.)) 
     770 
     771<h3>$(LNAME2 func-as-property, Functions as Array Properties)</h3> 
    757772 
    758773    $(P If the first parameter to a function is an array, the 
     
    768783--- 
    769784 
    770 <h2><a name="bounds">Array Bounds Checking</a></h2> 
     785<h2>$(LNAME2 bounds, Array Bounds Checking)</h2> 
    771786 
    772787    $(P It is an error to index an array with an index that is less than 
    773788    0 or greater than or equal to the array length. If an index is 
    774     out of bounds, an ArrayBoundsError exception is raised if detected 
    775     at runtime, and an error if detected at compile time. 
    776    A program may not rely on array bounds checking happening, for 
    777    example, the following program is incorrect: 
     789        out of bounds, $(V1 an ArrayBoundsError)$(V2 a RangeError) exception is 
     790        raised if detected at runtime, and an error if detected at compile 
     791        time.  A program may not rely on array bounds checking happening, for 
     792        example, the following program is incorrect: 
    778793    ) 
    779794 
     
    815830    ) 
    816831 
    817 <h2>Array Initialization</h2> 
    818  
    819 <h3>Default Initialization</h3> 
     832<h2>$(LNAME2 array-initialization, Array Initialization)</h2> 
     833 
     834<h3>$(LNAME2 default-initialization, Default Initialization)</h3> 
    820835 
    821836    $(UL  
     
    827842    ) 
    828843 
    829 <h3>Void Initialization</h3> 
     844<h3>$(LNAME2 void-initialization, Void Initialization)</h3> 
    830845 
    831846    $(P Void initialization happens when the $(I Initializer) for 
     
    837852    ) 
    838853 
    839 <h3>Static Initialization of Static Arrays</h3> 
     854<h3>$(LNAME2 static-init-static, Static Initialization of Static Arrays)</h3> 
    840855 
    841856    $(P Static initalizations are supplied by a list of array 
     
    863878 
    864879 
    865 <h2>Special Array Types</h2> 
    866  
    867 <a name="strings"><h3>Strings</h3></a
     880<h2>$(LNAME2 special-array, Special Array Types)</h2> 
     881 
     882<h3>$(LNAME2 strings, Strings)</h3
    868883 
    869884    $(P A string is 
     
    959974 
    960975--------- 
     976$(V1 
    961977cast(wchar [])"abc" // this is an array of wchar characters 
    962978"abc"w          // so is this 
     979) 
     980$(V2 
     981cast(immutable(wchar) [])"abc"  // this is an array of wchar characters 
     982"abc"w                  // so is this 
     983) 
    963984--------- 
    964985 
     
    977998w = 'bc';       // error - only one wchar character at a time 
    978999w = "b"[0];     // w is assigned the wchar character 'b' 
    979 w = \r[0];        // w is assigned the carriage return wchar character 
     1000w = "\r"[0];      // w is assigned the carriage return wchar character 
    9801001d = 'd';        // d is assigned the character 'd' 
    9811002--------- 
    9821003 
    983 <h4>C's printf() and Strings</h4> 
     1004<h4>$(LNAME2 printf, C's printf() and Strings)</h4> 
    9841005 
    9851006    $(P $(B printf()) is a C function and is not part of D. $(B printf()) 
     
    10311052--------- 
    10321053 
    1033 <h3>Implicit Conversions</h3> 
     1054<h3>$(LNAME2 implicit-conversions, Implicit Conversions)</h3> 
    10341055 
    10351056    $(P A pointer $(TT $(I T)*) can be implicitly converted to 
     
    10631084 
    10641085<hr> 
    1065 <h1><a name="associative">Associative Arrays</a></h1> 
     1086<h1>$(LNAME2 associative, Associative Arrays)</h1> 
    10661087 
    10671088    $(P Associative arrays have an index that is not necessarily an integer, 
     
    11041125    ) 
    11051126 
    1106 <h3>Using Classes as the KeyType</h3> 
     1127<h3>$(LNAME2 classes-as-keys, Using Classes as the KeyType)</h3> 
    11071128 
    11081129    $(P Classes can be used as the $(I KeyType). For this to work, 
     
    11331154 
    11341155$(V1      int $(B opEquals)(Object o))$(V2      bool $(B opEquals)(Object o)) 
    1135     {   Foo f = cast(Foo) o; 
    1136     return f && a == foo.a && b == foo.b; 
     1156    {   Foo foo = cast(Foo) o; 
     1157    return foo && a == foo.a && b == foo.b; 
    11371158    } 
    11381159 
    11391160    int $(B opCmp)(Object o) 
    1140     {   Foo f = cast(Foo) o; 
    1141     if (!f
     1161    {   Foo foo = cast(Foo) o; 
     1162    if (!foo
    11421163        return -1; 
    11431164    if (a == foo.a) 
     
    11531174    the class objects are the same or not.) 
    11541175 
    1155 <h3>Using Structs or Unions as the KeyType</h3> 
     1176<h3>$(LNAME2 structs-as-keys, Using Structs or Unions as the KeyType)</h3> 
    11561177 
    11571178    $(P If the $(I KeyType) is a struct or union type, 
     
    11791200$(V1      hash_t $(B toHash)() 
    11801201    {   hash_t hash; 
    1181     foreach (char c; s
     1202    foreach (char c; str
    11821203        hash = (hash * 9) + c; 
    11831204    return hash; 
     
    11951216$(V2      const hash_t $(B toHash)() 
    11961217    {   hash_t hash; 
    1197     foreach (char c; s
     1218    foreach (char c; str
    11981219        hash = (hash * 9) + c; 
    11991220    return hash; 
     
    12181239    the struct/union objects are the same or not.) 
    12191240 
    1220 <h3>Properties</h3> 
     1241<h3>$(LNAME2 aa-properties, Properties)</h3> 
    12211242 
    12221243Properties for associative arrays are: 
     
    12921313 
    12931314<hr> 
    1294 <h3>Associative Array Example: word count</h3> 
     1315<h3>$(LNAME2 aa-example, Associative Array Example: word count)</h3> 
    12951316 
    12961317--------- 
  • trunk/docsrc/changelog.dd

    r1773 r1810  
    6262    $(LI $(BUGZILLA 3853): core.sys.posix.stdio.pclose is missing) 
    6363    $(LI $(BUGZILLA 3917): opEquals for Ojbect could be more efficient) 
     64    $(LI $(BUGZILLA 4551): D2 Language Docs: http://www.digitalmars.com/d/2.0/arrays.html) 
     65    $(LI $(BUGZILLA 4590): Spec incorrectly describes array appending and memory stomping) 
    6466    ) 
    6567)