1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19  
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 /*
25  * Conversion parameters:
26  * inFile  = glib-Balanced-Binary-Trees.html
27  * outPack = glib
28  * outFile = BBTree
29  * strct   = GTree
30  * realStrct=
31  * ctorStrct=
32  * clss    = BBTree
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_tree_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- gtkc.paths
47  * 	- gtkc.Loader
48  * structWrap:
49  * 	- GTree* -> BBTree
50  * module aliases:
51  * local aliases:
52  * overrides:
53  */
54 
55 module glib.BBTree;
56 
57 public  import gtkc.glibtypes;
58 
59 private import gtkc.glib;
60 private import glib.ConstructionException;
61 
62 private import gtkc.paths;
63 private import gtkc.Loader;
64 
65 
66 
67 /**
68  * The GTree structure and its associated functions provide a sorted
69  * collection of key/value pairs optimized for searching and traversing
70  * in order.
71  *
72  * To create a new GTree use g_tree_new().
73  *
74  * To insert a key/value pair into a GTree use g_tree_insert().
75  *
76  * To lookup the value corresponding to a given key, use
77  * g_tree_lookup() and g_tree_lookup_extended().
78  *
79  * To find out the number of nodes in a GTree, use g_tree_nnodes(). To
80  * get the height of a GTree, use g_tree_height().
81  *
82  * To traverse a GTree, calling a function for each node visited in
83  * the traversal, use g_tree_foreach().
84  *
85  * To remove a key/value pair use g_tree_remove().
86  *
87  * To destroy a GTree, use g_tree_destroy().
88  */
89 public class BBTree
90 {
91 	
92 	/** the main Gtk struct */
93 	protected GTree* gTree;
94 	
95 	
96 	/** Get the main Gtk struct */
97 	public GTree* getBBTreeStruct()
98 	{
99 		return gTree;
100 	}
101 	
102 	
103 	/** the main Gtk struct as a void* */
104 	protected void* getStruct()
105 	{
106 		return cast(void*)gTree;
107 	}
108 	
109 	/**
110 	 * Sets our main struct and passes it to the parent class
111 	 */
112 	public this (GTree* gTree)
113 	{
114 		this.gTree = gTree;
115 	}
116 	
117 	~this ()
118 	{
119 		if (  Linker.isLoaded(LIBRARY.GLIB) && gTree !is null )
120 		{
121 			g_tree_unref(gTree);
122 		}
123 	}
124 	
125 	/**
126 	 */
127 	
128 	/**
129 	 * Creates a new GTree.
130 	 * Params:
131 	 * keyCompareFunc = the function used to order the nodes in the GTree.
132 	 * It should return values similar to the standard strcmp() function -
133 	 * 0 if the two arguments are equal, a negative value if the first argument
134 	 * comes before the second, or a positive value if the first argument comes
135 	 * after the second.
136 	 * Throws: ConstructionException GTK+ fails to create the object.
137 	 */
138 	public this (GCompareFunc keyCompareFunc)
139 	{
140 		// GTree * g_tree_new (GCompareFunc key_compare_func);
141 		auto p = g_tree_new(keyCompareFunc);
142 		if(p is null)
143 		{
144 			throw new ConstructionException("null returned by g_tree_new(keyCompareFunc)");
145 		}
146 		this(cast(GTree*) p);
147 	}
148 	
149 	/**
150 	 * Increments the reference count of tree by one. It is safe to call
151 	 * this function from any thread.
152 	 * Since 2.22
153 	 * Returns: the passed in GTree.
154 	 */
155 	public BBTree doref()
156 	{
157 		// GTree * g_tree_ref (GTree *tree);
158 		auto p = g_tree_ref(gTree);
159 		
160 		if(p is null)
161 		{
162 			return null;
163 		}
164 		
165 		return new BBTree(cast(GTree*) p);
166 	}
167 	
168 	/**
169 	 * Decrements the reference count of tree by one. If the reference count
170 	 * drops to 0, all keys and values will be destroyed (if destroy
171 	 * functions were specified) and all memory allocated by tree will be
172 	 * released.
173 	 * It is safe to call this function from any thread.
174 	 * Since 2.22
175 	 */
176 	public void unref()
177 	{
178 		// void g_tree_unref (GTree *tree);
179 		g_tree_unref(gTree);
180 	}
181 	
182 	/**
183 	 * Creates a new GTree with a comparison function that accepts user data.
184 	 * See g_tree_new() for more details.
185 	 * Params:
186 	 * keyCompareFunc = qsort()-style comparison function.
187 	 * keyCompareData = data to pass to comparison function.
188 	 * Throws: ConstructionException GTK+ fails to create the object.
189 	 */
190 	public this (GCompareDataFunc keyCompareFunc, void* keyCompareData)
191 	{
192 		// GTree * g_tree_new_with_data (GCompareDataFunc key_compare_func,  gpointer key_compare_data);
193 		auto p = g_tree_new_with_data(keyCompareFunc, keyCompareData);
194 		if(p is null)
195 		{
196 			throw new ConstructionException("null returned by g_tree_new_with_data(keyCompareFunc, keyCompareData)");
197 		}
198 		this(cast(GTree*) p);
199 	}
200 	
201 	/**
202 	 * Creates a new GTree like g_tree_new() and allows to specify functions
203 	 * to free the memory allocated for the key and value that get called when
204 	 * removing the entry from the GTree.
205 	 * Params:
206 	 * keyCompareFunc = qsort()-style comparison function.
207 	 * keyCompareData = data to pass to comparison function.
208 	 * keyDestroyFunc = a function to free the memory allocated for the key
209 	 * used when removing the entry from the GTree or NULL if you don't
210 	 * want to supply such a function.
211 	 * valueDestroyFunc = a function to free the memory allocated for the
212 	 * value used when removing the entry from the GTree or NULL if you
213 	 * don't want to supply such a function.
214 	 * Throws: ConstructionException GTK+ fails to create the object.
215 	 */
216 	public this (GCompareDataFunc keyCompareFunc, void* keyCompareData, GDestroyNotify keyDestroyFunc, GDestroyNotify valueDestroyFunc)
217 	{
218 		// GTree * g_tree_new_full (GCompareDataFunc key_compare_func,  gpointer key_compare_data,  GDestroyNotify key_destroy_func,  GDestroyNotify value_destroy_func);
219 		auto p = g_tree_new_full(keyCompareFunc, keyCompareData, keyDestroyFunc, valueDestroyFunc);
220 		if(p is null)
221 		{
222 			throw new ConstructionException("null returned by g_tree_new_full(keyCompareFunc, keyCompareData, keyDestroyFunc, valueDestroyFunc)");
223 		}
224 		this(cast(GTree*) p);
225 	}
226 	
227 	/**
228 	 * Inserts a key/value pair into a GTree. If the given key already exists
229 	 * in the GTree its corresponding value is set to the new value. If you
230 	 * supplied a value_destroy_func when creating the GTree, the old value is
231 	 * freed using that function. If you supplied a key_destroy_func when
232 	 * creating the GTree, the passed key is freed using that function.
233 	 * The tree is automatically 'balanced' as new key/value pairs are added,
234 	 * so that the distance from the root to every leaf is as small as possible.
235 	 * Params:
236 	 * key = the key to insert.
237 	 * value = the value corresponding to the key.
238 	 */
239 	public void insert(void* key, void* value)
240 	{
241 		// void g_tree_insert (GTree *tree,  gpointer key,  gpointer value);
242 		g_tree_insert(gTree, key, value);
243 	}
244 	
245 	/**
246 	 * Inserts a new key and value into a GTree similar to g_tree_insert().
247 	 * The difference is that if the key already exists in the GTree, it gets
248 	 * replaced by the new key. If you supplied a value_destroy_func when
249 	 * creating the GTree, the old value is freed using that function. If you
250 	 * supplied a key_destroy_func when creating the GTree, the old key is
251 	 * freed using that function.
252 	 * The tree is automatically 'balanced' as new key/value pairs are added,
253 	 * so that the distance from the root to every leaf is as small as possible.
254 	 * Params:
255 	 * key = the key to insert.
256 	 * value = the value corresponding to the key.
257 	 */
258 	public void replace(void* key, void* value)
259 	{
260 		// void g_tree_replace (GTree *tree,  gpointer key,  gpointer value);
261 		g_tree_replace(gTree, key, value);
262 	}
263 	
264 	/**
265 	 * Gets the number of nodes in a GTree.
266 	 * Returns: the number of nodes in the GTree.
267 	 */
268 	public int nnodes()
269 	{
270 		// gint g_tree_nnodes (GTree *tree);
271 		return g_tree_nnodes(gTree);
272 	}
273 	
274 	/**
275 	 * Gets the height of a GTree.
276 	 * If the GTree contains no nodes, the height is 0.
277 	 * If the GTree contains only one root node the height is 1.
278 	 * If the root node has children the height is 2, etc.
279 	 * Returns: the height of the GTree.
280 	 */
281 	public int height()
282 	{
283 		// gint g_tree_height (GTree *tree);
284 		return g_tree_height(gTree);
285 	}
286 	
287 	/**
288 	 * Gets the value corresponding to the given key. Since a GTree is
289 	 * automatically balanced as key/value pairs are added, key lookup is very
290 	 * fast.
291 	 * Params:
292 	 * key = the key to look up.
293 	 * Returns: the value corresponding to the key, or NULL if the key was not found.
294 	 */
295 	public void* lookup(void* key)
296 	{
297 		// gpointer g_tree_lookup (GTree *tree,  gconstpointer key);
298 		return g_tree_lookup(gTree, key);
299 	}
300 	
301 	/**
302 	 * Looks up a key in the GTree, returning the original key and the
303 	 * associated value and a gboolean which is TRUE if the key was found. This
304 	 * is useful if you need to free the memory allocated for the original key,
305 	 * for example before calling g_tree_remove().
306 	 * Params:
307 	 * lookupKey = the key to look up.
308 	 * origKey = returns the original key.
309 	 * value = returns the value associated with the key.
310 	 * Returns: TRUE if the key was found in the GTree.
311 	 */
312 	public int lookupExtended(void* lookupKey, void** origKey, void** value)
313 	{
314 		// gboolean g_tree_lookup_extended (GTree *tree,  gconstpointer lookup_key,  gpointer *orig_key,  gpointer *value);
315 		return g_tree_lookup_extended(gTree, lookupKey, origKey, value);
316 	}
317 	
318 	/**
319 	 * Calls the given function for each of the key/value pairs in the GTree.
320 	 * The function is passed the key and value of each pair, and the given
321 	 * data parameter. The tree is traversed in sorted order.
322 	 * The tree may not be modified while iterating over it (you can't
323 	 * add/remove items). To remove all items matching a predicate, you need
324 	 * to add each item to a list in your GTraverseFunc as you walk over
325 	 * the tree, then walk the list and remove each item.
326 	 * Params:
327 	 * func = the function to call for each node visited.
328 	 * If this function returns TRUE, the traversal is stopped.
329 	 * userData = user data to pass to the function.
330 	 */
331 	public void foreac(GTraverseFunc func, void* userData)
332 	{
333 		// void g_tree_foreach (GTree *tree,  GTraverseFunc func,  gpointer user_data);
334 		g_tree_foreach(gTree, func, userData);
335 	}
336 	
337 	/**
338 	 * Warning
339 	 * g_tree_traverse has been deprecated since version 2.2 and should not be used in newly-written code. The order of a balanced tree is somewhat arbitrary. If you
340 	 * just want to visit all nodes in sorted order, use g_tree_foreach()
341 	 * instead. If you really need to visit nodes in a different order, consider
342 	 * using an N-ary Tree.
343 	 * Calls the given function for each node in the GTree.
344 	 * Params:
345 	 * traverseFunc = the function to call for each node visited. If this
346 	 * function returns TRUE, the traversal is stopped.
347 	 * traverseType = the order in which nodes are visited, one of G_IN_ORDER,
348 	 * G_PRE_ORDER and G_POST_ORDER.
349 	 * userData = user data to pass to the function.
350 	 */
351 	public void traverse(GTraverseFunc traverseFunc, GTraverseType traverseType, void* userData)
352 	{
353 		// void g_tree_traverse (GTree *tree,  GTraverseFunc traverse_func,  GTraverseType traverse_type,  gpointer user_data);
354 		g_tree_traverse(gTree, traverseFunc, traverseType, userData);
355 	}
356 	
357 	/**
358 	 * Searches a GTree using search_func.
359 	 * The search_func is called with a pointer to the key of a key/value
360 	 * pair in the tree, and the passed in user_data. If search_func returns
361 	 * 0 for a key/value pair, then the corresponding value is returned as
362 	 * the result of g_tree_search(). If search_func returns -1, searching
363 	 * will proceed among the key/value pairs that have a smaller key; if
364 	 * search_func returns 1, searching will proceed among the key/value
365 	 * pairs that have a larger key.
366 	 * Params:
367 	 * searchFunc = a function used to search the GTree
368 	 * userData = the data passed as the second argument to search_func
369 	 * Returns: the value corresponding to the found key, or NULL if the key was not found.
370 	 */
371 	public void* search(GCompareFunc searchFunc, void* userData)
372 	{
373 		// gpointer g_tree_search (GTree *tree,  GCompareFunc search_func,  gconstpointer user_data);
374 		return g_tree_search(gTree, searchFunc, userData);
375 	}
376 	
377 	/**
378 	 * Removes a key/value pair from a GTree.
379 	 * If the GTree was created using g_tree_new_full(), the key and value
380 	 * are freed using the supplied destroy functions, otherwise you have to
381 	 * make sure that any dynamically allocated values are freed yourself.
382 	 * If the key does not exist in the GTree, the function does nothing.
383 	 * Params:
384 	 * key = the key to remove.
385 	 * Returns: TRUE if the key was found (prior to 2.8, this function returned nothing)
386 	 */
387 	public int remove(void* key)
388 	{
389 		// gboolean g_tree_remove (GTree *tree,  gconstpointer key);
390 		return g_tree_remove(gTree, key);
391 	}
392 	
393 	/**
394 	 * Removes a key and its associated value from a GTree without calling
395 	 * the key and value destroy functions.
396 	 * If the key does not exist in the GTree, the function does nothing.
397 	 * Params:
398 	 * key = the key to remove.
399 	 * Returns: TRUE if the key was found (prior to 2.8, this function returned nothing)
400 	 */
401 	public int steal(void* key)
402 	{
403 		// gboolean g_tree_steal (GTree *tree,  gconstpointer key);
404 		return g_tree_steal(gTree, key);
405 	}
406 	
407 	/**
408 	 * Removes all keys and values from the GTree and decreases its
409 	 * reference count by one. If keys and/or values are dynamically
410 	 * allocated, you should either free them first or create the GTree
411 	 * using g_tree_new_full(). In the latter case the destroy functions
412 	 * you supplied will be called on all keys and values before destroying
413 	 * the GTree.
414 	 */
415 	public void destroy()
416 	{
417 		// void g_tree_destroy (GTree *tree);
418 		g_tree_destroy(gTree);
419 	}
420 }