// SPDX-License-Identifier: MIT
function push(uint256[] memory _nums, uint256 _num) internal pure {
// 在可变数组的 末尾追加 一个value (_num)
mstore(add(_nums, mul(add(mload(_nums), 1), 0x20)), _num)
mstore(_nums, add(mload(_nums), 1))
// 0x40 是空闲内存指针的预定义位置 (value 为 空闲指针开始位)
mstore(0x40, add(mload(0x40), 0x20))
function pop(uint256[] memory _nums) internal pure returns (uint256 num_) {
num_ := mload(add(_nums, mul(mload(_nums), 0x20)))
mstore(_nums, sub(mload(_nums), 1))
function del(uint256[] memory _nums, uint256 _index) internal pure {
if lt(_index, mload(_nums)) {
// 将最后一个value 移到 _index 下标处
add(_nums, mul(add(_index, 1), 0x20)),
mload(add(_nums, mul(mload(_nums), 0x20)))
mstore(_nums, sub(mload(_nums), 1))
function get(uint256[] memory _nums, uint256 _index)
using Array for uint256[];
function push(uint256[] memory _nums, uint256 num)
returns (uint256[] memory)
function pop(uint256[] memory _nums)
returns (uint256[] memory, uint256)
uint256 num_ = _nums.pop();
function del(uint256[] memory _nums, uint256 _index)
returns (uint256[] memory)
) external pure returns (uint256[] memory) {
_nums.update(_index, _num);
function get(uint256[] memory _nums, uint256 _index)
return _nums.get(_index);