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

Changeset 3966

Show
Ignore:
Timestamp:
10/04/08 22:10:48 (2 months ago)
Author:
kris
Message:

Updated iterators to expose pointers, and doubled traversal rate

Files:

Legend:

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

    r3923 r3966  
    2929        --- 
    3030        Iterator iterator () 
    31         IteratorMatch iterator (V value) 
    3231        uint opApply (int delegate(ref V value) dg) 
    3332 
     
    154153                i.cell = list; 
    155154                i.head = list; 
     155                i.bump = &Iterator.fore; 
    156156                return i; 
    157157        } 
     
    159159        /*********************************************************************** 
    160160 
    161                 Return an iterator which filters on the provided value 
    162                  
    163         ***********************************************************************/ 
    164  
    165         final IteratorMatch iterator (V value) 
    166         { 
    167                 IteratorMatch m = void; 
    168                 m.host = iterator; 
    169                 m.match = value; 
    170                 return m; 
    171         } 
    172          
    173         /*********************************************************************** 
    174  
    175161 
    176162        ***********************************************************************/ 
     
    178164        final int opApply (int delegate(ref V value) dg) 
    179165        { 
    180                 auto freach = iterator.freach; 
    181                 return freach.opApply (dg); 
     166                return iterator.opApply (dg); 
    182167        } 
    183168 
     
    938923        /*********************************************************************** 
    939924 
    940                 foreach support for iterators 
    941                  
    942         ***********************************************************************/ 
    943  
    944         private struct Freach 
    945         { 
    946                 bool delegate(ref V) next; 
    947                  
     925                Iterator with no filtering 
     926 
     927        ***********************************************************************/ 
     928 
     929        private struct Iterator 
     930        { 
     931                Ref function(Ref) bump; 
     932                Ref               cell, 
     933                                  head, 
     934                                  prior; 
     935                CircularList      owner; 
     936                uint              mutation; 
     937 
     938                /*************************************************************** 
     939 
     940                        Did the container change underneath us? 
     941 
     942                ***************************************************************/ 
     943 
     944                bool valid () 
     945                { 
     946                        return owner.mutation is mutation; 
     947                }                
     948 
     949                /*************************************************************** 
     950 
     951                        Accesses the next value, and returns false when 
     952                        there are no further values to traverse 
     953 
     954                ***************************************************************/ 
     955 
     956                bool next (ref V v) 
     957                { 
     958                        auto n = next; 
     959                        return (n) ? v = *n, true : false; 
     960                } 
     961                 
     962                /*************************************************************** 
     963 
     964                        Return a pointer to the next value, or null when 
     965                        there are no further values to traverse 
     966 
     967                ***************************************************************/ 
     968 
     969                V* next () 
     970                { 
     971                        V* r; 
     972 
     973                        if (cell) 
     974                           { 
     975                           prior = cell; 
     976                           r = &cell.value; 
     977                           cell = bump (cell); 
     978                           if (cell is head) 
     979                               cell = null; 
     980                           } 
     981                        return r; 
     982                } 
     983 
     984                /*************************************************************** 
     985 
     986                        Foreach support 
     987 
     988                ***************************************************************/ 
     989 
    948990                int opApply (int delegate(ref V value) dg) 
    949991                { 
    950                         V   value; 
    951992                        int result; 
    952993 
    953                         while (next (value)) 
    954                                if ((result = dg(value)) != 0) 
    955                                     break; 
     994                        auto c = cell; 
     995                        while (c) 
     996                              { 
     997                              prior = c; 
     998                              if ((c = bump(c)) is head) 
     999                                   c = null; 
     1000                              if ((result = dg(prior.value)) != 0) 
     1001                                   break; 
     1002                              } 
     1003                        cell = c; 
    9561004                        return result; 
    957                 }                
    958         } 
    959          
    960         /*********************************************************************** 
    961  
    962                 Iterator with no filtering 
    963  
    964         ***********************************************************************/ 
    965  
    966         private struct Iterator 
    967         { 
    968                 Ref             cell, 
    969                                 head, 
    970                                 prior; 
    971                 CircularList    owner; 
    972                 uint            mutation; 
    973  
    974                 bool prev (ref V v) 
    975                 { 
    976                         if (cell is null) 
    977                             return false; 
    978  
    979                         prior = cell; 
    980                         v = cell.value; 
    981                         cell = cell.prev; 
    982                         if (cell is head) 
    983                             cell = null; 
    984                         return true; 
    985                 } 
    986  
    987                 bool next (ref V v) 
    988                 { 
    989                         if (cell is null) 
    990                             return false; 
    991  
    992                         prior = cell; 
    993                         v = cell.value; 
    994                         cell = cell.next; 
    995                         if (cell is head) 
    996                             cell = null; 
    997                         return true; 
    998                 } 
    999  
    1000                 void remove () 
     1005                }                                
     1006 
     1007                /*************************************************************** 
     1008 
     1009                        Remove value at the current iterator location 
     1010 
     1011                ***************************************************************/ 
     1012 
     1013                bool remove () 
    10011014                { 
    10021015                        if (prior) 
    10031016                           { 
    1004                            auto next = prior.next
    1005                            if (prior is owner.list
     1017                           auto next = bump (prior)
     1018                           if (prior is head
    10061019                               if (prior is next) 
    10071020                                   owner.list = null; 
    10081021                           else 
    1009                               owner.list = next; 
     1022                              head = next; 
    10101023 
    10111024                           prior.unlink; 
     
    10151028                           // ignore this change 
    10161029                           ++mutation; 
     1030                           return true; 
    10171031                           } 
    1018                 } 
    1019                  
    1020                 bool valid () 
    1021                 { 
    1022                         return owner.mutation is mutation; 
    1023                 } 
    1024                  
    1025                 Freach freach() 
    1026                 { 
    1027                         Freach f = {&next}; 
    1028                         return f; 
    1029                 } 
    1030                  
    1031                 Freach reverse() 
    1032                 { 
    1033                         cell = cell.prev; 
    1034                         head = head.prev; 
    1035                         Freach f = {&prev}; 
    1036                         return f; 
    1037                 } 
    1038         } 
    1039  
    1040         /*********************************************************************** 
    1041  
    1042                 Iterator with value filtering 
    1043                  
    1044         ***********************************************************************/ 
    1045  
    1046         private struct IteratorMatch 
    1047         { 
    1048                 Iterator host; 
    1049                 V        match; 
    1050  
    1051                 bool next (ref V v) 
    1052                 { 
    1053                         while (host.next (v)) 
    1054                                if (match == v) 
    1055                                    return true; 
    10561032                        return false; 
    10571033                } 
    10581034 
    1059                 void remove () 
     1035                /*************************************************************** 
     1036 
     1037                ***************************************************************/ 
     1038 
     1039                Iterator reverse () 
    10601040                { 
    1061                         host.remove; 
     1041                        if (bump is &fore) 
     1042                            bump = &back; 
     1043                        else 
     1044                           bump = &fore; 
     1045                        return *this; 
    10621046                } 
    1063                 
    1064                 bool valid () 
     1047 
     1048                /*************************************************************** 
     1049 
     1050                ***************************************************************/ 
     1051 
     1052                private static Ref fore (Ref p) 
    10651053                { 
    1066                         return host.valid
     1054                        return p.next
    10671055                } 
    1068                  
    1069                 Freach freach() 
     1056 
     1057                /*************************************************************** 
     1058 
     1059                ***************************************************************/ 
     1060 
     1061                private static Ref back (Ref p) 
    10701062                { 
    1071                         Freach f = {&next}; 
    1072                         return f; 
     1063                        return p.prev; 
    10731064                } 
    10741065        } 
     
    11021093 
    11031094                // explicit generic iteration    
    1104                 foreach (value; list.iterator.freach
     1095                foreach (value; list.iterator.reverse
    11051096                         Stdout.formatln ("> {}", value); 
    1106  
    1107                 // generic filtered iteration  
    1108                 foreach (value; list.iterator("foo").freach) 
    1109                          Stdout (value).newline; 
    1110  
    1111                 // generic filtered iteration  
    1112                 foreach (value; list.iterator.reverse) 
    1113                          Stdout.formatln ("< {}", value); 
    11141097 
    11151098                // generic iteration with optional remove 
    11161099                auto s = list.iterator; 
    1117                 foreach (value; s.freach
    1118                         {} // s.remove; 
     1100                foreach (value; s
     1101                        {} //s.remove; 
    11191102 
    11201103                // incremental iteration, with optional remove 
     
    11221105                auto iterator = list.iterator; 
    11231106                while (iterator.next(v)) 
    1124                       {} //iterator.remove; 
     1107                      {}//iterator.remove; 
    11251108                 
    11261109                // incremental iteration, with optional failfast 
  • trunk/tango/util/container/HashMap.d

    r3868 r3966  
    3131        --- 
    3232        Iterator iterator () 
    33         IteratorMatch iterator (V value) 
    3433        int opApply (int delegate(ref V value) dg) 
    3534        int opApply (int delegate(ref K key, ref V value) dg) 
     
    147146        /*********************************************************************** 
    148147 
    149                 Return an iterator which filters on the provided key 
    150                  
    151         ***********************************************************************/ 
    152  
    153         final IteratorMatch iterator (V value) 
    154         { 
    155                 IteratorMatch m = void; 
    156                 m.host = iterator; 
    157                 m.match = value; 
    158                 return m; 
    159         } 
    160          
    161         /*********************************************************************** 
    162  
    163148 
    164149        ***********************************************************************/ 
     
    166151        final int opApply (int delegate(ref K key, ref V value) dg) 
    167152        { 
    168                 auto freach = iterator.freach; 
    169                 return freach.opApply (dg); 
     153                return iterator.opApply (dg); 
    170154        } 
    171155 
     
    177161        final int opApply (int delegate(ref V value) dg) 
    178162        { 
    179                 auto freach = iterator.freach; 
    180                 return freach.opApply ((ref K k, ref V v) {return dg(v);}); 
     163                return iterator.opApply ((ref K k, ref V v) {return dg(v);}); 
    181164        } 
    182165 
     
    370353                   clone.buckets (buckets); 
    371354 
    372                    foreach (key, value; iterator.freach
     355                   foreach (key, value; iterator
    373356                            clone.add (key, value); 
    374357                   } 
     
    967950        /*********************************************************************** 
    968951 
    969                 foreach support for iterators 
    970                  
    971         ***********************************************************************/ 
    972  
    973         private struct Freach 
    974         { 
    975                 bool delegate(ref K, ref V) next; 
    976                  
    977                 int opApply (int delegate(ref K key, ref V value) dg) 
    978                 { 
    979                         K   key; 
    980                         V   value; 
    981                         int result; 
    982  
    983                         while (next (key, value)) 
    984                                if ((result = dg(key, value)) != 0) 
    985                                     break; 
    986                         return result; 
    987                 }                
    988         } 
    989          
    990         /*********************************************************************** 
    991  
    992952                Iterator with no filtering 
    993953 
     
    1003963                uint    mutation; 
    1004964 
     965                /*************************************************************** 
     966 
     967                        Did the container change underneath us? 
     968 
     969                ***************************************************************/ 
     970 
     971                bool valid () 
     972                { 
     973                        return owner.mutation is mutation; 
     974                }                
     975 
     976                /*************************************************************** 
     977 
     978                        Accesses the next value, and returns false when 
     979                        there are no further values to traverse 
     980 
     981                ***************************************************************/ 
     982 
    1005983                bool next (ref K k, ref V v) 
     984                { 
     985                        auto n = next (k); 
     986                        return (n) ? v = *n, true : false; 
     987                } 
     988                 
     989                /*************************************************************** 
     990 
     991                        Return a pointer to the next value, or null when 
     992                        there are no further values to traverse 
     993 
     994                ***************************************************************/ 
     995 
     996                V* next (ref K k) 
    1006997                { 
    1007998                        while (cell is null) 
     
    10091000                                   cell = table [row++]; 
    10101001                               else 
    1011                                   return false
     1002                                  return null
    10121003   
    10131004                        prior = cell; 
    10141005                        k = cell.key; 
    1015                         v = cell.value; 
    10161006                        cell = cell.next; 
    1017                         return true; 
     1007                        return &prior.value; 
     1008 
    10181009                } 
    10191010 
    1020                 void remove () 
     1011                /*************************************************************** 
     1012 
     1013                        Foreach support 
     1014 
     1015                ***************************************************************/ 
     1016 
     1017                int opApply (int delegate(ref K key, ref V value) dg) 
     1018                { 
     1019                        int result; 
     1020 
     1021                        auto c = cell; 
     1022                        loop: while (true) 
     1023                              { 
     1024                              while (c is null) 
     1025                                     if (row < table.length) 
     1026                                         c = table [row++]; 
     1027                                     else 
     1028                                        break loop; 
     1029   
     1030                              prior = c; 
     1031                              c = c.next; 
     1032                              if ((result = dg(prior.key, prior.value)) != 0) 
     1033                                   break loop; 
     1034                              } 
     1035 
     1036                        cell = c; 
     1037                        return result; 
     1038                }                                
     1039 
     1040                /*************************************************************** 
     1041 
     1042                        Remove value at the current iterator location 
     1043 
     1044                ***************************************************************/ 
     1045 
     1046                bool remove () 
    10211047                { 
    10221048                        if (prior) 
    10231049                            if (owner.removeNode (prior, &table[row-1])) 
    1024                                 // ignore this change 
    1025                                 ++mutation; 
     1050                               { 
     1051                               // ignore this change 
     1052                               ++mutation; 
     1053                               return true; 
     1054                               } 
     1055 
    10261056                        prior = null; 
    1027                 } 
    1028                  
    1029                 bool valid () 
    1030                 { 
    1031                         return owner.mutation is mutation; 
    1032                 } 
    1033                  
    1034                 Freach freach() 
    1035                 { 
    1036                         Freach f = {&next}; 
    1037                         return f; 
    1038                 } 
    1039         } 
    1040  
    1041         /*********************************************************************** 
    1042  
    1043                 Iterator with value filtering 
    1044                  
    1045         ***********************************************************************/ 
    1046  
    1047         private struct IteratorMatch 
    1048         { 
    1049                 Iterator host; 
    1050                 V        match; 
    1051  
    1052                 bool next (ref K k, ref V v) 
    1053                 { 
    1054                         while (host.next (k, v)) 
    1055                                if (match == v) 
    1056                                    return true; 
    10571057                        return false; 
    1058                 } 
    1059  
    1060                 void remove () 
    1061                 { 
    1062                         host.remove; 
    1063                 } 
    1064                 
    1065                 bool valid () 
    1066                 { 
    1067                         return host.valid; 
    1068                 } 
    1069                  
    1070                 Freach freach() 
    1071                 { 
    1072                         Freach f = {&next}; 
    1073                         return f; 
    10741058                } 
    10751059        } 
     
    11001084 
    11011085                // explicit generic iteration 
    1102                 foreach (key, value; map.iterator.freach) 
    1103                          Stdout.formatln ("{}:{}", key, value); 
    1104  
    1105                 // generic filtered iteration  
    1106                 foreach (key, value; map.iterator(2).freach) 
     1086                foreach (key, value; map.iterator) 
    11071087                         Stdout.formatln ("{}:{}", key, value); 
    11081088 
    11091089                // generic iteration with optional remove 
    11101090                auto s = map.iterator; 
    1111                 foreach (key, value; s.freach
     1091                foreach (key, value; s
    11121092                        {} // s.remove; 
    11131093 
  • trunk/tango/util/container/HashSet.d

    r3713 r3966  
    137137        final int opApply (int delegate(ref V value) dg) 
    138138        { 
    139                 auto freach = iterator.freach; 
    140                 return freach.opApply (dg); 
     139                return iterator.opApply (dg); 
    141140        } 
    142141 
     
    218217                   clone.buckets (buckets); 
    219218 
    220                    foreach (value; iterator.freach
     219                   foreach (value; iterator
    221220                            clone.add (value); 
    222221                   } 
     
    679678        /*********************************************************************** 
    680679 
    681                 foreach support for iterators 
    682                  
    683         ***********************************************************************/ 
    684  
    685         private struct Freach 
    686         { 
    687                 bool delegate(ref V) next; 
    688                  
    689                 int opApply (int delegate(ref V value) dg) 
    690                 { 
    691                         V   value; 
    692                         int result; 
    693  
    694                         while (next (value)) 
    695                                if ((result = dg(value)) != 0) 
    696                                     break; 
    697                         return result; 
    698                 }                
    699         } 
    700          
    701         /*********************************************************************** 
    702  
    703680                Iterator with no filtering 
    704681 
     
    714691                uint    mutation; 
    715692 
     693                /*************************************************************** 
     694 
     695                        Did the container change underneath us? 
     696 
     697                ***************************************************************/ 
     698 
     699                bool valid () 
     700                { 
     701                        return owner.mutation is mutation; 
     702                }                
     703 
     704                /*************************************************************** 
     705 
     706                        Accesses the next value, and returns false when 
     707                        there are no further values to traverse 
     708 
     709                ***************************************************************/ 
     710 
    716711                bool next (ref V v) 
     712                { 
     713                        auto n = next; 
     714                        return (n) ? v = *n, true : false; 
     715                } 
     716                 
     717                /*************************************************************** 
     718 
     719                        Return a pointer to the next value, or null when 
     720                        there are no further values to traverse 
     721 
     722                ***************************************************************/ 
     723 
     724                V* next () 
    717725                { 
    718726                        while (cell is null) 
     
    720728                                   cell = table [row++]; 
    721729                               else 
    722                                   return false
     730                                  return null
    723731   
    724732                        prior = cell; 
    725                         v = cell.value; 
    726733                        cell = cell.next; 
    727                         return true; 
     734                        return &prior.value; 
    728735                } 
    729736 
    730                 void remove () 
     737                /*************************************************************** 
     738 
     739                        Foreach support 
     740 
     741                ***************************************************************/ 
     742 
     743                int opApply (int delegate(ref V value) dg) 
     744                { 
     745                        int result; 
     746 
     747                        auto c = cell; 
     748                        loop: while (true) 
     749                              { 
     750                              while (c is null) 
     751                                     if (row < table.length) 
     752                                         c = table [row++]; 
     753                                     else 
     754                                        break loop; 
     755   
     756                              prior = c; 
     757                              c = c.next; 
     758                              if ((result = dg(prior.value)) != 0) 
     759                                   break loop; 
     760                              } 
     761 
     762                        cell = c; 
     763                        return result; 
     764                }                                
     765 
     766                /*************************************************************** 
     767 
     768                        Remove value at the current iterator location 
     769 
     770                ***************************************************************/ 
     771 
     772                bool remove () 
    731773                { 
    732774                        if (prior) 
    733775                            if (owner.remove (prior, row-1)) 
    734                                 // ignore this change 
    735                                 ++mutation; 
     776                               { 
     777                               // ignore this change 
     778                               ++mutation; 
     779                               return true; 
     780                               } 
     781 
    736782                        prior = null; 
    737                 } 
    738                  
    739                 bool valid () 
    740                 { 
    741                         return owner.mutation is mutation; 
    742                 } 
    743                  
    744                 Freach freach() 
    745                 { 
    746                         Freach f = {&next}; 
    747                         return f; 
     783                        return false; 
    748784                } 
    749785        } 
     
    775811 
    776812                // explicit generic iteration 
    777                 foreach (value; set.iterator.freach
     813                foreach (value; set.iterator
    778814                         Stdout (value).newline; 
    779815 
    780816                // generic iteration with optional remove 
    781817                auto s = set.iterator; 
    782                 foreach (value; s.freach
     818                foreach (value; s
    783819                        {} // s.remove; 
    784820 
  • trunk/tango/util/container/LinkedList.d

    r3964 r3966  
    2929        --- 
    3030    Iterator iterator () 
    31         IteratorMatch iterator (V value) 
    3231        int opApply (int delegate(ref V value) dg) 
    3332 
     
    907906        /*********************************************************************** 
    908907 
    909                 Iterator with no filtering 
     908                List iterator 
    910909 
    911910        ***********************************************************************/ 
     
    919918                uint            mutation; 
    920919 
     920                /*************************************************************** 
     921 
     922                        Did the container change underneath us? 
     923 
     924                ***************************************************************/ 
     925 
    921926                bool valid () 
    922927                { 
    923928                        return owner.mutation is mutation; 
    924929                }                
     930 
     931                /*************************************************************** 
     932 
     933                        Accesses the next value, and returns false when 
     934                        there are no further values to traverse 
     935 
     936                ***************************************************************/ 
    925937 
    926938                bool next (ref V v) 
     
    930942                } 
    931943                 
     944                /*************************************************************** 
     945 
     946                        Return a pointer to the next value, or null when 
     947                        there are no further values to traverse 
     948 
     949                ***************************************************************/ 
     950 
    932951                V* next () 
    933952                { 
     
    942961                } 
    943962 
     963                /*************************************************************** 
     964 
     965                        Foreach support 
     966 
     967                ***************************************************************/ 
     968 
    944969                int opApply (int delegate(ref V value) dg) 
    945970                { 
     
    950975                              { 
    951976                              prior = hook; 
     977                              hook = &n.next; 
    952978                              if ((result = dg(n.value)) != 0) 
    953979                                   break; 
    954                               n = *(hook = &n.next)
     980                              n = *hook
    955981                              } 
    956982                        node = n; 
    957983                        return result; 
    958984                }                                
     985 
     986                /*************************************************************** 
     987 
     988                        Remove value at the current iterator location 
     989 
     990                ***************************************************************/ 
    959991 
    960992                bool remove () 
     
    967999                           hook = prior; 
    9681000                           prior = null; 
     1001 
    9691002                           // ignore this change 
    9701003                           ++mutation;