Пример #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            // Make some sparse arrays.
            SparseIntArray array1 = new SparseIntArray(0);

            array1[1, 1]   = 101;
            array1[-1, -1] = -101;
            array1[3, 3]   = 303;
            array1[2, 4]   = 204;
            array1[4, 1]   = 401;

            SparseIntArray array2 = new SparseIntArray(0);

            array2[1, 0]   = 100;
            array2[3, 3]   = 303;
            array2[1, 4]   = 104;
            array2[4, 1]   = 401;
            array2[10, 10] = 1010;

            SparseIntArray array3 = array1.Add(array2);

            resultTextBox.Text =
                array1.ToString() + Environment.NewLine +
                array2.ToString() + Environment.NewLine +
                array3.ToString();
            resultTextBox.Select(0, 0);
        }
Пример #2
0
        // Add two SparseArrays representing matrices.
        public SparseIntArray Add(SparseIntArray other)
        {
            SparseIntArray result = new SparseIntArray(this.DefaultValue);

            // Variables to move through all of the arrays.
            IntArrayRow array1Row = this.TopSentinel.NextRow;
            IntArrayRow array2Row = other.TopSentinel.NextRow;
            IntArrayRow resultRow = result.TopSentinel;

            while ((array1Row != null) && (array2Row != null))
            {
                // Make a new result row.
                resultRow.NextRow     = new IntArrayRow();
                resultRow             = resultRow.NextRow;
                resultRow.RowSentinel = new IntArrayEntry();
                resultRow.NextRow     = null;

                // See which input row has the smaller row number.
                if (array1Row.RowNumber < array2Row.RowNumber)
                {
                    // array1Row comes first. Copy its values into result.
                    resultRow.RowNumber = array1Row.RowNumber;
                    CopyEntries(array1Row.RowSentinel.NextEntry, resultRow.RowSentinel);
                    array1Row = array1Row.NextRow;
                }
                else if (array2Row.RowNumber < array1Row.RowNumber)
                {
                    // array2Row comes first. Copy its values into result.
                    resultRow.RowNumber = array2Row.RowNumber;
                    CopyEntries(array2Row.RowSentinel.NextEntry, resultRow.RowSentinel);
                    array2Row = array2Row.NextRow;
                }
                else
                {
                    // The row numbers are the same. Add their values.
                    resultRow.RowNumber = array1Row.RowNumber;
                    AddEntries(
                        array1Row.RowSentinel.NextEntry,
                        array2Row.RowSentinel.NextEntry,
                        resultRow.RowSentinel);
                    array1Row = array1Row.NextRow;
                    array2Row = array2Row.NextRow;
                }
            }

            // Add any remaining rows.
            if (array1Row != null)
            {
                // Make a new result row.
                resultRow.NextRow     = new IntArrayRow();
                resultRow             = resultRow.NextRow;
                resultRow.RowNumber   = array1Row.RowNumber;
                resultRow.RowSentinel = new IntArrayEntry();
                resultRow.NextRow     = null;
                CopyEntries(array1Row.RowSentinel.NextEntry, resultRow.RowSentinel);
            }
            if (array2Row != null)
            {
                // Make a new result row.
                resultRow.NextRow     = new IntArrayRow();
                resultRow             = resultRow.NextRow;
                resultRow.RowNumber   = array2Row.RowNumber;
                resultRow.RowSentinel = new IntArrayEntry();
                resultRow.NextRow     = null;
                CopyEntries(array2Row.RowSentinel.NextEntry, resultRow.RowSentinel);
            }

            return(result);
        }