File: queue.c

package info (click to toggle)
libgee-0.8 0.20.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,704 kB
  • sloc: ansic: 134,718; sh: 4,838; makefile: 372
file content (283 lines) | stat: -rw-r--r-- 8,793 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* queue.c generated by valac 0.56.17, the Vala compiler
 * generated from queue.vala, do not modify */

/* queue.vala
 *
 * Copyright (C) 2009  Didier Villevalois
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Didier 'Ptitjes Villevalois <ptitjes@free.fr>
 */

#include "gee.h"
#include <glib-object.h>
#include <glib.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

static gboolean gee_queue_real_offer (GeeQueue* self,
                               gconstpointer element);
static gint gee_queue_real_drain (GeeQueue* self,
                           GeeCollection* recipient,
                           gint amount);
static GType gee_queue_get_type_once (void);

/**
 * Offers the specified element to this queue.
 *
 * @param element the element to offer to the queue
 *
 * @return        ``true`` if the element was added to the queue
 */
static gboolean
gee_queue_real_offer (GeeQueue* self,
                      gconstpointer element)
{
	gboolean result;
	result = gee_collection_add ((GeeCollection*) self, element);
	return result;
}

gboolean
gee_queue_offer (GeeQueue* self,
                 gconstpointer element)
{
	GeeQueueIface* _iface_;
	g_return_val_if_fail (self != NULL, FALSE);
	_iface_ = GEE_QUEUE_GET_INTERFACE (self);
	if (_iface_->offer) {
		return _iface_->offer (self, element);
	}
	return FALSE;
}

/**
 * Peeks (retrieves, but not remove) an element from this queue.
 *
 * @return the element peeked from the queue (or ``null`` if none was
 *         available)
 */
gpointer
gee_queue_peek (GeeQueue* self)
{
	GeeQueueIface* _iface_;
	g_return_val_if_fail (self != NULL, NULL);
	_iface_ = GEE_QUEUE_GET_INTERFACE (self);
	if (_iface_->peek) {
		return _iface_->peek (self);
	}
	return NULL;
}

/**
 * Polls (retrieves and remove) an element from this queue.
 *
 * @return the element polled from the queue (or ``null`` if none was
 *         available)
 */
gpointer
gee_queue_poll (GeeQueue* self)
{
	GeeQueueIface* _iface_;
	g_return_val_if_fail (self != NULL, NULL);
	_iface_ = GEE_QUEUE_GET_INTERFACE (self);
	if (_iface_->poll) {
		return _iface_->poll (self);
	}
	return NULL;
}

/**
 * Drains the specified amount of elements from this queue in the specified
 * recipient collection.
 *
 * @param recipient the recipient collection to drain the elements to
 * @param amount    the amount of elements to drain
 *
 * @return          the amount of elements that were actually drained
 */
static gint
gee_queue_real_drain (GeeQueue* self,
                      GeeCollection* recipient,
                      gint amount)
{
	gpointer item = NULL;
	gint drained = 0;
	gint result;
	g_return_val_if_fail (recipient != NULL, 0);
	item = NULL;
	drained = 0;
	while (TRUE) {
		gboolean _tmp0_ = FALSE;
		gboolean _tmp1_ = FALSE;
		gconstpointer _tmp5_;
		gint _tmp6_;
		if (amount == -1) {
			_tmp1_ = TRUE;
		} else {
			gint _tmp2_;
			amount = amount - 1;
			_tmp2_ = amount;
			_tmp1_ = _tmp2_ >= 0;
		}
		if (_tmp1_) {
			gpointer _tmp3_;
			gconstpointer _tmp4_;
			_tmp3_ = gee_queue_poll (self);
			((item == NULL) || (GEE_QUEUE_GET_INTERFACE (self)->get_g_destroy_func (self) == NULL)) ? NULL : (item = (GEE_QUEUE_GET_INTERFACE (self)->get_g_destroy_func (self) (item), NULL));
			item = _tmp3_;
			_tmp4_ = item;
			_tmp0_ = _tmp4_ != NULL;
		} else {
			_tmp0_ = FALSE;
		}
		if (!_tmp0_) {
			break;
		}
		_tmp5_ = item;
		gee_collection_add (recipient, _tmp5_);
		_tmp6_ = drained;
		drained = _tmp6_ + 1;
	}
	result = drained;
	((item == NULL) || (GEE_QUEUE_GET_INTERFACE (self)->get_g_destroy_func (self) == NULL)) ? NULL : (item = (GEE_QUEUE_GET_INTERFACE (self)->get_g_destroy_func (self) (item), NULL));
	return result;
}

