1 /**
2 Copyright 2019 Mark Fisher
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 **/
22 module dxx.alg.container;
23 
24 import dxx.alg.lambda;
25 
26 import std.traits,
27 	std.algorithm,
28 	std.typetuple;
29 
30 version(unittest) {
31 	import std.stdio,
32 		std.conv;
33 	import core.stdc.stdarg;
34 }
35 template Map(alias Func,args...) {
36 	static if (args.length > 1)
37 		alias Map = TypeTuple!(Apply!(Func,args[0]),Map!(Func,args[1..$]));
38 	else
39 		alias Map = Apply!(Func,args[0]);
40 }
41 
42 unittest {
43 	static void test(int x,int y,int z) {
44 		assert(x==4 && y==5 && z==6);
45 	}
46 	static int add2(int a) { return a+2; }
47 	int a=2,b=3,c=4;
48 	test(Map!(add2,a,b,c));
49 	test(Map!(add2,2,3,4));
50 	alias x = Map!(add2,2,3,4);
51 	test(x);
52 }
53 unittest {
54 	static int square(int arg)
55 	{
56 		return arg * arg;
57 	}
58 
59 	static int refSquare(ref int arg)
60 	{
61 		arg *= arg;
62 		return arg;
63 	}
64 
65 	static ref int refRetSquare(ref int arg)
66 	{
67 		arg *= arg;
68 		return arg;
69 	}
70 
71 	static void test(int a, int b)
72 	{
73 		assert(a == 4);
74 		assert(b == 16);
75 	}
76 
77 	static void testRef(ref int a, ref int b)
78 	{
79 		assert(a++ == 16);
80 		assert(b++ == 256);
81 	}
82 
83 	static int a = 2;
84 	static int b = 4;
85 
86 	test(Map!(square, a, b));
87 
88 	test(Map!(refSquare, a, b));
89 	assert(a == 4);
90 	assert(b == 16);
91 
92 	testRef(Map!(refRetSquare, a, b));
93 	assert(a == 17);
94 	assert(b == 257);
95 }
96 
97 //template Reduce(alias Func,alias Param,args...) {
98  //static if (args.length > 1)
99  //	alias Reduce = Apply!(Func,args[0],Reduce!(Func,args[1..$]));
100  //else
101  //	alias Reduce = Apply!(Func,args[0],Param);
102 //}
103 //
104 template removeElement(T,alias V) {
105   auto ref removeElement(ref T t) { return t.remove(a => a is V); }
106 }