Guest User

Untitled

a guest
Jan 16th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. pragma solidity 0.5.0;
  2. import './IterableMapping2.sol';
  3.  
  4.  
  5. /// @dev Models a uint -> uint mapping where it is possible to iterate over all keys.
  6. library IterableMapping1
  7. {
  8. struct itmap
  9. {
  10. mapping(string => IndexValue) data;
  11. KeyFlag[] keys;
  12. uint size;
  13. }
  14. struct IndexValue { uint keyIndex; IterableMapping2.itmap value; }
  15.  
  16. struct KeyFlag { string key; bool deleted; }
  17.  
  18. function insert(itmap storage self, string memory key, IterableMapping2.itmap storage value) public returns (bool replaced)
  19. {
  20. uint keyIndex = self.data[key].keyIndex;
  21. self.data[key].value = value;
  22. if (keyIndex > 0) {
  23. }
  24. else
  25. {
  26. keyIndex = self.keys.length++;
  27. self.data[key].keyIndex = keyIndex + 1;
  28. self.keys[keyIndex].key = key;
  29. self.size++;
  30. return false;
  31. }
  32. }
  33. function remove(itmap storage self, string memory key) public returns (bool success)
  34. {
  35. uint keyIndex = self.data[key].keyIndex;
  36. if (keyIndex == 0)
  37. return false;
  38. delete self.data[key];
  39. self.keys[keyIndex - 1].deleted = true;
  40. self.size --;
  41. return true;
  42.  
  43. }
  44. function contains(itmap storage self, string memory key) public view returns (bool)
  45. {
  46. return self.data[key].keyIndex > 0;
  47. }
  48. function iterate_start(itmap storage self) public view returns (uint keyIndex)
  49. {
  50. return iterate_next(self, uint(-1));
  51. }
  52. function iterate_valid(itmap storage self, uint keyIndex) public view returns (bool)
  53. {
  54. return keyIndex < self.keys.length;
  55. }
  56. function iterate_next(itmap storage self, uint keyIndex) public view returns (uint r_keyIndex)
  57. {
  58. keyIndex++;
  59. while (keyIndex < self.keys.length && self.keys[keyIndex].deleted)
  60. keyIndex++;
  61. return keyIndex;
  62. }
  63. function iterate_get(itmap storage self, uint keyIndex) public view returns (string memory key, IterableMapping2.itmap storage value)
  64. {
  65. key = self.keys[keyIndex].key;
  66.  
  67. value = self.data[key].value;
  68. }
  69. }
Add Comment
Please, Sign In to add comment