Skip to content

Commit 6377efb

Browse files
author
Dominic Beger
committed
Implement custom typified Clone-method and the IEquatable interface
1 parent 9a5cf21 commit 6377efb

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
lines changed

SharpMath/Geometry/Matrix.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SharpMath.Geometry
99
/// <summary>
1010
/// Represents a matrix.
1111
/// </summary>
12-
public class Matrix : ICloneable
12+
public class Matrix : IEquatable<Matrix>
1313
{
1414
private readonly double[,] _fields;
1515

@@ -441,6 +441,33 @@ public override int GetHashCode()
441441
}
442442
}
443443

444+
public bool Equals(Matrix other)
445+
{
446+
if (ReferenceEquals(null, other))
447+
return false;
448+
449+
return this == other;
450+
}
451+
452+
/// <summary>
453+
/// Creates a new object that is a copy of the current instance.
454+
/// </summary>
455+
/// <returns>
456+
/// A new object that is a copy of this instance.
457+
/// </returns>
458+
public T Clone<T>() where T : Matrix, new()
459+
{
460+
var cloneMatrix = new T();
461+
for (uint y = 0; y < RowCount; ++y)
462+
{
463+
for (uint x = 0; x < RowCount; ++x)
464+
{
465+
cloneMatrix[y, x] = this[y, x];
466+
}
467+
}
468+
return cloneMatrix;
469+
}
470+
444471
/// <summary>
445472
/// Implements the operator ==.
446473
/// </summary>

SharpMath/Geometry/Point.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace SharpMath.Geometry
1010
/// <summary>
1111
/// Represents a point.
1212
/// </summary>
13-
public class Point : IEnumerable<double>, ICloneable
13+
public class Point : IEnumerable<double>, IEquatable<Point>
1414
{
1515
private readonly double[] _coordinateValues;
1616

@@ -175,6 +175,20 @@ public static Point Subtract(Point first, Point second)
175175
return resultPoint;
176176
}
177177

178+
/// <summary>
179+
/// Creates a new object that is a copy of the current instance.
180+
/// </summary>
181+
/// <returns>
182+
/// A new object that is a copy of this instance.
183+
/// </returns>
184+
public T Clone<T>() where T : Point, new()
185+
{
186+
var clonePoint = new T();
187+
for (uint i = 0; i < Dimension; ++i)
188+
clonePoint[i] = this[i];
189+
return clonePoint;
190+
}
191+
178192
/// <summary>
179193
/// Determines whether the specified <see cref="object" />, is equal to this instance.
180194
/// </summary>
@@ -210,6 +224,14 @@ public override int GetHashCode()
210224
}
211225
}
212226

227+
public bool Equals(Point other)
228+
{
229+
if (ReferenceEquals(null, other))
230+
return false;
231+
232+
return this == other;
233+
}
234+
213235
/// <summary>
214236
/// Implements the operator ==.
215237
/// </summary>

SharpMath/Geometry/Vector.cs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace SharpMath.Geometry
1212
/// <summary>
1313
/// Represents a vector.
1414
/// </summary>
15-
public class Vector : IEnumerable<double>, ICloneable
15+
public class Vector : IEnumerable<double>, IEquatable<Vector>
1616
{
1717
// ReSharper disable once InconsistentNaming
1818
protected readonly double[] _coordinateValues;
@@ -105,20 +105,6 @@ private bool IsZeroVector
105105
get { return this.All(c => FloatingNumber.AreApproximatelyEqual(c, 0)); }
106106
}
107107

108-
/// <summary>
109-
/// Creates a new object that is a copy of the current instance.
110-
/// </summary>
111-
/// <returns>
112-
/// A new object that is a copy of this instance.
113-
/// </returns>
114-
public object Clone()
115-
{
116-
var cloneVector = new Vector(Dimension);
117-
for (uint i = 0; i < Dimension; ++i)
118-
cloneVector[i] = this[i];
119-
return cloneVector;
120-
}
121-
122108
/// <summary>
123109
/// Returns an enumerator that iterates through the collection of coordinates.
124110
/// </summary>
@@ -561,6 +547,28 @@ public override int GetHashCode()
561547
return hash;
562548
}
563549
}
550+
551+
public bool Equals(Vector other)
552+
{
553+
if (ReferenceEquals(null, other))
554+
return false;
555+
556+
return this == other;
557+
}
558+
559+
/// <summary>
560+
/// Creates a new object that is a copy of the current instance.
561+
/// </summary>
562+
/// <returns>
563+
/// A new object that is a copy of this instance.
564+
/// </returns>
565+
public T Clone<T>() where T : Vector, new()
566+
{
567+
var cloneVector = new T();
568+
for (uint i = 0; i < Dimension; ++i)
569+
cloneVector[i] = this[i];
570+
return cloneVector;
571+
}
564572

565573
/// <summary>
566574
/// Implements the operator ==.

0 commit comments

Comments
 (0)