Skip to content

Instantly share code, notes, and snippets.

@pipermerriam
Created October 19, 2015 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pipermerriam/68d3c81cf201049a87c2 to your computer and use it in GitHub Desktop.
Save pipermerriam/68d3c81cf201049a87c2 to your computer and use it in GitHub Desktop.
import "libraries/GroveLib.sol";
import "libraries/StringUtils.sol";
contract IterableStruct {
GroveLib.Index theIndex;
uint _id;
struct MyStruct {
uint id;
bytes32 value;
}
mapping (int => MyStruct) my_structs;
function addThing(bytes32 value) {
_id++;
var thing = my_structs[_id];
thing.id = _id;
thing.value = value;
GroveLib.insert(theIndex, StringUtils.uintToBytes(uint(thing.id)), int(thing.id));
}
function iterateOverThings() {
bytes32 id = GroveLib.query(theIndex, ">=", 0);
while (id != 0x0) {
thing = my_structs[StringUtils.bytesToUint(id)];
// do something with the thing?
id = Grove.getNextNode(id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment