commit fdbd7c059c4a9c9bbb05060a6c07b10a8d5babaa Author: Jarrad Hope Date: Fri Jul 3 11:32:09 2015 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1000939 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +rlp +ethereum \ No newline at end of file diff --git a/contracts/minheap.se b/contracts/minheap.se new file mode 100644 index 0000000..2ec9655 --- /dev/null +++ b/contracts/minheap.se @@ -0,0 +1,61 @@ +# Min Heap +# bids and asks +# with price, amount and trader encoded in a single 256 bit value + +data heap[2**256] + + +def push(item): + + length = heap[0] + k = length + 1 + heap[k] = item + log(k) + while k > 1: + log(666) + bottom = heap[k] + top = heap[k/2] + log(top) + log(bottom) + if bottom < top: + heap[k] = top + heap[k/2] = bottom + k /= 2 + else: + k = 0 + log(k) + heap[0] = k + + +def pop(): + length = heap[0] + if !length: + return(0) + prevtop = heap[1] + heap[1] = heap[length] + heap[length] = 0 + top = heap[1] + k = 1 + while k * 2 < length: + bottom1 = heap[k * 2] + bottom2 = heap[k * 2 + 1] + if bottom1 < top and (bottom1 < bottom2 or k * 2 + 1 >= length): + heap[k] = bottom1 + heap[k * 2] = top + k = k * 2 + elif bottom2 < top and bottom2 < bottom1 and k * 2 + 1 < length: + heap[k] = bottom2 + heap[k * 2 + 1] = top + k = k * 2 + 1 + else: + k = length + heap[0] = length - 1 + return(prevtop) + + +def top(): + return(heap[1]) + + +def length(): + return(heap[0]) diff --git a/contracts/xorlist.se b/contracts/xorlist.se new file mode 100644 index 0000000..b5c11e2 --- /dev/null +++ b/contracts/xorlist.se @@ -0,0 +1,10 @@ +# XOR List +# For Trade Keys, points to hashmap for trade data +# http://www.geeksforgeeks.org/xor-linked-list-a-memory-efficient-doubly-linked-list-set-2/ + +data xorlist[](key) +data hashmap[](value) + + +def xoritem(): + return("monkey" xor "chowder") diff --git a/test.py b/test.py new file mode 100644 index 0000000..6252b42 --- /dev/null +++ b/test.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +from ethereum import tester, slogging + +state = tester.state() + + +def listener(msg): + if msg['event'] == 'LOG': + print(msg) + +slogging.log_listeners.listeners.append(listener) + +with open('contracts/minheap.se') as fh: + minheap_se = fh.read() + +with open('contracts/xorlist.se') as fh: + xorlist_se = fh.read() + + +minheap = state.abi_contract(minheap_se) +xorlist = state.abi_contract(xorlist_se) + +minheap.push(5) +minheap.push(10) +# minheap.pop() +print(minheap.top())