Beispiel #1
0
        public void Add(T item, int index)
        {
            // index is out of the range
            if (index > _size - 1)
            {
                throw new IndexOutOfRangeException();
            }

            var newArray = new VectorArray <T[]>(1);
            var newSize  = 0;
            var isAdded  = false;

            var outerIndex = index / _vector;
            var innerIndex = index % _vector;

            var matrixRowCount = _size % _vector == 0 ? _size / _vector : (_size / _vector) + 1;

            // insert item in the middle of the array
            for (int i = 0; i < matrixRowCount; i++)
            {
                for (int j = 0; j < _vector; j++)
                {
                    // check for resize
                    if (newSize == newArray.Size * _vector)
                    {
                        newArray.Add(new T[_vector]);
                    }

                    // add new item
                    if (i == outerIndex && j == innerIndex)
                    {
                        newArray.Get(i)[j] = item;
                        isAdded            = true;
                        newSize++;
                    }

                    if (isAdded)
                    {
                        if (_array.Get(i)[j].Equals(default(T)))
                        {
                            // skip default elements which are not set
                            continue;
                        }

                        if (j + 1 <= _vector - 1)
                        {
                            // copy from existing array with +1 shift
                            newArray.Get(i)[j + 1] = _array.Get(i)[j];
                        }
                        else
                        {
                            if (newSize == newArray.Size * _vector)
                            {
                                newArray.Add(new T[_vector]);
                            }

                            newArray.Get(i + 1)[0] = _array.Get(i)[j];
                        }
                    }
                    else
                    {
                        // copy from existing array
                        newArray.Get(i)[j] = _array.Get(i)[j];
                    }

                    newSize++;
                }
            }

            _array = newArray;
            _size  = newSize;
        }
Beispiel #2
0
        public void Add(T item, int index)
        {
            // index is out of the range
            if (index > Size - 1)
            {
                throw new IndexOutOfRangeException();
            }

            var rowIndexToInsert    = GetRowIndex(index);
            var columnIndexToInsert = GetColumnIndex(index);

            var newArray      = new VectorArray <T[]>(1);
            var newActualSize = new int[_actualSize.Length];

            // copy all the rows before the row to insert new item
            for (var i = 0; i < rowIndexToInsert; i++)
            {
                newArray.Add(_array.Get(i));
                newActualSize[i] = _actualSize[i];
            }

            var isAdded        = false;
            var processNextRow = false;

            // insert item in the middle of the array
            for (var i = rowIndexToInsert; i < _actualSize.Length; i++)
            {
                for (var j = 0; j < _vector; j++)
                {
                    processNextRow = false;

                    // check size
                    if (i == newArray.Size)
                    {
                        newArray.Add(new T[_vector]);
                    }

                    // add new item
                    if (!isAdded && j == columnIndexToInsert)
                    {
                        newArray.Get(i)[j] = item;
                        isAdded            = true;
                        newActualSize[i]  += 1;
                    }

                    if (isAdded)
                    {
                        if (_array.Get(i)[j].Equals(default(T)))
                        {
                            // skip default elements which are not set
                            break;
                        }

                        if (j + 1 <= _vector - 1)
                        {
                            // copy from existing array with +1 shift
                            newArray.Get(i)[j + 1] = _array.Get(i)[j];
                            newActualSize[i]      += 1;
                        }
                        else
                        {
                            // check if there is a capacity in the new array
                            if (i + 1 == newArray.Size)
                            {
                                newArray.Add(new T[_vector]);

                                if (i + 1 > newActualSize.Length)
                                {
                                    // add new row to the actual size counter
                                    var tempActualSize = new int[newActualSize.Length + 1];
                                    for (var k = 0; k < newActualSize.Length; k++)
                                    {
                                        tempActualSize[k] = newActualSize[k];
                                    }

                                    newActualSize = tempActualSize;
                                }
                            }

                            // shift to the next row
                            newArray.Get(i + 1)[0] = _array.Get(i)[j];
                            newActualSize[i + 1]  += 1;
                            processNextRow         = true;
                        }
                    }
                    else
                    {
                        // copy from existing array
                        newArray.Get(i)[j] = _array.Get(i)[j];
                        newActualSize[i]  += 1;
                    }
                }

                if (processNextRow)
                {
                    continue;
                }

                rowIndexToInsert = i;
                break;
            }

            // copy all the rows which were not changed
            for (var i = rowIndexToInsert + 1; i < _actualSize.Length; i++)
            {
                newArray.Add(_array.Get(i));
                newActualSize[i] = _actualSize[i];
            }

            _array      = newArray;
            _actualSize = newActualSize;
        }
Beispiel #3
0
        public T Remove(int index)
        {
            // index is out of the range
            if (index > _size - 1)
            {
                throw new IndexOutOfRangeException();
            }

            var newArray  = new VectorArray <T[]>(1);
            var newSize   = 0;
            var isRemoved = false;

            var outerIndex  = index / _vector;
            var innerIndex  = index % _vector;
            var removedItem = _array.Get(outerIndex)[innerIndex];

            var matrixRowCount = _size % _vector == 0 ? _size / _vector : (_size / _vector) + 1;

            for (var i = 0; i < matrixRowCount; i++)
            {
                for (var j = 0; j < _vector; j++)
                {
                    // check for resize
                    if (newSize == newArray.Size * _vector)
                    {
                        newArray.Add(new T[_vector]);
                    }

                    if (i == outerIndex && j == innerIndex)
                    {
                        isRemoved = true;
                        break;
                    }

                    // copy from existing array
                    newArray.Get(i)[j] = _array.Get(i)[j];

                    newSize++;
                }

                if (isRemoved)
                {
                    break;
                }
            }

            // check if not the last item
            if (newSize < _size - 1)
            {
                var continueRowIndex  = (newSize + 1) / _vector;
                var continueCellIndex = (newSize + 1) % _vector;
                for (var i = continueRowIndex; i < matrixRowCount; i++)
                {
                    for (var j = continueCellIndex; j < _vector; j++)
                    {
                        // check for resize
                        if (newSize == newArray.Size * _vector)
                        {
                            newArray.Add(new T[_vector]);
                        }

                        if (j - 1 > -1 && j < _vector)
                        {
                            newArray.Get(i)[j - 1] = _array.Get(i)[j];
                        }
                        else if (j == 0)
                        {
                            newArray.Get(i - 1)[_vector - 1] = _array.Get(i)[j];
                        }

                        newSize++;
                    }

                    continueCellIndex = 0;
                }
            }

            // remove unused matrix row
            if (newSize % _vector == 0)
            {
                newArray.Remove(newSize / _vector);
            }

            _size  = newSize;
            _array = newArray;
            return(removedItem);
        }