LINKEDLIST

Constant LINKEDLIST 

Source
pub const LINKEDLIST: &str = "\nconstruct LinkedList {\n\tnext\n\tvalue\n\t;\n\tconstruct { this | }\n\tlen { mega | with this ;\n\t\tdef i this:value null eq not _mega =i\n\t\twhile { this:next null eq not } {\n\t\t\tthis:next =this\n\t\t\ti ++ =i\n\t\t}\n\t\ti\n\t}\n\tpush { | with item this ;\n\t\titem:unwrap;\n\t\tthis:value null eq if {\n\t\t\titem this:=value;\n\t\t\t2 stop\n\t\t}\n\t\tLinkedList:new\n\t\tdup :=value;<item>\n\t\t \tthis:last-entry:=next;\n\t}\n\tlast-entry { list | with this ;\n\t\twhile { this:next null eq not } {\n\t\t\tthis:next =this\n\t\t}\n\t\tthis\n\t}\n\tpeek { value | with this ;\n\t\tthis:last-entry:value\n\t}\n\tpop { item | with this ;\n\t\tdef item this =item\n\t\tnull\n\t\twhile { item:next null eq not } {\n\t\t\tpop item\n\t\t\titem:next =item\n\t\t}\n\t\t\"if theres no next item in this\";\n\t\tdup null eq if {\n\t\t\tpop\n\t\t\tthis:value (null this:=value;) 2 stop\n\t\t}\n\t\t:=next;<null>\n\t\titem:value\n\t}\n\tpush-front { | with item this ;\n\t\titem:unwrap;\n\t\tthis:value null eq if {\n\t\t\titem this:=value;\n\t\t\t2 stop\n\t\t}\n\t\t\"append new next identical to this, then make this contain new value\";\n\t\tdef new LinkedList:new =new\n\t\tthis:next new:=next;\n\t\tthis:value new:=value;\n\t\titem this:=value;\n\t\tnew this:=next;\n\t}\n\tinsert { | with item index this ;\n\t\titem:unwrap;\n\t\tindex 0 eq if {\n\t\t\titem this:push-front 2 stop\n\t\t}\n\t\tdef list this =list\n\t\twhile { index 1 gt } {\n\t\t\tlist:next =list\n\t\t\tindex -- =index\n\t\t}\n\t\titem list:next:push-front\n\t}\n\tpop-front { item | with this ;\n\t\tthis:value\n\t\tthis:next null eq dup if {\n\t\t\tnull this:=value\n\t\t}\n\t\tnot if {\n\t\t\tthis:next:value this:=value\n\t\t\tthis:next:next this:=next\n\t\t}\n\t}\n\tremove { item | with index this ;\n\t\tindex 0 eq if {\n\t\t\tthis:pop-front 2 stop\n\t\t}\n\t\tdef list this =list\n\t\twhile { index 1 gt } {\n\t\t\tlist:next =list\n\t\t\tindex -- =index\n\t\t}\n\t\tlist:next:pop-front\n\t}\n\tget { item | with index this ;\n\t\twhile { index 0 gt } {\n\t\t\tthis:next =this\n\t\t\tindex -- =index\n\t\t}\n\t\tthis:value\n\t}\n\tset { item | with value index this ;\n\t\twhile { index 0 gt } {\n\t\t\tthis:next =this\n\t\t\tindex -- =index\n\t\t}\n\t\tthis:value value this:=value\n\t}\n\tforeach { | with callable this ;\n\t\twhile { this null eq not } {\n\t\t\tthis:value dup null eq not if { callable:call; 0 } pop\n\t\t\tthis:next =this\n\t\t}\n\t}\n\titer { LinkedListIter | with this ;\n\t\tthis LinkedListIter:new\n\t}\n\tappend { this | with array this ;\n\t\tdef i 0 =i\n\t\tdef e this:last-entry =e\n\t\tthis:value null eq if {\n\t\t\t0 array:get this:=value;\n\t\t\ti ++ =i\n\t\t}\n\t\twhile { i array:len lt } {\n\t\t\tLinkedList:new dup e:=next =e\n\t\t\ti array:get e:=value\n\t\t\ti ++ =i\n\t\t}\n\t\tthis\n\t}\n\tto-array { array | with this ;\n\t\tdef i 0 =i\n\t\tdef a this:len anew =a\n\t\twhile { i a:len lt } {\n\t\t\tthis:value i a:set;\n\t\t\tthis:next =this\n\t\t\ti ++ =i\n\t\t}\n\t\ta\n\t}\n}\n\nconstruct LinkedListIter {\n\tcur-list\n\t;\n\tconstruct { this | with list this ;\n\t\tlist this:=cur-list\n\t\tthis\n\t}\n\tnext { item | with this ;\n\t\tthis:cur-list dup null eq if { 2 stop }\n\t\t:value\n\t\tthis:cur-list:next this:=cur-list;\n\t}\n}\n\ninclude _Iter in LinkedListIter\n";