C#Clone Generic

时间:2015-10-19 21:07:23

标签: c# operator-overloading

我有一个类Matrix,并尝试重载此类中的+运算符。 问题出在matrix1matrix2,我无法创建此对象的克隆。我甚至试图使用一些for循环来创建它,但它会抛出一个OutOfMemoryException

    public static Matrix<T> operator +(Matrix<T> matrix1, Matrix<T> matrix2)
    {
        Matrix<T> m1 = matrix1;
        Matrix<T> m2 = matrix2;
        Matrix<T> result = new Matrix<T>();

        if (m1.Cols != m2.Cols || m1.Rows != m2.Rows)
        {
            if (m1.Rows > m2.Rows)
                m2.Rows = m1.Rows;
            else if (m1.Rows < m2.Rows)
                m1.Rows = m2.Rows;
            if (m1.Cols > m2.Cols)
                m2.Cols = m1.Cols;
            else if (m1.Cols < m2.Cols)
                m1.Cols = m2.Cols;
        }

        for (int i = 0; i < m1.Rows; i++)
        {
            result.Add(new List<T>());

            for (int j = 0; j < m1.Cols; j++)
            {
                result[i].Add(new T());
                result[i][j] = Sum<T>(m1[i, j], m2[i, j]);
            }
        }

        result.Cols = m1.Cols;
        return result;
    }

我这样做了

    public static List<List<T>> ListClone(List<List<T>> data)
    {  
        var new_data= new List<List<T>>();

        foreach (var list in data)
        {
            new_data.Add(ListClone(list)); 
        }

        return new_data;
    }

    public static List<T> ListClone(List<T> list)
    {
        var new_list = new List<T>();

        foreach(var i in list)
        {
            new_list.Add((T)i);
        }

        return new_list;
    }

    public static Matrix<T> operator +(Matrix<T> matrix1, Matrix<T> matrix2)
    {
        var m1 = new Matrix<T>();
        m1.Cols = matrix1.Cols;
        m1.Rows = matrix1.Rows;
        m1.Data = ListClone(matrix1.data);

        var m2 = new Matrix<T>();
        m2.Cols = matrix2.Cols;
        m2.Rows = matrix2.Rows;
        m2.Data = ListClone(matrix2.data);

        Matrix<T> result = new Matrix<T>();

        if (m1.Cols!=m2.Cols || m1.Rows!=m2.Rows)
        {
            if (m1.Rows > m2.Rows)
                m2.Rows = m1.Rows;
            else if (m1.Rows < m2.Rows)
                m1.Rows = m2.Rows;
            if (m1.Cols > m2.Cols)
                m2.Cols = m1.Cols;
            else if (m1.Cols < m2.Cols)
                m1.Cols = m2.Cols;

        }

        for (int i = 0; i < m1.Rows; i++)
        {
            result.Add(new List<T>());

            for (int j = 0; j < m1.Cols; j++)
            {
                result[i].Add(new T());
                result[i][j] = Sum<T>(m1[i, j], m2[i, j]);
            }
        }

        result.Cols = m1.Cols;
        return result;           
    }

但也许有人有更好的idiea如何做这个缓和?

1 个答案:

答案 0 :(得分:2)

首先,不要使用List<List<T>>制作矩阵。只需使用List<T>并自己找出访问运营商。

其次,为什么允许用户设置行数或列数?使矩阵不可变并修复构造中的尺寸。

public class Matrix<T> : ICloneable
    where T : struct, IComparable, IFormattable, IConvertible, IComparable<T>, IEquatable<T>
{
    private readonly int _rows;
    private readonly int _columns;
    private readonly IList<T> _data;

    public Matrix(int rows, int columns)
    {
        _rows = rows;
        _columns = columns;
        _data = new List<T>(Rows * Columns);
    }

    private Matrix(int rows, int columns, IList<T> data)
    {
        _rows = rows;
        _columns = columns;
        _data = new List<T>(data);
    }

    public int Rows
    {
        get { return _rows; }
    }

    public int Columns
    {
        get { return _columns; }
    }

    public T Element(int row, int column)
    {
        return _data[row * column];
    }

    public object Clone()
    {
        return new Matrix<T>(Rows, Columns, _data);
    }
}

我作为练习离开的其他运营商