From 6c3f2b718492ae052f9ea8e9b8899df67bca2270 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 23 Feb 2013 18:37:11 +0100 Subject: [PATCH] LA: Regroup Vector IList implemenation, fix portable build --- src/Numerics/LinearAlgebra/Generic/Vector.cs | 140 +++++++++---------- 1 file changed, 66 insertions(+), 74 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index 33a959d9..96b22a92 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -31,7 +31,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic using System.Collections.Generic; using System.Numerics; using System.Text; - using Distributions; using Numerics; using Properties; using Storage; @@ -44,11 +43,11 @@ namespace MathNet.Numerics.LinearAlgebra.Generic [Serializable] public abstract class Vector : #if PORTABLE - IFormattable, IEnumerable, IEquatable> + IFormattable, IEnumerable, IEquatable>, IList, IList #else - IFormattable, IEnumerable, IEquatable>, ICloneable, IList, IList + IFormattable, IEnumerable, IEquatable>, IList, IList, ICloneable #endif - where T : struct, IEquatable, IFormattable + where T : struct, IEquatable, IFormattable { /// /// The zero value for type T. @@ -1514,29 +1513,41 @@ namespace MathNet.Numerics.LinearAlgebra.Generic #endregion #region IList - int IList.Add(object value) + + bool IList.IsReadOnly { - throw new NotSupportedException(); + get { return false; } } - bool IList.Contains(object value) + bool IList.IsFixedSize + { + get { return true; } + } + + object IList.this[int index] + { + get { return Storage[index]; } + set { Storage[index] = (T)value; } + } + + int IList.IndexOf(object value) { if (!(value is T)) - return false; - else { - return ((IList)this).Contains((T)value); + return -1; } + + return ((IList)this).IndexOf((T)value); } - int IList.IndexOf(object value) + bool IList.Contains(object value) { if (!(value is T)) - return -1; - else { - return ((IList)this).IndexOf((T)value); + return false; } + + return ((ICollection)this).Contains((T)value); } void IList.Insert(int index, object value) @@ -1544,14 +1555,9 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new NotSupportedException(); } - bool IList.IsFixedSize - { - get { return true; } - } - - bool IList.IsReadOnly + int IList.Add(object value) { - get { return false; } + throw new NotSupportedException(); } void IList.Remove(object value) @@ -1564,77 +1570,54 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new NotSupportedException(); } - object IList.this[int index] + #endregion + #region ICollection + + bool ICollection.IsSynchronized { - get - { - return this[index]; - } - set - { - this[index] = (T)value; - } + get { return false; } + } + + object ICollection.SyncRoot + { + get { return null; } } void ICollection.CopyTo(Array array, int index) { if (array == null) - throw new ArgumentNullException("array"); - if (index < 0) - throw new ArgumentOutOfRangeException("index"); - if (array.Rank > 1) - throw new ArgumentException("Only accept one dimensional arrays.", "array"); - if (array.LongLength < index + this.Count) - throw new ArgumentException("Destination array not large enough to store elements requested to copy."); - - try { - this.Storage.CopySubVectorTo(new DenseVectorStorage(array.Length, (T[])array), 0, index, this.Count, skipClearing: true); + throw new ArgumentNullException("array"); } - catch (InvalidCastException) + if (array.Rank != 1) { - throw new ArgumentException("Input type does not match type of Vector", "array"); + throw new ArgumentException(Resources.ArgumentSingleDimensionArray, "array"); } - } - bool ICollection.IsSynchronized - { - get { return false; } + Storage.CopySubVectorTo(new DenseVectorStorage(array.Length, (T[])array), 0, index, Count); } - object ICollection.SyncRoot - { - get { return null; } - } #endregion + #region ICollection - #region IList - void ICollection.Add(T item) - { - throw new NotSupportedException(); - } - int IList.IndexOf(T item) + bool ICollection.IsReadOnly { - for (int ii = 0; ii < this.Count; ++ii) - { - if (this[ii].Equals(item)) - return ii; - } - return -1; + get { return false; } } - void IList.Insert(int index, T item) + void ICollection.Add(T item) { throw new NotSupportedException(); } - void IList.RemoveAt(int index) + bool ICollection.Remove(T item) { throw new NotSupportedException(); } bool ICollection.Contains(T item) { + // Do NOT convert this loop to LINQ (since LINQ would redirect to this very method)! foreach (var x in this) { if (x.Equals(item)) @@ -1646,27 +1629,36 @@ namespace MathNet.Numerics.LinearAlgebra.Generic void ICollection.CopyTo(T[] array, int arrayIndex) { if (array == null) + { throw new ArgumentNullException("array"); - if (arrayIndex < 0) - throw new ArgumentOutOfRangeException("arrayIndex"); - if (array.Rank > 1) - throw new ArgumentException("Only accept one dimensional arrays.", "array"); - if (array.LongLength < arrayIndex + this.Count) - throw new ArgumentException("Destination array not large enough to store elements requested to copy."); + } + + Storage.CopySubVectorTo(new DenseVectorStorage(array.Length, array), 0, arrayIndex, Count); + } - this.Storage.CopySubVectorTo(new DenseVectorStorage(array.Length, array), 0, arrayIndex, this.Count, skipClearing: true); + #endregion + #region IList + int IList.IndexOf(T item) + { + for (int i = 0; i < Count; ++i) + { + if (this[i].Equals(item)) + return i; + } + return -1; } - bool ICollection.IsReadOnly + void IList.Insert(int index, T item) { - get { return false; } + throw new NotSupportedException(); } - bool ICollection.Remove(T item) + void IList.RemoveAt(int index) { throw new NotSupportedException(); } + #endregion #endregion