Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1.  
  2. // Get the end of the list
  3. func int endOfListSub(var int list) { // subfunction to provide usersafety
  4. if MEM_ReadInt(list+4) {
  5. return endOfList(MEM_ReadInt(list+4));
  6. };
  7. return list;
  8. };
  9. func int endOfList(var int list) {
  10. if (!list) {
  11. MEM_Error("endOfList: no valid pointer");
  12. };
  13. return endOfListSub(list);
  14. };
  15.  
  16. func int endOfListSubS(var int list) {
  17. if MEM_ReadInt(list+8) {
  18. return endOfList(MEM_ReadInt(list+4));
  19. };
  20. return list;
  21. };
  22. func int endOfListS(var int list) {
  23. if (!list) {
  24. MEM_Error("endOfListS: no valid pointer");
  25. };
  26. return endOfListSubS(list);
  27. };
  28.  
  29.  
  30. // Get the length of the list
  31. func int lengthOfListSub(var int list, var int nr) { // subfunction to provide usersafety
  32. if MEM_ReadInt(list+4) {
  33. return lengthOfListSub(MEM_ReadInt(list+4), nr+1);
  34. };
  35. return nr;
  36. };
  37. func int lengthOfList(var int list) {
  38. if (!list) {
  39. MEM_Error("lengthOfList(): no valid pointer");
  40. };
  41. return lengthOfListSub(list, 0);
  42. };
  43.  
  44. func int lengthOfListSubS(var int list, var int nr) {
  45. if MEM_ReadInt(list+8) {
  46. return lengthOfListSub(MEM_ReadInt(list+8), nr+1);
  47. };
  48. return nr;
  49. };
  50. func int lengthOfListS(var int list) {
  51. if (!list) {
  52. MEM_Error("lengthOfListS: no valid pointer");
  53. };
  54. return lengthOfListSubS(list, 0);
  55. };
  56.  
  57.  
  58. // Get a specific node of the list by number
  59. func int getNodeSub(var int list, var int nr) { // subfunction to provide usersafety
  60. if (!nr) {
  61. return list;
  62. };
  63. return getNodeSub(MEM_ReadInt(list+4), nr-1);
  64. };
  65. func int getNode(var int list, var int nr) { // nr. Node of list 'list'
  66. if nr >= lengthOfList(list) {
  67. MEM_Error("getNode(): nr is greater than the list");
  68. };
  69. return getNodeSub(list, nr);
  70. };
  71.  
  72. func int getNodeSubS(var int list, var int nr) {
  73. if (!nr) {
  74. return list;
  75. };
  76. return getNodeSubS(MEM_ReadInt(list+8), nr-1);
  77. };
  78. func int getNodeS(var int list, var int nr) {
  79. if nr >= lengthOfListS(list) {
  80. MEM_Error("getNode: nr is greater than the list");
  81. };
  82. return getNodeSub(list, nr);
  83. };
  84.  
  85.  
  86. // add a node to a list
  87. func void addNode(var int list, var int data) {
  88. if (!list) {
  89. MEM_Error("addNode: no valid list");
  90. return;
  91. };
  92. var int ptr; ptr = MEM_Alloc(8);
  93. MEM_WriteInt(endOfList(list)+4, ptr);
  94. MEM_WriteInt(ptr, data);
  95. };
  96.  
  97. func void addNodeS(var int list, var int data) {
  98. if (!list) {
  99. MEM_Error("addNode: no valid list");
  100. return;
  101. };
  102. var int ptr; ptr = MEM_Alloc(12);
  103. MEM_WriteInt(endOfList(list)+8, ptr);
  104. MEM_WriteInt(ptr+4, data);
  105. };
  106.  
  107.  
  108. // delete a node of a list by number
  109. func void deleteNode(var int list, var int nodeNr) {
  110. var int prev; prev = getNode(list, nodeNr-1);
  111. var int del; del = MEM_ReadInt(prev);
  112. MEM_WriteInt(prev, MEM_ReadInt(del));
  113. MEM_Free(del);
  114. };
  115.  
  116. func void deleteNodeS(var int list, var int nodeNr) {
  117. var int prev; prev = getNodeS(list, nodeNr-1);
  118. var int del; del = MEM_ReadInt(prev+4);
  119. MEM_WriteInt(prev+4, MEM_ReadInt(del+4));
  120. MEM_Free(del);
  121. };
  122.  
  123.  
  124. // delete a complete list
  125. func void deleteList(var int list) {
  126. if (!list) {
  127. MEM_Error("deleteList: invalid list");
  128. return;
  129. };
  130. var int pos; pos = MEM_StackPos.position;
  131. if (!MEM_ReadInt(list)) {
  132. deleteNodeS(list, 0);
  133. return;
  134. };
  135. deleteNodeS(MEM_ReadInt(list), 0);
  136. MEM_StackPos.position = pos;
  137. };
  138.  
  139. func void deleteListS(var int list) {
  140. if (!list) {
  141. MEM_Error("deleteListS: invalid list");
  142. return;
  143. };
  144. var int pos; pos = MEM_StackPos.position;
  145. if (!MEM_ReadInt(list+4)) {
  146. deleteNodeS(list, 0);
  147. return;
  148. };
  149. deleteNodeS(MEM_ReadInt(list+4), 0);
  150. MEM_StackPos.position = pos;
  151. };
  152.  
  153.  
  154. // Call a function for every node and pass the node with it
  155. func void ForList(var int list, var string function) {
  156. var int pos; pos = MEM_StackPos.position;
  157. if (list) {
  158. MEM_PushIntParam(list);
  159. MEM_CallByString(function);
  160. list = MEM_ReadInt(list+4);
  161. MEM_StackPos.position = pos;
  162. };
  163. };
  164.  
  165. func void ForListS(var int list, var string function) {
  166. var int pos; pos = MEM_StackPos.position;
  167. if (list) {
  168. MEM_PushIntParam(list);
  169. MEM_CallByString(function);
  170. list = MEM_ReadInt(list+8);
  171. MEM_StackPos.position = pos;
  172. };
  173. };
  174.  
  175.  
  176. // copys a list to an array (memory of the size 4*nodes Bytes, contains the data in every word)
  177. var int ListArrayPtr;
  178. var int ListArrayNr;
  179. func void List_ToArraySub(var int list) {
  180. MEM_WriteInt(ListArrayPtr+(4*ListArrayNr), MEM_ReadInt(list));
  181. ListArrayNr += 1;
  182. };
  183. func int List_ToArray(var int list) {
  184. ListArrayPtr = MEM_Alloc(LengthOfList(list)*4);
  185. ListArrayNr = 0;
  186. ForList(list, "LIST_TOARRAYSUB");
  187. return ListArrayPtr;
  188. };
  189.  
  190. func void List_ToArraySubS(var int list) {
  191. MEM_WriteInt(ListArrayPtr+(4*ListArrayNr), MEM_ReadInt(list+4));
  192. ListArrayNr += 1;
  193. };
  194. func int List_ToArrayS(var int list) {
  195. ListArrayPtr = MEM_Alloc(LengthOfListS(list)*4);
  196. ListArrayNr = 0;
  197. ForListS(list, "LIST_TOARRAYSUBS");
  198. return ListArrayPtr;
  199. };
  200.  
  201.  
  202. // copys a list to an static array
  203. var MEMINT_HELPERCLASS CopyToINT_MinusOne;
  204. func int List_CopyToINT() {
  205. MEMINT_StackPopInst();
  206. MEMINT_StackPushInst(zPAR_TOK_PUSHINT);
  207.  
  208. ListArrayPtr = MEMINT_StackPopInt();
  209. var int list; list = MEMINT_StackPopInt();
  210. ForList(list, "LIST_TOARRAYSUB");
  211. return ListArrayPtr;
  212. };
  213. var MEMINT_HELPERCLASS CopyTo_MinusOne;
  214. func int List_CopyTo(var int list, var int statArr) {
  215. MEM_Error ("List_CopyTo was called before List_Init");
  216. };
  217.  
  218. var MEMINT_HELPERCLASS CopyToINT_MinusOneS;
  219. func int List_CopyToINTS() {
  220. MEMINT_StackPopInst();
  221. MEMINT_StackPushInst(zPAR_TOK_PUSHINT);
  222.  
  223. ListArrayPtr = MEMINT_StackPopInt();
  224. var int list; list = MEMINT_StackPopInt();
  225. ForList(list, "LIST_TOARRAYSUBS");
  226. return ListArrayPtr;
  227. };
  228. var MEMINT_HELPERCLASS CopyTo_MinusOneS;
  229. func int List_CopyToS() {
  230. MEM_Error ("List_CopyToS was called before List_Init");
  231. };
  232.  
  233.  
  234. func int List_Init() {
  235. MEMINT_StatArrs_ReplaceFunc (CopyTo_MinusOne + 1, CopyToInt_MinusOne + 1);
  236. MEMINT_StatArrs_ReplaceFunc(CopyTo_MinusOneS + 1, CopyToINT_MinusOneS + 1);
  237. };
  238.  
  239. // sets the data 'data' of node 'node'
  240. func void List_Set(var int node, var int data) {
  241. MEM_WriteInt(node, data);
  242. };
  243.  
  244. func void List_SetS(var int node, var int data) {
  245. MEM_WriteInt(node+4, data);
  246. };
  247.  
  248.  
  249. // returns the last node which contains the pointer 'data'
  250. var int List_ContainsNode;
  251. var int List_ContainsData;
  252. func void List_ContainsSub(var int list) {
  253. if List_ContainsData == MEM_ReadInt(list) {
  254. List_ContainsNode = list;
  255. };
  256. };
  257. func int List_Contains(var int list, var int data) {
  258. List_ContainsNode = -1;
  259. List_ContainsData = data;
  260. ForList(list, "LIST_CONTAINSSUB");
  261. return List_ContainsNode;
  262. };
  263.  
  264. func void List_ContainsSubS(var int list) {
  265. if List_ContainsData == MEM_ReadInt(list+4) {
  266. List_ContainsNode = list;
  267. };
  268. };
  269. func int List_ContainsS(var int list, var int data) {
  270. List_ContainsNode = -1;
  271. List_ContainsData = data;
  272. ForListS(list, "LIST_CONTAINSSUBS");
  273. return List_ContainsNode;
  274. };
  275.  
  276. // adds a node between node nr. 'offset' and node nr. 'offset + 1'
  277. func void List_AddOffset(var int list, var int offset, var int data) {
  278. var int prev; prev = getNode(list, offset);
  279. var int next; next = MEM_ReadInt(prev);
  280. MEM_WriteInt(prev, MEM_Alloc(8));
  281. MEM_WriteInt(MEM_ReadInt(prev), next);
  282. MEM_WriteInt(MEM_ReadInt(prev)+4, data);
  283. };
  284. func void List_AddOffsetS(var int list, var int offset, var int data) {
  285. var int prev; prev = getNodeS(list, offset);
  286. var int next; next = MEM_ReadInt(prev+4);
  287. MEM_WriteInt(prev, MEM_Alloc(12));
  288. MEM_WriteInt(MEM_ReadInt(prev+4), next);
  289. MEM_WriteInt(MEM_ReadInt(prev+8), data);
  290. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement