// Get or set an array value. public int this[int row, int col] { get { // Find the entry. IntArrayEntry entry = FindEntry(row, col, false); // If we didn't find it, return the default value. if (entry == null) { return(DefaultValue); } // Return the entry's value. return(entry.Value); } set { // See if this is the default value. if (value.Equals(DefaultValue)) { // Remove the entry from the array. DeleteEntry(row, col); } else { // Save the new value. // Find the entry, creating it if necessary. IntArrayEntry entry = FindEntry(row, col, true); // Save the value. entry.Value = value; } } }
// Find the ArrayEntry for this column. // If create is true, create the ArrayEntry if it doesn't exist. private IntArrayEntry FindColumn(IntArrayEntry entry, int col, bool create) { // Find the entry before the required one. IntArrayEntry before = FindColumnBefore(entry, col); // If we found it, return it. if ((before.NextEntry != null) && (before.NextEntry.ColumnNumber == col)) { return(before.NextEntry); } // We didn't find it. See if we should create it. if (create) { // Create the new ArrayEntry. IntArrayEntry newEntry = new IntArrayEntry(); newEntry.ColumnNumber = col; // Insert it in the row's column list. newEntry.NextEntry = before.NextEntry; before.NextEntry = newEntry; // Return it. return(newEntry); } // We didn't find it and shouldn't create it. Return null. return(null); }
// Find the ArrayEntry for this column. private IntArrayEntry FindColumnBefore(IntArrayEntry entry, int col) { // Find the entry before the required one. while ((entry.NextEntry != null) && (entry.NextEntry.ColumnNumber < col)) { entry = entry.NextEntry; } // Return the ArrayRow before the row or null. return(entry); }
// Copy the entries starting at fromEntry into // the destination entry list after toEntry. private void CopyEntries(IntArrayEntry fromEntry, IntArrayEntry toEntry) { while (fromEntry != null) { toEntry.NextEntry = new IntArrayEntry(); toEntry = toEntry.NextEntry; toEntry.ColumnNumber = fromEntry.ColumnNumber; toEntry.Value = fromEntry.Value; toEntry.NextEntry = null; // Move to the next entry. fromEntry = fromEntry.NextEntry; } }
// Add the entries in the two lists fromEntry1 and fromEntry2 // and save the sums in the destination entry list after toEntry. private void AddEntries(IntArrayEntry fromEntry1, IntArrayEntry fromEntry2, IntArrayEntry toEntry) { // Repeat as long as either from list has items. while ((fromEntry1 != null) && (fromEntry2 != null)) { // Make the new result entry. toEntry.NextEntry = new IntArrayEntry(); toEntry = toEntry.NextEntry; toEntry.NextEntry = null; // See which column number is smaller. if (fromEntry1.ColumnNumber < fromEntry2.ColumnNumber) { // Copy the fromEntry1 entry. toEntry.ColumnNumber = fromEntry1.ColumnNumber; toEntry.Value = fromEntry1.Value; fromEntry1 = fromEntry1.NextEntry; } else if (fromEntry2.ColumnNumber < fromEntry1.ColumnNumber) { // Copy the fromEntry2 entry. toEntry.ColumnNumber = fromEntry2.ColumnNumber; toEntry.Value = fromEntry2.Value; fromEntry2 = fromEntry2.NextEntry; } else { // The column numbers are the same. Add both entries. toEntry.ColumnNumber = fromEntry1.ColumnNumber; toEntry.Value = fromEntry1.Value + fromEntry2.Value; fromEntry1 = fromEntry1.NextEntry; fromEntry2 = fromEntry2.NextEntry; } } // Add the rest of the entries from the list that is not empty. if (fromEntry1 != null) { CopyEntries(fromEntry1, toEntry); } if (fromEntry2 != null) { CopyEntries(fromEntry2, toEntry); } }
// Delete the indicated entry if it exists. public void DeleteEntry(int row, int col) { // Find the row before the entry's row. IntArrayRow rowBefore = FindRowBefore(row); // If we didn't find the row, we're done. IntArrayRow arrayRow = rowBefore.NextRow; if ((arrayRow == null) || (arrayRow.RowNumber != row)) { return; } // Find the entry before this entry's entry. IntArrayEntry entryBefore = FindColumnBefore(arrayRow.RowSentinel, col); IntArrayEntry arrayEntry = entryBefore.NextEntry; // If we didn't find the entry, we're done. if ((arrayEntry == null) || (arrayEntry.ColumnNumber != col)) { return; } // Remove the entry from the row's list. entryBefore.NextEntry = arrayEntry.NextEntry; // arrayEntry.NextEntry = null; // free(arrayEntry); // If there are no more entries in the row, remove it. IntArrayEntry arraySentinel = arrayRow.RowSentinel; if (arraySentinel.NextEntry == null) { rowBefore.NextRow = arrayRow.NextRow; // arrayRow.RowSentinel = null; // free(arraySentinel); // free(arrayRow); } }