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