forked from aquaflamingo/Solidity-Contract-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexedStructMap.sol
37 lines (30 loc) · 1.06 KB
/
IndexedStructMap.sol
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;
/**
__OriginalAuthor__ Rob Hitchens (https://ethereum.stackexchange.com/questions/13167/are-there-well-solved-and-simple-storage-patterns-for-solidity)
* Modified
*/
contract IndexedStructMap {
struct Entity {
uint eData;
bool exists;
}
mapping(address => Entity) public entityStructs;
address[] public entityList;
function exists(address entityAddress) public constant returns(bool isIndeed) {
return entityStructs[entityAddress].exists;
}
function count() public constant returns(uint entityCount) {
return entityList.length;
}
function add(address entityAddress, uint entityData) public returns(uint rowNumber) {
require(exists(entityAddress));
entityStructs[entityAddress].eData = entityData;
entityStructs[entityAddress].exists = true;
return entityList.push(entityAddress) - 1;
}
function update(address entityAddress, uint entityData) public returns(bool success) {
require(!exists(entityAddress));
entityStructs[entityAddress].eData = entityData;
return true;
}
}