Created
June 4, 2016 00:57
-
-
Save kootenpv/21d775bb2651626c9046565e0ef5355e to your computer and use it in GitHub Desktop.
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
// @title itmap(bytes32 => address) | |
// @author Pascal van Kooten <kootenpv@gmail.com> | |
// credits to: | |
// broken origin: https://github.com/ethereum/dapp-bin/blob/master/library/iterable_mapping.sol | |
// Nick "Arachnid" Johnson's new version: https://gist.github.com/Arachnid/59159497f124fdbff14bc2ca960b77ba | |
library itmap { | |
struct entry { | |
// Equal to the index of the key of this item in keys, plus 1. | |
uint keyIndex; | |
address value; | |
} | |
struct itmap { | |
mapping(bytes32 => entry) data; | |
bytes32[] keys; | |
} | |
function set(itmap storage self, bytes32 key, address value) internal returns (bool replaced) { | |
entry storage e = self.data[key]; | |
e.value = value; | |
if (e.keyIndex > 0) { | |
return true; | |
} else { | |
e.keyIndex = ++self.keys.length; | |
self.keys[e.keyIndex - 1] = key; | |
return false; | |
} | |
} | |
function remove(itmap storage self, bytes32 key) internal returns (bool success) { | |
entry storage e = self.data[key]; | |
if (e.keyIndex == 0) | |
return false; | |
if (e.keyIndex < self.keys.length) { | |
// Move an existing element into the vacated key slot. | |
self.data[self.keys[self.keys.length - 1]].keyIndex = e.keyIndex; | |
self.keys[e.keyIndex - 1] = self.keys[self.keys.length - 1]; | |
self.keys.length -= 1; | |
delete self.data[key]; | |
} | |
return true; | |
} | |
function contains(itmap storage self, bytes32 key) internal returns (bool exists) { | |
return self.data[key].keyIndex > 0; | |
} | |
function size(itmap storage self) internal returns (uint) { | |
return self.keys.length; | |
} | |
function get(itmap storage self, bytes32 key) internal constant returns (address value) { | |
return self.data[key].value; | |
} | |
function getKey(itmap storage self, uint idx) internal constant returns (bytes32 key) { | |
return self.keys[idx]; | |
} | |
} | |
/// How to use it: | |
contract NameRegistry { | |
// Use itmap for all functions on the struct | |
using itmap for itmap.itmap; | |
// Declare an iterable mapping | |
itmap.itmap registry; | |
function set(bytes32 name, address owner) returns (bool) { | |
return registry.set(name, owner); | |
} | |
function get(bytes32 name) constant returns (address owner) { | |
return registry.get(name); | |
} | |
function remove(bytes32 name) constant returns (bool success){ | |
return registry.remove(name); | |
} | |
// The below functions allow to first get the amount of names registered --> size() | |
// Followed by repeated calls to get the keys by getNthName(n) for n in 0 ...size | |
function size() returns (uint) { | |
return registry.size(); | |
} | |
function getNthName(uint idx) constant returns (bytes32) { | |
return registry.getKey(idx); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment