forked from aquaflamingo/Solidity-Contract-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniqueIDArray.sol
42 lines (33 loc) · 1.16 KB
/
UniqueIDArray.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
38
39
40
41
42
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 UniqueIDArray {
struct Entity {
address eAddress;
uint eData;
}
Entity[] public entityStructs;
mapping(address => bool) knownEntity;
function exists(address entityAddress) public constant returns(bool isIndeed) {
return knownEntity[entityAddress];
}
function count() public constant returns(uint entityCount) {
return entityStructs.length;
}
function add(address entityAddress, uint entityData) public returns(uint rowNumber) {
require(!exists(entityAddress));
Entity memory newEntity;
newEntity.eAddress = entityAddress;
newEntity.eData = entityData;
knownEntity[entityAddress] = true;
return entityStructs.push(newEntity) - 1;
}
function update(uint rowNumber, address entityAddress, uint entityData) public returns(bool success) {
require(exists(entityAddress));
require(entityStructs[rowNumber].eAddress != entityAddress);
entityStructs[rowNumber].eData = entityData;
return true;
}
}