Swap 2 elements by index

@param string|int $index
@param string|int $newIndex

@return bool
352846
public function swapIndex($index, $newIndex) : bool
    {
        if ($newIndex < 0 || $newIndex > sizeof($this->elements)) {
            return false;
        }

        $object = $this->elements[$index];
        $newObject = $this->elements[$newIndex];
        $this->elements[$newIndex] = $object;
        $this->elements[$index] = $newObject;

        return true;
    }
874224
private function mergeArrayFromIndex(array $to, array $from, $index = 0)
    {
        $size = count($from);

        if ($size > $index) for ($i = $index; $i < $size; ++$i) $to[] = $from[$i];

        return $to;
    }
1051757
public function swap(int $index1, int $index2) : void
    {
        if ($index1 !== $index2) {
            $value = $this[$index1];
            $this[$index1] = $this[$index2];
            $this[$index2] = $value;
        }
    }
180873
public function withIndex(int $index): self
    {
        $state = clone $this;
        $state->_index = $index;
        return $state;
    }
1224348
public function withTransversal($index, $value)
    {
        $res = clone $this;

        $res->transversals = $this->insertInList($res->transversals, $index, $value);

        return $res;
    }
794044
public function setActionIndex(int $index = null) : self
    {
        if ($index) {
            return $this->addAttribute('data-action-index', (string) $index);
        } else {
            return $this->removeAttribute('data-index-action');
        }
    }
504933
public static void swap(short[] shortArray, int index1, int index2) {
        TrivialSwap.swap(shortArray, index1, shortArray, index2);
    }
669149
public function compareIndexes(Index $oldIndex, Index $newIndex)
    {
        return ($oldIndex->getName() !== $newIndex->getName())
            || ($oldIndex->getColumnNames() !== $newIndex->getColumnNames())
            || ($oldIndex->isUnique() !== $newIndex->isUnique());
    }
704327
public function remove($index)
	{
		if ($this->has($index))
		{
			Arr::forget($this->data, $index);
			Arr::forget($this->metas, $this->firstIndex($index));
		}

		return $this;
	}
222727
public function addIndex($index, $after=null) {

        // Detect duplicates first
        if ($this->getIndex($index->getName())) {
            throw new coding_exception('Duplicate index '.$index->getName().' specified in table '.$this->getName());
        }

        // Make sure there are no keys with the index column specs because they would collide.
        $newfields = $index->getFields();
        $allkeys = $this->getKeys();
        foreach ($allkeys as $key) {
            $fields = $key->getFields();
            if ($fields === $newfields) {
                throw new coding_exception('Key '.$key->getName().' collides with index'.$index->getName().' specified in table '.$this->getName());
            }
        }

        // Calculate the previous and next indexes
        $previndex = null;
        $nextindex = null;

        if (!$after) {
            $allindexes = $this->getIndexes();
            if (!empty($allindexes)) {
                end($allindexes);
                $previndex = $allindexes[key($allindexes)];
            }
        } else {
            $previndex = $this->getIndex($after);
        }
        if ($previndex && $previndex->getNext()) {
            $nextindex = $this->getIndex($previndex->getNext());
        }

        // Set current index previous and next attributes
        if ($previndex) {
            $index->setPrevious($previndex->getName());
            $previndex->setNext($index->getName());
        }
        if ($nextindex) {
            $index->setNext($nextindex->getName());
            $nextindex->setPrevious($index->getName());
        }

        // Some more attributes
        $index->setLoaded(true);
        $index->setChanged(true);
        // Add the new index
        $this->indexes[] = $index;
        // Reorder the indexes
        $this->orderIndexes($this->indexes);
        // Recalculate the hash
        $this->calculateHash(true);
        // We have one new index, so the table has changed
        $this->setChanged(true);
    }
800027
public function offsetSet($index, $newval)
    {
        if (!$newval instanceof TableOfContents\File) {
            throw new \InvalidArgumentException('A table of contents may only be filled with File objects');
        }

        $basename = basename($newval->getFilename());
        if (strpos($basename, '.') !== false) {
            $basename = substr($basename, 0, strpos($basename, '.'));
        }

        if (strtolower($basename) === 'index') {
            $this->modules[] = $newval;
        }

        parent::offsetSet($newval->getFilename(), $newval);
    }
255756
public function invert($index = 0) {
		$pcs = $this->bits;
		$pcs = \ianring\BitmaskUtils::reflectBitmask($pcs);
		$pcs = \ianring\BitmaskUtils::rotateBitmask($pcs, -1, 1);
		$pcs = \ianring\BitmaskUtils::rotateBitmask($pcs, 0, $index);
		$this->bits = $pcs;
	}
308760
public function isBetterThan(Index $index)
    {
        if ($this->hasSameColumnNames($index->getColumnNames()) && $this->isUnique() && !$index->isUnique()) {
            return true;
        }

        return false;
    }
960308
public function offsetSet($index, $newval)
    {
        if (!$newval instanceof $this->currencyClass) {
            throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', get_class($newval)));
        }

        parent::offsetSet($index, $newval);
    }
1260137
public static void swap(int[] intArray, int index1, int index2) {
        XORSwap.swap(intArray, index1, intArray, index2);
    }
929245
public function shiftUp(int $index) : void
    {
        if ($index + 1 === $this->count()) {
            return;
        }

        $this->swap($index, $index + 1);
    }
966216
public function callbackChangeSortIndex($new, $old)
    {
        // get old position
        $newPos = (new Query)->select([$this->sortIndexFieldName])->from($this->refTableName)->where([$this->imageIdFieldName => $new['originalImageId']])->scalar();
        $oldPos = (new Query)->select([$this->sortIndexFieldName])->from($this->refTableName)->where([$this->imageIdFieldName => $old['originalImageId']])->scalar();
        
        // switch positions
        $changeNewPos = Yii::$app->db->createCommand()->update($this->refTableName, [$this->sortIndexFieldName => $oldPos], [
            $this->imageIdFieldName => $new['originalImageId'],
            $this->refFieldName => $this->itemId,
        ])->execute();
        
        $changeOldPos = Yii::$app->db->createCommand()->update($this->refTableName, [$this->sortIndexFieldName => $newPos], [
            $this->imageIdFieldName => $old['originalImageId'],
            $this->refFieldName => $this->itemId,
        ])->execute();
        
        return true;
    }