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