-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.d
More file actions
137 lines (118 loc) · 3.16 KB
/
algorithm.d
File metadata and controls
137 lines (118 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
module stdex.algorithm;
import std.algorithm;
import std.array;
import std.functional;
import std.range;
import std.traits;
void forEach(alias func, Range)(Range range)
{
alias unaryFun!func f;
foreach (e; range)
f(e);
}
auto sorted(alias pred = "a < b", InputRange)(InputRange range)
{
static assert(isInputRange!InputRange, "input must be an input range.");
static assert(!isInfinite!InputRange, "input must not be an infinite range.");
return range.array.sort!pred();
}
auto argMin(alias f, alias less = "a < b", InputRange)(InputRange args)
{
static assert(isInputRange!InputRange, "args must be an input range.");
static assert(!isInfinite!InputRange, "args must not be an infinite range.");
assert(!args.empty, "Cannot find argmin of empty range.");
auto best = args.front;
auto bestVal = unaryFun!f(best);
args.popFront();
while (!args.empty)
{
auto testVal = unaryFun!f(args.front);
if (binaryFun!less(testVal, bestVal))
{
bestVal = testVal;
best = args.front;
}
args.popFront();
}
return best;
}
auto argMax(alias f, alias more = "a > b", InputRange)(InputRange args)
{
return argMin!(f, more, InputRange)(args);
}
ulong[ElementType!Range] histogram(Range)(Range input)
{
static assert(isInputRange!Range, "input must be an input range.");
static assert(!isInfinite!Range, "input must not be an infinite range.");
ulong[ElementType!Range] histo;
foreach (e; input)
histo[e]++;
return histo;
}
unittest
{
import std.stdio;
writeln("testing stdex.algorithm.histogram");
auto h1 = [1, 1, 2, 2, 2, 3, 4, 5, 5].histogram();
assert(h1[1] == 2);
assert(h1[2] == 3);
assert(h1[3] == 1);
assert(h1[4] == 1);
assert(h1[5] == 2);
}
ulong fnv1(Range)(Range x)
{
ulong h = 14695981039346656037UL;
foreach (b; x)
h = (h * 1099511628211UL) ^ b;
return h;
}
auto longestCommonSubsequence(R1, R2)(R1 a, R2 b)
{
// TODO: use hirschberg's algorithm
alias T = CommonType!(Unqual!(ElementType!R1), Unqual!(ElementType!R1));
auto L = new uint[][](a.length + 1, b.length + 1);
foreach (i; 0..a.length)
{
import std.stdio;
writeln(i);
foreach (j; 0..b.length)
L[i + 1][j + 1] = (a[i] == b[j]) ? (1 + L[i][j]) :
max(L[i + 1][j], L[i][j + 1]);
}
Unqual!T[] result;
for (auto i = a.length, j = b.length; i > 0 && j > 0; ) {
if (a[i - 1] == b[j - 1]) {
result ~= a[i - 1];
i--;
j--;
} else
if (L[i][j - 1] < L[i - 1][j] || L[i][j - 1] == L[i - 1][j] && a[i - 1] < b[j - 1])
i--;
else
j--;
}
result.reverse();
return result;
}
///
unittest
{
assert(longestCommonSubsequence("hello"d, "hero"d) == "heo"d);
}
bool hasSubsequence(R1, R2)(R1 a, R2 b)
{
while (!a.empty && !b.empty)
{
if (a.front == b.front)
b.popFront();
a.popFront();
}
return b.empty;
}
///
unittest
{
assert("hello".hasSubsequence("hlo"));
assert(!"hello".hasSubsequence("hle"));
}