/src/wrappers/glib/partially-implemented/g_list_string.e

http://github.com/tybor/Liberty · Specman e · 576 lines · 226 code · 134 blank · 216 comment · 2 complexity · 17daa5cdf6eba8d5e2b41ff7aa92b68a MD5 · raw file

  1. indexing
  2. description: "A special version of GList (doubly-linked list) containing strings."
  3. copyright: "(C) 2007 Paolo Redaelli "
  4. license: "LGPL v2 or later"
  5. date: "$Date:$"
  6. revision: "$Revision:$"
  7. class G_LIST_STRING
  8. inherit
  9. C_STRUCT
  10. rename
  11. is_not_null as wrapped_object_exists
  12. redefine
  13. copy
  14. end
  15. -- TODO: COLLECTION[STRING] or G_LIST[C_STRING]
  16. G_FREEZABLE
  17. insert
  18. GLIST_EXTERNALS undefine fill_tagged_out_memory end
  19. GLIST_STRUCT
  20. creation make, from_external_pointer
  21. feature
  22. make is
  23. do
  24. handle := default_pointer
  25. end
  26. first: STRING is
  27. do
  28. create Result.from_external_copy (glist_struct_get_data (handle))
  29. end
  30. last: like first is
  31. do
  32. create Result.from_external_copy (glist_struct_get_data (g_list_last (handle)))
  33. end
  34. item (i: INTEGER): like first is
  35. do
  36. create Result.from_external_copy (g_list_nth_data (handle, i))
  37. end
  38. put (a_string: like first; i: INTEGER) is
  39. require -- else
  40. valid_item: a_string/=Void
  41. thawed: not is_freezed
  42. do
  43. glist_struct_set_data (g_list_nth(handle,i), a_string.to_external)
  44. end
  45. swap (i,j: INTEGER) is
  46. require thawed: not is_freezed
  47. local ith,jth,tmp: POINTER
  48. do
  49. ith := g_list_nth_data (handle,i)
  50. jth := g_list_nth_data (handle,j)
  51. tmp := glist_struct_get_data(ith)
  52. glist_struct_set_data (ith, glist_struct_get_data(jth))
  53. glist_struct_set_data (jth, tmp)
  54. end
  55. set_all_with (v: like first) is
  56. require thawed: not is_freezed
  57. local ith:POINTER
  58. do
  59. from ith:=handle
  60. until ith.is_null
  61. loop
  62. glist_struct_set_data (ith, v.to_external)
  63. ith := glist_struct_get_next (ith)
  64. end
  65. end
  66. clear_all is
  67. require thawed: not is_freezed
  68. do
  69. not_yet_implemented
  70. end
  71. add_first (a_string: like first) is
  72. require thawed: not is_freezed
  73. do
  74. handle := g_list_prepend (handle, a_string.to_external)
  75. end
  76. add_last (a_string: like first) is
  77. -- Note that add_last has to traverse the entire list to find
  78. -- the end, which is inefficient when adding multiple
  79. -- elements. A common idiom to avoid the inefficiency is to
  80. -- prepend the elements and reverse the list when all
  81. -- elements have been added.
  82. require thawed: not is_freezed
  83. do
  84. handle := g_list_append (handle, a_string.to_external)
  85. end
  86. add (a_string: like first; an_index: INTEGER) is
  87. require thawed: not is_freezed
  88. do
  89. handle := g_list_insert (handle, a_string.to_external, an_index-1)
  90. end
  91. append_collection (other: COLLECTION[STRING]) is
  92. require thawed: not is_freezed
  93. do
  94. check implemented: False end
  95. not_yet_implemented -- TODO
  96. end
  97. force (a_string: like first; an_index: INTEGER) is do not_yet_implemented end
  98. remove_first is
  99. require thawed: not is_freezed
  100. do
  101. handle:=g_list_delete_link (handle, handle)
  102. end
  103. remove (an_index: INTEGER) is
  104. require thawed: not is_freezed
  105. do
  106. handle:=g_list_delete_link (handle,
  107. g_list_nth_data (handle, an_index-1))
  108. end
  109. remove_last is
  110. require thawed: not is_freezed
  111. do
  112. handle:=g_list_delete_link (handle,g_list_last (handle))
  113. end
  114. clear_count, clear_count_and_capacity is
  115. -- Discard all items (is_empty is True after that call). Frees
  116. -- all of the memory used by a GList. The freed elements are
  117. -- added to the GAllocator free list.
  118. require thawed: not is_freezed
  119. do
  120. g_list_free (handle)
  121. handle := default_pointer
  122. end
  123. has (x: like first): BOOLEAN is
  124. -- Look for x using is_equal for comparison. Note: current
  125. -- implementation is just a copy of `fast_has'; try using
  126. -- `fast_has' whenever possible since an implementation of
  127. -- `has' that really uses `is_equal' will be quite slow.
  128. do
  129. Result:=fast_has(x)
  130. end
  131. fast_has (a_string: like first): BOOLEAN is
  132. -- Look for x using basic = for comparison.
  133. do
  134. if (g_list_find(handle,a_string.to_external).is_not_null)
  135. then Result:=True
  136. else check Result=False end
  137. end
  138. end
  139. first_index_of (a_string: like first): INTEGER is
  140. -- Give the index of the first occurrence of element using
  141. -- is_equal for comparison. Answer upper + 1 when element is
  142. -- not inside.
  143. do
  144. Result:=g_list_index(handle,a_string.to_external)
  145. end
  146. index_of (a_string: like first): INTEGER is
  147. do
  148. Result:=first_index_of(a_string)
  149. end
  150. reverse_index_of (a_string: like first; start_index: INTEGER): INTEGER is do not_yet_implemented end
  151. fast_first_index_of (a_string: like first): INTEGER is
  152. -- Give the index of the first occurrence of element using
  153. -- basic = for comparison. Answer upper + 1 when element is
  154. -- not inside.
  155. do
  156. check implemented: False end
  157. not_yet_implemented -- TODO
  158. end
  159. fast_index_of (a_string: like first): INTEGER is do not_yet_implemented end
  160. fast_reverse_index_of (a_string: like first; start_index: INTEGER): INTEGER is
  161. -- Using basic = comparison, gives the index of the first
  162. -- occurrence of element at or before start_index. Search is
  163. -- done in reverse direction, which means from the
  164. -- start_index down to the lower index . Answer lower -1 when
  165. -- the search fail.
  166. do
  167. check implemented: False end
  168. not_yet_implemented -- TODO
  169. end
  170. is_equal_map (other: LINKED_COLLECTION [STRING]): BOOLEAN is
  171. -- Do both collections have the same lower, upper, and items?
  172. -- Feature is_equal is used for comparison of items.
  173. do
  174. check implemented: False end
  175. not_yet_implemented -- TODO
  176. end
  177. all_default: BOOLEAN is
  178. -- Do all items have their type s default value? Note: for
  179. -- non Void items, the test is performed with the is_default
  180. -- predicate.
  181. do
  182. check implemented: False end
  183. not_yet_implemented -- TODO
  184. end
  185. copy (other: like Current) is
  186. do
  187. check implemented: False end
  188. not_yet_implemented -- TODO
  189. end
  190. occurrences (a_string: like first): INTEGER is
  191. -- Number of occurrences of element using is_equal for comparison.
  192. do
  193. check implemented: False end
  194. not_yet_implemented -- TODO
  195. end
  196. fast_occurrences (a_string: like first): INTEGER is
  197. do
  198. check implemented: False end
  199. not_yet_implemented -- TODO
  200. end
  201. replace_all (old_value, new_value: like first) is
  202. require thawed: not is_freezed
  203. do
  204. check implemented: False end
  205. not_yet_implemented -- TODO
  206. end
  207. fast_replace_all (old_value, new_value: like first) is
  208. require thawed: not is_freezed
  209. do
  210. check implemented: False end
  211. not_yet_implemented -- TODO
  212. end
  213. slice (min, max: INTEGER): G_LIST_STRING is
  214. do
  215. check implemented: False end
  216. not_yet_implemented -- TODO
  217. end
  218. reverse is
  219. require thawed: not is_freezed
  220. local old_handle: POINTER
  221. do
  222. old_handle := handle
  223. handle:=g_list_reverse (handle)
  224. g_list_free (handle) -- TODO is this call correct?
  225. end
  226. upper,count: INTEGER is
  227. do
  228. Result:=g_list_length(handle)
  229. ensure -- then
  230. positive: Result >= 0
  231. end
  232. is_empty: BOOLEAN is
  233. do
  234. Result:= (handle.is_null)
  235. end
  236. from_collection (model: COLLECTION[STRING]) is do not_yet_implemented end
  237. new_iterator: ITERATOR[STRING] is
  238. do
  239. create {ITERATOR_ON_G_LIST_STRING} Result.make (Current)
  240. ensure valid: Result/=Void
  241. end
  242. feature -- Memory management
  243. dispose is
  244. do
  245. -- We override the default dispose routine; list nodes are not
  246. -- allocated with malloc() so we should not use free()
  247. g_list_free (handle)
  248. handle:= default_pointer
  249. end
  250. -- Glib's doc, useful for implementing unimplemented
  251. -- typedef struct {
  252. -- gpointer data;
  253. -- GList *next;
  254. -- } GList;
  255. -- The GList struct is used for each element in the singly-linked list.
  256. -- gpointer data; holds the element's data, which can be a pointer to any kind of data, or any integer value using the Type Conversion Macros.
  257. -- GList *next; contains the link to the next element in the list.
  258. -- g_list_alloc ()
  259. -- GList* g_list_alloc (void);
  260. -- Allocates space for one GList element. It is called by the g_list_append(), g_list_prepend(), g_list_insert() and g_list_insert_sorted() functions and so is rarely used on its own.
  261. -- Returns : a pointer to the newly-allocated GList element.
  262. append (a_string: like first) is
  263. -- Adds `a_string' on to the end of the list.
  264. do
  265. handle:=g_list_append (handle, a_string.to_external)
  266. -- Note: The return value is the new start of the list, which may have changed, so make sure you store the new value.
  267. -- Note: g_list_append() has to traverse the entire list to
  268. -- find the end, which is inefficient when adding multiple
  269. -- elements. A common idiom to avoid the inefficiency is to
  270. -- prepend the elements and reverse the list when all
  271. -- elements have been added.
  272. -- /* Notice that these are initialized to the empty list. */
  273. -- GList *list = NULL, *number_list = NULL;
  274. -- /* This is a list of strings. */
  275. -- list = g_list_append (list, "first");
  276. -- list = g_list_append (list, "second");
  277. -- /* This is a list of integers. */
  278. -- number_list = g_list_append (number_list, GINT_TO_POINTER (27));
  279. -- number_list = g_list_append (number_list, GINT_TO_POINTER (14));
  280. end
  281. prepend (a_string: like first) is
  282. -- Adds a new element on to the start of the list.
  283. require valid_item: a_string/=Void
  284. do
  285. handle := g_list_prepend (handle,a_string.to_external)
  286. -- Note: The return value is the new start of the list, which
  287. -- may have changed, so make sure you store the new value.
  288. -- /* Notice that it is initialized to the empty list. */
  289. -- GList *list = NULL; list = g_list_prepend (list,
  290. -- "last"); list = g_list_prepend (list, "first");
  291. end
  292. -- g_list_insert ()
  293. -- GList* g_list_insert (GList *list, gpointer data, gint position);
  294. -- Inserts a new element into the list at the given position.
  295. -- list : a GList.
  296. -- data : the data for the new element.
  297. -- 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.
  298. -- Returns : the new start of the GList.
  299. -- g_list_insert_before ()
  300. -- GList* g_list_insert_before (GList *list, GList *sibling, gpointer data);
  301. -- Inserts a node before sibling containing data. Returns the new head of the list.
  302. -- list : a GList.
  303. -- sibling : node to insert data before.
  304. -- data : data to put in the newly-inserted node.
  305. -- Returns : new head of the list.
  306. -- g_list_insert_sorted ()
  307. -- GList* g_list_insert_sorted (GList *list, gpointer data, GCompareFunc func);
  308. -- Inserts a new element into the list, using the given comparison function to determine its position.
  309. -- list : a GList.
  310. -- data : the data for the new element.
  311. -- 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.
  312. -- Returns : the new start of the GList.
  313. -- g_list_remove ()
  314. -- GList* g_list_remove (GList *list, gconstpointer data);
  315. -- 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.
  316. -- list : a GList.
  317. -- data : the data of the element to remove.
  318. -- Returns : the new start of the GList.
  319. -- g_list_remove_link ()
  320. -- GList* g_list_remove_link (GList *list, GList *link_);
  321. -- 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.
  322. -- list : a GList.
  323. -- link_ : an element in the GList.
  324. -- Returns : the new start of the GList, without the element.
  325. -- g_list_delete_link ()
  326. -- GList* g_list_delete_link (GList *list, GList *link_);
  327. -- Deletes a node of list. Returns the new list head.
  328. -- list : a GList.
  329. -- link_ : node to delete.
  330. -- Returns : new head of list.
  331. -- g_list_remove_all ()
  332. -- GList* g_list_remove_all (GList *list, gconstpointer data);
  333. -- 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.
  334. -- list : a GList.
  335. -- data : data to remove.
  336. -- Returns : new head of list.
  337. -- g_list_free ()
  338. -- void g_list_free (GList *list);
  339. -- Frees all of the memory used by a GList. The freed elements are added to the GAllocator free list.
  340. -- list : a GList.
  341. -- g_list_free_1 ()
  342. -- void g_list_free_1 (GList *list);
  343. -- Frees one GList element. It is usually used after g_list_remove_link().
  344. -- list : a GList element.
  345. -- g_list_length ()
  346. -- guint g_list_length (GList *list);
  347. -- Gets the number of elements in a GList.
  348. -- list : a GList.
  349. -- Returns : the number of elements in the GList.
  350. -- g_list_copy ()
  351. -- GList* g_list_copy (GList *list);
  352. -- Copies a GList.
  353. -- Note that this is a "shallow" copy. If the list elements consist of pointers to data, the pointers are copied but the actual data isn't.
  354. -- list : a GList.
  355. -- Returns : a copy of list.
  356. -- g_list_reverse ()
  357. -- GList* g_list_reverse (GList *list);
  358. -- Reverses a GList.
  359. -- list : a GList.
  360. -- Returns : the start of the reversed GList.
  361. -- g_list_sort ()
  362. -- GList* g_list_sort (GList *list, GCompareFunc compare_func);
  363. -- Sorts a GList using the given comparison function.
  364. -- list : a GList.
  365. -- compare_func : the comparison function used to sort the GList. This function is passed the data from 2 elements of the GList and should return 0 if they are equal, a negative value if the first element comes before the second, or a positive value if the first element comes after the second.
  366. -- Returns : the start of the sorted GList.
  367. -- g_list_sort_with_data ()
  368. -- GList* g_list_sort_with_data (GList *list, GCompareDataFunc compare_func, gpointer user_data);
  369. -- Like g_list_sort(), but the sort function accepts a user data argument.
  370. -- list : a GList
  371. -- compare_func : comparison function.
  372. -- user_data : data to pass to comparison function.
  373. -- Returns : new head of the list.
  374. -- g_list_concat ()
  375. -- GList* g_list_concat (GList *list1, GList *list2);
  376. -- Adds the second GList onto the end of the first GList. Note that the elements of the second GList are not copied. They are used directly.
  377. -- list1 : a GList.
  378. -- list2 : the GList to add to the end of the first GList.
  379. -- Returns : the start of the new GList.
  380. -- g_list_foreach ()
  381. -- void g_list_foreach (GList *list, GFunc func, gpointer user_data);
  382. -- Calls a function for each element of a GList.
  383. -- list : a GList.
  384. -- func : the function to call with each element's data.
  385. -- user_data : user data to pass to the function.
  386. -- g_list_last ()
  387. -- GList* g_list_last (GList *list);
  388. -- Gets the last element in a GList.
  389. -- list : a GList.
  390. -- Returns : the last element in the GList, or NULL if the GList has no elements.
  391. -- g_list_next()
  392. -- #define g_list_next(list)
  393. -- A convenience macro to gets the next element in a GList.
  394. -- list : an element in a GList.
  395. -- Returns : the next element, or NULL if there are no more elements.
  396. -- g_list_nth ()
  397. -- GList* g_list_nth (GList *list, guint n);
  398. -- Gets the element at the given position in a GList.
  399. -- list : a GList.
  400. -- n : the position of the element, counting from 0.
  401. -- Returns : the element, or NULL if the position is off the end of the GList.
  402. -- g_list_nth_data ()
  403. -- gpointer g_list_nth_data (GList *list, guint n);
  404. -- Gets the data of the element at the given position.
  405. -- list : a GList.
  406. -- n : the position of the element.
  407. -- Returns : the element's data, or NULL if the position is off the end of the GList.
  408. -- g_list_find ()
  409. -- GList* g_list_find (GList *list, gconstpointer data);
  410. -- Finds the element in a GList which contains the given data.
  411. -- list : a GList.
  412. -- data : the element data to find.
  413. -- Returns : the found GList element, or NULL if it is not found.
  414. -- g_list_find_custom ()
  415. -- GList* g_list_find_custom (GList *list, gconstpointer data, GCompareFunc func);
  416. -- Finds an element in a GList, using a supplied function to find the desired element. It iterates over the list, calling the given function which should return 0 when the desired element is found. The function takes two gconstpointer arguments, the GList element's data as the first argument and the given user data.
  417. -- list : a GList.
  418. -- data : user data passed to the function.
  419. -- func : the function to call for each element. It should return 0 when the desired element is found.
  420. -- Returns : the found GList element, or NULL if it is not found.
  421. -- g_list_position ()
  422. -- gint g_list_position (GList *list, GList *llink);
  423. -- Gets the position of the given element in the GList (starting from 0).
  424. -- list : a GList.
  425. -- llink : an element in the GList.
  426. -- Returns : the position of the element in the GList, or -1 if the element is not found.
  427. -- g_list_index ()
  428. -- gint g_list_index (GList *list, gconstpointer data);
  429. -- Gets the position of the element containing the given data (starting from 0).
  430. -- list : a GList.
  431. -- data : the data to find.
  432. -- Returns : the index of the element containing the data, or -1 if the data is not found.
  433. -- g_list_push_allocator ()
  434. -- void g_list_push_allocator (GAllocator *allocator);
  435. -- Sets the allocator to use to allocate GList elements. Use g_list_pop_allocator() to restore the previous allocator.
  436. -- Note that this function is not available if GLib has been compiled with --disable-mem-pools
  437. -- allocator : the GAllocator to use when allocating GList elements.
  438. -- g_list_pop_allocator ()
  439. -- void g_list_pop_allocator (void);
  440. -- Restores the previous GAllocator, used when allocating GList elements.
  441. -- Note that this function is not available if GLib has been compiled with --disable-mem-pools
  442. end