Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. # Copyright (c) 2017 Christian Calderon
  2.  
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9.  
  10. # The above copyright notice and this permission notice shall be included in all
  11. # copies or substantial portions of the Software.
  12.  
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20.  
  21. inset('MacroLib/common.sem')
  22. macro MAX_KEY_COUNT: 1024
  23. macro OFFSET: 3
  24. macro Creator: self.storage[0]
  25. macro Birthday: self.storage[1]
  26. macro KeyCount: self.storage[2]
  27. macro keys[$i]: self.storage[$i + OFFSET]
  28. macro map[$k]: self.storage[$k + MAX_KEY_COUNT + OFFSET]
  29.  
  30.  
  31. def init():
  32. Creator = msg.sender
  33. Birthday = block.number
  34.  
  35.  
  36. def any():
  37. IF_NOT_EQ(msg.sender, Creator, THROW())
  38. IF_NOT_ZERO(msg.value, THROW())
  39.  
  40.  
  41. def insert(key, val):
  42. with i = 0:
  43. with oldKeyCount = KeyCount:
  44. while((i < oldKeyCount) and (keys[i] != key)):
  45. if(keys[i]):
  46. i += 1
  47.  
  48. IF_LT(i, oldKeyCount, return(False: bool)) # don't use this function to update an existing key!
  49. IF_EQ(i, MAX_KEY_COUNT, return(False: bool)) # too many keys!
  50.  
  51. keys[i] = key
  52. map[key] = val
  53. KeyCount = oldKeyCount + 1
  54. return(True: bool)
  55.  
  56.  
  57. def lookup(key):
  58. return(map[key])
  59.  
  60.  
  61. def delete(key):
  62. with i = 0:
  63. with oldKeyCount = KeyCount:
  64. while((i < oldKeyCount) and (keys[i] != key)):
  65. if(keys[i]):
  66. i += 1
  67.  
  68. # This key isn't used!
  69. IF_EQ(i, oldKeyCount, return(False: bool))
  70.  
  71. keys[i] = 0
  72. map[key] = 0
  73. KeyCount = oldKeyCount - 1
  74. return(True: bool)
  75.  
  76.  
  77. def update(key, val):
  78. with oldVal = map[key]:
  79. # don't add keys with this function!
  80. IF_ZERO(oldVal, return(False: bool))
  81. map[key] = val
  82. return(True: bool)
  83.  
  84.  
  85. def getKeyCount():
  86. return(KeyCount)
  87.  
  88.  
  89. def getItems():
  90. with keycount = KeyCount:
  91. with data = alloc(keycount*64):
  92. with i = 0:
  93. while(i < keycount):
  94. with key = keys[i]:
  95. if key:
  96. data[2*i] = key
  97. data[2*i + 1] = map[key]
  98. i += 1
  99. return(data, items=(2*keyCount))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement