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

http://github.com/tybor/Liberty · Specman e · 415 lines · 199 code · 71 blank · 145 comment · 0 complexity · 9aa398fe919b05ea13a2fd73b096eaad MD5 · raw file

  1. indexing
  2. description: "Arrays of arbitrary elements which grow automatically as elements are added."
  3. copyright: "(C) 2005 Paolo Redaelli "
  4. license: "LGPL v2 or later"
  5. date: "$Date:$"
  6. revision: "$REvision:$"
  7. -- Description: G_ARRAYS are similar to standard C arrays,
  8. -- except that they grow automatically as elements are added.
  9. -- Array elements can be of any size (though all elements of
  10. -- one array are the same size), and the array can be
  11. -- automatically cleared to '0's and zero-terminated.
  12. -- To create a new array use g_array_new().
  13. -- To add elements to an array, use g_array_append_val(),
  14. -- g_array_append_vals(), g_array_prepend_val(), and
  15. -- g_array_prepend_vals().
  16. -- To access an element of an array, use g_array_index().
  17. -- To set the size of an array, use g_array_set_size().
  18. -- To free an array, use g_array_free().
  19. -- Example 3. Using a GArray to store gint values
  20. -- GArray *garray;
  21. -- gint i;
  22. -- /* We create a new array to store gint values.
  23. -- We don't want it zero-terminated or cleared to 0's. */
  24. -- garray = g_array_new (FALSE, FALSE, sizeof (gint));
  25. -- for (i = 0; i < 10000; i++)
  26. -- g_array_append_val (garray, i);
  27. -- for (i = 0; i < 10000; i++)
  28. -- if (g_array_index (garray, gint, i) != i)
  29. -- g_print ("ERROR: got %d instead of %d\n",
  30. -- g_array_index (garray, gint, i), i);
  31. -- g_array_free (garray, TRUE);
  32. class G_ARRAY [ITEM->C_STRUCT]
  33. inherit C_STRUCT
  34. -- TODO: make it a proper heir of ARRAY, FAST_ARRAY or
  35. -- ARRAYED_COLLECTION
  36. insert WRAPPER_FACTORY [ITEM]
  37. creation make, empty, from_external_pointer
  38. feature {} -- Creation
  39. make (zero_terminated, cleared: BOOLEAN) is
  40. -- Creates a new G_ARRAY. `zero_terminated' is True when the
  41. -- array should have an extra element at the end which is set
  42. -- to 0. `cleared' is True if elements should be
  43. -- automatically cleared to 0 when they are allocated.
  44. local element_size: INTEGER
  45. do
  46. -- element_size is the size of each element in bytes.
  47. from_external_pointer
  48. (g_array_new (zero_terminated.to_integer,
  49. cleared.to_integer,
  50. element_size))
  51. end
  52. with_capacity (a_capacity: INTEGER) is
  53. -- Creates a new GArray with `a_capacity' elements
  54. -- preallocated. This avoids frequent reallocation, if you
  55. -- are going to add many elements to the array. Note however
  56. -- that the size of the array is still 0. Note: (low level
  57. -- details) the array will be zero terminated, i.e. il will
  58. -- have an extra element at the end with all bits
  59. -- cleared. The allocated memory will be not cleared.
  60. require positive_capacity:
  61. -- a_capacity > 0
  62. local element_size: INTEGER
  63. do
  64. from_external_pointer
  65. (g_array_sized_new (1, -- zero_terminated,
  66. 0, -- clear_,
  67. element_size,
  68. a_capacity))
  69. end
  70. feature
  71. append (an_item: like first) is
  72. -- Adds the value on to the end of the array. The array will
  73. -- grow in size automatically if necessary.
  74. require item_not_void: an_item /= Void
  75. do
  76. g_array_append_val(handle,a_value.handle)
  77. -- Note: g_array_append_val() is a macro which uses a
  78. -- reference to the value parameter v. This means that you cannot use
  79. -- it with literal values such as "27". You must use variables.
  80. end
  81. append_values (some_items: ARRAY[ITEM]) is
  82. -- Adds `some_elements' onto the end of the array.
  83. require
  84. items_not_void: some_items /= Void
  85. items_not_empty: not some_items.is_empty
  86. local ptr: POINTER
  87. do
  88. ptr:=g_array_append_vals (handle, some_items.to_external, some_items.count)
  89. check
  90. -- g_array_prepend_vals should return the pointer to the GArray
  91. ptr = handle
  92. end
  93. end
  94. prepend (an_item: like first) is
  95. -- Adds `an_item' on to the start of the array. The array
  96. -- will grow in size automatically if necessary.
  97. -- This operation is slower than `append' since the existing
  98. -- elements in the array have to be moved to make space for
  99. -- the new element.
  100. require item_not_void: an_item /= Void
  101. do
  102. g_array_prepend_val (handle,an_item.handle)
  103. -- Note: g_array_prepend_val() is a macro which uses a
  104. -- reference to the value parameter v. This means that you
  105. -- cannot use it with literal values such as "27". You must
  106. -- use variables.
  107. end
  108. prepend_values (some_items: ARRAY[ITEM]) is
  109. -- Adds `some_items' onto the start of the array.
  110. -- This operation is slower than `append_values' since the
  111. -- existing elements in the array have to be moved to make
  112. -- space for the new elements.
  113. require
  114. items_not_void: some_items /= Void
  115. items_not_empty: not some_items.is_empty
  116. local ptr: POINTER
  117. do
  118. ptr:=g_array_prepend_vals (handle, some_items.to_external, some_items.count)
  119. check
  120. -- g_array_prepend_vals should return the pointer to the GArray
  121. ptr = handle
  122. end
  123. end
  124. insert_value (an_item: ITEM; an_index: INTEGER) is
  125. -- Inserts `an_item' into Current array at `an_index'.
  126. require
  127. item_not_void: an_item /= Void
  128. valid_index: is_valid_index (an_index)
  129. local ptr: POINTER
  130. do
  131. ptr:=g_array_insert_val (handle, an_index, an_item.handle)
  132. check
  133. ptr_is_c_garray: ptr = handle
  134. end
  135. -- Note: g_array_insert_val() is a macro which uses a
  136. -- reference to the value parameter v. This means that you
  137. -- cannot use it with literal values such as "27". You must
  138. -- use variables.
  139. ensure -- TODO: value_put
  140. end
  141. insert_values (some_values: ARRAY[ITEM]; an_index: INTEGER) is
  142. -- Inserts `some_values' into Current GArray at `an_index'.
  143. require
  144. values_not_void: some_values /= Void
  145. values_not_empty: not some_values.is_empty
  146. local ptr: POINTER
  147. do
  148. ptr:=g_array_insert_vals(handle, an_index,
  149. some_values.to_external,
  150. some_values.count)
  151. check
  152. ptr_is_c_garray: ptr = handle
  153. end
  154. end
  155. remove_index (an_index: INTEGER) is
  156. -- Removes the element at `an_index' from Current GArray. The
  157. -- following elements are moved down one place.
  158. require valid_index: is_valid_index (an_index)
  159. local ptr: POINTER
  160. do
  161. ptr:=g_array_remove_index(handle,an_index)
  162. check
  163. ptr_is_c_garray: ptr = handle
  164. end
  165. ensure count_decreased: count = old count - 1
  166. end
  167. remove_index_fast (an_index: INTEGER) is
  168. -- Removes the element at `an_index' from Current GArray. The
  169. -- last element in the array is used to fill in the space, so
  170. -- this function does not preserve the order of the
  171. -- GArray. But it is faster than `remove_index'.
  172. require valid_index: is_valid_index (an_index)
  173. local ptr: POINTER
  174. do
  175. ptr:=g_array_remove_index_fast(handle,an_index)
  176. check
  177. ptr_is_c_garray: ptr = handle
  178. end
  179. ensure
  180. count_decreased: count = old count - 1
  181. last_moved_to_an_index: old last = item(an_index)
  182. end
  183. remove_range (an_index, a_number: INTEGER) is
  184. -- Removes `a_number' elements starting at `an_index' from a
  185. -- GArray. The following elements are moved to close the gap.
  186. require
  187. valid_index: is_valid_index (an_index)
  188. valid_range: is_valid_index (an_index+a_lenght-1)
  189. local ptr: POINTER
  190. do
  191. ptr:=g_array_remove_range(handle,an_index,a_lenght)
  192. check
  193. ptr_is_c_garray: ptr = handle
  194. end
  195. ensure
  196. count_decreased: count = old count - a_number
  197. end
  198. -- TODO: sort ()
  199. -- void g_array_sort (GArray *array, GCompareFunc compare_func);
  200. -- Sorts a GArray using compare_func which should be a
  201. -- qsort()-style comparison function (returns -1 for first arg is
  202. -- less than second arg, 0 for equal, 1 if first arg is greater
  203. -- than second arg). array : a GArray. compare_func : comparison
  204. -- function. g_array_sort_with_data ()
  205. -- void g_array_sort_with_data (GArray *array, GCompareDataFunc
  206. -- compare_func, gpointer user_data);
  207. -- Like g_array_sort(), but the comparison function receives a user
  208. -- data argument. array : a GArray. compare_func : comparison
  209. -- function. user_data : data to pass to compare_func.
  210. item (an_index: INTEGER): ITEM is
  211. -- the element of Current GArray at `an_index'.
  212. -- Example 4. Getting a pointer to an element in a GArray
  213. -- EDayViewEvent *event; /* This gets a pointer to the 3rd
  214. -- element in the array of EDayViewEvent structs. */ event =
  215. -- &g_array_index (events, EDayViewEvent, 3);
  216. local ptr: POINTER
  217. do
  218. ptr := g_array_index(handle,an_index)
  219. -- The return value is cast to the given type.
  220. ensure implemented: False
  221. end
  222. set_size (a_length: INTEGER) is
  223. -- Sets the size of the array, expanding it if necessary. If the array
  224. -- was created with `cleared' set to True, the new elements are
  225. -- set to Void.
  226. require positive_length: a_length >= 0
  227. local ptr: POINTER
  228. do
  229. ptr:=g_array_set_size(handle,a_length)
  230. end
  231. dispose is
  232. local p: POINTER
  233. do
  234. -- g_array_free frees the memory allocated for the GArray. If
  235. -- free_segment is TRUE it frees the memory block holding the
  236. -- elements as well. Pass FALSE if you want to free the
  237. -- GArray wrapper but preserve the underlying array for use
  238. -- elsewhere. free_segment : if TRUE the actual element data
  239. -- is freed as well.
  240. p:=g_array_free (handle,
  241. 0 -- free_segment=False, i.e. do not
  242. -- free the actual data
  243. )
  244. unstore_eiffel_wrapper
  245. handle:=default_pointer
  246. end
  247. feature {} -- External calls
  248. struct_size: INTEGER is
  249. external "C inline use <glib.h>"
  250. alias "sizeof(GArray)"
  251. end
  252. g_array_new (zero_terminated_bool, clear_bool, an_element_size: INTEGER): POINTER is
  253. -- GArray* g_array_new (gboolean zero_terminated, gboolean
  254. -- clear_, guint element_size);
  255. -- TODO: an_element_size should be NATURAL
  256. external "C use <glib.h>"
  257. end
  258. g_array_sized_new (zero_terminated_bool, clear_bool, an_element_size, a_reserverd_size: INTEGER): POINTER is
  259. -- GArray* g_array_sized_new (gboolean zero_terminated, gboolean clear_, guint element_size, guint reserved_size);
  260. -- TODO: an_element_size should be NATURAL
  261. -- TODO: a_reserved_size should be NATURAL
  262. external "C use <glib.h>"
  263. end
  264. g_array_append_val (a,v: POINTER) is
  265. -- #define g_array_append_val (a,v)
  266. external "C macro use <glib.h>"
  267. end
  268. g_array_append_vals (an_array, some_data: INTEGER; a_len: INTEGER): POINTER is
  269. -- GArray* g_array_append_vals (GArray *array, gconstpointer
  270. -- data, guint len);
  271. -- TODO: a_len should be NATURAL
  272. external "C use <glib.h>"
  273. end
  274. g_array_prepend_val (a,v: POINTER) is
  275. -- #define g_array_prepend_val (a,v)
  276. external "C macro use <glib.h>"
  277. end
  278. g_array_prepend_vals (an_array, some_data: POINTER; a_len: INTEGER): POINTER is
  279. -- GArray* g_array_prepend_vals (GArray *array, gconstpointer
  280. -- data, guint len);
  281. -- TODO: a_len should be NATURAL
  282. external "C use <glib.h>"
  283. end
  284. g_array_insert_val (an_array: POINTER; an_index: INTEGER; a_value: POINTER) is
  285. -- #define g_array_insert_val (a,i,v)
  286. external "C macro use <glib.h>"
  287. end
  288. g_array_insert_vals (an_array: POINTER; guint_index: INTEGER; some_data: POINTER; guint_len: INTEGER): POINTER is
  289. -- GArray* g_array_insert_vals (GArray *array, guint index_,
  290. -- gconstpointer data, guint len);
  291. -- TODO: guint_index and guint_len should be NATURALs
  292. external "C use <glib.h>"
  293. end
  294. g_array_remove_index (an_array: POINTER; guint_index: INTEGER): POINTER is
  295. -- GArray* g_array_remove_index (GArray *array, guint
  296. -- index_);
  297. -- TODO: guint_index should be NATURAL
  298. external "C use <glib.h>"
  299. end
  300. g_array_remove_index_fast (an_array: POINTER; guint_index: INTEGER): POINTER is
  301. -- GArray* g_array_remove_index_fast (GArray *array, guint
  302. -- index_);
  303. -- TODO: guint_index should be NATURAL
  304. external "C use <glib.h>"
  305. end
  306. g_array_remove_range (an_array: POINTER; guint_index, guint_length: INTEGER): POINTER is
  307. -- GArray* g_array_remove_range (GArray *array, guint index_,
  308. -- guint length);
  309. -- TODO: guint_index and guint_len should be NATURALs
  310. external "C use <glib.h>"
  311. end
  312. g_array_sort (an_array, a_compare_func: POINTER) is
  313. -- void g_array_sort (GArray *array, GCompareFunc compare_func);
  314. external "C use <glib.h>"
  315. end
  316. g_array_sort_with_data (an_array, a_compare_func, some_data: POINTER) is
  317. -- void g_array_sort_with_data (GArray *array,
  318. -- GCompareDataFunc compare_func, gpointer user_data);
  319. external "C use <glib.h>"
  320. end
  321. g_array_index (an_array: POINTER; an_index: INTEGER): POINTER is
  322. -- #define g_array_index (a,t,i)
  323. external "C macro use <glib.h>"
  324. alias "g_array_index ($an_array, (void *), $an_index)"
  325. end
  326. g_array_set_size (an_array: POINTER; a_length): POINTER is
  327. -- GArray* g_array_set_size (GArray *array, guint length);
  328. -- TODO: a_length should be NATURAL
  329. external "C use <glib.h>"
  330. end
  331. g_array_free (an_array: POINTER; free_segment_bool: INTEGER): POINTER is
  332. -- gchar* g_array_free (GArray *array, gboolean free_segment);
  333. external "C use <glib.h>"
  334. end
  335. feature {} -- Struct access
  336. -- GArray
  337. -- typedef struct {
  338. -- gchar *data;
  339. -- guint len;
  340. -- } GArray;
  341. -- Contains the public fields of an Array.
  342. -- gchar *data; a pointer to the element data. The data may be moved as elements are added to the GArray.
  343. -- guint len; the number of elements in the GArray.
  344. end