Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

containers.d

Go to the documentation of this file.
00001 /*
00002  * Examples for Mango's Containers
00003  */
00004 
00005 import mango.containers.Container;
00006 import mango.containers.Iterator;
00007 import mango.containers.ImmutableList;
00008 import mango.containers.List;
00009 import mango.containers.ListMutators;
00010 import mango.containers.LinkedList;
00011 import mango.containers.Map;
00012 import mango.containers.ArrayList;
00013 import mango.containers.Set;
00014 import mango.containers.HashMap;
00015 import mango.containers.HashSet;
00016 import mango.containers.ConcurrentHashMap;
00017 import mango.containers.Map;
00018 import mango.containers.Util;
00019 
00020 import mango.io.Stdout;
00021 
00022 void main() {
00023   ImmutableList1();
00024   //HashMap1();
00025   //HashSet1();
00026   //ConcurrentHashMap1();
00027   //Utils1();
00028   //AltCompares1();
00029   //Mutators1();
00030   //AltCompares2();
00031   //misc();
00032 }
00033 
00034 void Mutators1() {
00035   void print(List!(int) list) {
00036     foreach(int i; list) {
00037       Stdout.put(i).cr();
00038     }
00039   }
00040   MutableList!(int) list = new ArrayList!(int)();
00041   list.append(1).append(2).append(3).append(4);
00042   print(list);
00043   list.mutate!(Reverse)();
00044   print(list);
00045 }
00046 
00047 void misc() {
00048   struct S {
00049     int i;
00050   }
00051 
00052   ConcurrentHashMap!(int,int) mapII;  //This is the only one that works.
00053   //ConcurrentHashMap!(int,char[]) mapIC;
00054   //ConcurrentHashMap!(char[],int) mapCI;
00055   //ConcurrentHashMap!(char[],char[]) mapCC;
00056 
00057   //ConcurrentHashMap!(int,S) mapIS;
00058   //ConcurrentHashMap!(S,int) mapSI;
00059   //ConcurrentHashMap!(S,S) mapSS;
00060 
00061   //ConcurrentHashMap!(float,float) mapFF;
00062 }
00063 
00064 /*************************
00065   For AltCompare tests
00066  *************************/
00067 class Test {
00068   int A;
00069   int B;
00070   this (int A, int B) {
00071     this.A = A;
00072     this.B = B;
00073   }
00074   int opEquals(Object o) {
00075     Test t = cast(Test)o;
00076     assert(t);
00077     return t.A == A;
00078   }
00079   uint toHash() {
00080     return cast(uint) A;
00081   }
00082 }
00083 
00084 int myEquals(Test a, Test b) {
00085   return a.B == b.B;
00086 }
00087 int myCmp(Test a, Test b) {
00088   return a.B - b.B;
00089 }
00090 uint myHash(Test t) {
00091   return cast(uint)t.B;
00092 }
00093 
00094 void AltCompares1() {
00095   alias HashMap!(Test, char[], myEquals, myCmp, myHash) MyMap;
00096   MutableMap!(Test,char[]) map = new MyMap(5);
00097   map[new Test(5,6)] = "six, not five";
00098   map[new Test(10,9)] = "nine, not ten";
00099   Stdout.put(map[new Test(7,6)]).cr();
00100   Stdout.put(map[new Test(15,9)]).cr();
00101 }
00102 
00103 void AltCompares2() {
00104   /+alias ConcurrentHashMap!(Test, char[], myEquals, myCmp, myHash) MyMap;
00105   MutableMap!(Test,char[]) map = new MyMap(5);
00106   map[new Test(5,6)] = "six, not five";
00107   map[new Test(10,9)] = "nine, not ten";
00108   Stdout.put(map[new Test(7,6)]).cr();
00109   Stdout.put(map[new Test(15,9)]).cr();+/
00110 }
00111 /**************************
00112   End AltCompares
00113  **************************/
00114 
00115 
00116 void Utils1() {
00117   void print(uint hash) {
00118     Stdout.put(hash).cr();
00119   }
00120 
00121   struct S {
00122     int i;
00123     float f;
00124     uint toHash() {
00125       return 0;
00126     }
00127   }
00128   static S s = { 5 ,7.5 };
00129   print(Util!(S).hash(s));
00130   print(Util!(S*).hash(&s));
00131 
00132   print(Util!(float).hash(85.78));
00133   print(Util!(char[]).hash("foobar"));
00134   print(Util!(long).hash(987616513l));
00135 }
00136 
00137 void ConcurrentHashMap1() {
00138   ConcurrentHashMap!(int,int) mapA = new ConcurrentHashMap!(int,int)();
00139   mapA.put(1,2);
00140   mapA.put(3,6);
00141   mapA.put(4,8);
00142 
00143   assert(mapA.get(4) == 8);
00144   mapA[5] = 10;
00145   mapA.remove(5);
00146   assert(mapA.keySet().contains(3));
00147   assert(mapA.valueSet().contains(8));
00148   assert(mapA.size == 3);
00149 
00150   int value;
00151   mapA.remove(4,value);
00152   mapA.put(5,value);
00153   assert(mapA[5] == value);
00154 }
00155 
00156 void HashMap1() {
00157   MutableMap!(char[], int) mapA = new HashMap!(char[],int)();
00158   mapA["foo"] = 3;
00159   mapA["bar"] = 5;
00160   mapA["foobar"] = 6;
00161 
00162   assert(mapA["bar"] == 5);
00163   
00164   MutableMapIterator!(char[], int) iter = mapA.iterator();
00165   while (iter.hasNext()) {
00166     char[] s;
00167     int i;
00168     iter.next(s,i);
00169     Stdout.put(s).put(": ").put(i).cr();
00170   }
00171 }
00172 
00173 void HashSet1() {
00174   MutableSet!(int) setA = new HashSet!(int)();
00175   setA.add(5);
00176   setA.add(7);
00177   setA.add(6);
00178   setA.add(13);
00179   MutableIterator!(int) iter = setA.iterator();
00180   while(iter.hasNext()) {
00181     int i = iter.next();
00182     if (i%2==0) {
00183       iter.remove();
00184       Stdout.put("No evens allowed!!! (Removed number ").put(i).put(")").cr();
00185     } else {
00186       Stdout.put(i).cr();
00187     }
00188   }
00189 }
00190 
00191 //An example of how to populate and use an ImmutableList
00192 void ImmutableList1() {
00193   int count;
00194   int max = 10;
00195   
00196   bit addElements(out int i) {
00197     i = count++;
00198     return cast(bit)(i<max);
00199   }
00200 
00201   void print(List!(int) list) {
00202     Stdout.put("Content:").cr();
00203     Iterator!(int) iter = list.iterator();
00204     while (iter.hasNext()) {
00205       Stdout.put(iter.next()).cr();
00206     }
00207     Stdout.put("--------").cr();
00208   }
00209 
00210 
00211   ImmutableList!(int) list1 = new ImmutableList!(int)(&addElements);
00212   print(list1);
00213   count -= 5;
00214   max = 15;
00215   ImmutableList!(int) list2 = new ImmutableList!(int)(&addElements);
00216   print(list2);
00217   ImmutableList!(int) common = list1.intersection(list2);
00218   print(common);
00219 }

Generated on Mon Nov 14 10:59:37 2005 for Mango by  doxygen 1.4.0