Update Changelog
[profile/ivi/libgee.git] / gee / queue.c
1 /* queue.c generated by valac 0.18.0, the Vala compiler
2  * generated from queue.vala, do not modify */
3
4 /* queue.vala
5  *
6  * Copyright (C) 2009  Didier Villevalois
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
21  *
22  * Author:
23  *      Didier 'Ptitjes Villevalois <ptitjes@free.fr>
24  */
25
26 #include <glib.h>
27 #include <glib-object.h>
28
29
30 #define GEE_TYPE_TRAVERSABLE (gee_traversable_get_type ())
31 #define GEE_TRAVERSABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_TRAVERSABLE, GeeTraversable))
32 #define GEE_IS_TRAVERSABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_TRAVERSABLE))
33 #define GEE_TRAVERSABLE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEE_TYPE_TRAVERSABLE, GeeTraversableIface))
34
35 typedef struct _GeeTraversable GeeTraversable;
36 typedef struct _GeeTraversableIface GeeTraversableIface;
37
38 #define GEE_TRAVERSABLE_TYPE_STREAM (gee_traversable_stream_get_type ())
39
40 #define GEE_TYPE_LAZY (gee_lazy_get_type ())
41 #define GEE_LAZY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_LAZY, GeeLazy))
42 #define GEE_LAZY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEE_TYPE_LAZY, GeeLazyClass))
43 #define GEE_IS_LAZY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_LAZY))
44 #define GEE_IS_LAZY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEE_TYPE_LAZY))
45 #define GEE_LAZY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEE_TYPE_LAZY, GeeLazyClass))
46
47 typedef struct _GeeLazy GeeLazy;
48 typedef struct _GeeLazyClass GeeLazyClass;
49
50 #define GEE_TYPE_ITERATOR (gee_iterator_get_type ())
51 #define GEE_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_ITERATOR, GeeIterator))
52 #define GEE_IS_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_ITERATOR))
53 #define GEE_ITERATOR_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEE_TYPE_ITERATOR, GeeIteratorIface))
54
55 typedef struct _GeeIterator GeeIterator;
56 typedef struct _GeeIteratorIface GeeIteratorIface;
57
58 #define GEE_TYPE_ITERABLE (gee_iterable_get_type ())
59 #define GEE_ITERABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_ITERABLE, GeeIterable))
60 #define GEE_IS_ITERABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_ITERABLE))
61 #define GEE_ITERABLE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEE_TYPE_ITERABLE, GeeIterableIface))
62
63 typedef struct _GeeIterable GeeIterable;
64 typedef struct _GeeIterableIface GeeIterableIface;
65
66 #define GEE_TYPE_COLLECTION (gee_collection_get_type ())
67 #define GEE_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_COLLECTION, GeeCollection))
68 #define GEE_IS_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_COLLECTION))
69 #define GEE_COLLECTION_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEE_TYPE_COLLECTION, GeeCollectionIface))
70
71 typedef struct _GeeCollection GeeCollection;
72 typedef struct _GeeCollectionIface GeeCollectionIface;
73
74 #define GEE_TYPE_QUEUE (gee_queue_get_type ())
75 #define GEE_QUEUE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_QUEUE, GeeQueue))
76 #define GEE_IS_QUEUE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_QUEUE))
77 #define GEE_QUEUE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEE_TYPE_QUEUE, GeeQueueIface))
78
79 typedef struct _GeeQueue GeeQueue;
80 typedef struct _GeeQueueIface GeeQueueIface;
81
82 typedef gboolean (*GeeForallFunc) (gpointer g, void* user_data);
83 typedef enum  {
84         GEE_TRAVERSABLE_STREAM_YIELD,
85         GEE_TRAVERSABLE_STREAM_CONTINUE,
86         GEE_TRAVERSABLE_STREAM_END
87 } GeeTraversableStream;
88
89 typedef GeeTraversableStream (*GeeStreamFunc) (GeeTraversableStream state, GeeLazy* g, GeeLazy** lazy, void* user_data);
90 struct _GeeIteratorIface {
91         GTypeInterface parent_iface;
92         gboolean (*next) (GeeIterator* self);
93         gboolean (*has_next) (GeeIterator* self);
94         gpointer (*get) (GeeIterator* self);
95         void (*remove) (GeeIterator* self);
96         gboolean (*get_valid) (GeeIterator* self);
97         gboolean (*get_read_only) (GeeIterator* self);
98 };
99
100 typedef gpointer (*GeeFoldFunc) (gpointer g, gpointer a, void* user_data);
101 typedef gpointer (*GeeMapFunc) (gpointer g, void* user_data);
102 typedef gboolean (*GeePredicate) (gconstpointer g, void* user_data);
103 struct _GeeTraversableIface {
104         GTypeInterface parent_iface;
105         GType (*get_g_type) (GeeTraversable* self);
106         GBoxedCopyFunc (*get_g_dup_func) (GeeTraversable* self);
107         GDestroyNotify (*get_g_destroy_func) (GeeTraversable* self);
108         gboolean (*foreach) (GeeTraversable* self, GeeForallFunc f, void* f_target);
109         GeeIterator* (*stream) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeStreamFunc f, void* f_target, GDestroyNotify f_target_destroy_notify);
110         gpointer (*fold) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeFoldFunc f, void* f_target, gpointer seed);
111         GeeIterator* (*map) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeMapFunc f, void* f_target);
112         GeeIterator* (*scan) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeFoldFunc f, void* f_target, gpointer seed);
113         GeeIterator* (*filter) (GeeTraversable* self, GeePredicate pred, void* pred_target, GDestroyNotify pred_target_destroy_notify);
114         GeeIterator* (*chop) (GeeTraversable* self, gint offset, gint length);
115         GType (*get_element_type) (GeeTraversable* self);
116 };
117
118 struct _GeeIterableIface {
119         GTypeInterface parent_iface;
120         GType (*get_g_type) (GeeIterable* self);
121         GBoxedCopyFunc (*get_g_dup_func) (GeeIterable* self);
122         GDestroyNotify (*get_g_destroy_func) (GeeIterable* self);
123         GeeIterator* (*iterator) (GeeIterable* self);
124 };
125
126 struct _GeeCollectionIface {
127         GTypeInterface parent_iface;
128         GType (*get_g_type) (GeeCollection* self);
129         GBoxedCopyFunc (*get_g_dup_func) (GeeCollection* self);
130         GDestroyNotify (*get_g_destroy_func) (GeeCollection* self);
131         gboolean (*contains) (GeeCollection* self, gconstpointer item);
132         gboolean (*add) (GeeCollection* self, gconstpointer item);
133         gboolean (*remove) (GeeCollection* self, gconstpointer item);
134         void (*clear) (GeeCollection* self);
135         gboolean (*add_all) (GeeCollection* self, GeeCollection* collection);
136         gboolean (*contains_all) (GeeCollection* self, GeeCollection* collection);
137         gboolean (*remove_all) (GeeCollection* self, GeeCollection* collection);
138         gboolean (*retain_all) (GeeCollection* self, GeeCollection* collection);
139         gpointer* (*to_array) (GeeCollection* self, int* result_length1);
140         gint (*get_size) (GeeCollection* self);
141         gboolean (*get_is_empty) (GeeCollection* self);
142         gboolean (*get_read_only) (GeeCollection* self);
143         GeeCollection* (*get_read_only_view) (GeeCollection* self);
144 };
145
146 struct _GeeQueueIface {
147         GTypeInterface parent_iface;
148         GType (*get_g_type) (GeeQueue* self);
149         GBoxedCopyFunc (*get_g_dup_func) (GeeQueue* self);
150         GDestroyNotify (*get_g_destroy_func) (GeeQueue* self);
151         gboolean (*offer) (GeeQueue* self, gconstpointer element);
152         gpointer (*peek) (GeeQueue* self);
153         gpointer (*poll) (GeeQueue* self);
154         gint (*drain) (GeeQueue* self, GeeCollection* recipient, gint amount);
155         gint (*get_capacity) (GeeQueue* self);
156         gint (*get_remaining_capacity) (GeeQueue* self);
157         gboolean (*get_is_full) (GeeQueue* self);
158 };
159
160
161
162 GType gee_traversable_stream_get_type (void) G_GNUC_CONST;
163 gpointer gee_lazy_ref (gpointer instance);
164 void gee_lazy_unref (gpointer instance);
165 GParamSpec* gee_param_spec_lazy (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
166 void gee_value_set_lazy (GValue* value, gpointer v_object);
167 void gee_value_take_lazy (GValue* value, gpointer v_object);
168 gpointer gee_value_get_lazy (const GValue* value);
169 GType gee_lazy_get_type (void) G_GNUC_CONST;
170 GType gee_iterator_get_type (void) G_GNUC_CONST;
171 GType gee_traversable_get_type (void) G_GNUC_CONST;
172 GType gee_iterable_get_type (void) G_GNUC_CONST;
173 GType gee_collection_get_type (void) G_GNUC_CONST;
174 GType gee_queue_get_type (void) G_GNUC_CONST;
175 gboolean gee_queue_offer (GeeQueue* self, gconstpointer element);
176 static gboolean gee_queue_real_offer (GeeQueue* self, gconstpointer element);
177 gboolean gee_collection_add (GeeCollection* self, gconstpointer item);
178 gpointer gee_queue_peek (GeeQueue* self);
179 gpointer gee_queue_poll (GeeQueue* self);
180 gint gee_queue_drain (GeeQueue* self, GeeCollection* recipient, gint amount);
181 static gint gee_queue_real_drain (GeeQueue* self, GeeCollection* recipient, gint amount);
182 #define GEE_QUEUE_UNBOUNDED_CAPACITY (-1)
183 gint gee_queue_get_capacity (GeeQueue* self);
184 gint gee_queue_get_remaining_capacity (GeeQueue* self);
185 gboolean gee_queue_get_is_full (GeeQueue* self);
186
187
188 /**
189  * Offers the specified element to this queue.
190  *
191  * @param element the element to offer to the queue
192  *
193  * @return        ``true`` if the element was added to the queue
194  */
195 static gboolean gee_queue_real_offer (GeeQueue* self, gconstpointer element) {
196         gboolean result = FALSE;
197         gconstpointer _tmp0_;
198         gboolean _tmp1_ = FALSE;
199         _tmp0_ = element;
200         _tmp1_ = gee_collection_add ((GeeCollection*) self, _tmp0_);
201         result = _tmp1_;
202         return result;
203 }
204
205
206 gboolean gee_queue_offer (GeeQueue* self, gconstpointer element) {
207         g_return_val_if_fail (self != NULL, FALSE);
208         return GEE_QUEUE_GET_INTERFACE (self)->offer (self, element);
209 }
210
211
212 /**
213  * Peeks (retrieves, but not remove) an element from this queue.
214  *
215  * @return the element peeked from the queue (or ``null`` if none was
216  *         available)
217  */
218 gpointer gee_queue_peek (GeeQueue* self) {
219         g_return_val_if_fail (self != NULL, NULL);
220         return GEE_QUEUE_GET_INTERFACE (self)->peek (self);
221 }
222
223
224 /**
225  * Polls (retrieves and remove) an element from this queue.
226  *
227  * @return the element polled from the queue (or ``null`` if none was
228  *         available)
229  */
230 gpointer gee_queue_poll (GeeQueue* self) {
231         g_return_val_if_fail (self != NULL, NULL);
232         return GEE_QUEUE_GET_INTERFACE (self)->poll (self);
233 }
234
235
236 /**
237  * Drains the specified amount of elements from this queue in the specified
238  * recipient collection.
239  *
240  * @param recipient the recipient collection to drain the elements to
241  * @param amount    the amount of elements to drain
242  *
243  * @return          the amount of elements that were actually drained
244  */
245 static gint gee_queue_real_drain (GeeQueue* self, GeeCollection* recipient, gint amount) {
246         gint result = 0;
247         gpointer item;
248         gint drained;
249         g_return_val_if_fail (recipient != NULL, 0);
250         item = NULL;
251         drained = 0;
252         while (TRUE) {
253                 gboolean _tmp0_ = FALSE;
254                 gboolean _tmp1_ = FALSE;
255                 gint _tmp2_;
256                 gboolean _tmp5_;
257                 gboolean _tmp8_;
258                 GeeCollection* _tmp9_;
259                 gconstpointer _tmp10_;
260                 gint _tmp11_;
261                 _tmp2_ = amount;
262                 if (_tmp2_ == (-1)) {
263                         _tmp1_ = TRUE;
264                 } else {
265                         gint _tmp3_;
266                         gint _tmp4_;
267                         _tmp3_ = amount;
268                         amount = _tmp3_ - 1;
269                         _tmp4_ = amount;
270                         _tmp1_ = _tmp4_ >= 0;
271                 }
272                 _tmp5_ = _tmp1_;
273                 if (_tmp5_) {
274                         gpointer _tmp6_ = NULL;
275                         gconstpointer _tmp7_;
276                         _tmp6_ = gee_queue_poll (self);
277                         ((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));
278                         item = _tmp6_;
279                         _tmp7_ = item;
280                         _tmp0_ = _tmp7_ != NULL;
281                 } else {
282                         _tmp0_ = FALSE;
283                 }
284                 _tmp8_ = _tmp0_;
285                 if (!_tmp8_) {
286                         break;
287                 }
288                 _tmp9_ = recipient;
289                 _tmp10_ = item;
290                 gee_collection_add (_tmp9_, _tmp10_);
291                 _tmp11_ = drained;
292                 drained = _tmp11_ + 1;
293         }
294         result = drained;
295         ((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));
296         return result;
297 }
298
299
300 gint gee_queue_drain (GeeQueue* self, GeeCollection* recipient, gint amount) {
301         g_return_val_if_fail (self != NULL, 0);
302         return GEE_QUEUE_GET_INTERFACE (self)->drain (self, recipient, amount);
303 }
304
305
306 gint gee_queue_get_capacity (GeeQueue* self) {
307         g_return_val_if_fail (self != NULL, 0);
308         return GEE_QUEUE_GET_INTERFACE (self)->get_capacity (self);
309 }
310
311
312 gint gee_queue_get_remaining_capacity (GeeQueue* self) {
313         g_return_val_if_fail (self != NULL, 0);
314         return GEE_QUEUE_GET_INTERFACE (self)->get_remaining_capacity (self);
315 }
316
317
318 gboolean gee_queue_get_is_full (GeeQueue* self) {
319         g_return_val_if_fail (self != NULL, FALSE);
320         return GEE_QUEUE_GET_INTERFACE (self)->get_is_full (self);
321 }
322
323
324 static void gee_queue_base_init (GeeQueueIface * iface) {
325         static gboolean initialized = FALSE;
326         if (!initialized) {
327                 initialized = TRUE;
328                 /**
329                  * The capacity of this queue (or ``null`` if capacity is not bound).
330                  */
331                 g_object_interface_install_property (iface, g_param_spec_int ("capacity", "capacity", "capacity", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
332                 /**
333                  * The remaining capacity of this queue (or ``null`` if capacity is not
334                  * bound).
335                  */
336                 g_object_interface_install_property (iface, g_param_spec_int ("remaining-capacity", "remaining-capacity", "remaining-capacity", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
337                 /**
338                  * Specifies whether this queue is full.
339                  */
340                 g_object_interface_install_property (iface, g_param_spec_boolean ("is-full", "is-full", "is-full", FALSE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
341                 iface->offer = gee_queue_real_offer;
342                 iface->drain = gee_queue_real_drain;
343         }
344 }
345
346
347 /**
348  * A collection designed for holding elements prior to processing.
349  *
350  * Although all Queue implementations do not limit the amount of elements they
351  * can contain, this interface supports for capacity-bounded queues. When
352  * capacity is not bound, then the {@link capacity} and
353  * {@link remaining_capacity} both return {@link UNBOUNDED_CAPACITY}.
354  *
355  * This interface defines methods that will never fail whatever the state of
356  * the queue is. For capacity-bounded queues, those methods will either return
357  * ``false`` or ``null`` to specify that the insert or retrieval did not occur
358  * because the queue was full or empty.
359  *
360  * Queue implementations are not limited to First-In-First-Out behavior and can
361  * propose different ordering of their elements. Each Queue implementation have
362  * to specify how it orders its elements.
363  *
364  * Queue implementations do not allow insertion of ``null`` elements, although
365  * some implementations, such as {@link LinkedList}, do not prohibit insertion
366  * of ``null``. Even in the implementations that permit it, ``null`` should not be
367  * inserted into a Queue, as ``null`` is also used as a special return value by
368  * the poll method to indicate that the queue contains no elements.
369  */
370 GType gee_queue_get_type (void) {
371         static volatile gsize gee_queue_type_id__volatile = 0;
372         if (g_once_init_enter (&gee_queue_type_id__volatile)) {
373                 static const GTypeInfo g_define_type_info = { sizeof (GeeQueueIface), (GBaseInitFunc) gee_queue_base_init, (GBaseFinalizeFunc) NULL, (GClassInitFunc) NULL, (GClassFinalizeFunc) NULL, NULL, 0, 0, (GInstanceInitFunc) NULL, NULL };
374                 GType gee_queue_type_id;
375                 gee_queue_type_id = g_type_register_static (G_TYPE_INTERFACE, "GeeQueue", &g_define_type_info, 0);
376                 g_type_interface_add_prerequisite (gee_queue_type_id, GEE_TYPE_COLLECTION);
377                 g_once_init_leave (&gee_queue_type_id__volatile, gee_queue_type_id);
378         }
379         return gee_queue_type_id__volatile;
380 }
381
382
383