// Find the ArrayRow for this row. // If create is true, create the ArrayRow if it doesn't exist. private ArrayRow FindRow(int row, bool create) { // Find the ArrayRow before the one we want. ArrayRow before = FindRowBefore(row); // If we found it, return it. if ((before.NextRow != null) && (before.NextRow.RowNumber == row)) { return(before.NextRow); } // We didn't find it. See if we should create it. if (create) { // Create the new ArrayRow. ArrayRow newRow = new ArrayRow(); newRow.RowNumber = row; // Insert it in the row list. newRow.NextRow = before.NextRow; before.NextRow = newRow; // Create the row's sentinel. newRow.RowSentinel = new ArrayEntry(); newRow.RowSentinel.NextEntry = null; // Return it. return(newRow); } // We didn't find it and shouldn't create it. Return null. return(null); }
// Create the sentinels. public SparseArray(T defaultValue) { // Save the default value. DefaultValue = defaultValue; // Create the row sentinel. TopSentinel = new ArrayRow(); TopSentinel.NextRow = null; }
// Find the ArrayRow before this row. private ArrayRow FindRowBefore(int row) { // Start at the sentinel. ArrayRow arrayRow = TopSentinel; // Find the row before the required one. while ((arrayRow.NextRow != null) && (arrayRow.NextRow.RowNumber < row)) { arrayRow = arrayRow.NextRow; } // Return the ArrayRow before the row or null. return(arrayRow); }
// Find the ArrayEntry for this row and column. // If create is true, create the ArrayEntry if it doesn't exist. private ArrayEntry FindEntry(int row, int col, bool create) { // Find the entry's row. ArrayRow arrayRow = FindRow(row, create); // If we didn't find it (or create it), return null. if (arrayRow == null) { return(null); } // Find the entry in this row and return it. return(FindColumn(arrayRow.RowSentinel, col, create)); }
// Iterator. public IEnumerator <ArrayValue <T> > GetEnumerator() { ArrayRow arrayRow = TopSentinel.NextRow; while (arrayRow != null) { ArrayEntry arrayEntry = arrayRow.RowSentinel.NextEntry; while (arrayEntry != null) { yield return(new ArrayValue <T> { Row = arrayRow.RowNumber, Column = arrayEntry.ColumnNumber, Value = arrayEntry.Value }); arrayEntry = arrayEntry.NextEntry; } arrayRow = arrayRow.NextRow; } }
protected override IAsyncResult BeginExecute(AsyncCodeActivityContext context, AsyncCallback callback, object state) { DataTable dataTable = DataTable.Get(context); DataRow dataRow = DataRow.Get(context); object[] objs = ArrayRow.Get(context); try { if (dataRow != null) { dataTable.Rows.Add(dataRow); } else { object[] newObjs = null; if (objs.Length > dataTable.Columns.Count) { List <object> list = new List <object>(); for (int i = 0; i < dataTable.Columns.Count; i++) { list.Add(objs[i]); } newObjs = list.ToArray(); dataTable.Rows.Add(newObjs); } else { dataTable.Rows.Add(objs); } } } catch (Exception e) { SharedObject.Instance.Output(SharedObject.enOutputType.Error, "增加数据库行失败", e.Message); throw e; } m_Delegate = new runDelegate(Run); return(m_Delegate.BeginInvoke(callback, state)); }
// Delete the indicated entry if it exists. public void DeleteEntry(int row, int col) { // Find the row before the entry's row. ArrayRow rowBefore = FindRowBefore(row); // If we didn't find the row, we're done. ArrayRow arrayRow = rowBefore.NextRow; if ((arrayRow == null) || (arrayRow.RowNumber != row)) { return; } // Find the entry before this entry's entry. ArrayEntry entryBefore = FindColumnBefore(arrayRow.RowSentinel, col); ArrayEntry 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. ArrayEntry arraySentinel = arrayRow.RowSentinel; if (arraySentinel.NextEntry == null) { rowBefore.NextRow = arrayRow.NextRow; // arrayRow.RowSentinel = null; // free(arraySentinel); // free(arrayRow); } }
/// <summary> /// Odczytuje wiersz ArrayRow macierzy HAL z tokenem-kluczem /// /// Odcztytuje parę uint => ArrayRow<uint> /// </summary> /// <param name="reader">Strumień binarny do odczytu</param> /// <returns>Wiersz macierzy HAL z tokenem-kluczem</returns> public static KeyValuePair<uint, ArrayRow<uint>> readArrayRow(BinaryReader reader) { uint rowId; ArrayRow<uint> row = new ArrayRow<uint>(); /* Odczyt ID i count dla wiersza */ rowId = reader.ReadUInt32(); row.count = reader.ReadInt32(); int cellsCount = reader.ReadInt32(); row.cells = new KeyValuePair<uint, Cell>[cellsCount]; /* ID tokenu ostatnio odczytanej komórki */ uint id = 0; for (int i = 0; i < cellsCount; i++) { Cell cell = new Cell(); /* Odczytaj bajt informacyjny */ byte infoByte = reader.ReadByte(); /* Odczytaj różnicę ID od poprzedniej komórki, dodaj do ID, odczytaj count i sum (patrz Cell) */ id += loadUint(reader, ref infoByte); cell.count = (int)loadUint(reader, ref infoByte); cell.sum = (int)loadUint(reader, ref infoByte); /* Wstaw odczytaną komórkę do wiersza */ row.cells[i] = new KeyValuePair<uint, Cell>(id, cell); } /* TODO: to powinno być zbędne, komórki powinny być sortowane przy zapisie */ Array.Sort<KeyValuePair<uint, Cell>>(row.cells, delegate(KeyValuePair<uint, Cell> firstPair, KeyValuePair<uint, Cell> nextPair) { return firstPair.Key.CompareTo(nextPair.Key); } ); return new KeyValuePair<uint, ArrayRow<uint>>(rowId, row); }
/// <summary> /// Oblicza dystans między dwoma tokenami /// /// Jeśli wektory (wiersze) nie mają wspólnych wymiarów, zwracane jest -1, w przeciwnym razie /// odległość wg miary Minkowskiego przy r = 1. /// </summary> /// <param name="row1">Wiersz macierzy HAL pierwszego tokenu</param> /// <param name="row2">Wiersz macierzy HAL drugiego tokenu</param> /// <returns>Odległość między tokenami</returns> private int calculateDistance(ArrayRow<uint> row1, ArrayRow<uint> row2) { int index1 = 0, index2 = 0, count1 = row1.cells.Length, count2 = row2.cells.Length, distance = 0; while (index1 < count1 && index2 < count2) { if (row1.cells[index1].Key < row2.cells[index2].Key) { distance += row1.cells[index1].Value.sum; index1++; } else if (row1.cells[index1].Key > row2.cells[index2].Key) { distance += row2.cells[index2].Value.sum; index2++; } else { distance += Math.Abs(row1.cells[index1].Value.sum - row2.cells[index2].Value.sum); index1++; index2++; } } while (index1 < count1) { distance += row1.cells[index1].Value.sum; index1++; } while (index2 < count2) { distance += row2.cells[index2].Value.sum; index2++; } if (distance == 0) return -1; return distance; }
/// <summary> /// Adds boxes from array row number rowNum /// </summary> /// <param name="row"></param> /// <param name="rowNum"></param> /// <param name="noData"></param> public void addArrayRow(int[] row, int rowNum, int noData) { ArrayRow ar = new ArrayRow(row); addRow(new Row(ar.Row), rowNum, row.Length, noData); }