-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterableMapping.sol
More file actions
33 lines (28 loc) · 874 Bytes
/
Copy pathIterableMapping.sol
File metadata and controls
33 lines (28 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract IterMapping {
mapping(string => uint256) public ages;
string[] public keys;
mapping(string => bool) public isInserted;
function set(string memory _name, uint256 _age) public {
ages[_name] = _age;
if (!isInserted[_name]) {
isInserted[_name] = true;
keys.push(_name);
}
}
function length() public view returns (uint256) {
return keys.length;
}
function get(uint256 _index) public view returns (uint256) {
string memory key = keys[_index];
return ages[key];
}
function values() public view returns (uint256[] memory) {
uint[] memory vals = new uint[](keys.length);
for(uint i = 0; i < keys.length; i++){
vals[i] = ages[keys[i]];
}
return vals;
}
}