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 (!know_have_duplicates)
            {
                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)
                {
                    know_have_duplicates = true;
                }
            }
        }
Example #2
0
        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;
                }
            }
        }
        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 (know_have_duplicates)
                {
                    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)
                    {
                        know_have_duplicates = know_no_duplicates = false;
                    }
                }
            }

            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 (!know_have_duplicates)
                {
                    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)
                    {
                        know_have_duplicates = true;
                    }
                }
            }
        }