// Source: https://ethereum.stackexchange.com/questions/13167/are-there-well-solved-and-simple-storage-patterns-for-solidity pragma solidity ^0.4.18; // Simple List Using Array contract simpleList { struct EntityStruct { address entityAddress; uint entityData; // more fields } EntityStruct[] public entityStructs; function newEntity(address entityAddress, uint entityData) public returns(uint rowNumber) { EntityStruct memory _newEntity; _newEntity.entityAddress = entityAddress; _newEntity.entityData = entityData; return entityStructs.push(_newEntity)-1; } function getEntityCount() public constant returns(uint entityCount) { return entityStructs.length; } } //Mapping with Struct contract mappingWithStruct { struct EntityStruct { uint entityData; bool isEntity; } mapping (address => EntityStruct) public entityStructs; function isEntity(address entityAddress) public constant returns(bool isIndeed) { return entityStructs[entityAddress].isEntity; } function newEntity(address entityAddress, uint entityData) public returns(bool success) { if(isEntity(entityAddress)) revert(); entityStructs[entityAddress].entityData = entityData; entityStructs[entityAddress].isEntity = true; return true; } function deleteEntity(address entityAddress) public returns(bool success) { if(!isEntity(entityAddress)) revert(); entityStructs[entityAddress].isEntity = false; return true; } function updateEntity(address entityAddress, uint entityData) public returns(bool success) { if(!isEntity(entityAddress)) revert(); entityStructs[entityAddress].entityData = entityData; return true; } } //Array of Structs with Unique Ids contract arrayWithUniqueIds { struct EntityStruct { address entityAddress; uint entityData; } EntityStruct[] public entityStructs; mapping(address => bool) knownEntity; function isEntity(address entityAddress) public constant returns(bool isIndeed) { return knownEntity[entityAddress]; } function getEntityCount() public constant returns(uint entityCount) { return entityStructs.length; } function newEntity(address entityAddress, uint entityData) public returns(uint rowNumber) { if(isEntity(entityAddress)) revert(); EntityStruct memory _newEntity; _newEntity.entityAddress = entityAddress; _newEntity.entityData = entityData; knownEntity[entityAddress] = true; return entityStructs.push(_newEntity) - 1; } function updateEntity(uint rowNumber, address entityAddress, uint entityData) public returns(bool success) { if(!isEntity(entityAddress)) revert(); if(entityStructs[rowNumber].entityAddress != entityAddress) revert(); entityStructs[rowNumber].entityData = entityData; return true; } } // Mapped Structs with Index contract MappedStructsWithIndex { struct EntityStruct { uint entityData; bool isEntity; } mapping(address => EntityStruct) public entityStructs; address[] public entityList; function isEntity(address entityAddress) public constant returns(bool isIndeed) { return entityStructs[entityAddress].isEntity; } function getEntityCount() public constant returns(uint entityCount) { return entityList.length; } function newEntity(address entityAddress, uint entityData) public returns(uint rowNumber) { if(isEntity(entityAddress)) revert(); entityStructs[entityAddress].entityData = entityData; entityStructs[entityAddress].isEntity = true; return entityList.push(entityAddress) - 1; } function updateEntity(address entityAddress, uint entityData) public returns(bool success) { if(!isEntity(entityAddress)) revert(); entityStructs[entityAddress].entityData = entityData; return true; } } // Mapped Structs with Delete-enabled Index contract mappedWithUnorderedIndexAndDelete { struct EntityStruct { uint entityData; uint listPointer; } mapping(address => EntityStruct) public entityStructs; address[] public entityList; function isEntity(address entityAddress) public constant returns(bool isIndeed) { if(entityList.length == 0) return false; return (entityList[entityStructs[entityAddress].listPointer] == entityAddress); } function getEntityCount() public constant returns(uint entityCount) { return entityList.length; } function newEntity(address entityAddress, uint entityData) public returns(bool success) { if(isEntity(entityAddress)) revert(); entityStructs[entityAddress].entityData = entityData; entityStructs[entityAddress].listPointer = entityList.push(entityAddress) - 1; return true; } function updateEntity(address entityAddress, uint entityData) public returns(bool success) { if(!isEntity(entityAddress)) revert(); entityStructs[entityAddress].entityData = entityData; return true; } function deleteEntity(address entityAddress) public returns(bool success) { if(!isEntity(entityAddress)) revert(); uint rowToDelete = entityStructs[entityAddress].listPointer; address keyToMove = entityList[entityList.length-1]; entityList[rowToDelete] = keyToMove; entityStructs[keyToMove].listPointer = rowToDelete; entityList.length--; return true; } }