Created
August 30, 2014 11:10
-
-
Save henryscala/f8f5538f0e7bc8a72f2f to your computer and use it in GitHub Desktop.
DoubleLinkedCycleList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shared alias Vec2 => [Integer,Integer]; | |
shared alias Vec2List => CycleList<Vec2>; | |
Integer x = 0; | |
Integer y = 1; | |
shared class CycleListNode<T>(nodeValue,prev=null,next=null) { | |
shared variable T nodeValue; | |
shared variable CycleListNode<T>? prev; | |
shared variable CycleListNode<T>? next; | |
//return non-null prev | |
shared CycleListNode<T> actualPrev { | |
CycleListNode<T>? thePrev = prev; | |
assert(is CycleListNode<T> thePrev); | |
return thePrev; | |
} | |
//return non-null next | |
shared CycleListNode<T> actualNext { | |
CycleListNode<T>? theNext = next; | |
assert(is CycleListNode<T> theNext); | |
return theNext; | |
} | |
} | |
shared class CycleList<T>() given T satisfies Object { | |
shared variable CycleListNode<T>? head=null; | |
shared variable Integer size = 0; | |
shared CycleListNode<T> actualHead { | |
assert(size > 0); | |
CycleListNode<T>? theHead = head; | |
assert (exists theHead); | |
return theHead; | |
} | |
shared void push(T t){ | |
value node = CycleListNode<T>(t); | |
if(exists h = head){ | |
node.next = h; | |
node.prev = h.prev; | |
node.actualPrev.next = node; | |
h.prev = node; | |
} else { | |
node.prev = node; | |
node.next = node; | |
head = node; | |
} | |
size ++; | |
} | |
shared void remove(CycleListNode<T> node){ | |
assert(size > 0); | |
CycleListNode<T> prevNode = node.actualPrev; | |
CycleListNode<T> nextNode = node.actualNext; | |
if (size == 1) { | |
head = null; | |
} else { | |
prevNode.next = nextNode; | |
nextNode.prev = prevNode; | |
} | |
size --; | |
} | |
shared actual String string { | |
variable CycleListNode<T>? node = head; | |
variable String result="["; | |
while(exists curNode = node){ | |
result += curNode.nodeValue.string+","; | |
CycleListNode<T> nextNode = curNode.actualNext; | |
node = nextNode; | |
CycleListNode<T>? headNode = head; | |
assert(is CycleListNode<T> headNode); | |
if(nextNode == headNode ){ | |
break; | |
} | |
} | |
result +="]"; | |
return result; | |
} | |
} | |
shared class Canvas(String id){ | |
dynamic { | |
dynamic context; | |
dynamic canvas; | |
dynamic { | |
canvas = document.getElementById(id); | |
context = canvas.getContext("2d"); | |
} | |
} | |
shared void moveTo(Vec2 point){ | |
dynamic { | |
context.moveTo(point[x],point[y]); | |
} | |
} | |
shared void lineTo(Vec2 point){ | |
dynamic { | |
context.lineTo(point[x],point[y]); | |
} | |
} | |
shared void stroke(){ | |
dynamic { | |
context.stroke(); | |
} | |
} | |
} | |
shared void run() { | |
Canvas c = Canvas("canvas"); | |
c.moveTo([50,50]); | |
c.lineTo([100,50]); | |
c.lineTo([100,100]); | |
c.lineTo([50,100]); | |
c.stroke(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment