Проверенный контрактТочноеMIT
IterableMapping
Адрес0x36ff808dc7e8c94a469e109eb8528144d5ee2a11
- Баланс
0 ETH
- Транзакций
1
- Компилятор
solc 0.8.30
- Тот же код
ещё 4 контракта
Подробности
- Компилятор
- solc 0.8.30+commit.73712a01
- Язык
- solidity
- Оптимизация
- Нет
- Лицензия
- MIT
- Совпадение
- Точноекод создания: да, код в сети: да
- Проверен на Sourcify
- Создан в блоке
- 25 821 173
- Транзакция создания
- 0x3d7469ad6a…a4a0fde8c5
contracts/mappingV2.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
library IterableMapping {
struct Map {
address[] keys;
mapping(address => uint256) values;
mapping(address => uint256) indexOf;
mapping(address => bool) inserted;
}
error IndexOutOfBounds(uint256 index, uint256 length);
error UnderflowValue(address key, uint256 current, uint256 subtracted);
/*//////////////////////////////////////////////////////////////
READS
//////////////////////////////////////////////////////////////*/
/// @notice Value stored for `key` (0 if absent).
function get(Map storage map, address key) internal view returns (uint256) {
return map.values[key];
}
/// @notice True if `key` currently exists in the map.
function contains(Map storage map, address key) internal view returns (bool) {
return map.inserted[key];
}
/// @notice Safe read: returns (exists, value) without reverting.
function tryGet(Map storage map, address key) internal view returns (bool exists, uint256 value) {
return (map.inserted[key], map.values[key]);
}
/// @notice Index of `key`, or -1 if absent. Kept for backward compatibility.
function getIndexOfKey(Map storage map, address key) internal view returns (int256) {
if (!map.inserted[key]) {
return -1;
}
return int256(map.indexOf[key]);
}
/// @notice Key stored at `index`. Reverts if out of range.
function getKeyAtIndex(Map storage map, uint256 index) internal view returns (address) {
if (index >= map.keys.length) revert IndexOutOfBounds(index, map.keys.length);
return map.keys[index];
}
/// @notice Number of keys in the map.
function size(Map storage map) internal view returns (uint256) {
return map.keys.length;
}
/// @notice A copy of all keys. O(n) memory; use for off-chain reads or small sets.
/// @dev Named getKeys (not keys) to avoid colliding with the struct's `keys`
/// member under `using IterableMapping for Map`.
function getKeys(Map storage map) internal view returns (address[] memory) {
return map.keys;
}
/// @notice A copy of all values, aligned with `keys()`. O(n) memory.
function getValues(Map storage map) internal view returns (uint256[] memory values) {
uint256 len = map.keys.length;
values = new uint256[](len);
for (uint256 i; i < len; ++i) {
values[i] = map.values[map.keys[i]];
}
}
/*//////////////////////////////////////////////////////////////
WRITES
//////////////////////////////////////////////////////////////*/
/// @notice Set `key` to `val`, inserting it if new.
function set(Map storage map, address key, uint256 val) internal {
if (map.inserted[key]) {
map.values[key] = val;
} else {
map.inserted[key] = true;
map.values[key] = val;
map.indexOf[key] = map.keys.length;
map.keys.push(key);
}
}
/// @notice Add `delta` to `key`'s value, inserting the key if new.
function add(Map storage map, address key, uint256 delta) internal returns (uint256 newValue) {
newValue = map.values[key] + delta;
set(map, key, newValue);
}
/// @notice Subtract `delta` from `key`'s value. Reverts on underflow.
function sub(Map storage map, address key, uint256 delta) internal returns (uint256 newValue) {
uint256 current = map.values[key];
if (delta > current) revert UnderflowValue(key, current, delta);
newValue = current - delta;
set(map, key, newValue);
}
/// @notice Convenience: +1.
function increment(Map storage map, address key) internal returns (uint256) {
return add(map, key, 1);
}
/// @notice Convenience: -1. Reverts on underflow.
function decrement(Map storage map, address key) internal returns (uint256) {
return sub(map, key, 1);
}
/// @notice Remove `key` in O(1) via swap-and-pop. No-op if absent.
function remove(Map storage map, address key) internal {
if (!map.inserted[key]) {
return;
}
uint256 index = map.indexOf[key];
uint256 lastIndex = map.keys.length - 1;
address lastKey = map.keys[lastIndex];
// Move the last key into the removed slot (harmless self-move when
// removing the last element).
map.keys[index] = lastKey;
map.indexOf[lastKey] = index;
map.keys.pop();
delete map.inserted[key];
delete map.values[key];
delete map.indexOf[key];
}
}
/*//////////////////////////////////////////////////////////////////////////
Example consumer
--------------------------------------------------------------------------
Demonstrates `using IterableMapping for IterableMapping.Map` and that the
internal library links with zero deployment/linking overhead. Delete this
if you only need the library.
//////////////////////////////////////////////////////////////////////////*/
contract IterableMappingFunc {
using IterableMapping for IterableMapping.Map;
IterableMapping.Map private _map;
function set(address key, uint256 val) external {
_map.set(key, val);
}
function add(address key, uint256 delta) external returns (uint256) {
return _map.add(key, delta);
}
function sub(address key, uint256 delta) external returns (uint256) {
return _map.sub(key, delta);
}
function remove(address key) external {
_map.remove(key);
}
function get(address key) external view returns (uint256) {
return _map.get(key);
}
function contains(address key) external view returns (bool) {
return _map.contains(key);
}
function size() external view returns (uint256) {
return _map.size();
}
function keyAt(uint256 index) external view returns (address) {
return _map.getKeyAtIndex(index);
}
function allKeys() external view returns (address[] memory) {
return _map.getKeys();
}
function allValues() external view returns (uint256[] memory) {
return _map.getValues();
}
}
Настройки компилятораПоказать
{
"optimizer": {
"runs": 200,
"enabled": false
},
"remappings": []
}Исходный код и метаданные: Sourcify.
Исходный код принадлежит авторам контракта.