Skip to content

Instantly share code, notes, and snippets.

@libert-xyz
Created September 5, 2022 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libert-xyz/f0802d12a835aa90d9b28d63b66c630e to your computer and use it in GitHub Desktop.
Save libert-xyz/f0802d12a835aa90d9b28d63b66c630e to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.16+commit.07a7930e.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Mapping_loop {
mapping(address => uint) public balances;
mapping(address => bool) public inserted;
address[] public keys;
function set(address _key, uint _val) external {
balances[_key] = _val;
if (!inserted[_key]) {
inserted[_key] = true;
keys.push(_key);
}
}
function first() external view returns (uint) {
return balances[keys[0]];
}
function last() external view returns (uint) {
return balances[keys[keys.length - 1]];
}
function get (uint _i) external view returns (uint) {
return balances[keys[_i]];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment