summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2017-11-01 13:03:48 +0100
committerEugen Wissner <belka@caraus.de>2017-11-01 13:03:48 +0100
commit12fb9ff9f669680daa430cc65f57ac52ec702501 (patch)
tree1d1310e62c6b6b34eaf4a7ae14286c7cfb8d5a80
parent392cdcf192a386c88c3d64d2a3903dd9a1d8d8e8 (diff)
downloadtanya-12fb9ff9f669680daa430cc65f57ac52ec702501.tar.gz
Add algorithm.mutation.swap
-rw-r--r--source/tanya/algorithm/mutation.d27
-rw-r--r--source/tanya/container/array.d15
-rw-r--r--source/tanya/container/list.d1
-rw-r--r--source/tanya/container/set.d1
-rw-r--r--source/tanya/container/string.d2
-rw-r--r--source/tanya/math/mp.d3
-rw-r--r--source/tanya/memory/smartref.d2
7 files changed, 41 insertions, 10 deletions
diff --git a/source/tanya/algorithm/mutation.d b/source/tanya/algorithm/mutation.d
index 0076486..2543cf9 100644
--- a/source/tanya/algorithm/mutation.d
+++ b/source/tanya/algorithm/mutation.d
@@ -204,3 +204,30 @@ T move(T)(ref T source)
move(x, x);
assert(x == 5);
}
+
+/**
+ * Exchanges the values of $(D_PARAM a) and $(D_PARAM b).
+ *
+ * $(D_PSYMBOL swap) moves the contents of $(D_PARAM a) and $(D_PARAM b)
+ * without calling its postblits or destructors.
+ *
+ * Params:
+ * a = The first object.
+ * a = The second object.
+ */
+void swap(T)(ref T a, ref T b) @trusted
+{
+ T tmp = void;
+ moveEmplace(a, tmp);
+ moveEmplace(b, a);
+ moveEmplace(tmp, b);
+}
+
+///
+@nogc nothrow pure @safe unittest
+{
+ int a = 3, b = 5;
+ swap(a, b);
+ assert(a == 5);
+ assert(b == 3);
+}
diff --git a/source/tanya/container/array.d b/source/tanya/container/array.d
index 4891588..ee0667c 100644
--- a/source/tanya/container/array.d
+++ b/source/tanya/container/array.d
@@ -20,9 +20,6 @@ import std.algorithm.mutation : bringToFront,
copy,
fill,
initializeAll,
- moveEmplaceAll,
- moveAll,
- swap,
uninitializedFill;
import std.meta;
import tanya.algorithm.mutation;
@@ -272,7 +269,10 @@ struct Array(T)
{
// Move each element.
reserve(init.length_);
- moveEmplaceAll(init.data[0 .. init.length_], this.data[0 .. init.length_]);
+ foreach (ref target; this.data[0 .. init.length_])
+ {
+ moveEmplace(*init.data++, target);
+ }
this.length_ = init.length_;
// Destructor of init should destroy it here.
}
@@ -661,7 +661,12 @@ struct Array(T)
body
{
auto end = this.data + this.length;
- moveAll(Range(this, r.end, end), Range(this, r.begin, end));
+ auto source = Range(this, r.end, end);
+ auto target = Range(this, r.begin, end);
+ for (; !source.empty; source.popFront(), target.popFront())
+ {
+ move(source.front, target.front);
+ }
length = length - r.length;
return Range(this, r.begin, this.data + length);
}
diff --git a/source/tanya/container/list.d b/source/tanya/container/list.d
index 696d1e9..d1f384b 100644
--- a/source/tanya/container/list.d
+++ b/source/tanya/container/list.d
@@ -15,7 +15,6 @@
module tanya.container.list;
import std.algorithm.comparison;
-import std.algorithm.mutation : swap;
import std.algorithm.searching;
import tanya.algorithm.mutation;
import tanya.container.entry;
diff --git a/source/tanya/container/set.d b/source/tanya/container/set.d
index 3bf0dc5..17b7dc7 100644
--- a/source/tanya/container/set.d
+++ b/source/tanya/container/set.d
@@ -15,7 +15,6 @@
*/
module tanya.container.set;
-import std.algorithm.mutation : swap;
import tanya.algorithm.mutation;
import tanya.container;
import tanya.container.entry;
diff --git a/source/tanya/container/string.d b/source/tanya/container/string.d
index ca5b946..73aab41 100644
--- a/source/tanya/container/string.d
+++ b/source/tanya/container/string.d
@@ -27,7 +27,7 @@
module tanya.container.string;
import std.algorithm.comparison;
-import std.algorithm.mutation : bringToFront, copy, swap;
+import std.algorithm.mutation : bringToFront, copy;
import std.algorithm.searching;
static import std.range;
import tanya.algorithm.mutation;
diff --git a/source/tanya/math/mp.d b/source/tanya/math/mp.d
index 6980a29..59c2baf 100644
--- a/source/tanya/math/mp.d
+++ b/source/tanya/math/mp.d
@@ -15,8 +15,9 @@
module tanya.math.mp;
import std.algorithm.comparison;
-import std.algorithm.mutation;
+import std.algorithm.mutation : fill, copy, reverse;
import std.range;
+import tanya.algorithm.mutation;
import tanya.container.array;
import tanya.encoding.ascii;
import tanya.memory;
diff --git a/source/tanya/memory/smartref.d b/source/tanya/memory/smartref.d
index bc7999f..25a16d3 100644
--- a/source/tanya/memory/smartref.d
+++ b/source/tanya/memory/smartref.d
@@ -24,7 +24,7 @@
module tanya.memory.smartref;
import std.algorithm.comparison;
-import std.algorithm.mutation;
+import tanya.algorithm.mutation;
import tanya.conv;
import tanya.exception;
import tanya.memory;