Created
December 11, 2017 03:04
-
-
Save willitscale/2a504d0bbfd35370ac2b864bf9f37a60 to your computer and use it in GitHub Desktop.
Example implementation of solidity mapping
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
pragma solidity ^0.4.0; | |
import "github.com/willitscale/solidity-util/lib/Strings.sol"; | |
contract MapExample { | |
using Strings for string; | |
// This would be the same as mapping(string => uint) | |
string[] mapKeys; | |
uint[] mapValues; | |
// This would be the same as map["myKey"]; | |
function getValue(string key) | |
public | |
returns (uint) { | |
for(uint i = 0; i < mapKeys.length; i++) { | |
if (mapKeys[i].compareTo(key)) { | |
return mapValues[i]; | |
} | |
} | |
// Nothing was found so an invalid key was specified | |
revert(); | |
} | |
// This would be the same as map["myKey"] = 123; | |
function setValue(string key, uint value) | |
public { | |
mapKeys.push(key); | |
mapValues.push(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is this the same