Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity 0.5.0;
- import './IterableMapping2.sol';
- /// @dev Models a uint -> uint mapping where it is possible to iterate over all keys.
- library IterableMapping1
- {
- struct itmap
- {
- mapping(string => IndexValue) data;
- KeyFlag[] keys;
- uint size;
- }
- struct IndexValue { uint keyIndex; IterableMapping2.itmap value; }
- struct KeyFlag { string key; bool deleted; }
- function insert(itmap storage self, string memory key, IterableMapping2.itmap storage value) public returns (bool replaced)
- {
- uint keyIndex = self.data[key].keyIndex;
- self.data[key].value = value;
- if (keyIndex > 0) {
- }
- else
- {
- keyIndex = self.keys.length++;
- self.data[key].keyIndex = keyIndex + 1;
- self.keys[keyIndex].key = key;
- self.size++;
- return false;
- }
- }
- function remove(itmap storage self, string memory key) public returns (bool success)
- {
- uint keyIndex = self.data[key].keyIndex;
- if (keyIndex == 0)
- return false;
- delete self.data[key];
- self.keys[keyIndex - 1].deleted = true;
- self.size --;
- return true;
- }
- function contains(itmap storage self, string memory key) public view returns (bool)
- {
- return self.data[key].keyIndex > 0;
- }
- function iterate_start(itmap storage self) public view returns (uint keyIndex)
- {
- return iterate_next(self, uint(-1));
- }
- function iterate_valid(itmap storage self, uint keyIndex) public view returns (bool)
- {
- return keyIndex < self.keys.length;
- }
- function iterate_next(itmap storage self, uint keyIndex) public view returns (uint r_keyIndex)
- {
- keyIndex++;
- while (keyIndex < self.keys.length && self.keys[keyIndex].deleted)
- keyIndex++;
- return keyIndex;
- }
- function iterate_get(itmap storage self, uint keyIndex) public view returns (string memory key, IterableMapping2.itmap storage value)
- {
- key = self.keys[keyIndex].key;
- value = self.data[key].value;
- }
- }
Add Comment
Please, Sign In to add comment