Created
July 3, 2016 18:31
-
-
Save fpelliccioni/89448531aba8872c58b873ed6346ba83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.slideshare.net/AndreiAlexandrescu2/accu-keynote-by-andrei-alexandrescu | |
// conservative implementation | |
Range find_1(Range, E)(Range r, E e) { | |
for (; !r.empty; r.popFront) { | |
if (r.front == e) break; | |
} | |
return r; | |
} | |
// indexed implementation | |
Range find_2(Range, E)(Range r, E e) { | |
size_t i = 0; | |
for (; i != r.length; ++i) { | |
if (r[i] == e) break; | |
} | |
return r[i .. $]; | |
} | |
// sentinel implementation | |
Range find_3(Range, E)(Range r, E e) { | |
auto c = r[$ - 1]; | |
r[$ - 1] = e; | |
size_t i = 0; | |
{ | |
scope(exit) r[$ - 1] = c; | |
for (;; ++i) | |
if (r[i] == e) break; | |
} | |
if (i + 1 == r.length && c != e) | |
++i; | |
return r[i .. $]; | |
} | |
R find_4(R, E)(R r, E e) | |
if (isInputRange!R && is(typeof(r.front == e) == bool)) | |
{ | |
static if (isRandomAccessRange!R && hasSlicing!R) { | |
static if (is(typeof(() nothrow {r[0] = r[0];}))) { | |
// sentinel implementation | |
//writeln("sentinel implementation"); | |
return find_3(r, e); | |
} else { | |
// indexed implementation | |
//writeln("indexed implementation"); | |
return find_2(r, e); | |
} | |
} else { | |
// conservative implementation | |
//writeln("conservative implementation"); | |
return find_1(r, e); | |
} | |
} | |
// ------------------------------------------------ | |
// Tests | |
// ------------------------------------------------ | |
import std.algorithm; | |
import std.conv; | |
import std.range; | |
import std.stdio; | |
import core.thread; | |
void writability_test() { | |
int[] s = [1, 2, 3, 4]; | |
immutable int[] si = [1, 2, 3, 4]; | |
//writeln(s); | |
//writeln(s[1 .. $]); | |
//writeln(find_1(s, 3)); | |
//writeln(find_2(s, 3)); | |
//writeln(find_3(s, 3)); | |
writeln(find_4(s, 3)); | |
writeln(find_4(si, 3)); | |
} | |
void data_race_tests() { | |
writeln("data_race_tests() starting..."); | |
int[] shared_data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
auto m = reduce!(max)(shared_data); | |
const int pool_size = 8; | |
Thread[pool_size] pool; | |
for (int i = 0; i < pool_size; ++i) { | |
auto t = new Thread({ | |
for (int k = 0; k < 1000000; ++k) { | |
//find_4(shared_data, m); // works fine | |
find_4(shared_data, m + i); // let's see what happens | |
} | |
writeln("for: " ~ to!string(m + i) ~ " - result: " ~ to!string(shared_data[$ - 1])); | |
}); | |
pool[i] = t; | |
t.start(); | |
} | |
writeln("waiting for threads..."); | |
for (int i = 0; i < pool_size; ++i) { | |
pool[i].join(); | |
} | |
writeln("data_race_tests() finished..."); | |
} | |
void main() { | |
//writability_test(); | |
data_race_tests(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment