Skip to content

Instantly share code, notes, and snippets.

@SerpentChris
Created March 29, 2017 23:52
Show Gist options
  • Save SerpentChris/5fa0296efc7257706f7a1968dc7f258a to your computer and use it in GitHub Desktop.
Save SerpentChris/5fa0296efc7257706f7a1968dc7f258a to your computer and use it in GitHub Desktop.
# Copyright (c) 2017 Christian Calderon
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
inset('MacroLib/common.sem')
macro MAX_KEY_COUNT: 1024
macro OFFSET: 3
macro Creator: self.storage[0]
macro Birthday: self.storage[1]
macro KeyCount: self.storage[2]
macro keys[$i]: self.storage[$i + OFFSET]
macro map[$k]: self.storage[$k + MAX_KEY_COUNT + OFFSET]
def init():
Creator = msg.sender
Birthday = block.number
def any():
IF_NOT_EQ(msg.sender, Creator, THROW())
IF_NOT_ZERO(msg.value, THROW())
def insert(key, val):
with i = 0:
with oldKeyCount = KeyCount:
while((i < oldKeyCount) and (keys[i] != key)):
if(keys[i]):
i += 1
IF_LT(i, oldKeyCount, return(False: bool)) # don't use this function to update an existing key!
IF_EQ(i, MAX_KEY_COUNT, return(False: bool)) # too many keys!
keys[i] = key
map[key] = val
KeyCount = oldKeyCount + 1
return(True: bool)
def lookup(key):
return(map[key])
def delete(key):
with i = 0:
with oldKeyCount = KeyCount:
while((i < oldKeyCount) and (keys[i] != key)):
if(keys[i]):
i += 1
# This key isn't used!
IF_EQ(i, oldKeyCount, return(False: bool))
keys[i] = 0
map[key] = 0
KeyCount = oldKeyCount - 1
return(True: bool)
def update(key, val):
with oldVal = map[key]:
# don't add keys with this function!
IF_ZERO(oldVal, return(False: bool))
map[key] = val
return(True: bool)
def getKeyCount():
return(KeyCount)
def getItems():
with keycount = KeyCount:
with data = alloc(keycount*64):
with i = 0:
while(i < keycount):
with key = keys[i]:
if key:
data[2*i] = key
data[2*i + 1] = map[key]
i += 1
return(data, items=(2*keyCount))
@SerpentChris
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment