Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changes between Version 35 and Version 36 of ChapterStorage

Show
Ignore:
Author:
keinfarbton (IP: 85.180.21.52)
Timestamp:
10/06/08 21:27:55 (6 years ago)
Comment:

more about using, iterator

Legend:

Unmodified
Added
Removed
Modified
  • ChapterStorage

    v35 v36  
    6868{{{ 
    6969#!d 
    70     auto list1 = new LinkedList!( int ); 
     70    auto list = new LinkedList!( int ); 
     71    auto map  = new HashMap!( char[], char[] ); 
    7172}}} 
    7273 
    8182== Add and remove elements == 
    8283 
    83 Each container type has it own set of methods. 
    84 {{{ 
    85 #!d 
    86     list1.prepend( 2 ); 
    87     list1.append( 3 ); 
    88     list1.append( 4 ); 
    89     list2.append( "The first item in list1" ); 
    90 }}} 
    91  
    92 {{{ 
    93 #!d 
    94     int oldValue = list1.removeHead(); // removes the '2' 
    95 }}} 
     84Each container type has its own set of methods. All containers support {{{add}}}. 
     85{{{ 
     86#!d 
     87    list.add( 2 ); 
     88    map.add( "first", "value one" ); 
     89}}} 
     90 
     91Some containers have specialized methods to add values at a certain index like {{{addAt}}}, {{{append}}} or {{{prepend}}} 
     92{{{ 
     93#!d 
     94    list.prepend( 4 );  // now '4, 2' 
     95    list.addAt( 1, 3 ); // now '4, 3, 2' 
     96    list.append( 1 );   // now '4, 3, 2, 1' 
     97}}} 
     98 
     99{{{ 
     100#!d 
     101    int oldValue = list.removeHead(); // now '3, 2, 1' and oldValue == 4 
     102}}} 
     103 
     104There are more methods for adding, removing, searching, retrival, ... See the API doc of the container. 
    96105 
    97106== Visiting contained elements == 
    98 ''This section is not completed yet and is a copy of the collection doc''[[br]] 
     107 
    99108The typical, and simplest pattern is 
    100109 
    101110{{{ 
    102111#!d 
     112    foreach (value; list) 
     113             Stdout.formatln ("Value: {}", value); 
    103114    foreach (key, value; map) 
    104115             Stdout.formatln ("Key: {}, Value: {}", key, value); 
    105116}}} 
    106117 
    107 One variation is using only 'value' instead of both 'key' and 'value'. 
     118For the map, one variation is also using only 'value' instead of both 'key' and 'value'. 
    108119Both of these utilize a slightly more efficient mechanism to visit each contained element (or element pair), 
    109120as they can avoid heap activity when instantiating an '''Iterator'''. 
    110121 
    111 Collections also expose iterators, supporting both ''foreach'' and a familiar '''more()'''/'''next()''' pattern. 
     122Containers also expose iterators, supporting both ''foreach'' and a familiar '''bool next(ref V v)''' pattern. 
     123{{{ 
     124#!d 
     125    V value; 
     126    auto it = list.iterator; 
     127    while(it.next(value)) 
     128             Stdout.formatln ("Value: {}", value); 
     129}}} 
     130'''Note:''' The iterators also support a {{{remove}}} method. 
     131 
     132 
    112133Each container exposes method(s) to instantiate an appropriate '''Iterator''' 
    113134   
    114135{{{ 
    115136#!d 
    116     auto it = map.keys(); 
    117  
    118     // The call to .more returns true if there are more elements, 
    119     // and switches the iterator to the next one if available. 
    120     while(it.more){ 
    121           char[] key;  
    122           auto value = it.get(key); 
     137    auto it = map.iterator(); 
     138 
     139    char[] key, value; 
     140    while(it.next(key, value){ 
    123141          Stdout.formatln ("Key: {}, Value:{}", key, value); 
    124142    } 
    125143}}} 
    126144 
    127 For associative containers, the iterator's '''get''' method has an out parameter for key retrival. 
    128  
    129 The '''keys()''' method is used to also get a set of the key values. For the 'normal' containers, like '''!LinkSeq''' etc. the '''elements()''' method is used to get a conventional iterator. Each container also supports ''foreach()'' directly. 
    130  
    131145=== Asynchronous mutation during iteration === 
    132146''This section is not completed yet and is a copy of the collection doc''[[br]] 
    133147 
    134148Iterators are sensitive to container changes whilst they operate, and will throw an exception when this occurs. This it to ensure the client sees a consistent view of the container for the lifetime of the iterator. 
    135  
    136 === Caution with iterators === 
    137 ''This section is not completed yet and is a copy of the collection doc''[[br]] 
    138  
    139 It is currently not possible to modify the container while iterating over it with ''foreach'' or an iterator. To delete certain elements from a container it is necessary to do it in two steps. First find the elements to delete, then do the deletion 
    140  
    141 {{{ 
    142 #!d 
    143 // 1. Build a list of elements to delete 
    144 auto dellist = new LinkSeq!(int); 
    145 foreach( int i; container ){ 
    146   if( i >= 3 && i < 7 ){ 
    147     // container.remove( i ); /* NOT POSSIBLE */ 
    148     dellist.append( i ); 
    149   } 
    150 } 
    151 // 2. Iterate over this deletion list and 
    152 // delete the items in the original container. 
    153 foreach( int i; dellist ){ 
    154     container.remove( i ); 
    155 } 
    156 }}} 
    157  
    158 This may be rectified in the future.  
    159149 
    160150== !InterleavingIterator ==