内存(memory) 动态数组 进行 增删改查 操作.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
library Array {
function push(uint256[] memory _nums, uint256 _num) internal pure {
assembly {
// 在可变数组的 末尾追加 一个value (_num)
mstore(add(_nums, mul(add(mload(_nums), 1), 0x20)), _num)
// 可变数组的length 加 1
mstore(_nums, add(mload(_nums), 1))
// 0x40 是空闲内存指针的预定义位置 (value 为 空闲指针开始位)
mstore(0x40, add(mload(0x40), 0x20))
}
}
function pop(uint256[] memory _nums) internal pure returns (uint256 num_) {
assembly {
// 取出可变数组的最后一个value
num_ := mload(add(_nums, mul(mload(_nums), 0x20)))
// length - 1
mstore(_nums, sub(mload(_nums), 1))
}
}
function del(uint256[] memory _nums, uint256 _index) internal pure {
assembly {
// 下标位置需 小于 数组.length
if lt(_index, mload(_nums)) {
// 将最后一个value 移到 _index 下标处
mstore(
add(_nums, mul(add(_index, 1), 0x20)),
mload(add(_nums, mul(mload(_nums), 0x20)))
)
// length - 1
mstore(_nums, sub(mload(_nums), 1))
}
}
}
function update(
uint256[] memory _nums,
uint256 _index,
uint256 _num
) internal pure {
_nums[_index] = _num;
}
function get(uint256[] memory _nums, uint256 _index)
internal
pure
returns (uint256)
{
return _nums[_index];
}
}
contract testArr {
using Array for uint256[];
function push(uint256[] memory _nums, uint256 num)
external
pure
returns (uint256[] memory)
{
_nums.push(num);
return _nums;
}
function pop(uint256[] memory _nums)
external
pure
returns (uint256[] memory, uint256)
{
uint256 num_ = _nums.pop();
return (_nums, num_);
}
function del(uint256[] memory _nums, uint256 _index)
external
pure
returns (uint256[] memory)
{
_nums.del(_index);
return _nums;
}
function update(
uint256[] memory _nums,
uint256 _index,
uint256 _num
) external pure returns (uint256[] memory) {
_nums.update(_index, _num);
return _nums;
}
function get(uint256[] memory _nums, uint256 _index)
external
pure
returns (uint256)
{
return _nums.get(_index);
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!