From d2ae4895c401444d824931310af619c2736461dd Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 1 Aug 2020 15:48:07 +0200 Subject: [PATCH] Added index operator for cloning a range Closing https://code.tsommer.org/thorsten/ExaArray/issues/5 --- ExaArray/ExaArray1D.cs | 17 +++++++++++++++++ ExaArrayTests/ExaArray1DTests.cs | 6 ++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/ExaArray/ExaArray1D.cs b/ExaArray/ExaArray1D.cs index 51e002b..94f9e23 100644 --- a/ExaArray/ExaArray1D.cs +++ b/ExaArray/ExaArray1D.cs @@ -188,6 +188,23 @@ namespace Exa } } + /// + /// Creates a new ExaArray1D from this instance, respecting the given range. + /// + /// + /// When T is a value type, data gets copied as values. When T is a reference type, the pointers + /// to the original objects are copied. Thus, this factory method does not create a deep copy. + /// + /// Performance: O(n) + /// + /// The indices are inclusive. + /// + /// The first source element which should be part of the new array. + /// The last source element which should be part of the new array. + /// The new instance + /// Throws, when one or both of the indices are out of range. + public ExaArray1D this[ulong indexFrom, ulong indexTo] => ExaArray1D.CreateFrom(this, indexFrom, indexTo); + /// /// Yields an enumerator across all elements. /// diff --git a/ExaArrayTests/ExaArray1DTests.cs b/ExaArrayTests/ExaArray1DTests.cs index 7400637..dc69630 100644 --- a/ExaArrayTests/ExaArray1DTests.cs +++ b/ExaArrayTests/ExaArray1DTests.cs @@ -539,13 +539,15 @@ namespace ExaArrayTests var exPerf = new ExaArray1D(Strategy.MAX_PERFORMANCE); exPerf.Extend(3 * MAX); // more than one chunk - var next = ExaArray1D.CreateFrom(exPerf, 0, exPerf.Length - 1); + var next1 = ExaArray1D.CreateFrom(exPerf, 0, exPerf.Length - 1); var next2 = exPerf.Clone(); var next3 = exPerf.Clone(0, exPerf.Length - 1); + var next4 = exPerf[0, exPerf.Length - 1]; - Assert.That(next.Length, Is.EqualTo(exPerf.Length)); + Assert.That(next1.Length, Is.EqualTo(exPerf.Length)); Assert.That(next2.Length, Is.EqualTo(exPerf.Length)); Assert.That(next3.Length, Is.EqualTo(exPerf.Length)); + Assert.That(next4.Length, Is.EqualTo(exPerf.Length)); } [Test]