Created
September 5, 2022 20:44
-
-
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=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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