1 /*
2 Copyright (C) 2003 Cedric Cellier, Dominique Lavault
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18 #include "slist.h"
19 #include "list.h"
20 #include "log.h"
21 #include "memspool.h"
22 #include "stack.h"
23 #include <assert.h>
24 
25 /* heavy use of list */
26 
27 struct s_gltv_slist {
28 	GLTV_LIST list;
29 	int (*comp)(void *, void *);
30 };
31 typedef struct s_gltv_slist slist;
32 
gltv_slist_new(unsigned total,int optimize_for_size,int (* comp)(void *,void *))33 GLTV_SLIST gltv_slist_new(unsigned total, int optimize_for_size, int (*comp)(void *, void *)) {
34 	slist *this_slist;
35 	this_slist = gltv_memspool_alloc(sizeof(slist));
36 	if (!this_slist) return 0;
37 	this_slist->list = gltv_list_new(total, optimize_for_size);
38 	if (! this_slist->list) {
39 		gltv_memspool_unregister(this_slist);
40 		return 0;
41 	}
42 	this_slist->comp = comp;
43 	return (GLTV_SLIST)this_slist;
44 }
45 
gltv_slist_del(GLTV_SLIST l)46 void gltv_slist_del(GLTV_SLIST l) {
47 	gltv_list_del(l->list);
48 	gltv_memspool_unregister(l);
49 }
50 
gltv_slist_clear(GLTV_SLIST l)51 void gltv_slist_clear(GLTV_SLIST l) {
52 	gltv_list_clear(l->list);
53 }
54 
gltv_slist_size(GLTV_SLIST l)55 unsigned gltv_slist_size(GLTV_SLIST l) {
56 	return gltv_list_size(l->list);
57 }
58 
gltv_slist_get(GLTV_SLIST l,unsigned order)59 void *gltv_slist_get(GLTV_SLIST l, unsigned order) {
60 	return gltv_list_get(l->list, order);
61 }
62 
63 /* to keep ordering, we remove then insert back */
gltv_slist_set(GLTV_SLIST l,unsigned order,void * value)64 void gltv_slist_set(GLTV_SLIST l, unsigned order, void *value) {
65 	gltv_slist_remove(l, order);
66 	gltv_slist_insert(l, value);
67 }
68 
gltv_slist_remove(GLTV_SLIST l,unsigned order)69 void *gltv_slist_remove(GLTV_SLIST l, unsigned order) {
70 	return gltv_list_remove(l->list, order);
71 }
72 
73 /* all the real work is done at insert */
gltv_slist_insert(GLTV_SLIST l,void * value)74 unsigned gltv_slist_insert(GLTV_SLIST l, void *value) {
75 	unsigned size = gltv_list_size(l->list);
76 	unsigned index;
77 	for (index=0; index<size; index++) {
78 		int c = l->comp(gltv_list_get(l->list, index), value);
79 		if (c>=0) break;
80 	}
81 	gltv_list_insert(l->list, index, value);
82 	return index;
83 }
84 
85