PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/vm/list.h

https://code.google.com/p/python-on-a-chip/
C Header | 183 lines | 28 code | 21 blank | 134 comment | 0 complexity | 56a14f361650496056271f48280504db MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. # This file is Copyright 2002 Dean Hall.
  3. # This file is part of the PyMite VM.
  4. # This file is licensed under the MIT License.
  5. # See the LICENSE file for details.
  6. */
  7. #ifndef __LIST_H__
  8. #define __LIST_H__
  9. /**
  10. * \file
  11. * \brief List Object Type
  12. *
  13. * List object type header.
  14. */
  15. /**
  16. * List obj
  17. *
  18. * Mutable ordered sequence of objects. Contains ptr to linked list of nodes.
  19. */
  20. typedef struct PmList_s
  21. {
  22. /** Object descriptor */
  23. PmObjDesc_t od;
  24. /** List length; number of objs linked */
  25. uint16_t length;
  26. /** Ptr to linked list of nodes */
  27. pSeglist_t val;
  28. } PmList_t,
  29. *pPmList_t;
  30. /**
  31. * Allocates a new List object.
  32. *
  33. * If there is not enough memory to allocate the List,
  34. * the return status will indicate an OutOfMemoryError
  35. * that must be passed up to the interpreter.
  36. * Otherwise, a ptr to the list is returned by reference
  37. * and the return status is OK.
  38. *
  39. * @param r_pobj Return; addr of ptr to obj
  40. * @return Return status
  41. */
  42. PmReturn_t list_new(pPmObj_t *r_pobj);
  43. /**
  44. * Gets the object in the list at the index.
  45. *
  46. * @param plist Ptr to list obj
  47. * @param index Index into list
  48. * @param r_pobj Return by reference; ptr to item
  49. * @return Return status
  50. */
  51. PmReturn_t list_getItem(pPmObj_t plist, int16_t index, pPmObj_t *r_pobj);
  52. /**
  53. * Sets the item in the list at the index.
  54. *
  55. * @param plist Ptr to list
  56. * @param index Index into list
  57. * @param pobj Ptr to obj to put into list
  58. * @return Return status
  59. */
  60. PmReturn_t list_setItem(pPmObj_t plist, int16_t index, pPmObj_t pobj);
  61. /**
  62. * Makes a copy of the given list.
  63. *
  64. * Allocate the necessary memory for root and nodes.
  65. * Duplicate ptrs to objs.
  66. *
  67. * @param pobj Ptr to source list
  68. * @param r_pobj Return; Addr of ptr to return obj
  69. * @return Return status
  70. */
  71. PmReturn_t list_copy(pPmObj_t pobj, pPmObj_t *r_pobj);
  72. /**
  73. * Appends the given obj to the end of the given list.
  74. *
  75. * Allocate the memory for the node.
  76. * Do not copy obj, just reuse ptr.
  77. *
  78. * @param plist Ptr to list
  79. * @param pobj Ptr to item to append
  80. * @return Return status
  81. */
  82. PmReturn_t list_append(pPmObj_t plist, pPmObj_t pobj);
  83. /**
  84. * Creates a new list with the contents of psrclist
  85. * copied pint number of times.
  86. * This implements the python code "[0,...] * N"
  87. * where the list can be any list and N is an integer.
  88. *
  89. * @param psrclist The source list to replicate
  90. * @param n The integer number of times to replicate it
  91. * @param r_pnewlist Return; new list with its contents set.
  92. * @return Return status
  93. */
  94. PmReturn_t list_replicate(pPmObj_t psrclist, int16_t n, pPmObj_t *r_pnewlist);
  95. /**
  96. * Inserts the object into the list at the desired index.
  97. *
  98. * @param plist Ptr to list obj
  99. * @param pobj Ptr to obj to insert
  100. * @param index Index of where to insert obj
  101. * @return Return status
  102. */
  103. PmReturn_t list_insert(pPmObj_t plist, int16_t index, pPmObj_t pobj);
  104. /**
  105. * Removes a given object from the list.
  106. *
  107. * @param plist Ptr to list obj
  108. * @param item Ptr to object to be removed
  109. * @return Return status
  110. */
  111. PmReturn_t list_remove(pPmObj_t plist, pPmObj_t item);
  112. /**
  113. * Finds the first index of the item that matches pitem.
  114. * Returns an ValueError Exception if the item is not found.
  115. *
  116. * @param plist Ptr to list obj
  117. * @param pitem Ptr to object to be removed
  118. * @param r_index Return by reference; ptr to index (C uint16)
  119. * @return Return status
  120. */
  121. PmReturn_t list_index(pPmObj_t plist, pPmObj_t pitem, uint16_t *r_index);
  122. /**
  123. * Removes the item at the given index.
  124. * Raises a TypeError if the first argument is not a list.
  125. * Raises an IndexError if the index is out of bounds.
  126. *
  127. * @param plist Ptr to list obj
  128. * @param index Index of item to remove
  129. * @return Return status
  130. */
  131. PmReturn_t list_delItem(pPmObj_t plist, int16_t index);
  132. #ifdef HAVE_PRINT
  133. /**
  134. * Prints out a list. Uses obj_print() to print elements.
  135. *
  136. * @param pobj Object to print.
  137. * @return Return status
  138. */
  139. PmReturn_t list_print(pPmObj_t pobj);
  140. #endif /* HAVE_PRINT */
  141. /**
  142. * Removes all items from the list and zeroes the length.
  143. *
  144. * @param plist List to clear
  145. * @return Return status
  146. */
  147. PmReturn_t list_clear(pPmObj_t plist);
  148. #ifdef HAVE_SLICE
  149. /**
  150. * Creates a new list containing the described slice of the given list
  151. *
  152. * @param plist Ptr to list obj
  153. * @param pstart Ptr to int object of slice start index
  154. * @param pend Ptr to int object of slice end index
  155. * @param pstride Ptr to int object of slice stride value
  156. * @param r_pslice Reference of ptr to object containing resulting slice object
  157. * @return Return status
  158. */
  159. PmReturn_t list_slice(pPmObj_t plist, pPmObj_t pstart, pPmObj_t pend,
  160. pPmObj_t pstride, pPmObj_t *r_pslice);
  161. #endif /* HAVE_SLICE */
  162. #endif /* __LIST_H__ */