-removed string_intern

-removed unused traits
This commit is contained in:
Mergul 2019-04-06 14:15:02 +00:00
parent 201681e4ca
commit 77f67004dd
2 changed files with 2 additions and 166 deletions

View file

@ -1,138 +0,0 @@
module ecs.string_intern;
import ecs.hash_map;
import ecs.traits : isForeachDelegateWithI;
import std.experimental.allocator;
import std.experimental.allocator.mallocator;
import std.traits : Parameters;
private __gshared static HashMap!(const(char)[], StringIntern) gStringInterns;
struct StringIntern {
private const(char)* strPtr;
this(const(char)[] fromStr) {
opAssign(fromStr);
}
void reset() {
strPtr=null;
}
size_t length() {
if (strPtr is null) {
return 0;
}
return *cast(size_t*)(strPtr - 8);
}
const(char)[] str() {
if (strPtr is null) {
return null;
}
return strPtr[0 .. length];
}
const(char)[] cstr() {
if (strPtr is null) {
return "\0";
}
return strPtr[0 .. length + 1];
}
bool opEquals()(auto ref const StringIntern s) {
return strPtr == s.strPtr;
}
bool opEquals()(auto ref const(char[]) s) {
return str() == s;
}
void opAssign(const(char)[] fromStr) {
if (fromStr.length == 0) {
return;
}
StringIntern defaultValue;
StringIntern internedStr = gStringInterns.get(fromStr, defaultValue);
if (internedStr.length == 0) {
internedStr.strPtr = allocStr(fromStr).ptr;
gStringInterns.add(internedStr.str, internedStr);
}
strPtr = internedStr.strPtr;
}
const(char)[] opSlice() {
if (strPtr is null) {
return null;
}
return strPtr[0 .. length];
}
private const(char)[] allocStr(const(char)[] fromStr) {
char[] data = Mallocator.instance.makeArray!(char)(fromStr.length + size_t.sizeof + 1);
size_t* len = cast(size_t*) data.ptr;
*len = fromStr.length;
data[size_t.sizeof .. $ - 1] = fromStr;
data[$ - 1] = '\0';
return data[size_t.sizeof .. $ - 1];
}
}
unittest {
static assert(StringIntern.sizeof == size_t.sizeof);
const(char)[] chA = ['a', 'a'];
char[] chB = ['o', 't', 'h', 'e', 'r'];
const(char)[] chC = ['o', 't', 'h', 'e', 'r'];
string chD = "other";
StringIntern strA;
StringIntern strB = StringIntern("");
StringIntern strC = StringIntern("a");
StringIntern strD = "a";
StringIntern strE = "aa";
StringIntern strF = chA;
StringIntern strG = chB;
assert(strA == strB);
assert(strA != strC);
assert(strC == strD);
assert(strD != strE);
assert(strE == strF);
assert(strD.length == 1);
assert(strE.length == 2);
assert(strG.length == 5);
strA = "other";
assert(strA == "other");
assert(strA == chB);
assert(strA == chC);
assert(strA == chD);
assert(strA.str.ptr[strA.str.length] == '\0');
assert(strA.cstr[$ - 1] == '\0');
foreach (char c; strA) {
}
foreach (int i, char c; strA) {
}
foreach (ubyte i, char c; strA) {
}
foreach (c; strA) {
}
}
unittest {
import ecs.hash_map : HashMap;
HashMap!(StringIntern, StringIntern) map;
map.add(StringIntern("aaa"), StringIntern("bbb"));
map.add(StringIntern("aaa"), StringIntern("bbb"));
assert(map.length == 1);
assert(map.get(StringIntern("aaa")) == StringIntern("bbb"));
}

View file

@ -1,28 +1,7 @@
module ecs.traits; module ecs.traits;
import std.traits; import std.traits;
bool isForeachDelegateWithI(DG)() {
return is(DG == delegate) && is(ReturnType!DG == int)
&& Parameters!DG.length == 2 && isIntegral!(Parameters!(DG)[0]);
}
unittest {
assert(isForeachDelegateWithI!(int delegate(int, double)));
assert(isForeachDelegateWithI!(int delegate(int, double) @nogc nothrow));
assert(!isForeachDelegateWithI!(int delegate(double, double)));
}
bool isForeachDelegateWithoutI(DG)() {
return is(DG == delegate) && is(ReturnType!DG == int) && Parameters!DG.length == 1;
}
unittest {
assert(isForeachDelegateWithoutI!(int delegate(int)));
assert(isForeachDelegateWithoutI!(int delegate(size_t) @nogc nothrow));
assert(!isForeachDelegateWithoutI!(void delegate(int)));
}
bool isForeachDelegateWithTypes(DG, Types...)() { bool isForeachDelegateWithTypes(DG, Types...)() {
return is(DG == delegate) && is(ReturnType!DG == int) && is(Parameters!DG == Types); return is(DG == delegate) && is(ReturnType!DG == int) && is(Parameters!DG == Types);
} }
@ -31,9 +10,4 @@ unittest {
assert(isForeachDelegateWithTypes!(int delegate(int, int), int, int)); assert(isForeachDelegateWithTypes!(int delegate(int, int), int, int));
assert(isForeachDelegateWithTypes!(int delegate(ref int, ref int), int, int)); assert(isForeachDelegateWithTypes!(int delegate(ref int, ref int), int, int));
assert(!isForeachDelegateWithTypes!(int delegate(double), int, int)); assert(!isForeachDelegateWithTypes!(int delegate(double), int, int));
} }
auto assumeNoGC(T)(T t) if (isFunctionPointer!T || isDelegate!T) {
enum attrs = functionAttributes!T | FunctionAttribute.nogc;
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
}