use of union types in shuffle.

Also switch instead of if else
This commit is contained in:
Francesco Mecca 2019-04-27 09:22:36 +02:00
parent 209707f0b9
commit 4799d9d58b

View file

@ -3,26 +3,46 @@ import std.array : array;
import std.random; import std.random;
import std.file; import std.file;
const (string[]) shuffle(string[] args, const string t) import vibe.http.server;
import taggedalgebraic;
struct Error {};
union U {
string[] args;
Error e;
}
alias Result = TaggedUnion!U;
auto shuffle(string[] args, const string t)
{ {
if(t == "mt"){ Result r;
switch(t){
case "mt":
Mt19937 gen; Mt19937 gen;
gen.seed(unpredictableSeed); gen.seed(unpredictableSeed);
args.randomShuffle(gen); args.randomShuffle(gen);
} else if (t == "x"){ break;
case "x":
Xorshift32 gen; Xorshift32 gen;
gen.seed(unpredictableSeed); gen.seed(unpredictableSeed);
args.randomShuffle(gen); args.randomShuffle(gen);
} else if (t == "du"){ break;
case "du":
auto gen = DevRandomGen!"/dev/urandom"(); auto gen = DevRandomGen!"/dev/urandom"();
args.randomShuffle(gen); args.randomShuffle(gen);
} else if (t == "dr"){ break;
case "dr":
auto gen = DevRandomGen!"/dev/random"(); auto gen = DevRandomGen!"/dev/random"();
args.randomShuffle(gen); args.randomShuffle(gen);
} else { break;
throw new Exception("Wrong arguments"); default:
r = Error();
return r;
} }
return args;
r = args;
return r ;
} }