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 module glib.BBTree;
26 
27 private import glib.ConstructionException;
28 private import glib.c.functions;
29 public  import glib.c.types;
30 public  import gtkc.glibtypes;
31 private import gtkd.Loader;
32 
33 
34 /**
35  * The GTree struct is an opaque data structure representing a
36  * [balanced binary tree][glib-Balanced-Binary-Trees]. It should be
37  * accessed only by using the following functions.
38  */
39 public class BBTree
40 {
41 	/** the main Gtk struct */
42 	protected GTree* gTree;
43 	protected bool ownedRef;
44 
45 	/** Get the main Gtk struct */
46 	public GTree* getBBTreeStruct(bool transferOwnership = false)
47 	{
48 		if (transferOwnership)
49 			ownedRef = false;
50 		return gTree;
51 	}
52 
53 	/** the main Gtk struct as a void* */
54 	protected void* getStruct()
55 	{
56 		return cast(void*)gTree;
57 	}
58 
59 	/**
60 	 * Sets our main struct and passes it to the parent class.
61 	 */
62 	public this (GTree* gTree, bool ownedRef = false)
63 	{
64 		this.gTree = gTree;
65 		this.ownedRef = ownedRef;
66 	}
67 
68 	~this ()
69 	{
70 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
71 			g_tree_unref(gTree);
72 	}
73 
74 
75 	/**
76 	 * Removes all keys and values from the #GTree and decreases its
77 	 * reference count by one. If keys and/or values are dynamically
78 	 * allocated, you should either free them first or create the #GTree
79 	 * using g_tree_new_full(). In the latter case the destroy functions
80 	 * you supplied will be called on all keys and values before destroying
81 	 * the #GTree.
82 	 */
83 	public void destroy()
84 	{
85 		g_tree_destroy(gTree);
86 	}
87 
88 	alias foreac = foreach_;
89 	/**
90 	 * Calls the given function for each of the key/value pairs in the #GTree.
91 	 * The function is passed the key and value of each pair, and the given
92 	 * @data parameter. The tree is traversed in sorted order.
93 	 *
94 	 * The tree may not be modified while iterating over it (you can't
95 	 * add/remove items). To remove all items matching a predicate, you need
96 	 * to add each item to a list in your #GTraverseFunc as you walk over
97 	 * the tree, then walk the list and remove each item.
98 	 *
99 	 * Params:
100 	 *     func = the function to call for each node visited.
101 	 *         If this function returns %TRUE, the traversal is stopped.
102 	 *     userData = user data to pass to the function
103 	 */
104 	public void foreach_(GTraverseFunc func, void* userData)
105 	{
106 		g_tree_foreach(gTree, func, userData);
107 	}
108 
109 	/**
110 	 * Gets the height of a #GTree.
111 	 *
112 	 * If the #GTree contains no nodes, the height is 0.
113 	 * If the #GTree contains only one root node the height is 1.
114 	 * If the root node has children the height is 2, etc.
115 	 *
116 	 * Returns: the height of @tree
117 	 */
118 	public int height()
119 	{
120 		return g_tree_height(gTree);
121 	}
122 
123 	/**
124 	 * Inserts a key/value pair into a #GTree.
125 	 *
126 	 * If the given key already exists in the #GTree its corresponding value
127 	 * is set to the new value. If you supplied a @value_destroy_func when
128 	 * creating the #GTree, the old value is freed using that function. If
129 	 * you supplied a @key_destroy_func when creating the #GTree, the passed
130 	 * key is freed using that function.
131 	 *
132 	 * The tree is automatically 'balanced' as new key/value pairs are added,
133 	 * so that the distance from the root to every leaf is as small as possible.
134 	 *
135 	 * Params:
136 	 *     key = the key to insert
137 	 *     value = the value corresponding to the key
138 	 */
139 	public void insert(void* key, void* value)
140 	{
141 		g_tree_insert(gTree, key, value);
142 	}
143 
144 	/**
145 	 * Gets the value corresponding to the given key. Since a #GTree is
146 	 * automatically balanced as key/value pairs are added, key lookup
147 	 * is O(log n) (where n is the number of key/value pairs in the tree).
148 	 *
149 	 * Params:
150 	 *     key = the key to look up
151 	 *
152 	 * Returns: the value corresponding to the key, or %NULL
153 	 *     if the key was not found
154 	 */
155 	public void* lookup(void* key)
156 	{
157 		return g_tree_lookup(gTree, key);
158 	}
159 
160 	/**
161 	 * Looks up a key in the #GTree, returning the original key and the
162 	 * associated value. This is useful if you need to free the memory
163 	 * allocated for the original key, for example before calling
164 	 * g_tree_remove().
165 	 *
166 	 * Params:
167 	 *     lookupKey = the key to look up
168 	 *     origKey = returns the original key
169 	 *     value = returns the value associated with the key
170 	 *
171 	 * Returns: %TRUE if the key was found in the #GTree
172 	 */
173 	public bool lookupExtended(void* lookupKey, out void* origKey, out void* value)
174 	{
175 		return g_tree_lookup_extended(gTree, lookupKey, &origKey, &value) != 0;
176 	}
177 
178 	/**
179 	 * Gets the number of nodes in a #GTree.
180 	 *
181 	 * Returns: the number of nodes in @tree
182 	 */
183 	public int nnodes()
184 	{
185 		return g_tree_nnodes(gTree);
186 	}
187 
188 	alias doref = ref_;
189 	/**
190 	 * Increments the reference count of @tree by one.
191 	 *
192 	 * It is safe to call this function from any thread.
193 	 *
194 	 * Returns: the passed in #GTree
195 	 *
196 	 * Since: 2.22
197 	 */
198 	public BBTree ref_()
199 	{
200 		auto __p = g_tree_ref(gTree);
201 
202 		if(__p is null)
203 		{
204 			return null;
205 		}
206 
207 		return new BBTree(cast(GTree*) __p);
208 	}
209 
210 	/**
211 	 * Removes a key/value pair from a #GTree.
212 	 *
213 	 * If the #GTree was created using g_tree_new_full(), the key and value
214 	 * are freed using the supplied destroy functions, otherwise you have to
215 	 * make sure that any dynamically allocated values are freed yourself.
216 	 * If the key does not exist in the #GTree, the function does nothing.
217 	 *
218 	 * Params:
219 	 *     key = the key to remove
220 	 *
221 	 * Returns: %TRUE if the key was found (prior to 2.8, this function
222 	 *     returned nothing)
223 	 */
224 	public bool remove(void* key)
225 	{
226 		return g_tree_remove(gTree, key) != 0;
227 	}
228 
229 	/**
230 	 * Inserts a new key and value into a #GTree similar to g_tree_insert().
231 	 * The difference is that if the key already exists in the #GTree, it gets
232 	 * replaced by the new key. If you supplied a @value_destroy_func when
233 	 * creating the #GTree, the old value is freed using that function. If you
234 	 * supplied a @key_destroy_func when creating the #GTree, the old key is
235 	 * freed using that function.
236 	 *
237 	 * The tree is automatically 'balanced' as new key/value pairs are added,
238 	 * so that the distance from the root to every leaf is as small as possible.
239 	 *
240 	 * Params:
241 	 *     key = the key to insert
242 	 *     value = the value corresponding to the key
243 	 */
244 	public void replace(void* key, void* value)
245 	{
246 		g_tree_replace(gTree, key, value);
247 	}
248 
249 	/**
250 	 * Searches a #GTree using @search_func.
251 	 *
252 	 * The @search_func is called with a pointer to the key of a key/value
253 	 * pair in the tree, and the passed in @user_data. If @search_func returns
254 	 * 0 for a key/value pair, then the corresponding value is returned as
255 	 * the result of g_tree_search(). If @search_func returns -1, searching
256 	 * will proceed among the key/value pairs that have a smaller key; if
257 	 * @search_func returns 1, searching will proceed among the key/value
258 	 * pairs that have a larger key.
259 	 *
260 	 * Params:
261 	 *     searchFunc = a function used to search the #GTree
262 	 *     userData = the data passed as the second argument to @search_func
263 	 *
264 	 * Returns: the value corresponding to the found key, or %NULL
265 	 *     if the key was not found
266 	 */
267 	public void* search(GCompareFunc searchFunc, void* userData)
268 	{
269 		return g_tree_search(gTree, searchFunc, userData);
270 	}
271 
272 	/**
273 	 * Removes a key and its associated value from a #GTree without calling
274 	 * the key and value destroy functions.
275 	 *
276 	 * If the key does not exist in the #GTree, the function does nothing.
277 	 *
278 	 * Params:
279 	 *     key = the key to remove
280 	 *
281 	 * Returns: %TRUE if the key was found (prior to 2.8, this function
282 	 *     returned nothing)
283 	 */
284 	public bool steal(void* key)
285 	{
286 		return g_tree_steal(gTree, key) != 0;
287 	}
288 
289 	/**
290 	 * Calls the given function for each node in the #GTree.
291 	 *
292 	 * Deprecated: The order of a balanced tree is somewhat arbitrary.
293 	 * If you just want to visit all nodes in sorted order, use
294 	 * g_tree_foreach() instead. If you really need to visit nodes in
295 	 * a different order, consider using an [n-ary tree][glib-N-ary-Trees].
296 	 *
297 	 * Params:
298 	 *     traverseFunc = the function to call for each node visited. If this
299 	 *         function returns %TRUE, the traversal is stopped.
300 	 *     traverseType = the order in which nodes are visited, one of %G_IN_ORDER,
301 	 *         %G_PRE_ORDER and %G_POST_ORDER
302 	 *     userData = user data to pass to the function
303 	 */
304 	public void traverse(GTraverseFunc traverseFunc, GTraverseType traverseType, void* userData)
305 	{
306 		g_tree_traverse(gTree, traverseFunc, traverseType, userData);
307 	}
308 
309 	/**
310 	 * Decrements the reference count of @tree by one.
311 	 * If the reference count drops to 0, all keys and values will
312 	 * be destroyed (if destroy functions were specified) and all
313 	 * memory allocated by @tree will be released.
314 	 *
315 	 * It is safe to call this function from any thread.
316 	 *
317 	 * Since: 2.22
318 	 */
319 	public void unref()
320 	{
321 		g_tree_unref(gTree);
322 	}
323 
324 	/**
325 	 * Creates a new #GTree.
326 	 *
327 	 * Params:
328 	 *     keyCompareFunc = the function used to order the nodes in the #GTree.
329 	 *         It should return values similar to the standard strcmp() function -
330 	 *         0 if the two arguments are equal, a negative value if the first argument
331 	 *         comes before the second, or a positive value if the first argument comes
332 	 *         after the second.
333 	 *
334 	 * Returns: a newly allocated #GTree
335 	 *
336 	 * Throws: ConstructionException GTK+ fails to create the object.
337 	 */
338 	public this(GCompareFunc keyCompareFunc)
339 	{
340 		auto __p = g_tree_new(keyCompareFunc);
341 
342 		if(__p is null)
343 		{
344 			throw new ConstructionException("null returned by new");
345 		}
346 
347 		this(cast(GTree*) __p);
348 	}
349 
350 	/**
351 	 * Creates a new #GTree like g_tree_new() and allows to specify functions
352 	 * to free the memory allocated for the key and value that get called when
353 	 * removing the entry from the #GTree.
354 	 *
355 	 * Params:
356 	 *     keyCompareFunc = qsort()-style comparison function
357 	 *     keyCompareData = data to pass to comparison function
358 	 *     keyDestroyFunc = a function to free the memory allocated for the key
359 	 *         used when removing the entry from the #GTree or %NULL if you don't
360 	 *         want to supply such a function
361 	 *     valueDestroyFunc = a function to free the memory allocated for the
362 	 *         value used when removing the entry from the #GTree or %NULL if you
363 	 *         don't want to supply such a function
364 	 *
365 	 * Returns: a newly allocated #GTree
366 	 *
367 	 * Throws: ConstructionException GTK+ fails to create the object.
368 	 */
369 	public this(GCompareDataFunc keyCompareFunc, void* keyCompareData, GDestroyNotify keyDestroyFunc, GDestroyNotify valueDestroyFunc)
370 	{
371 		auto __p = g_tree_new_full(keyCompareFunc, keyCompareData, keyDestroyFunc, valueDestroyFunc);
372 
373 		if(__p is null)
374 		{
375 			throw new ConstructionException("null returned by new_full");
376 		}
377 
378 		this(cast(GTree*) __p);
379 	}
380 
381 	/**
382 	 * Creates a new #GTree with a comparison function that accepts user data.
383 	 * See g_tree_new() for more details.
384 	 *
385 	 * Params:
386 	 *     keyCompareFunc = qsort()-style comparison function
387 	 *     keyCompareData = data to pass to comparison function
388 	 *
389 	 * Returns: a newly allocated #GTree
390 	 *
391 	 * Throws: ConstructionException GTK+ fails to create the object.
392 	 */
393 	public this(GCompareDataFunc keyCompareFunc, void* keyCompareData)
394 	{
395 		auto __p = g_tree_new_with_data(keyCompareFunc, keyCompareData);
396 
397 		if(__p is null)
398 		{
399 			throw new ConstructionException("null returned by new_with_data");
400 		}
401 
402 		this(cast(GTree*) __p);
403 	}
404 }