Skip to content

Instantly share code, notes, and snippets.

@schveiguy
Created May 22, 2020 17:17
Show Gist options
  • Save schveiguy/cb364e44f7eb5db4c9f2f884822f3e64 to your computer and use it in GitHub Desktop.
Save schveiguy/cb364e44f7eb5db4c9f2f884822f3e64 to your computer and use it in GitHub Desktop.
dotdotdot test case
template NewFilter(alias pred, T...)
{
import std.meta : AliasSeq;
static if(T.length == 1)
{
static if(pred!T)
alias NewFilter = AliasSeq!(T);
else
alias NewFilter = AliasSeq!();
}
else
{
alias NewFilter = NewFilter!(pred, T)...;
}
}
@safe unittest
{
import std.traits : isNarrowString, isUnsigned;
import std.meta : AliasSeq;
alias Types1 = AliasSeq!(string, wstring, dchar[], char[], dstring, int);
alias TL1 = NewFilter!(isNarrowString, Types1);
static assert(is(TL1 == AliasSeq!(string, wstring, char[])));
alias Types2 = AliasSeq!(int, byte, ubyte, dstring, dchar, uint, ulong);
alias TL2 = NewFilter!(isUnsigned, Types2);
static assert(is(TL2 == AliasSeq!(ubyte, uint, ulong)));
}
template staticIota(size_t N)
{
import std.meta : AliasSeq;
string buildIota()
{
import std.range : iota;
import std.format : format;
return format("alias staticIota = AliasSeq!(%(%d,%));", iota(N));
}
mixin(buildIota());
}
template NewNoDuplicates(T...)
{
import std.meta : AliasSeq;
template getAlias(size_t idx) {
alias list = T[0 .. idx];
alias item = T[idx];
import std.algorithm: canFind;
static if([__traits(isSame, list, item)...].canFind(true))
alias getAlias = AliasSeq!();
else
alias getAlias = AliasSeq!(T[idx]);
}
alias idxList = staticIota!(T.length)[1 .. $];
alias NewNoDuplicates = AliasSeq!(T[0], getAlias!(idxList)...);
}
@safe unittest
{
import std.meta : AliasSeq;
alias Types = AliasSeq!(int, long, long, int, float);
alias TL = NewNoDuplicates!(Types);
static assert(is(TL == AliasSeq!(int, long, float)));
}
@safe unittest
{
import std.meta : AliasSeq, Repeat;
import std.range : iota;
// https://issues.dlang.org/show_bug.cgi?id=14561: huge enums
alias LongList = Repeat!(1500, int);
static assert(NewNoDuplicates!LongList.length == 1);
// https://issues.dlang.org/show_bug.cgi?id=17995: huge enums, revisited
alias a = NewNoDuplicates!(AliasSeq!(1, Repeat!(1000, 3)));
alias b = NewNoDuplicates!(AliasSeq!(1, Repeat!(10, 3)));
static assert(a.length == b.length);
static assert(NewNoDuplicates!(aliasSeqOf!(iota(7)), aliasSeqOf!(iota(7))) == aliasSeqOf!(iota(7)));
static assert(NewNoDuplicates!(aliasSeqOf!(iota(8)), aliasSeqOf!(iota(8))) == aliasSeqOf!(iota(8)));
}
template NewFilter(alias pred, T...)
{
import std.meta : AliasSeq;
static if(T.length == 1)
{
static if(pred!T) alias NewFilter = T[0];
else alias NewFilter = AliasSeq!();
}
else
{
alias NewFilter = AliasSeq!((NewFilter!(pred, T)...));
}
}
@safe unittest
{
import std.traits : isNarrowString, isUnsigned;
import std.meta : AliasSeq;
alias Types1 = AliasSeq!(string, wstring, dchar[], char[], dstring, int);
alias TL1 = NewFilter!(isNarrowString, Types1);
static assert(is(TL1 == AliasSeq!(string, wstring, char[])));
alias Types2 = AliasSeq!(int, byte, ubyte, dstring, dchar, uint, ulong);
alias TL2 = NewFilter!(isUnsigned, Types2);
static assert(is(TL2 == AliasSeq!(ubyte, uint, ulong)));
}
template staticIota(size_t N)
{
import std.meta : AliasSeq;
string buildIota()
{
import std.range : iota;
import std.format : format;
return format("alias staticIota = AliasSeq!(%(%d,%));", iota(N));
}
mixin(buildIota());
}
template NewNoDuplicates(T...)
{
import std.meta : AliasSeq;
template getAlias(size_t idx) {
alias list = T[0 .. idx];
alias item = T[idx];
import std.algorithm: canFind;
static if([__traits(isSame, list, item)...].canFind(true))
alias getAlias = AliasSeq!();
else
alias getAlias = T[idx];
}
alias idxList = staticIota!(T.length)[1 .. $];
alias NewNoDuplicates = AliasSeq!(T[0], (getAlias!(idxList)...));
}
@safe unittest
{
import std.meta : AliasSeq;
alias Types = AliasSeq!(int, long, long, int, float);
alias TL = NewNoDuplicates!(Types);
static assert(is(TL == AliasSeq!(int, long, float)));
}
@safe unittest
{
import std.meta : AliasSeq, Repeat;
import std.range : iota;
// https://issues.dlang.org/show_bug.cgi?id=14561: huge enums
alias LongList = Repeat!(1500, int);
static assert(NewNoDuplicates!LongList.length == 1);
// https://issues.dlang.org/show_bug.cgi?id=17995: huge enums, revisited
alias a = NewNoDuplicates!(AliasSeq!(int, Repeat!(1000, char)));
alias b = NewNoDuplicates!(AliasSeq!(int, Repeat!(10, char)));
static assert(a.length == b.length);
/*static assert(NewNoDuplicates!(aliasSeqOf!(iota(7)), aliasSeqOf!(iota(7))) == aliasSeqOf!(iota(7)));
static assert(NewNoDuplicates!(aliasSeqOf!(iota(8)), aliasSeqOf!(iota(8))) == aliasSeqOf!(iota(8)));*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment