Changeset 885

Show
Ignore:
Timestamp:
05/14/06 01:45:38 (3 years ago)
Author:
teqdruid
Message:

Updated containers test module, closed a TODO in List.d, added another opApply to List

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/mango/containers/List.d

    r884 r885  
    4141private import mango.containers.Container; 
    4242private import mango.containers.Set; 
     43private import mango.containers.HashSet; 
    4344private import mango.containers.Iterator; 
    4445private import mango.containers.Util; 
     
    102103       ************************************************************************/ 
    103104    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 
    104113 
    105114 
     
    152161        return -1; 
    153162    } 
    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; 
    157198    } 
    158199     
  • trunk/mango/test/containers.d

    r737 r885  
    2121import mango.containers.ThreadSafeList; 
    2222 
    23 import mango.io.Stdout; 
     23private import mango.io.Stdout; 
    2424 
    2525void main() { 
    26   Pool1(); 
     26  //Pool1(); 
    2727  //TSList1(); 
    2828  //Stack1(); 
     
    3131  //HashSet1(); 
    3232  //ConcurrentHashMap1(); 
     33  //ConcurrentHashMap3(); 
    3334  //Utils1(); 
    3435  //AltCompares1(); 
     
    3637  //AltCompares2(); 
    3738  //misc(); 
     39    ListIndexes1(); 
     40} 
     41 
     42void 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); 
    3854} 
    3955 
     
    5975  MutableList!(char[]) al = new ArrayList!(char[])(); 
    6076  ThreadSafeList!(char[]) list = new ThreadSafeList!(char[])(al); 
    61   assert(al is null); 
     77  //assert(al is null); 
    6278  list.append("Hello"); 
    6379} 
     
    93109} 
    94110 
    95 void misc() { 
     111void ConcurrentHashMap2() { 
    96112  struct S { 
    97113    int i; 
     
    99115 
    100116  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; 
    110126} 
    111127 
     
    180196  print(Util!(float).hash(85.78)); 
    181197  print(Util!(char[]).hash("foobar")); 
    182   print(Util!(long).hash(987616513l)); 
     198  print(Util!(long).hash(987616513L)); 
    183199} 
    184200 
     
    200216  mapA.put(5,value); 
    201217  assert(mapA[5] == value); 
     218} 
     219 
     220private import std.thread; 
     221void 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); 
    202263} 
    203264