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

http://github.com/tybor/Liberty · Specman e · 592 lines · 236 code · 140 blank · 216 comment · 1 complexity · 9471f264b33996f64a9d361b034c5c38 MD5 · raw file

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