Changeset 885
- Timestamp:
- 05/14/06 01:45:38 (3 years ago)
- Files:
-
- trunk/mango/containers/List.d (modified) (3 diffs)
- trunk/mango/test/containers.d (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/mango/containers/List.d
r884 r885 41 41 private import mango.containers.Container; 42 42 private import mango.containers.Set; 43 private import mango.containers.HashSet; 43 44 private import mango.containers.Iterator; 44 45 private import mango.containers.Util; … … 102 103 ************************************************************************/ 103 104 public abstract Set!(int) indexesOf(V item); 105 106 /*********************************************************************** 107 108 To support a foreach with the index supplied. 109 110 ************************************************************************/ 111 public abstract int opApply(int delegate(inout int, inout V) dg); 112 public abstract int opApply(int delegate(inout V) dg); //TODO: Remove this when DMD gets fixed 104 113 105 114 … … 152 161 return -1; 153 162 } 154 155 Set!(int) indexesOf(V item) { 156 return null; //TODO 163 164 int opApply(int delegate(inout int, inout V) dg) { 165 Iterator!(V) iter = iterator(); 166 int i=0; 167 while (iter.hasNext()) { 168 V v = iter.next; 169 if (dg(i,v) != 0) 170 return -1; 171 i++; 172 } 173 return 0; 174 } 175 176 177 int opApply(int delegate(inout V) dg) { //TODO: remove this when DMD gets fixed 178 Iterator!(V) iter = iterator(); 179 while (iter.hasNext()) { 180 V v = iter.next; 181 if (dg(v) != 0) 182 return -1; 183 } 184 return 0; 185 } 186 187 Set!(int) indexesOf(V item) 188 { 189 HashSet!(int) set = new HashSet!(int)(); 190 foreach (int i, V element; this) 191 { 192 if (Util!(V).equals(item, element)) 193 { 194 set.add(i); 195 } 196 } 197 return set; 157 198 } 158 199 trunk/mango/test/containers.d
r737 r885 21 21 import mango.containers.ThreadSafeList; 22 22 23 import mango.io.Stdout;23 private import mango.io.Stdout; 24 24 25 25 void main() { 26 Pool1();26 //Pool1(); 27 27 //TSList1(); 28 28 //Stack1(); … … 31 31 //HashSet1(); 32 32 //ConcurrentHashMap1(); 33 //ConcurrentHashMap3(); 33 34 //Utils1(); 34 35 //AltCompares1(); … … 36 37 //AltCompares2(); 37 38 //misc(); 39 ListIndexes1(); 40 } 41 42 void ListIndexes1() 43 { 44 ArrayList!(char[]) list = new ArrayList!(char[])(); 45 list ~= "bob"; 46 list ~= "joe"; 47 list ~= "walter"; 48 list ~= "bob"; 49 list ~= "bob"; 50 Set!(int) indxs = list.indexesOf("bob"); 51 foreach(int i; indxs) 52 Stdout(i)(","c); 53 Stdout(CR); 38 54 } 39 55 … … 59 75 MutableList!(char[]) al = new ArrayList!(char[])(); 60 76 ThreadSafeList!(char[]) list = new ThreadSafeList!(char[])(al); 61 assert(al is null);77 //assert(al is null); 62 78 list.append("Hello"); 63 79 } … … 93 109 } 94 110 95 void misc() {111 void ConcurrentHashMap2() { 96 112 struct S { 97 113 int i; … … 99 115 100 116 ConcurrentHashMap!(int,int) mapII; //This is the only one that works. 101 //ConcurrentHashMap!(int,char[]) mapIC;102 //ConcurrentHashMap!(char[],int) mapCI;103 //ConcurrentHashMap!(char[],char[]) mapCC;104 105 //ConcurrentHashMap!(int,S) mapIS;106 //ConcurrentHashMap!(S,int) mapSI;107 //ConcurrentHashMap!(S,S) mapSS;108 109 //ConcurrentHashMap!(float,float) mapFF;117 ConcurrentHashMap!(int,char[]) mapIC; 118 ConcurrentHashMap!(char[],int) mapCI; 119 ConcurrentHashMap!(char[],char[]) mapCC; 120 121 ConcurrentHashMap!(int,S) mapIS; 122 ConcurrentHashMap!(S,int) mapSI; 123 ConcurrentHashMap!(S,S) mapSS; 124 125 ConcurrentHashMap!(float,float) mapFF; 110 126 } 111 127 … … 180 196 print(Util!(float).hash(85.78)); 181 197 print(Util!(char[]).hash("foobar")); 182 print(Util!(long).hash(987616513 l));198 print(Util!(long).hash(987616513L)); 183 199 } 184 200 … … 200 216 mapA.put(5,value); 201 217 assert(mapA[5] == value); 218 } 219 220 private import std.thread; 221 void ConcurrentHashMap3() 222 { 223 ConcurrentHashMap!(char[], char[]) mapA = new ConcurrentHashMap!(char[], char[])(); 224 assert(mapA.isThreadSafe()); 225 mapA["bob"] = "reilly"; 226 mapA["joe"] = "blow"; 227 mapA["bob"] = "murphy"; 228 foreach (char[] k, char[] v; mapA) 229 Stdout.put(k).put(": "c).put(v).put(CR); 230 231 ConcurrentHashMap!(int, int) mapB = new ConcurrentHashMap!(int, int)(); 232 for (int i=0; i < 100_000; i++) 233 { 234 mapB[i] = i*i; 235 } 236 237 for (int i=0; i < 100_000; i++) 238 { 239 assert(mapB[i] == i*i); 240 } 241 Stdout("Ints check out OK"c)(CR); 242 243 mapB.clear(); 244 245 Thread t1 = new Thread(delegate int(){ 246 for (int i=0; i<1_000_000; i+=2) 247 mapB[i] = i*i*i; 248 return 0; 249 }); 250 Thread t2 = new Thread(delegate int(){ 251 for (int i=1; i<1_000_000; i+=2) 252 mapB[i] = i*i; 253 return 0; 254 }); 255 t1.run(); 256 t2.run(); 257 t1.wait(); 258 t2.wait(); 259 260 for (int i=0; i<1_000_000; i++) 261 assert((i%2 == 0 && mapB[i] == i*i*i) || (i%2 == 1 && mapB[i] == i*i)); 262 Stdout("Threaded ints checkout"c)(CR); 202 263 } 203 264
