1 /* iterator.c generated by valac 0.18.0, the Vala compiler
2 * generated from iterator.vala, do not modify */
6 * Copyright (C) 2007-2008 Jürg Billeter
7 * Copyright (C) 2009 Didier Villevalois, Maciej Piechotka
8 * Copyright (C) 2010-2011 Maciej Piechotka
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Jürg Billeter <j@bitron.ch>
26 * Maciej Piechotka <uzytkownik2@gmail.com>
27 * Didier 'Ptitjes Villevalois <ptitjes@free.fr>
31 #include <glib-object.h>
34 #define GEE_TYPE_TRAVERSABLE (gee_traversable_get_type ())
35 #define GEE_TRAVERSABLE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_TRAVERSABLE, GeeTraversable))
36 #define GEE_IS_TRAVERSABLE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_TRAVERSABLE))
37 #define GEE_TRAVERSABLE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEE_TYPE_TRAVERSABLE, GeeTraversableIface))
39 typedef struct _GeeTraversable GeeTraversable;
40 typedef struct _GeeTraversableIface GeeTraversableIface;
42 #define GEE_TRAVERSABLE_TYPE_STREAM (gee_traversable_stream_get_type ())
44 #define GEE_TYPE_LAZY (gee_lazy_get_type ())
45 #define GEE_LAZY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_LAZY, GeeLazy))
46 #define GEE_LAZY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEE_TYPE_LAZY, GeeLazyClass))
47 #define GEE_IS_LAZY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_LAZY))
48 #define GEE_IS_LAZY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEE_TYPE_LAZY))
49 #define GEE_LAZY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEE_TYPE_LAZY, GeeLazyClass))
51 typedef struct _GeeLazy GeeLazy;
52 typedef struct _GeeLazyClass GeeLazyClass;
54 #define GEE_TYPE_ITERATOR (gee_iterator_get_type ())
55 #define GEE_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_ITERATOR, GeeIterator))
56 #define GEE_IS_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_ITERATOR))
57 #define GEE_ITERATOR_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GEE_TYPE_ITERATOR, GeeIteratorIface))
59 typedef struct _GeeIterator GeeIterator;
60 typedef struct _GeeIteratorIface GeeIteratorIface;
62 #define GEE_TYPE_UNFOLD_ITERATOR (gee_unfold_iterator_get_type ())
63 #define GEE_UNFOLD_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEE_TYPE_UNFOLD_ITERATOR, GeeUnfoldIterator))
64 #define GEE_UNFOLD_ITERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEE_TYPE_UNFOLD_ITERATOR, GeeUnfoldIteratorClass))
65 #define GEE_IS_UNFOLD_ITERATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEE_TYPE_UNFOLD_ITERATOR))
66 #define GEE_IS_UNFOLD_ITERATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEE_TYPE_UNFOLD_ITERATOR))
67 #define GEE_UNFOLD_ITERATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEE_TYPE_UNFOLD_ITERATOR, GeeUnfoldIteratorClass))
69 typedef struct _GeeUnfoldIterator GeeUnfoldIterator;
70 typedef struct _GeeUnfoldIteratorClass GeeUnfoldIteratorClass;
71 #define _gee_lazy_unref0(var) ((var == NULL) ? NULL : (var = (gee_lazy_unref (var), NULL)))
72 #define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
73 typedef struct _Block6Data Block6Data;
74 #define _g_destroy_func0(var) (((var == NULL) || (g_destroy_func == NULL)) ? NULL : (var = (g_destroy_func (var), NULL)))
76 typedef gboolean (*GeeForallFunc) (gpointer g, void* user_data);
78 GEE_TRAVERSABLE_STREAM_YIELD,
79 GEE_TRAVERSABLE_STREAM_CONTINUE,
80 GEE_TRAVERSABLE_STREAM_END
81 } GeeTraversableStream;
83 typedef GeeTraversableStream (*GeeStreamFunc) (GeeTraversableStream state, GeeLazy* g, GeeLazy** lazy, void* user_data);
84 typedef gpointer (*GeeFoldFunc) (gpointer g, gpointer a, void* user_data);
85 typedef gpointer (*GeeMapFunc) (gpointer g, void* user_data);
86 typedef gboolean (*GeePredicate) (gconstpointer g, void* user_data);
87 struct _GeeTraversableIface {
88 GTypeInterface parent_iface;
89 GType (*get_g_type) (GeeTraversable* self);
90 GBoxedCopyFunc (*get_g_dup_func) (GeeTraversable* self);
91 GDestroyNotify (*get_g_destroy_func) (GeeTraversable* self);
92 gboolean (*foreach) (GeeTraversable* self, GeeForallFunc f, void* f_target);
93 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);
94 gpointer (*fold) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeFoldFunc f, void* f_target, gpointer seed);
95 GeeIterator* (*map) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeMapFunc f, void* f_target);
96 GeeIterator* (*scan) (GeeTraversable* self, GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeFoldFunc f, void* f_target, gpointer seed);
97 GeeIterator* (*filter) (GeeTraversable* self, GeePredicate pred, void* pred_target, GDestroyNotify pred_target_destroy_notify);
98 GeeIterator* (*chop) (GeeTraversable* self, gint offset, gint length);
99 GType (*get_element_type) (GeeTraversable* self);
102 struct _GeeIteratorIface {
103 GTypeInterface parent_iface;
104 gboolean (*next) (GeeIterator* self);
105 gboolean (*has_next) (GeeIterator* self);
106 gpointer (*get) (GeeIterator* self);
107 void (*remove) (GeeIterator* self);
108 gboolean (*get_valid) (GeeIterator* self);
109 gboolean (*get_read_only) (GeeIterator* self);
112 typedef GeeLazy* (*GeeUnfoldFunc) (void* user_data);
116 GBoxedCopyFunc g_dup_func;
117 GDestroyNotify g_destroy_func;
118 GeeIterator* current;
124 GType gee_traversable_stream_get_type (void) G_GNUC_CONST;
125 gpointer gee_lazy_ref (gpointer instance);
126 void gee_lazy_unref (gpointer instance);
127 GParamSpec* gee_param_spec_lazy (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
128 void gee_value_set_lazy (GValue* value, gpointer v_object);
129 void gee_value_take_lazy (GValue* value, gpointer v_object);
130 gpointer gee_value_get_lazy (const GValue* value);
131 GType gee_lazy_get_type (void) G_GNUC_CONST;
132 GType gee_traversable_get_type (void) G_GNUC_CONST;
133 GType gee_iterator_get_type (void) G_GNUC_CONST;
134 gboolean gee_iterator_next (GeeIterator* self);
135 gboolean gee_iterator_has_next (GeeIterator* self);
136 gpointer gee_iterator_get (GeeIterator* self);
137 void gee_iterator_remove (GeeIterator* self);
138 GeeIterator* gee_iterator_unfold (GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeUnfoldFunc f, void* f_target, GDestroyNotify f_target_destroy_notify, GeeLazy* current);
139 GeeUnfoldIterator* gee_unfold_iterator_new (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeUnfoldFunc func, void* func_target, GDestroyNotify func_target_destroy_notify, GeeLazy* current);
140 GeeUnfoldIterator* gee_unfold_iterator_construct (GType object_type, GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeUnfoldFunc func, void* func_target, GDestroyNotify func_target_destroy_notify, GeeLazy* current);
141 GType gee_unfold_iterator_get_type (void) G_GNUC_CONST;
142 GeeIterator* gee_iterator_concat (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeIterator* iters);
143 static Block6Data* block6_data_ref (Block6Data* _data6_);
144 static void block6_data_unref (void * _userdata_);
145 gboolean gee_iterator_get_valid (GeeIterator* self);
146 static GeeLazy* __lambda2_ (Block6Data* _data6_);
147 GeeLazy* gee_lazy_new_from_value (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, gconstpointer item);
148 GeeLazy* gee_lazy_construct_from_value (GType object_type, GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, gconstpointer item);
149 static GeeLazy* ___lambda2__gee_unfold_func (gpointer self);
150 gboolean gee_iterator_get_read_only (GeeIterator* self);
154 * Advances to the next element in the iteration.
156 * @return ``true`` if the iterator has a next element
158 gboolean gee_iterator_next (GeeIterator* self) {
159 g_return_val_if_fail (self != NULL, FALSE);
160 return GEE_ITERATOR_GET_INTERFACE (self)->next (self);
165 * Checks whether there is a next element in the iteration.
167 * @return ``true`` if the iterator has a next element
169 gboolean gee_iterator_has_next (GeeIterator* self) {
170 g_return_val_if_fail (self != NULL, FALSE);
171 return GEE_ITERATOR_GET_INTERFACE (self)->has_next (self);
176 * Returns the current element in the iteration.
178 * @return the current element in the iteration
180 gpointer gee_iterator_get (GeeIterator* self) {
181 g_return_val_if_fail (self != NULL, NULL);
182 return GEE_ITERATOR_GET_INTERFACE (self)->get (self);
187 * Removes the current element in the iteration. The cursor is set in an
188 * in-between state. Both {@link get} and {@link remove} will fail until
189 * the next move of the cursor (calling {@link next}).
191 void gee_iterator_remove (GeeIterator* self) {
192 g_return_if_fail (self != NULL);
193 GEE_ITERATOR_GET_INTERFACE (self)->remove (self);
198 * Create iterator from unfolding function. The lazy value is
199 * force-evaluated before progressing to next element.
201 * @param f Unfolding function
202 * @param current If iterator is to be valid it contains the current value of it
204 GeeIterator* gee_iterator_unfold (GType a_type, GBoxedCopyFunc a_dup_func, GDestroyNotify a_destroy_func, GeeUnfoldFunc f, void* f_target, GDestroyNotify f_target_destroy_notify, GeeLazy* current) {
205 GeeIterator* result = NULL;
206 GeeUnfoldFunc _tmp0_;
208 GDestroyNotify _tmp0__target_destroy_notify;
210 GeeUnfoldIterator* _tmp2_;
212 _tmp0__target = f_target;
213 _tmp0__target_destroy_notify = f_target_destroy_notify;
214 f_target_destroy_notify = NULL;
217 _tmp2_ = gee_unfold_iterator_new (a_type, (GBoxedCopyFunc) a_dup_func, a_destroy_func, _tmp0_, _tmp0__target, _tmp0__target_destroy_notify, _tmp1_);
218 result = (GeeIterator*) _tmp2_;
219 (f_target_destroy_notify == NULL) ? NULL : (f_target_destroy_notify (f_target), NULL);
222 f_target_destroy_notify = NULL;
223 _gee_lazy_unref0 (current);
229 * Concatinate iterators.
231 * @param iters Iterators of iterators
232 * @return Iterator containg values of each iterator
234 static gpointer _g_object_ref0 (gpointer self) {
235 return self ? g_object_ref (self) : NULL;
239 static Block6Data* block6_data_ref (Block6Data* _data6_) {
240 g_atomic_int_inc (&_data6_->_ref_count_);
245 static void block6_data_unref (void * _userdata_) {
247 _data6_ = (Block6Data*) _userdata_;
248 if (g_atomic_int_dec_and_test (&_data6_->_ref_count_)) {
250 GBoxedCopyFunc g_dup_func;
251 GDestroyNotify g_destroy_func;
252 g_type = _data6_->g_type;
253 g_dup_func = _data6_->g_dup_func;
254 g_destroy_func = _data6_->g_destroy_func;
255 _g_object_unref0 (_data6_->current);
256 _g_object_unref0 (_data6_->iters);
257 g_slice_free (Block6Data, _data6_);
262 static GeeLazy* __lambda2_ (Block6Data* _data6_) {
264 GBoxedCopyFunc g_dup_func;
265 GDestroyNotify g_destroy_func;
266 GeeLazy* result = NULL;
267 g_type = _data6_->g_type;
268 g_dup_func = _data6_->g_dup_func;
269 g_destroy_func = _data6_->g_destroy_func;
272 _tmp0_ = _data6_->current;
273 if (_tmp0_ == NULL) {
275 gboolean _tmp2_ = FALSE;
276 _tmp1_ = _data6_->iters;
277 _tmp2_ = gee_iterator_next (_tmp1_);
280 gpointer _tmp4_ = NULL;
281 _tmp3_ = _data6_->iters;
282 _tmp4_ = gee_iterator_get (_tmp3_);
283 _g_object_unref0 (_data6_->current);
284 _data6_->current = (GeeIterator*) _tmp4_;
291 gboolean _tmp6_ = FALSE;
292 _tmp5_ = _data6_->current;
293 _tmp6_ = gee_iterator_next (_tmp5_);
296 gpointer _tmp8_ = NULL;
300 _tmp7_ = _data6_->current;
301 _tmp8_ = gee_iterator_get (_tmp7_);
303 _tmp10_ = gee_lazy_new_from_value (g_type, (GBoxedCopyFunc) g_dup_func, g_destroy_func, _tmp9_);
305 _g_destroy_func0 (_tmp9_);
309 _g_object_unref0 (_data6_->current);
310 _data6_->current = NULL;
317 static GeeLazy* ___lambda2__gee_unfold_func (gpointer self) {
319 result = __lambda2_ (self);
324 GeeIterator* gee_iterator_concat (GType g_type, GBoxedCopyFunc g_dup_func, GDestroyNotify g_destroy_func, GeeIterator* iters) {
325 GeeIterator* result = NULL;
332 GeeIterator* _tmp7_ = NULL;
333 g_return_val_if_fail (iters != NULL, NULL);
334 _data6_ = g_slice_new0 (Block6Data);
335 _data6_->_ref_count_ = 1;
336 _data6_->g_type = g_type;
337 _data6_->g_dup_func = g_dup_func;
338 _data6_->g_destroy_func = g_destroy_func;
340 _tmp1_ = _g_object_ref0 (_tmp0_);
341 _g_object_unref0 (_data6_->iters);
342 _data6_->iters = _tmp1_;
343 _data6_->current = NULL;
344 _tmp2_ = _data6_->iters;
345 _tmp3_ = gee_iterator_get_valid (_tmp2_);
349 gpointer _tmp6_ = NULL;
350 _tmp5_ = _data6_->iters;
351 _tmp6_ = gee_iterator_get (_tmp5_);
352 _g_object_unref0 (_data6_->current);
353 _data6_->current = (GeeIterator*) _tmp6_;
355 _tmp7_ = gee_iterator_unfold (g_type, (GBoxedCopyFunc) g_dup_func, g_destroy_func, ___lambda2__gee_unfold_func, block6_data_ref (_data6_), block6_data_unref, NULL);
357 block6_data_unref (_data6_);
363 gboolean gee_iterator_get_valid (GeeIterator* self) {
364 g_return_val_if_fail (self != NULL, FALSE);
365 return GEE_ITERATOR_GET_INTERFACE (self)->get_valid (self);
369 gboolean gee_iterator_get_read_only (GeeIterator* self) {
370 g_return_val_if_fail (self != NULL, FALSE);
371 return GEE_ITERATOR_GET_INTERFACE (self)->get_read_only (self);
375 static void gee_iterator_base_init (GeeIteratorIface * iface) {
376 static gboolean initialized = FALSE;
380 * Determines wheather the call to {@link get} is legal. It is false at the
381 * beginning and after {@link remove} call and true otherwise.
383 g_object_interface_install_property (iface, g_param_spec_boolean ("valid", "valid", "valid", FALSE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
385 * Determines wheather the call to {@link remove} is legal assuming the
386 * iterator is valid. The value must not change in runtime hence the user
387 * of iterator may cache it.
389 g_object_interface_install_property (iface, g_param_spec_boolean ("read-only", "read-only", "read-only", FALSE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
395 * An iterator over a collection.
397 * Gee's iterators are "on-track" iterators. They always point to an item
398 * except before the first call to {@link next}, or, when an
399 * item has been removed, until the next call to {@link next}.
401 * Please note that when the iterator is out of track, neither {@link get} nor
402 * {@link remove} are defined and both will fail. After the next call to
403 * {@link next}, they will be defined again.
405 GType gee_iterator_get_type (void) {
406 static volatile gsize gee_iterator_type_id__volatile = 0;
407 if (g_once_init_enter (&gee_iterator_type_id__volatile)) {
408 static const GTypeInfo g_define_type_info = { sizeof (GeeIteratorIface), (GBaseInitFunc) gee_iterator_base_init, (GBaseFinalizeFunc) NULL, (GClassInitFunc) NULL, (GClassFinalizeFunc) NULL, NULL, 0, 0, (GInstanceInitFunc) NULL, NULL };
409 GType gee_iterator_type_id;
410 gee_iterator_type_id = g_type_register_static (G_TYPE_INTERFACE, "GeeIterator", &g_define_type_info, 0);
411 g_type_interface_add_prerequisite (gee_iterator_type_id, G_TYPE_OBJECT);
412 g_type_interface_add_prerequisite (gee_iterator_type_id, GEE_TYPE_TRAVERSABLE);
413 g_once_init_leave (&gee_iterator_type_id__volatile, gee_iterator_type_id);
415 return gee_iterator_type_id__volatile;