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 gtk.Bitset;
26 
27 private import glib.ConstructionException;
28 private import gobject.ObjectG;
29 private import gtk.c.functions;
30 public  import gtk.c.types;
31 private import gtkd.Loader;
32 
33 
34 /**
35  * A `GtkBitset` represents a set of unsigned integers.
36  * 
37  * Another name for this data structure is "bitmap".
38  * 
39  * The current implementation is based on [roaring bitmaps](https://roaringbitmap.org/).
40  * 
41  * A bitset allows adding a set of integers and provides support for set operations
42  * like unions, intersections and checks for equality or if a value is contained
43  * in the set. `GtkBitset` also contains various functions to query metadata about
44  * the bitset, such as the minimum or maximum values or its size.
45  * 
46  * The fastest way to iterate values in a bitset is [struct@Gtk.BitsetIter].
47  * 
48  * The main use case for `GtkBitset` is implementing complex selections for
49  * [iface@Gtk.SelectionModel].
50  */
51 public class Bitset
52 {
53 	/** the main Gtk struct */
54 	protected GtkBitset* gtkBitset;
55 	protected bool ownedRef;
56 
57 	/** Get the main Gtk struct */
58 	public GtkBitset* getBitsetStruct(bool transferOwnership = false)
59 	{
60 		if (transferOwnership)
61 			ownedRef = false;
62 		return gtkBitset;
63 	}
64 
65 	/** the main Gtk struct as a void* */
66 	protected void* getStruct()
67 	{
68 		return cast(void*)gtkBitset;
69 	}
70 
71 	/**
72 	 * Sets our main struct and passes it to the parent class.
73 	 */
74 	public this (GtkBitset* gtkBitset, bool ownedRef = false)
75 	{
76 		this.gtkBitset = gtkBitset;
77 		this.ownedRef = ownedRef;
78 	}
79 
80 	~this ()
81 	{
82 		if ( Linker.isLoaded(LIBRARY_GTK) && ownedRef )
83 			gtk_bitset_unref(gtkBitset);
84 	}
85 
86 
87 	/** */
88 	public static GType getType()
89 	{
90 		return gtk_bitset_get_type();
91 	}
92 
93 	/**
94 	 * Creates a new empty bitset.
95 	 *
96 	 * Returns: A new empty bitset
97 	 *
98 	 * Throws: ConstructionException GTK+ fails to create the object.
99 	 */
100 	public this()
101 	{
102 		auto __p = gtk_bitset_new_empty();
103 
104 		if(__p is null)
105 		{
106 			throw new ConstructionException("null returned by new_empty");
107 		}
108 
109 		this(cast(GtkBitset*) __p);
110 	}
111 
112 	/**
113 	 * Creates a bitset with the given range set.
114 	 *
115 	 * Params:
116 	 *     start = first value to add
117 	 *     nItems = number of consecutive values to add
118 	 *
119 	 * Returns: A new bitset
120 	 *
121 	 * Throws: ConstructionException GTK+ fails to create the object.
122 	 */
123 	public this(uint start, uint nItems)
124 	{
125 		auto __p = gtk_bitset_new_range(start, nItems);
126 
127 		if(__p is null)
128 		{
129 			throw new ConstructionException("null returned by new_range");
130 		}
131 
132 		this(cast(GtkBitset*) __p);
133 	}
134 
135 	/**
136 	 * Adds @value to @self if it wasn't part of it before.
137 	 *
138 	 * Params:
139 	 *     value = value to add
140 	 *
141 	 * Returns: %TRUE if @value was not part of @self and @self
142 	 *     was changed.
143 	 */
144 	public bool add(uint value)
145 	{
146 		return gtk_bitset_add(gtkBitset, value) != 0;
147 	}
148 
149 	/**
150 	 * Adds all values from @start (inclusive) to @start + @n_items
151 	 * (exclusive) in @self.
152 	 *
153 	 * Params:
154 	 *     start = first value to add
155 	 *     nItems = number of consecutive values to add
156 	 */
157 	public void addRange(uint start, uint nItems)
158 	{
159 		gtk_bitset_add_range(gtkBitset, start, nItems);
160 	}
161 
162 	/**
163 	 * Adds the closed range [@first, @last], so @first, @last and all
164 	 * values in between. @first must be smaller than @last.
165 	 *
166 	 * Params:
167 	 *     first = first value to add
168 	 *     last = last value to add
169 	 */
170 	public void addRangeClosed(uint first, uint last)
171 	{
172 		gtk_bitset_add_range_closed(gtkBitset, first, last);
173 	}
174 
175 	/**
176 	 * Interprets the values as a 2-dimensional boolean grid with the given @stride
177 	 * and inside that grid, adds a rectangle with the given @width and @height.
178 	 *
179 	 * Params:
180 	 *     start = first value to add
181 	 *     width = width of the rectangle
182 	 *     height = height of the rectangle
183 	 *     stride = row stride of the grid
184 	 */
185 	public void addRectangle(uint start, uint width, uint height, uint stride)
186 	{
187 		gtk_bitset_add_rectangle(gtkBitset, start, width, height, stride);
188 	}
189 
190 	/**
191 	 * Checks if the given @value has been added to @self
192 	 *
193 	 * Params:
194 	 *     value = the value to check
195 	 *
196 	 * Returns: %TRUE if @self contains @value
197 	 */
198 	public bool contains(uint value)
199 	{
200 		return gtk_bitset_contains(gtkBitset, value) != 0;
201 	}
202 
203 	/**
204 	 * Creates a copy of @self.
205 	 *
206 	 * Returns: A new bitset that contains the same
207 	 *     values as @self
208 	 */
209 	public Bitset copy()
210 	{
211 		auto __p = gtk_bitset_copy(gtkBitset);
212 
213 		if(__p is null)
214 		{
215 			return null;
216 		}
217 
218 		return ObjectG.getDObject!(Bitset)(cast(GtkBitset*) __p, true);
219 	}
220 
221 	/**
222 	 * Sets @self to be the symmetric difference of @self and @other.
223 	 *
224 	 * The symmetric difference is set @self to contain all values that
225 	 * were either contained in @self or in @other, but not in both.
226 	 * This operation is also called an XOR.
227 	 *
228 	 * It is allowed for @self and @other to be the same bitset. The bitset
229 	 * will be emptied in that case.
230 	 *
231 	 * Params:
232 	 *     other = the `GtkBitset` to compute the difference from
233 	 */
234 	public void difference(Bitset other)
235 	{
236 		gtk_bitset_difference(gtkBitset, (other is null) ? null : other.getBitsetStruct());
237 	}
238 
239 	/**
240 	 * Returns %TRUE if @self and @other contain the same values.
241 	 *
242 	 * Params:
243 	 *     other = another `GtkBitset`
244 	 *
245 	 * Returns: %TRUE if @self and @other contain the same values
246 	 */
247 	public bool equals(Bitset other)
248 	{
249 		return gtk_bitset_equals(gtkBitset, (other is null) ? null : other.getBitsetStruct()) != 0;
250 	}
251 
252 	/**
253 	 * Returns the largest value in @self.
254 	 *
255 	 * If @self is empty, 0 is returned.
256 	 *
257 	 * Returns: The largest value in @self
258 	 */
259 	public uint getMaximum()
260 	{
261 		return gtk_bitset_get_maximum(gtkBitset);
262 	}
263 
264 	/**
265 	 * Returns the smallest value in @self.
266 	 *
267 	 * If @self is empty, `G_MAXUINT` is returned.
268 	 *
269 	 * Returns: The smallest value in @self
270 	 */
271 	public uint getMinimum()
272 	{
273 		return gtk_bitset_get_minimum(gtkBitset);
274 	}
275 
276 	/**
277 	 * Returns the value of the @nth item in self.
278 	 *
279 	 * If @nth is >= the size of @self, 0 is returned.
280 	 *
281 	 * Params:
282 	 *     nth = index of the item to get
283 	 *
284 	 * Returns: the value of the @nth item in @self
285 	 */
286 	public uint getNth(uint nth)
287 	{
288 		return gtk_bitset_get_nth(gtkBitset, nth);
289 	}
290 
291 	/**
292 	 * Gets the number of values that were added to the set.
293 	 *
294 	 * For example, if the set is empty, 0 is returned.
295 	 *
296 	 * Note that this function returns a `guint64`, because when all
297 	 * values are set, the return value is `G_MAXUINT + 1`. Unless you
298 	 * are sure this cannot happen (it can't with `GListModel`), be sure
299 	 * to use a 64bit type.
300 	 *
301 	 * Returns: The number of values in the set.
302 	 */
303 	public ulong getSize()
304 	{
305 		return gtk_bitset_get_size(gtkBitset);
306 	}
307 
308 	/**
309 	 * Gets the number of values that are part of the set from @first to @last
310 	 * (inclusive).
311 	 *
312 	 * Note that this function returns a `guint64`, because when all values are
313 	 * set, the return value is `G_MAXUINT + 1`. Unless you are sure this cannot
314 	 * happen (it can't with `GListModel`), be sure to use a 64bit type.
315 	 *
316 	 * Params:
317 	 *     first = the first element to include
318 	 *     last = the last element to include
319 	 *
320 	 * Returns: The number of values in the set from @first to @last.
321 	 */
322 	public ulong getSizeInRange(uint first, uint last)
323 	{
324 		return gtk_bitset_get_size_in_range(gtkBitset, first, last);
325 	}
326 
327 	/**
328 	 * Sets @self to be the intersection of @self and @other.
329 	 *
330 	 * In other words, remove all values from @self that are not part of @other.
331 	 *
332 	 * It is allowed for @self and @other to be the same bitset. Nothing will
333 	 * happen in that case.
334 	 *
335 	 * Params:
336 	 *     other = the `GtkBitset` to intersect with
337 	 */
338 	public void intersect(Bitset other)
339 	{
340 		gtk_bitset_intersect(gtkBitset, (other is null) ? null : other.getBitsetStruct());
341 	}
342 
343 	/**
344 	 * Check if no value is contained in bitset.
345 	 *
346 	 * Returns: %TRUE if @self is empty
347 	 */
348 	public bool isEmpty()
349 	{
350 		return gtk_bitset_is_empty(gtkBitset) != 0;
351 	}
352 
353 	alias doref = ref_;
354 	/**
355 	 * Acquires a reference on the given `GtkBitset`.
356 	 *
357 	 * Returns: the `GtkBitset` with an additional reference
358 	 */
359 	public Bitset ref_()
360 	{
361 		auto __p = gtk_bitset_ref(gtkBitset);
362 
363 		if(__p is null)
364 		{
365 			return null;
366 		}
367 
368 		return ObjectG.getDObject!(Bitset)(cast(GtkBitset*) __p);
369 	}
370 
371 	/**
372 	 * Removes @value from @self if it was part of it before.
373 	 *
374 	 * Params:
375 	 *     value = value to add
376 	 *
377 	 * Returns: %TRUE if @value was part of @self and @self
378 	 *     was changed.
379 	 */
380 	public bool remove(uint value)
381 	{
382 		return gtk_bitset_remove(gtkBitset, value) != 0;
383 	}
384 
385 	/**
386 	 * Removes all values from the bitset so that it is empty again.
387 	 */
388 	public void removeAll()
389 	{
390 		gtk_bitset_remove_all(gtkBitset);
391 	}
392 
393 	/**
394 	 * Removes all values from @start (inclusive) to @start + @n_items (exclusive)
395 	 * in @self.
396 	 *
397 	 * Params:
398 	 *     start = first value to remove
399 	 *     nItems = number of consecutive values to remove
400 	 */
401 	public void removeRange(uint start, uint nItems)
402 	{
403 		gtk_bitset_remove_range(gtkBitset, start, nItems);
404 	}
405 
406 	/**
407 	 * Removes the closed range [@first, @last], so @first, @last and all
408 	 * values in between. @first must be smaller than @last.
409 	 *
410 	 * Params:
411 	 *     first = first value to remove
412 	 *     last = last value to remove
413 	 */
414 	public void removeRangeClosed(uint first, uint last)
415 	{
416 		gtk_bitset_remove_range_closed(gtkBitset, first, last);
417 	}
418 
419 	/**
420 	 * Interprets the values as a 2-dimensional boolean grid with the given @stride
421 	 * and inside that grid, removes a rectangle with the given @width and @height.
422 	 *
423 	 * Params:
424 	 *     start = first value to remove
425 	 *     width = width of the rectangle
426 	 *     height = height of the rectangle
427 	 *     stride = row stride of the grid
428 	 */
429 	public void removeRectangle(uint start, uint width, uint height, uint stride)
430 	{
431 		gtk_bitset_remove_rectangle(gtkBitset, start, width, height, stride);
432 	}
433 
434 	/**
435 	 * Shifts all values in @self to the left by @amount.
436 	 *
437 	 * Values smaller than @amount are discarded.
438 	 *
439 	 * Params:
440 	 *     amount = amount to shift all values to the left
441 	 */
442 	public void shiftLeft(uint amount)
443 	{
444 		gtk_bitset_shift_left(gtkBitset, amount);
445 	}
446 
447 	/**
448 	 * Shifts all values in @self to the right by @amount.
449 	 *
450 	 * Values that end up too large to be held in a #guint are discarded.
451 	 *
452 	 * Params:
453 	 *     amount = amount to shift all values to the right
454 	 */
455 	public void shiftRight(uint amount)
456 	{
457 		gtk_bitset_shift_right(gtkBitset, amount);
458 	}
459 
460 	/**
461 	 * This is a support function for `GListModel` handling, by mirroring
462 	 * the `GlistModel::items-changed` signal.
463 	 *
464 	 * First, it "cuts" the values from @position to @removed from
465 	 * the bitset. That is, it removes all those values and shifts
466 	 * all larger values to the left by @removed places.
467 	 *
468 	 * Then, it "pastes" new room into the bitset by shifting all values
469 	 * larger than @position by @added spaces to the right. This frees
470 	 * up space that can then be filled.
471 	 *
472 	 * Params:
473 	 *     position = position at which to slice
474 	 *     removed = number of values to remove
475 	 *     added = number of values to add
476 	 */
477 	public void splice(uint position, uint removed, uint added)
478 	{
479 		gtk_bitset_splice(gtkBitset, position, removed, added);
480 	}
481 
482 	/**
483 	 * Sets @self to be the subtraction of @other from @self.
484 	 *
485 	 * In other words, remove all values from @self that are part of @other.
486 	 *
487 	 * It is allowed for @self and @other to be the same bitset. The bitset
488 	 * will be emptied in that case.
489 	 *
490 	 * Params:
491 	 *     other = the `GtkBitset` to subtract
492 	 */
493 	public void subtract(Bitset other)
494 	{
495 		gtk_bitset_subtract(gtkBitset, (other is null) ? null : other.getBitsetStruct());
496 	}
497 
498 	alias unio = union_;
499 	/**
500 	 * Sets @self to be the union of @self and @other.
501 	 *
502 	 * That is, add all values from @other into @self that weren't part of it.
503 	 *
504 	 * It is allowed for @self and @other to be the same bitset. Nothing will
505 	 * happen in that case.
506 	 *
507 	 * Params:
508 	 *     other = the `GtkBitset` to union with
509 	 */
510 	public void union_(Bitset other)
511 	{
512 		gtk_bitset_union(gtkBitset, (other is null) ? null : other.getBitsetStruct());
513 	}
514 
515 	/**
516 	 * Releases a reference on the given `GtkBitset`.
517 	 *
518 	 * If the reference was the last, the resources associated to the @self are
519 	 * freed.
520 	 */
521 	public void unref()
522 	{
523 		gtk_bitset_unref(gtkBitset);
524 	}
525 }