Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next version of the containers library #104

Merged
merged 21 commits into from
Mar 21, 2018
Merged
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
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ insert_final_newline = true
indent_size = 4
tab_width = 4
trim_trailing_whitespace = true

[*.d]
indent_style = tab

[*.json]
indent_style = space
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ __test__*__
.directory
*.userprefs
.vscode
emsi_containers-test-unittest
8 changes: 7 additions & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@
],
"dependencies": {
"stdx-allocator": "~>2.77.0"
}
},
"configurations": [
{
"name": "unittest",
"versions": ["emsi_containers_unittest"]
}
]
}
46 changes: 29 additions & 17 deletions src/containers/cyclicbuffer.d
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ struct CyclicBuffer(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
/// ditto
alias insert = insertBack;

/// ditto
alias insertAnywhere = insertBack;

/// ditto
alias put = insertBack;

/**
* Removes the item at the start of the buffer.
*/
Expand All @@ -197,6 +203,9 @@ struct CyclicBuffer(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
.destroy(storage[pos]);
}

/// ditto
alias popFront = removeFront;

/**
* Removes the item at the end of the buffer.
*/
Expand All @@ -210,6 +219,9 @@ struct CyclicBuffer(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
.destroy(storage[pos]);
}

/// ditto
alias popBack = removeBack;

/// Accesses to the item at the start of the buffer.
auto ref front(this This)() nothrow pure @property @safe
{
Expand Down Expand Up @@ -434,7 +446,7 @@ private:
size_t start, end, _length;
}

version (unittest) private
version(emsi_containers_unittest) private
{
import std.algorithm.comparison : equal;
import stdx.allocator.gc_allocator : GCAllocator;
Expand Down Expand Up @@ -467,7 +479,7 @@ version (unittest) private
}
}

