/src/wrappers/glib/library/data_types/g_list.e

http://github.com/tybor/Liberty · Specman e · 268 lines · 130 code · 49 blank · 89 comment · 0 complexity · c77eabf766951d803e096a97b19e1ceb MD5 · raw file

  1. indexing
  2. description: "The GList provides a standard doubly-linked list data %
  3. %structure. See also G_LIST_TRAVERSABLE."
  4. copyright: "2008 Raphael Mack"
  5. license: "LGPL v2 or later"
  6. date: "$Date:$"
  7. revision: "$Revision:$"
  8. deferred class G_LIST [ITEM->WRAPPER]
  9. -- TODO: add test for all COLLECTION features (modify the version
  10. -- from SE testsuite?)
  11. -- Some GLib-using libraries requires that some instances of
  12. -- GList shall be modified only by the library, making it
  13. -- effectively freezed for the developer. In those cases
  14. -- G_LIST_TRAVERSABLE shall be used in place of G_LIST. If the
  15. -- library allows its clients to change the lists it returns it
  16. -- will return a G_LIST.
  17. inherit
  18. G_LIST_TRAVERSABLE[ITEM]
  19. undefine
  20. out_in_tagged_out_memory,
  21. fill_tagged_out_memory
  22. end
  23. COLLECTION[ITEM]
  24. undefine
  25. swap,
  26. has,
  27. fast_has,
  28. clear_all,
  29. append_collection
  30. end
  31. feature
  32. put (an_item: like first; i: INTEGER) is
  33. --
  34. do
  35. glist_struct_set_data (g_list_nth(handle,i.to_natural_32), an_item.handle)
  36. end
  37. swap (i,j: INTEGER) is
  38. -- exchange two elements
  39. local
  40. ith,jth,tmp: POINTER
  41. in,jn: NATURAL_32
  42. do
  43. in := i.to_natural_32; jn := j.to_natural_32
  44. ith := g_list_nth_data (handle,in)
  45. jth := g_list_nth_data (handle,jn)
  46. tmp := glist_struct_get_data(ith)
  47. glist_struct_set_data (ith, glist_struct_get_data(jth))
  48. glist_struct_set_data (jth, tmp)
  49. end
  50. set_all_with (v: like first) is
  51. --
  52. local
  53. ith:POINTER
  54. do
  55. from ith:=handle
  56. until ith.is_null
  57. loop
  58. glist_struct_set_data (ith, v.handle)
  59. ith := glist_struct_get_next (ith)
  60. end
  61. end
  62. clear_all is do not_yet_implemented end
  63. add_first (element: like first) is
  64. --
  65. do
  66. handle := g_list_prepend (handle, element.handle)
  67. end
  68. add_last (element: like first) is
  69. -- Note that add_last has to traverse the entire list to find
  70. -- the end, which is inefficient when adding multiple
  71. -- elements. A common idiom to avoid the inefficiency is to
  72. -- prepend the elements and reverse the list when all
  73. -- elements have been added.
  74. do
  75. handle := g_list_append (handle, element.handle)
  76. end
  77. add (element: like first; an_index: INTEGER) is
  78. do
  79. handle := g_list_insert (handle, element.handle, an_index)
  80. end
  81. append_collection (other: COLLECTION[ITEM]) is
  82. do
  83. other.do_all(agent add_last)
  84. end
  85. force (element: like first; an_index: INTEGER) is
  86. do
  87. not_yet_implemented
  88. -- TODO
  89. end
  90. remove_first is
  91. require not is_empty
  92. do
  93. handle:=g_list_delete_link (handle, handle)
  94. end
  95. remove (an_index: INTEGER) is
  96. require
  97. not is_empty
  98. valid_index(an_index)
  99. do
  100. handle:=g_list_delete_link (handle, g_list_nth_data (handle, an_index.to_natural_32))
  101. end
  102. remove_head (n: INTEGER) is
  103. require
  104. n < count
  105. do
  106. -- Ohh, how long I longed to write it *THIS* way! Thanks Adrian! Paolo 2010-05-30
  107. n.times(agent remove_first)
  108. end
  109. remove_tail (n: INTEGER) is
  110. require
  111. n < count
  112. do
  113. -- Ohh, how long I longed to write it *THIS* way! Thanks Adrian! Paolo 2010-05-30
  114. n.times(agent remove_last)
  115. end
  116. remove_last is
  117. require
  118. not is_empty
  119. do
  120. handle:=g_list_delete_link (handle,g_list_last (handle))
  121. end
  122. clear_count, clear_count_and_capacity is
  123. -- Discard all items (is_empty is True after that call). Frees
  124. -- all of the memory used by a GList. The freed elements are
  125. -- added to the GAllocator free list.
  126. do
  127. g_list_free (handle)
  128. handle := default_pointer
  129. end
  130. replace_all (old_value, new_value: like first) is
  131. do
  132. not_yet_implemented -- TODO
  133. end
  134. fast_replace_all (old_value, new_value: like first) is
  135. do
  136. not_yet_implemented -- TODO
  137. end
  138. reverse is
  139. local old_handle: POINTER
  140. do
  141. old_handle := handle
  142. handle:=g_list_reverse (old_handle)
  143. end
  144. append (an_item: like first) is
  145. -- Adds `an_item' on to the end of the list.
  146. do
  147. handle:=g_list_append (handle, an_item.handle)
  148. -- Note: The return value is the new start of the list, which may have changed, so make sure you store the new value.
  149. -- Note: g_list_append() has to traverse the entire list to
  150. -- find the end, which is inefficient when adding multiple
  151. -- elements. A common idiom to avoid the inefficiency is to
  152. -- prepend the elements and reverse the list when all
  153. -- elements have been added.
  154. -- /* Notice that these are initialized to the empty list. */
  155. -- GList *list = NULL, *number_list = NULL;
  156. -- /* This is a list of strings. */
  157. -- list = g_list_append (list, "first");
  158. -- list = g_list_append (list, "second");
  159. -- /* This is a list of integers. */
  160. -- number_list = g_list_append (number_list, GINT_TO_POINTER (27));
  161. -- number_list = g_list_append (number_list, GINT_TO_POINTER (14));
  162. end
  163. prepend (an_item: like first) is
  164. -- Adds a new element on to the start of the list.
  165. require valid_item: an_item/=Void
  166. do
  167. handle := g_list_prepend (handle,an_item.handle)
  168. -- Note: The return value is the new start of the list, which
  169. -- may have changed, so make sure you store the new value.
  170. -- /* Notice that it is initialized to the empty list. */
  171. -- GList *list = NULL; list = g_list_prepend (list,
  172. -- "last"); list = g_list_prepend (list, "first");
  173. end
  174. -- g_list_insert ()
  175. -- GList* g_list_insert (GList *list, gpointer data, gint position);
  176. -- Inserts a new element into the list at the given position.
  177. -- list : a GList.
  178. -- data : the data for the new element.
  179. -- position : the position to insert the element. If this is negative, or is larger than the number of elements in the list, the new element is added on to the end of the list.
  180. -- Returns : the new start of the GList.
  181. -- g_list_insert_before ()
  182. -- GList* g_list_insert_before (GList *slist, GList *sibling, gpointer data);
  183. -- Inserts a node before sibling containing data. Returns the new head of the list.
  184. -- slist : a GList.
  185. -- sibling : node to insert data before.
  186. -- data : data to put in the newly-inserted node.
  187. -- Returns : new head of the list.
  188. -- g_list_insert_sorted ()
  189. -- GList* g_list_insert_sorted (GList *list, gpointer data, GCompareFunc func);
  190. -- Inserts a new element into the list, using the given comparison function to determine its position.
  191. -- list : a GList.
  192. -- data : the data for the new element.
  193. -- func : the function to compare elements in the list. It should return a number > 0 if the first parameter comes after the second parameter in the sort order.
  194. -- Returns : the new start of the GList.
  195. -- g_list_remove ()
  196. -- GList* g_list_remove (GList *list, gconstpointer data);
  197. -- Removes an element from a GList. If two elements contain the same data, only the first is removed. If none of the elements contain the data, the GList is unchanged.
  198. -- list : a GList.
  199. -- data : the data of the element to remove.
  200. -- Returns : the new start of the GList.
  201. -- g_list_remove_link ()
  202. -- GList* g_list_remove_link (GList *list, GList *link_);
  203. -- Removes an element from a GList, without freeing the element. The removed element's next link is set to NULL, so that it becomes a self-contained list with one element.
  204. -- list : a GList.
  205. -- link_ : an element in the GList.
  206. -- Returns : the new start of the GList, without the element.
  207. -- g_list_delete_link ()
  208. -- GList* g_list_delete_link (GList *list, GList *link_);
  209. -- Deletes a node of list. Returns the new list head.
  210. -- list : a GList.
  211. -- link_ : node to delete.
  212. -- Returns : new head of list.
  213. -- g_list_remove_all ()
  214. -- GList* g_list_remove_all (GList *list, gconstpointer data);
  215. -- Removes all list nodes with data equal to data. Returns the new head of the list. Contrast with g_list_remove() which removes only the first node matching the given data.
  216. -- list : a GList.
  217. -- data : data to remove.
  218. -- Returns : new head of list.
  219. end