-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIterMapping.sol
More file actions
37 lines (32 loc) · 916 Bytes
/
IterMapping.sol
File metadata and controls
37 lines (32 loc) · 916 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
34
35
36
37
pragma solidity ^0.4.6;
/// @title Iterable Mapping Example
/// @author Adam Lemmon - <adamjlemmon@gmail.com>
contract IterMapping {
/**
* Storage
*/
// Map a key to the index of its value
mapping(uint=>uint) public keyToIndexMap;
string[] public values;
uint[] public keys;
event PublishAllKeyValuePairs(uint key, string value);
/**
* Public
*/
/// @dev Set a key's value by appending key and value to arrays
/// and update mapping with new value's index
function setKeyValue(uint key, string value) public {
keyToIndexMap[key] = values.length;
keys.push(key);
values.push(value);
}
/// @dev Iterate over an entire mapping
/// publishing each key / value pair
function publishAllKeyValuePairs() public {
for (var i = 0; i < keys.length; i++) {
uint valueIndex = keyToIndexMap[keys[i]];
string value = values[valueIndex];
PublishAllKeyValuePairs(keys[i], value);
}
}
}