unittest
version(emsi_containers_unittest) unittest
{
static void test(int size)
{
Expand Down Expand Up @@ -512,7 +524,7 @@ unittest
test(size);
}

unittest
version(emsi_containers_unittest) unittest
{
static void test(int prefix, int suffix, int newSize)
{
Expand All @@ -535,7 +547,7 @@ unittest
test(a, b, c);
}

unittest
version(emsi_containers_unittest) unittest
{
int a = 0;
{
Expand All @@ -560,7 +572,7 @@ unittest
assert(a == 21);
}

unittest
version(emsi_containers_unittest) unittest
{
int* a = new int;
CyclicBuffer!C b;
Expand All @@ -587,7 +599,7 @@ unittest
assert(*a == 0 || *a == 1);
}

unittest
version(emsi_containers_unittest) unittest
{
CyclicBuffer!int b;
b.insertFront(10);
Expand All @@ -608,7 +620,7 @@ unittest
assert(b[3] == 7);
}

unittest
version(emsi_containers_unittest) unittest
{
import std.range : isInputRange, isForwardRange, isBidirectionalRange, isRandomAccessRange;
CyclicBuffer!int b;
Expand All @@ -618,13 +630,13 @@ unittest
static assert(isRandomAccessRange!(typeof(b[])));
}

unittest
version(emsi_containers_unittest) unittest
{
CyclicBuffer!int b;
assert(b[].empty);
}

unittest
version(emsi_containers_unittest) unittest
{
FreeList!(Mallocator, 0, 64) alloc;
FreeList!(GCAllocator, 0, 64) alloc2;
Expand All @@ -633,7 +645,7 @@ unittest
auto b3 = CyclicBuffer!(int, GCAllocator)();
}

unittest
version(emsi_containers_unittest) unittest
{
static void testConst(const ref CyclicBuffer!int b, int x)
{
Expand Down Expand Up @@ -664,7 +676,7 @@ unittest
assert(b[][0] == 5);
}

unittest
version(emsi_containers_unittest) unittest
{
int a = 0;
{
Expand All @@ -688,7 +700,7 @@ unittest
assert(a == 10);
}

unittest
version(emsi_containers_unittest) unittest
{
CyclicBuffer!int b;
foreach (i; 0 .. 4)
Expand All @@ -702,7 +714,7 @@ unittest
assert(equal(b[], [2, 3, 4, 5]));
}

unittest
version(emsi_containers_unittest) unittest
{
CyclicBuffer!int b;
foreach (i; 0 .. 4)
Expand All @@ -718,7 +730,7 @@ unittest
assert(equal(b[], [3, 4, 5, 6]));
}

unittest
version(emsi_containers_unittest) unittest
{
static void test(ref CyclicBuffer!int b)
{
Expand Down Expand Up @@ -772,7 +784,7 @@ unittest
}
}

unittest
version(emsi_containers_unittest) unittest
{
CyclicBuffer!int b;
foreach (i; 0 .. 10)
Expand All @@ -782,7 +794,7 @@ unittest
assert(b.capacity >= 12);
}

unittest
version(emsi_containers_unittest) unittest
{
CyclicBuffer!int b;
foreach (i; 0 .. 6)
Expand All @@ -794,7 +806,7 @@ unittest
assert(equal(b[], [7, 6, 0, 1, 2, 3, 4, 5]));
}

unittest
version(emsi_containers_unittest) unittest
{
static class Foo
{
Expand Down
66 changes: 36 additions & 30 deletions src/containers/dynamicarray.d
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct DynamicArray(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
/**
* Inserts the given value into the end of the array.
*/
void insert(T value)
void insertBack(T value)
{
import stdx.allocator.mallocator : Mallocator;
import containers.internal.node : shouldAddGCRange;
Expand Down Expand Up @@ -152,7 +152,13 @@ struct DynamicArray(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
}

/// ditto
alias put = insert;
alias insert = insertBack;

/// ditto
alias insertAnywhere = insertBack;

/// ditto
alias put = insertBack;

/**
* ~= operator overload
Expand Down Expand Up @@ -311,7 +317,7 @@ struct DynamicArray(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
}

/**
* Removes the last element from the array.
* Removes the last element from the array.
*/
void removeBack()
{
Expand Down Expand Up @@ -381,7 +387,7 @@ private:
size_t l;
}

unittest
version(emsi_containers_unittest) unittest
{
import std.algorithm : equal;
import std.range : iota;
Expand Down Expand Up @@ -411,7 +417,7 @@ unittest
assert(ints[0] == 1337);
}

version(unittest)
version(emsi_containers_unittest)
{
class Cls
{
Expand All @@ -429,7 +435,7 @@ version(unittest)
}
}

unittest
version(emsi_containers_unittest) unittest
{
int a = 0;
{
Expand All @@ -439,7 +445,7 @@ unittest
assert(a == 0); // Destructor not called.
}

unittest
version(emsi_containers_unittest) unittest
{
import std.exception : assertThrown;
import core.exception : RangeError;
Expand Down Expand Up @@ -481,7 +487,7 @@ unittest
assert(two.length == 0);
}

unittest
version(emsi_containers_unittest) unittest
{
int a = 0;
DynamicArray!(Cls, Mallocator, true) arr;
Expand All @@ -491,7 +497,7 @@ unittest
assert(a == 0); // Destructor not called.
}

unittest
version(emsi_containers_unittest) unittest
{
DynamicArray!(int*, Mallocator, true) arr;

Expand All @@ -506,7 +512,7 @@ unittest
assert (*slice[1] == 2);
}

unittest
version(emsi_containers_unittest) unittest
{
import std.format : format;

Expand All @@ -526,22 +532,22 @@ unittest

}

@system unittest
version(emsi_containers_unittest) @system unittest
{
DynamicArray!int a;
a.reserve(1000);
assert(a.length == 0);
assert(a.empty);
assert(a.arr.length >= 1000);
int* p = a[].ptr;
foreach (i; 0 .. 1000)
{
a.insert(i);
}
assert(p is a[].ptr);
DynamicArray!int a;
a.reserve(1000);
assert(a.length == 0);
assert(a.empty);
assert(a.arr.length >= 1000);
int* p = a[].ptr;
foreach (i; 0 .. 1000)
{
a.insert(i);
}
assert(p is a[].ptr);
}

unittest
version(emsi_containers_unittest) unittest
{
// Ensure that Array.insert doesn't call the destructor for
// a struct whose state is uninitialized memory.
Expand All @@ -562,17 +568,17 @@ unittest
assert(a == 1);
}

@nogc unittest
version(emsi_containers_unittest) @nogc unittest
{
struct HStorage
{
import containers.dynamicarray: DynamicArray;
DynamicArray!int storage;
}
struct HStorage
{
import containers.dynamicarray: DynamicArray;
DynamicArray!int storage;
}
auto hs = HStorage();
}

@nogc unittest
version(emsi_containers_unittest) @nogc unittest
{
DynamicArray!char a;
const DynamicArray!char b = a ~ "def";
Expand Down
Loading