Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ DEPFILE := $(OBJDIR)/$(TARGETNAME).d

DFLAGS := $(DFLAGS) -preview=bitfields -preview=rvaluerefparam -preview=nosharedaccess -preview=in

ifeq ($(OS),windows)
SOURCES := $(shell dir /s /b $(SRCDIR)\\*.d)
else
SOURCES := $(shell find "$(SRCDIR)" -type f -name '*.d')
endif

# Set target file based on build type and OS
ifeq ($(BUILD_TYPE),exe)
Expand Down Expand Up @@ -93,30 +89,19 @@ else
endif

ifeq ($(CONFIG),unittest)
DFLAGS := $(DFLAGS) -unittest
DFLAGS := $(DFLAGS) -unittest -main
endif

-include $(DEPFILE)

$(TARGET):
ifeq ($(OS),windows)
@if not exist "obj" mkdir "obj" > nul 2>&1
@if not exist "$(subst /,\,$(OBJDIR))" mkdir "$(subst /,\,$(OBJDIR))" > nul 2>&1
@if not exist "bin" mkdir "bin" > nul 2>&1
@if not exist "$(subst /,\,$(TARGETDIR))" mkdir "$(subst /,\,$(TARGETDIR))" > nul 2>&1
else
mkdir -p $(OBJDIR) $(TARGETDIR)
endif
ifeq ($(D_COMPILER),ldc)
"$(DC)" $(DFLAGS) $(BUILD_CMD_FLAGS) -of$(TARGET) -od$(OBJDIR) -deps=$(DEPFILE) $(SOURCES)
else ifeq ($(D_COMPILER),dmd)
ifeq ($(BUILD_TYPE),lib)
"$(DC)" $(DFLAGS) $(BUILD_CMD_FLAGS) -of$(notdir $(TARGET)) -od$(OBJDIR) -makedeps $(SOURCES) > $(DEPFILE)
ifeq ($(OS),windows)
move "$(subst /,\,$(OBJDIR))\\$(notdir $(TARGET))" "$(subst /,\,$(TARGETDIR))" > nul
else
"$(DC)" $(DFLAGS) $(BUILD_CMD_FLAGS) -of$(OBJDIR)/$(notdir $(TARGET)) -od$(OBJDIR) -makedeps $(SOURCES) > $(DEPFILE)
mv "$(OBJDIR)/$(notdir $(TARGET))" "$(TARGETDIR)"
endif
else # exe
"$(DC)" $(DFLAGS) $(BUILD_CMD_FLAGS) -of$(TARGET) -od$(OBJDIR) -makedeps $(SOURCES) > $(DEPFILE)
endif
Expand Down
46 changes: 23 additions & 23 deletions src/urt/algorithm.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module urt.algorithm;

import urt.traits : lvalueOf;
import urt.traits : lvalue_of;
import urt.util : swap;

version = SmallSize;
Expand All @@ -10,18 +10,18 @@ nothrow @nogc:

