internal void Delete(int oldRecord) { if (oldRecord == -1) { return; } int index = FindIndexExact(oldRecord); if (index != -1) { if (_hasDuplicates == IndexDuplicatesState.True) { int c1 = 1; int c2 = 1; if (index > 0) { c1 = Key.CompareRecords(Array [index - 1], oldRecord); } if (index < Size - 1) { c2 = Key.CompareRecords(Array [index + 1], oldRecord); } if (c1 == 0 ^ c2 == 0) { _hasDuplicates = IndexDuplicatesState.Unknown; } } Remove(index); } }
private void MergeSort(int[] from, int[] to, int p, int r) { int q = (p + r) >> 1; if (q == p) { return; } MergeSort(to, from, p, q); MergeSort(to, from, q, r); // merge for (int middle = q, current = p;;) { int res = Key.CompareRecords(from[p], from[q]); if (res > 0) { to [current++] = from [q++]; if (q == r) { while (p < middle) { to[current++] = from[p++]; } break; } } else { if (res == 0) { _hasDuplicates = IndexDuplicatesState.True; } to [current++] = from [p++]; if (p == middle) { while (q < r) { to[current++] = from[q++]; } break; } } } }
private void Add(DataRow row, int newRecord) { int newIdx; if (newRecord < 0 || !Key.CanContain(newRecord)) { return; } if (Size == 0) { newIdx = 0; } else { newIdx = LazyBinarySearch(Array, 0, Size - 1, newRecord); // if newl value is greater - insert afer old value // else - insert before old value if (Key.CompareRecords(Array [newIdx], newRecord) < 0) { newIdx++; } } Insert(newIdx, newRecord); int c1 = 1; int c2 = 1; if (!(_hasDuplicates == IndexDuplicatesState.True)) { if (newIdx > 0) { c1 = Key.CompareRecords(Array [newIdx - 1], newRecord); } if (newIdx < Size - 1) { c2 = Key.CompareRecords(Array [newIdx + 1], newRecord); } if (c1 == 0 || c2 == 0) { _hasDuplicates = IndexDuplicatesState.True; } } }
private void RebuildIndex() { // consider better capacity approximation _array = new int [Key.Table.RecordCache.CurrentCapacity]; _size = 0; foreach (DataRow row in Key.Table.Rows) { int record = Key.GetRecord(row); if (record != -1) { _array [_size++] = record; } } _hasDuplicates = IndexDuplicatesState.False; // Note : MergeSort may update hasDuplicates to True Sort(); }
private void MergeSort(int[] from, int[] to,int p, int r) { int q = (p + r) >> 1; if (q == p) { return; } MergeSort(to, from, p, q); MergeSort(to, from, q, r); // merge for (int middle = q, current = p;;) { int res = Key.CompareRecords(from[p],from[q]); if (res > 0) { to[current++] = from[q++]; if (q == r) { while( p < middle) { to[current++] = from[p++]; } break; } } else { if (res == 0) { _hasDuplicates = IndexDuplicatesState.True; } to[current++] = from[p++]; if (p == middle) { while( q < r) { to[current++] = from[q++]; } break; } } } }
private void Add(DataRow row,int newRecord) { int newIdx; if (Size == 0) { newIdx = 0; } else { newIdx = LazyBinarySearch(Array,0,Size - 1,newRecord); // if newl value is greater - insert afer old value // else - insert before old value if (Key.CompareRecords(Array[newIdx],newRecord) < 0) { newIdx++; } } Insert(newIdx,newRecord); int c1 = 1; int c2 = 1; if (!(_hasDuplicates == IndexDuplicatesState.True)) { if (newIdx > 0) { c1 = Key.CompareRecords(Array[newIdx - 1],newRecord); } if (newIdx < Size - 1) { c2 = Key.CompareRecords(Array[newIdx + 1],newRecord); } if (c1 == 0 || c2 == 0) { _hasDuplicates = IndexDuplicatesState.True; } } }
internal void Update(DataRow row,int newRecord) { int oldRecord = Key.GetRecord(row); if (oldRecord == -1 || Size == 0) { Add(row,newRecord); return; } int oldIdx = FindIndex(oldRecord); if( oldIdx == -1 || Key.Table.RecordCache[Array[oldIdx]] != row ) { Add(row,newRecord); return; } int newIdx = -1; int compare = Key.CompareRecords(Array[oldIdx],newRecord); int start,end; int c1 = 1; int c2 = 1; if (compare == 0) { if (Array[oldIdx] == newRecord) { // we deal with the same record that didn't change // in the context of current index. // so , do nothing. return; } } else { if ((_hasDuplicates == IndexDuplicatesState.True)) { if (oldIdx > 0) { c1 = Key.CompareRecords(Array[oldIdx - 1],newRecord); } if (oldIdx < Size - 1) { c2 = Key.CompareRecords(Array[oldIdx + 1],newRecord); } if ((c1 == 0 ^ c2 == 0) && compare != 0) { _hasDuplicates = IndexDuplicatesState.Unknown; } } } if ((oldIdx == 0 && compare > 0) || (oldIdx == (Size - 1) && compare < 0) || (compare == 0)) { // no need to switch cells newIdx = oldIdx; } else { if (compare < 0) { // search after the old place start = oldIdx + 1; end = Size - 1; } else { // search before the old palce start = 0; end = oldIdx - 1; } newIdx = LazyBinarySearch(Array,start,end,newRecord); if (oldIdx < newIdx) { System.Array.Copy(Array,oldIdx + 1,Array,oldIdx,newIdx - oldIdx); } else if (oldIdx > newIdx){ System.Array.Copy(Array,newIdx,Array,newIdx + 1,oldIdx - newIdx); } } Array[newIdx] = newRecord; if (compare != 0) { if (!(_hasDuplicates == IndexDuplicatesState.True)) { if (newIdx > 0) { c1 = Key.CompareRecords(Array[newIdx - 1],newRecord); } if (newIdx < Size - 1) { c2 = Key.CompareRecords(Array[newIdx + 1],newRecord); } if (c1 == 0 || c2 == 0) { _hasDuplicates = IndexDuplicatesState.True; } } } }
internal void Delete(int oldRecord) { int index = FindIndex(oldRecord); if (index != -1) { if ((_hasDuplicates == IndexDuplicatesState.True)) { int c1 = 1; int c2 = 1; if (index > 0) { c1 = Key.CompareRecords(Array[index - 1],oldRecord); } if (index < Size - 1) { c2 = Key.CompareRecords(Array[index + 1],oldRecord); } if (c1 == 0 ^ c2 == 0) { _hasDuplicates = IndexDuplicatesState.Unknown; } } Remove(index); } }
private void RebuildIndex() { // consider better capacity approximation _array = new int[Key.Table.RecordCache.CurrentCapacity]; _size = 0; foreach(DataRow row in Key.Table.Rows) { int record = Key.GetRecord(row); if (record != -1) { _array[_size++] = record; } } _hasDuplicates = IndexDuplicatesState.False; // Note : MergeSort may update hasDuplicates to True Sort(); }
internal void Update(DataRow row, int oldRecord, DataRowVersion oldVersion, DataRowState oldState) { bool contains = Key.ContainsVersion(oldState, oldVersion); int newRecord = Key.GetRecord(row); // the record did not appeared in the index before update if (oldRecord == -1 || Size == 0 || !contains) { if (newRecord >= 0) { if (FindIndexExact(newRecord) < 0) { Add(row, newRecord); } } return; } // the record will not appeare in the index after update if (newRecord < 0 || !Key.CanContain(newRecord)) { Delete(oldRecord); return; } int oldIdx = FindIndexExact(oldRecord); if (oldIdx == -1) { Add(row, newRecord); return; } int newIdx = -1; int compare = Key.CompareRecords(Array [oldIdx], newRecord); int start, end; int c1 = 1; int c2 = 1; if (compare == 0) { if (Array [oldIdx] == newRecord) { // we deal with the same record that didn't change // in the context of current index. // so , do nothing. return; } } else { if (_hasDuplicates == IndexDuplicatesState.True) { if (oldIdx > 0) { c1 = Key.CompareRecords(Array [oldIdx - 1], newRecord); } if (oldIdx < Size - 1) { c2 = Key.CompareRecords(Array [oldIdx + 1], newRecord); } if ((c1 == 0 ^ c2 == 0) && compare != 0) { _hasDuplicates = IndexDuplicatesState.Unknown; } } } if ((oldIdx == 0 && compare > 0) || (oldIdx == (Size - 1) && compare < 0) || (compare == 0)) { // no need to switch cells newIdx = oldIdx; } else { if (compare < 0) { // search after the old place start = oldIdx + 1; end = Size - 1; } else { // search before the old palce start = 0; end = oldIdx - 1; } newIdx = LazyBinarySearch(Array, start, end, newRecord); if (oldIdx < newIdx) { System.Array.Copy(Array, oldIdx + 1, Array, oldIdx, newIdx - oldIdx); if (Key.CompareRecords(Array [newIdx], newRecord) > 0) { --newIdx; } } else if (oldIdx > newIdx) { System.Array.Copy(Array, newIdx, Array, newIdx + 1, oldIdx - newIdx); if (Key.CompareRecords(Array [newIdx], newRecord) < 0) { ++newIdx; } } } Array[newIdx] = newRecord; if (compare != 0) { if (!(_hasDuplicates == IndexDuplicatesState.True)) { if (newIdx > 0) { c1 = Key.CompareRecords(Array [newIdx - 1], newRecord); } if (newIdx < Size - 1) { c2 = Key.CompareRecords(Array [newIdx + 1], newRecord); } if (c1 == 0 || c2 == 0) { _hasDuplicates = IndexDuplicatesState.True; } } } }