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

Ticket #965: testcollections.d

File testcollections.d, 2.1 kB (added by schveiguy, 9 months ago)

file to demonstrate the removal capabilities

Line 
1 import tango.group.collection;
2 import tango.io.Stdout;
3
4 void outputView(T)(View!(T) v)
5 {
6   Stdout("[");
7   foreach(x; v)
8     Stdout(" ")(x);
9   Stdout("]").newline;
10 }
11
12 void outputMap(K, V)(MapView!(K, V) m)
13 {
14   Stdout("[");
15   foreach(k, v; m)
16     Stdout(" ")(k)(":")(v);
17   Stdout("]").newline;
18 }
19
20 void removeEveryX(T)(Dispenser!(T) v, int X)
21 {
22   int x = X;
23   auto iter = v.dispensableElements;
24   /*
25   while(iter.more())
26   {
27     auto n = iter.get();
28     if(--x == 0)
29     {
30       Stdout("removing element ")(n).newline;
31       iter.remove();
32       x = X;
33     }
34   }*/
35   foreach(n; iter)
36   {
37     if(--x == 0)
38     {
39       Stdout("removing element ")(n).newline;
40       iter.remove();
41       x = X;
42     }
43   }
44 }
45
46 int main(char[][] args)
47 {
48   int[] keys = [34789, 346,7, 3677,11, 85824, 986,1234,78,17890];
49   int[] values = [1,2,3,4,5,6,7,8,9,10];
50
51   void doView(Dispenser!(int) view)
52   {
53     outputView!(int)(view);
54     removeEveryX!(int)(view, 3);
55     outputView!(int)(view);
56   }
57
58   void doBag(char[] name, Bag!(int) bag)
59   {
60     Stdout(name ~ ":").newline;
61     bag.add(new ArrayIterator!(int)(keys));
62     doView(bag);
63   }
64
65   void doSeq(char[] name, Seq!(int) seq)
66   {
67     Stdout(name ~ ":").newline;
68     seq.append(new ArrayIterator!(int)(keys));
69     doView(seq);
70   }
71
72   void doSet(char[] name, Set!(int) set)
73   {
74     Stdout(name ~ ":").newline;
75     set.add(new ArrayIterator!(int)(keys));
76     doView(set);
77   }
78
79   void doMap(char[] name, Map!(int, int) map)
80   {
81     Stdout(name ~ ":").newline;
82     foreach(i, v; keys)
83       map[v] = values[i];
84     outputMap!(int, int)(map);
85     removeEveryX!(int)(map, 3);
86     outputMap!(int, int)(map);
87   }
88
89
90   doBag("ArrayBag", new ArrayBag!(int));
91   doBag("TreeBag", new TreeBag!(int));
92   doSeq("ArraySeq", new ArraySeq!(int));
93   doSeq("CircularSeq", new CircularSeq!(int));
94   doSeq("LinkSeq", new LinkSeq!(int));
95   doMap("HashMap", new HashMap!(int, int));
96   doMap("LinkMap", new LinkMap!(int, int));
97   doMap("TreeMap", new TreeMap!(int, int));
98   doSet("HashSet", new HashSet!(int));
99   return 0;
100 }