Skip to content

Commit e5c21fc

Browse files
committed
Fix #39
1 parent db72004 commit e5c21fc

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/containers/dynamicarray.d

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,38 @@ struct DynamicArray(T, Allocator = Mallocator, bool supportGC = shouldAddGCRange
125125
/// ditto
126126
alias put = insert;
127127

128+
/**
129+
* ~= operator overload
130+
*/
131+
void opOpAssign(string op)(T value) if (op == "~")
132+
{
133+
insert(value);
134+
}
135+
136+
/**
137+
* ~ operator overload
138+
*/
139+
typeof(this) opBinary(string op)(ref typeof(this) other) if (op == "~")
140+
{
141+
typeof(this) ret;
142+
foreach (value; arr[0 .. l])
143+
ret.insert(value);
144+
foreach (value; other.arr[0 .. other.l])
145+
ret.insert(value);
146+
return ret;
147+
}
148+
149+
/// ditto
150+
typeof(this) opBinary(string op)(T[] values) if (op == "~")
151+
{
152+
typeof(this) ret;
153+
foreach (value; arr[0 .. l])
154+
ret.insert(value);
155+
foreach (value; values)
156+
ret.insert(value);
157+
return ret;
158+
}
159+
128160
/**
129161
* Remove the item at the given index from the array.
130162
*/
@@ -340,3 +372,18 @@ unittest
340372
assert (*slice[0] == 1);
341373
assert (*slice[1] == 2);
342374
}
375+
376+
unittest
377+
{
378+
import std.format : format;
379+
380+
DynamicArray!int arr;
381+
foreach (int i; 0 .. 10)
382+
arr ~= i;
383+
assert(arr.length == 10, "arr.length = %d".format(arr.length));
384+
385+
auto arr2 = arr ~ arr;
386+
assert(arr2.length == 20);
387+
auto arr3 = arr2 ~ [100, 99, 98];
388+
assert(arr3.length == 23);
389+
}

0 commit comments

Comments
 (0)