gint
gee_queue_drain (GeeQueue* self,
                 GeeCollection* recipient,
                 gint amount)
{
	GeeQueueIface* _iface_;
	g_return_val_if_fail (self != NULL, 0);
	_iface_ = GEE_QUEUE_GET_INTERFACE (self);
	if (_iface_->drain) {
		return _iface_->drain (self, recipient, amount);
	}
	return -1;
}

gint
gee_queue_get_capacity (GeeQueue* self)
{
	GeeQueueIface* _iface_;
	g_return_val_if_fail (self != NULL, 0);
	_iface_ = GEE_QUEUE_GET_INTERFACE (self);
	if (_iface_->get_capacity) {
		return _iface_->get_capacity (self);
	}
	return -1;
}

gint
gee_queue_get_remaining_capacity (GeeQueue* self)
{
	GeeQueueIface* _iface_;
	g_return_val_if_fail (self != NULL, 0);
	_iface_ = GEE_QUEUE_GET_INTERFACE (self);
	if (_iface_->get_remaining_capacity) {
		return _iface_->get_remaining_capacity (self);
	}
	return -1;
}

gboolean
gee_queue_get_is_full (GeeQueue* self)
{
	GeeQueueIface* _iface_;
	g_return_val_if_fail (self != NULL, FALSE);
	_iface_ = GEE_QUEUE_GET_INTERFACE (self);
	if (_iface_->get_is_full) {
		return _iface_->get_is_full (self);
	}
	return FALSE;
}

static void
gee_queue_default_init (GeeQueueIface * iface,
                        gpointer iface_data)
{
	/**
	 * The capacity of this queue (or ``UNBOUNDED_CAPACITY`` if capacity is not bound).
	 */
	g_object_interface_install_property (iface, g_param_spec_int ("capacity", "capacity", "capacity", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	/**
	 * The remaining capacity of this queue (or ``UNBOUNDED_CAPACITY`` if capacity is not
	 * bound).
	 */
	g_object_interface_install_property (iface, g_param_spec_int ("remaining-capacity", "remaining-capacity", "remaining-capacity", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	/**
	 * Specifies whether this queue is full.
	 */
	g_object_interface_install_property (iface, g_param_spec_boolean ("is-full", "is-full", "is-full", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	iface->offer = gee_queue_real_offer;
	iface->drain = gee_queue_real_drain;
}

/**
 * A collection designed for holding elements prior to processing.
 *
 * Although all Queue implementations do not limit the amount of elements they
 * can contain, this interface supports for capacity-bounded queues. When
 * capacity is not bound, then the {@link capacity} and
 * {@link remaining_capacity} both return {@link UNBOUNDED_CAPACITY}.
 *
 * This interface defines methods that will never fail whatever the state of
 * the queue is. For capacity-bounded queues, those methods will either return
 * ``false`` or ``null`` to specify that the insert or retrieval did not occur
 * because the queue was full or empty.
 *
 * Queue implementations are not limited to First-In-First-Out behavior and can
 * offer different orderings of their elements. Each Queue implementation must
 * specify how it orders its elements.
 *
 * Queue implementations do not allow insertion of ``null`` elements, although
 * some implementations, such as {@link LinkedList}, do not prohibit insertion
 * of ``null``. Even in the implementations that permit it, ``null`` should not be
 * inserted into a Queue, as ``null`` is also used as a special return value by
 * the poll method to indicate that the queue contains no elements.
 */
static GType
gee_queue_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GeeQueueIface), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gee_queue_default_init, (GClassFinalizeFunc) NULL, NULL, 0, 0, (GInstanceInitFunc) NULL, NULL };
	GType gee_queue_type_id;
	gee_queue_type_id = g_type_register_static (G_TYPE_INTERFACE, "GeeQueue", &g_define_type_info, 0);
	g_type_interface_add_prerequisite (gee_queue_type_id, GEE_TYPE_COLLECTION);
	return gee_queue_type_id;
}

GType
gee_queue_get_type (void)
{
	static volatile gsize gee_queue_type_id__once = 0;
	if (g_once_init_enter (&gee_queue_type_id__once)) {
		GType gee_queue_type_id;
		gee_queue_type_id = gee_queue_get_type_once ();
		g_once_init_leave (&gee_queue_type_id__once, gee_queue_type_id);
	}
	return gee_queue_type_id__once;
}