Changeset 3964
- Timestamp:
- 10/04/08 15:16:45 (2 months ago)
- Files:
-
- trunk/tango/util/container/LinkedList.d (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/tango/util/container/LinkedList.d
r3942 r3964 81 81 private alias Slink!(V) Type; 82 82 83 private alias Type *Ref; 83 private alias Type* Ref; 84 private alias V* VRef; 84 85 85 86 private alias Heap!(Type) Alloc; … … 160 161 /*********************************************************************** 161 162 162 Return an iterator which filters on the provided value163 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 176 163 177 164 ***********************************************************************/ … … 179 166 final int opApply (int delegate(ref V value) dg) 180 167 { 181 auto freach = iterator.freach; 182 return freach.opApply (dg); 168 return iterator.opApply (dg); 183 169 } 184 170 … … 921 907 /*********************************************************************** 922 908 923 foreach support for iterators924 925 ***********************************************************************/926 927 private struct Freach928 {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 945 909 Iterator with no filtering 946 910 … … 955 919 uint mutation; 956 920 921 bool valid () 922 { 923 return owner.mutation is mutation; 924 } 925 957 926 bool next (ref V v) 958 927 { 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; 966 930 } 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 } 967 959 968 960 bool remove () … … 981 973 return false; 982 974 } 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 filtering999 1000 ***********************************************************************/1001 1002 private struct IteratorMatch1003 {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 }1030 975 } 1031 976 } … … 1055 1000 1056 1001 // explicit generic iteration 1057 foreach (value; set.iterator .freach)1002 foreach (value; set.iterator) //.freach) 1058 1003 Stdout.formatln ("{}", value); 1059 1004 1060 1005 // 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; 1063 1008 1064 1009 // generic iteration with optional remove 1065 1010 auto s = set.iterator; 1066 foreach (value; s .freach)1011 foreach (value; s)///.freach) 1067 1012 if (value == "foo") 1068 1013 s.remove; … … 1116 1061 auto xx = test.iterator; 1117 1062 int ii; 1118 while (xx.next( ii)) {} //foreach (value; test) {}1063 while (xx.next()) {} //foreach (value; test) {} 1119 1064 Stdout.formatln ("{} element iteration: {}/s", test.size, test.size/w.stop); 1120 1065 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 1121 1077 test.check; 1122 1078 }












