PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

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