auto compare(T, U)(auto ref T a, auto ref U b)
{
static if (__traits(compiles, lvalueOf!T.opCmp(lvalueOf!U)))
static if (__traits(compiles, lvalue_of!T.opCmp(lvalue_of!U)))
return a.opCmp(b);
else static if (__traits(compiles, lvalueOf!U.opCmp(lvalueOf!T)))
else static if (__traits(compiles, lvalue_of!U.opCmp(lvalue_of!T)))
return -b.opCmp(a);
else static if (is(T : A[], A))
{
import urt.traits : isPrimitive;
import urt.traits : is_primitive;

auto ai = a.ptr;
auto bi = b.ptr;
size_t len = a.length < b.length ? a.length : b.length;
static if (isPrimitive!A)
static if (is_primitive!A)
{
// compare strings
foreach (i; 0 .. len)
Expand All @@ -48,7 +48,7 @@ auto compare(T, U)(auto ref T a, auto ref U b)
return a < b ? -1 : (a > b ? 1 : 0);
}

size_t binarySearch(alias pred = void, T, Cmp...)(T[] arr, auto ref Cmp cmpArgs)
size_t binary_search(alias pred = void, T, Cmp...)(T[] arr, auto ref Cmp cmp_args)
{
T* p = arr.ptr;
size_t low = 0;
Expand All @@ -59,15 +59,15 @@ size_t binarySearch(alias pred = void, T, Cmp...)(T[] arr, auto ref Cmp cmpArgs)
static if (is(pred == void))
{
// should we chase the first in a sequence of same values?
if (p[mid] < cmpArgs[0])
if (p[mid] < cmp_args[0])
low = mid + 1;
else
high = mid;
}
else
{
// should we chase the first in a sequence of same values?
int cmp = pred(p[mid], cmpArgs);
int cmp = pred(p[mid], cmp_args);
if (cmp < 0)
low = mid + 1;
else
Expand All @@ -76,12 +76,12 @@ size_t binarySearch(alias pred = void, T, Cmp...)(T[] arr, auto ref Cmp cmpArgs)
}
static if (is(pred == void))
{
if (p[low] == cmpArgs[0])
if (p[low] == cmp_args[0])
return low;
}
else
{
if (pred(p[low], cmpArgs) == 0)
if (pred(p[low], cmp_args) == 0)
return low;
}
return arr.length;
Expand All @@ -93,7 +93,7 @@ void qsort(alias pred = void, T)(T[] arr)
version (SmallSize)
{
static if (is(pred == void))
static if (__traits(compiles, lvalueOf!T.opCmp(lvalueOf!T)))
static if (__traits(compiles, lvalue_of!T.opCmp(lvalue_of!T)))
static int compare(const void* a, const void* b) nothrow @nogc
=> (*cast(const T*)a).opCmp(*cast(const T*)b);
else
Expand Down Expand Up @@ -165,12 +165,12 @@ unittest
assert(s.x == arr[i]);

// test binary search, not that they're sorted...
assert(binarySearch(arr, -1) == 0);
assert(binarySearch!(s => s.x < 30 ? -1 : s.x > 30 ? 1 : 0)(arr2) == 3);
assert(binarySearch(arr, 0) == arr.length);
assert(binary_search(arr, -1) == 0);
assert(binary_search!(s => s.x < 30 ? -1 : s.x > 30 ? 1 : 0)(arr2) == 3);
assert(binary_search(arr, 0) == arr.length);

int[10] rep = [1, 10, 10, 10, 10, 10, 10, 10, 10, 100];
assert(binarySearch(rep, 10) == 1);
assert(binary_search(rep, 10) == 1);
}


Expand All @@ -181,34 +181,34 @@ version (SmallSize)
// just one generic implementation to minimise the code...
// kinda slow though... look at all those multiplies!
// maybe there's some way to make this faster :/
void qsort(void[] arr, size_t elementSize, int function(const void* a, const void* b) nothrow @nogc compare, void function(void* a, void* b) nothrow @nogc swap)
void qsort(void[] arr, size_t element_size, int function(const void* a, const void* b) nothrow @nogc compare, void function(void* a, void* b) nothrow @nogc swap)
{
void* p = arr.ptr;
size_t length = arr.length / elementSize;
size_t length = arr.length / element_size;
if (length > 1)
{
size_t pivotIndex = length / 2;
void* pivot = p + pivotIndex*elementSize;
void* pivot = p + pivotIndex*element_size;

size_t i = 0;
size_t j = length - 1;

while (i <= j)
{
while (compare(p + i*elementSize, pivot) < 0) i++;
while (compare(p + j*elementSize, pivot) > 0) j--;
while (compare(p + i*element_size, pivot) < 0) i++;
while (compare(p + j*element_size, pivot) > 0) j--;
if (i <= j)
{
swap(p + i*elementSize, p + j*elementSize);
swap(p + i*element_size, p + j*element_size);
i++;
j--;
}
}

if (j > 0)
qsort(p[0 .. (j + 1)*elementSize], elementSize, compare, swap);
qsort(p[0 .. (j + 1)*element_size], element_size, compare, swap);
if (i < length)
qsort(p[i*elementSize .. length*elementSize], elementSize, compare, swap);
qsort(p[i*element_size .. length*element_size], element_size, compare, swap);
}
}
}
42 changes: 21 additions & 21 deletions src/urt/async.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Promise!(ReturnType!Fun)* async(alias Fun, size_t stackSize = DefaultStackSize,

// TODO: nice to rework this; maybe make stackSize a not-template-arg, and receive a function call/closure object which stores the args
Promise!(ReturnType!Fun)* async(size_t stackSize = DefaultStackSize, Fun, Args...)(Fun fun, auto ref Args args)
if (isSomeFunction!Fun)
if (is_some_function!Fun)
{
alias Result = ReturnType!Fun;
Promise!Result* r = cast(Promise!Result*)defaultAllocator().alloc(Promise!Result.sizeof, Promise!Result.alignof);
Expand Down Expand Up @@ -60,7 +60,7 @@ Promise!(ReturnType!Fun)* async(size_t stackSize = DefaultStackSize, Fun, Args..

void freePromise(T)(ref Promise!T* promise)
{
assert(promise.state() != Promise!T.State.Pending, "Promise still pending!");
assert(promise.state() != PromiseState.Pending, "Promise still pending!");
defaultAllocator().freeT(promise);
promise = null;
}
Expand All @@ -84,15 +84,15 @@ void asyncUpdate()
}


struct Promise(Result)
enum PromiseState
{
enum State
{
Pending,
Ready,
Failed,
}
Pending,
Ready,
Failed
}

struct Promise(Result)
{
// construct using `async()` functions...
this() @disable;
this(ref typeof(this)) @disable; // disable copy constructor
Expand All @@ -117,29 +117,29 @@ struct Promise(Result)
}
}

State state() const
PromiseState state() const
{
if (async.fibre.wasAborted())
return State.Failed;
return PromiseState.Failed;
else if (async.fibre.isFinished())
return State.Ready;
return PromiseState.Ready;
else
return State.Pending;
return PromiseState.Pending;
}

bool finished() const
=> state() != State.Pending;
=> state() != PromiseState.Pending;

ref Result result()
{
assert(state() == State.Ready, "Promise not fulfilled!");
assert(state() == PromiseState.Ready, "Promise not fulfilled!");
static if (!is(Result == void))
return value;
}

void abort()
{
assert(state() == State.Pending, "Promise already fulfilled!");
assert(state() == PromiseState.Pending, "Promise already fulfilled!");
async.fibre.abort();
}

Expand Down Expand Up @@ -180,11 +180,11 @@ unittest
}

auto p = async!fun(1, 2);
assert(p.state() == p.State.Ready);
assert(p.state() == PromiseState.Ready);
assert(p.result() == 3);
freePromise(p);
p = async!fun(10, 20);
assert(p.state() == p.State.Ready);
assert(p.state() == PromiseState.Ready);
assert(p.result() == 30);
freePromise(p);

Expand All @@ -201,13 +201,13 @@ unittest
}

auto p_yield = async(&fun_yield);
assert(p_yield.state() == p_yield.State.Pending);
assert(p_yield.state() == PromiseState.Pending);
assert(val == 1);
asyncUpdate();
assert(p_yield.state() == p_yield.State.Pending);
assert(p_yield.state() == PromiseState.Pending);
assert(val == 2);
asyncUpdate();
assert(p_yield.state() == p_yield.State.Ready);
assert(p_yield.state() == PromiseState.Ready);
assert(val == 3);
assert(p_yield.result() == 4);
freePromise(p_yield);
Expand Down
Loading
Loading