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

Changeset 3964

Show
Ignore:
Timestamp:
10/04/08 15:16:45 (2 months ago)
Author:
kris
Message:

exposed element-pointers via the iterator

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/util/container/LinkedList.d

    r3942 r3964  
    8181        private alias Slink!(V) Type; 
    8282         
    83         private alias Type      *Ref; 
     83        private alias Type*     Ref; 
     84        private alias V*        VRef; 
    8485 
    8586        private alias Heap!(Type) Alloc; 
     
    160161        /*********************************************************************** 
    161162 
    162                 Return an iterator which filters on the provided value 
    163                  
    164         ***********************************************************************/ 
    165  
    166         final IteratorMatch iterator (V value) 
    167         { 
    168                 IteratorMatch m = void; 
    169                 m.host = iterator; 
    170                 m.match = value; 
    171                 return m; 
    172         } 
    173          
    174         /*********************************************************************** 
    175  
    176163 
    177164        ***********************************************************************/ 
     
    179166        final int opApply (int delegate(ref V value) dg) 
    180167        { 
    181                 auto freach = iterator.freach; 
    182                 return freach.opApply (dg); 
     168                return iterator.opApply (dg); 
    183169        } 
    184170 
     
    921907        /*********************************************************************** 
    922908 
    923                 foreach support for iterators 
    924                  
    925         ***********************************************************************/ 
    926  
    927         private struct Freach 
    928         { 
    929                 bool delegate(ref V) next; 
    930                  
    931                 int opApply (int delegate(ref V value) dg) 
    932                 { 
    933                         V   value; 
    934                         int result; 
    935  
    936                         while (next (value)) 
    937                                if ((result = dg(value)) != 0) 
    938                                     break; 
    939                         return result; 
    940                 }                
    941         } 
    942          
    943         /*********************************************************************** 
    944  
    945909                Iterator with no filtering 
    946910 
     
    955919                uint            mutation; 
    956920 
     921                bool valid () 
     922                { 
     923                        return owner.mutation is mutation; 
     924                }                
     925 
    957926                bool next (ref V v) 
    958927                { 
    959                         if (node is null) 
    960                             return false; 
    961  
    962                         prior = hook; 
    963                         v = node.value; 
    964                         node = *(hook = &node.next); 
    965                         return true; 
     928                        auto n = next; 
     929                        return (n) ? v = *n, true : false; 
    966930                } 
     931                 
     932                V* next () 
     933                { 
     934                        V* r; 
     935                        if (node) 
     936                           { 
     937                           prior = hook; 
     938                           r = &node.value; 
     939                           node = *(hook = &node.next); 
     940                           } 
     941                        return r; 
     942                } 
     943 
     944                int opApply (int delegate(ref V value) dg) 
     945                { 
     946                        int result; 
     947 
     948                        auto n = node; 
     949                        while (n) 
     950                              { 
     951                              prior = hook; 
     952                              if ((result = dg(n.value)) != 0) 
     953                                   break; 
     954                              n = *(hook = &n.next); 
     955                              } 
     956                        node = n; 
     957                        return result; 
     958                }                                
    967959 
    968960                bool remove () 
     
    981973                        return false; 
    982974                } 
    983                  
    984                 bool valid () 
    985                 { 
    986                         return owner.mutation is mutation; 
    987                 } 
    988                  
    989                 Freach freach() 
    990                 { 
    991                         Freach f = {&next}; 
    992                         return f; 
    993                 } 
    994         } 
    995  
    996         /*********************************************************************** 
    997  
    998                 Iterator with value filtering 
    999                  
    1000         ***********************************************************************/ 
    1001  
    1002         private struct IteratorMatch 
    1003         { 
    1004                 Iterator host; 
    1005                 V        match; 
    1006  
    1007                 bool next (ref V v) 
    1008                 { 
    1009                         while (host.next (v)) 
    1010                                if (match == v) 
    1011                                    return true; 
    1012                         return false; 
    1013                 } 
    1014  
    1015                 void remove () 
    1016                 { 
    1017                         host.remove; 
    1018                 } 
    1019                 
    1020                 bool valid () 
    1021                 { 
    1022                         return host.valid; 
    1023                 } 
    1024                  
    1025                 Freach freach() 
    1026                 { 
    1027                         Freach f = {&next}; 
    1028                         return f; 
    1029                 } 
    1030975        } 
    1031976} 
     
    10551000 
    10561001                // explicit generic iteration    
    1057                 foreach (value; set.iterator.freach) 
     1002                foreach (value; set.iterator) //.freach) 
    10581003                         Stdout.formatln ("{}", value); 
    10591004 
    10601005                // generic filtered iteration  
    1061                 foreach (value; set.iterator("foo").freach) 
    1062                          Stdout (value).newline; 
     1006                //foreach (value; set.iterator("foo").freach) 
     1007                //         Stdout (value).newline; 
    10631008 
    10641009                // generic iteration with optional remove 
    10651010                auto s = set.iterator; 
    1066                 foreach (value; s.freach) 
     1011                foreach (value; s)///.freach) 
    10671012                         if (value == "foo") 
    10681013                             s.remove; 
     
    11161061                auto xx = test.iterator; 
    11171062                int ii; 
    1118                 while (xx.next(ii)) {} //foreach (value; test) {} 
     1063                while (xx.next()) {} //foreach (value; test) {} 
    11191064                Stdout.formatln ("{} element iteration: {}/s", test.size, test.size/w.stop); 
    11201065 
     1066                // benchmark iteration 
     1067                w.start; 
     1068                foreach (v; test) {} //foreach (value; test) {} 
     1069                Stdout.formatln ("{} foreach iteration: {}/s", test.size, test.size/w.stop); 
     1070 
     1071 
     1072                // benchmark iteration 
     1073                w.start;              
     1074                foreach (ref iii; test.iterator) {} //foreach (value; test) {} 
     1075                Stdout.formatln ("{} foreach iteration: {}/s", test.size, test.size/w.stop); 
     1076 
    11211077                test.check; 
    11221078        }