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.HashTable; 26 27 private import glib.ConstructionException; 28 private import glib.ListG; 29 private import glib.c.functions; 30 public import glib.c.types; 31 public import gtkc.glibtypes; 32 33 34 /** 35 * The #GHashTable struct is an opaque data structure to represent a 36 * [Hash Table][glib-Hash-Tables]. It should only be accessed via the 37 * following functions. 38 */ 39 public class HashTable 40 { 41 /** the main Gtk struct */ 42 protected GHashTable* gHashTable; 43 protected bool ownedRef; 44 45 /** Get the main Gtk struct */ 46 public GHashTable* getHashTableStruct(bool transferOwnership = false) 47 { 48 if (transferOwnership) 49 ownedRef = false; 50 return gHashTable; 51 } 52 53 /** the main Gtk struct as a void* */ 54 protected void* getStruct() 55 { 56 return cast(void*)gHashTable; 57 } 58 59 /** 60 * Sets our main struct and passes it to the parent class. 61 */ 62 public this (GHashTable* gHashTable, bool ownedRef = false) 63 { 64 this.gHashTable = gHashTable; 65 this.ownedRef = ownedRef; 66 } 67 68 69 /** 70 * This is a convenience function for using a #GHashTable as a set. It 71 * is equivalent to calling g_hash_table_replace() with @key as both the 72 * key and the value. 73 * 74 * When a hash table only ever contains keys that have themselves as the 75 * corresponding value it is able to be stored more efficiently. See 76 * the discussion in the section description. 77 * 78 * Params: 79 * key = a key to insert 80 * 81 * Returns: %TRUE if the key did not exist yet 82 * 83 * Since: 2.32 84 */ 85 public bool add(void* key) 86 { 87 return g_hash_table_add(gHashTable, key) != 0; 88 } 89 90 /** 91 * Checks if @key is in @hash_table. 92 * 93 * Params: 94 * key = a key to check 95 * 96 * Returns: %TRUE if @key is in @hash_table, %FALSE otherwise. 97 * 98 * Since: 2.32 99 */ 100 public bool contains(void* key) 101 { 102 return g_hash_table_contains(gHashTable, key) != 0; 103 } 104 105 /** 106 * Destroys all keys and values in the #GHashTable and decrements its 107 * reference count by 1. If keys and/or values are dynamically allocated, 108 * you should either free them first or create the #GHashTable with destroy 109 * notifiers using g_hash_table_new_full(). In the latter case the destroy 110 * functions you supplied will be called on all keys and values during the 111 * destruction phase. 112 */ 113 public void destroy() 114 { 115 g_hash_table_destroy(gHashTable); 116 } 117 118 /** 119 * Calls the given function for key/value pairs in the #GHashTable 120 * until @predicate returns %TRUE. The function is passed the key 121 * and value of each pair, and the given @user_data parameter. The 122 * hash table may not be modified while iterating over it (you can't 123 * add/remove items). 124 * 125 * Note, that hash tables are really only optimized for forward 126 * lookups, i.e. g_hash_table_lookup(). So code that frequently issues 127 * g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of 128 * once per every entry in a hash table) should probably be reworked 129 * to use additional or different data structures for reverse lookups 130 * (keep in mind that an O(n) find/foreach operation issued for all n 131 * values in a hash table ends up needing O(n*n) operations). 132 * 133 * Params: 134 * predicate = function to test the key/value pairs for a certain property 135 * userData = user data to pass to the function 136 * 137 * Returns: The value of the first key/value pair is returned, 138 * for which @predicate evaluates to %TRUE. If no pair with the 139 * requested property is found, %NULL is returned. 140 * 141 * Since: 2.4 142 */ 143 public void* find(GHRFunc predicate, void* userData) 144 { 145 return g_hash_table_find(gHashTable, predicate, userData); 146 } 147 148 /** 149 * Calls the given function for each of the key/value pairs in the 150 * #GHashTable. The function is passed the key and value of each 151 * pair, and the given @user_data parameter. The hash table may not 152 * be modified while iterating over it (you can't add/remove 153 * items). To remove all items matching a predicate, use 154 * g_hash_table_foreach_remove(). 155 * 156 * See g_hash_table_find() for performance caveats for linear 157 * order searches in contrast to g_hash_table_lookup(). 158 * 159 * Params: 160 * func = the function to call for each key/value pair 161 * userData = user data to pass to the function 162 */ 163 public void foreac(GHFunc func, void* userData) 164 { 165 g_hash_table_foreach(gHashTable, func, userData); 166 } 167 168 /** 169 * Calls the given function for each key/value pair in the 170 * #GHashTable. If the function returns %TRUE, then the key/value 171 * pair is removed from the #GHashTable. If you supplied key or 172 * value destroy functions when creating the #GHashTable, they are 173 * used to free the memory allocated for the removed keys and values. 174 * 175 * See #GHashTableIter for an alternative way to loop over the 176 * key/value pairs in the hash table. 177 * 178 * Params: 179 * func = the function to call for each key/value pair 180 * userData = user data to pass to the function 181 * 182 * Returns: the number of key/value pairs removed 183 */ 184 public uint foreachRemove(GHRFunc func, void* userData) 185 { 186 return g_hash_table_foreach_remove(gHashTable, func, userData); 187 } 188 189 /** 190 * Calls the given function for each key/value pair in the 191 * #GHashTable. If the function returns %TRUE, then the key/value 192 * pair is removed from the #GHashTable, but no key or value 193 * destroy functions are called. 194 * 195 * See #GHashTableIter for an alternative way to loop over the 196 * key/value pairs in the hash table. 197 * 198 * Params: 199 * func = the function to call for each key/value pair 200 * userData = user data to pass to the function 201 * 202 * Returns: the number of key/value pairs removed. 203 */ 204 public uint foreachSteal(GHRFunc func, void* userData) 205 { 206 return g_hash_table_foreach_steal(gHashTable, func, userData); 207 } 208 209 /** 210 * Retrieves every key inside @hash_table. The returned data is valid 211 * until changes to the hash release those keys. 212 * 213 * This iterates over every entry in the hash table to build its return value. 214 * To iterate over the entries in a #GHashTable more efficiently, use a 215 * #GHashTableIter. 216 * 217 * Returns: a #GList containing all the keys 218 * inside the hash table. The content of the list is owned by the 219 * hash table and should not be modified or freed. Use g_list_free() 220 * when done using the list. 221 * 222 * Since: 2.14 223 */ 224 public ListG getKeys() 225 { 226 auto p = g_hash_table_get_keys(gHashTable); 227 228 if(p is null) 229 { 230 return null; 231 } 232 233 return new ListG(cast(GList*) p); 234 } 235 236 /** 237 * Retrieves every key inside @hash_table, as an array. 238 * 239 * The returned array is %NULL-terminated but may contain %NULL as a 240 * key. Use @length to determine the true length if it's possible that 241 * %NULL was used as the value for a key. 242 * 243 * Note: in the common case of a string-keyed #GHashTable, the return 244 * value of this function can be conveniently cast to (const gchar **). 245 * 246 * This iterates over every entry in the hash table to build its return value. 247 * To iterate over the entries in a #GHashTable more efficiently, use a 248 * #GHashTableIter. 249 * 250 * You should always free the return result with g_free(). In the 251 * above-mentioned case of a string-keyed hash table, it may be 252 * appropriate to use g_strfreev() if you call g_hash_table_steal_all() 253 * first to transfer ownership of the keys. 254 * 255 * Returns: a 256 * %NULL-terminated array containing each key from the table. 257 * 258 * Since: 2.40 259 */ 260 public void*[] getKeysAsArray() 261 { 262 uint length; 263 264 auto p = g_hash_table_get_keys_as_array(gHashTable, &length); 265 266 return p[0 .. length]; 267 } 268 269 /** 270 * Retrieves every value inside @hash_table. The returned data 271 * is valid until @hash_table is modified. 272 * 273 * This iterates over every entry in the hash table to build its return value. 274 * To iterate over the entries in a #GHashTable more efficiently, use a 275 * #GHashTableIter. 276 * 277 * Returns: a #GList containing all the values 278 * inside the hash table. The content of the list is owned by the 279 * hash table and should not be modified or freed. Use g_list_free() 280 * when done using the list. 281 * 282 * Since: 2.14 283 */ 284 public ListG getValues() 285 { 286 auto p = g_hash_table_get_values(gHashTable); 287 288 if(p is null) 289 { 290 return null; 291 } 292 293 return new ListG(cast(GList*) p); 294 } 295 296 /** 297 * Inserts a new key and value into a #GHashTable. 298 * 299 * If the key already exists in the #GHashTable its current 300 * value is replaced with the new value. If you supplied a 301 * @value_destroy_func when creating the #GHashTable, the old 302 * value is freed using that function. If you supplied a 303 * @key_destroy_func when creating the #GHashTable, the passed 304 * key is freed using that function. 305 * 306 * Params: 307 * key = a key to insert 308 * value = the value to associate with the key 309 * 310 * Returns: %TRUE if the key did not exist yet 311 */ 312 public bool insert(void* key, void* value) 313 { 314 return g_hash_table_insert(gHashTable, key, value) != 0; 315 } 316 317 /** 318 * Looks up a key in a #GHashTable. Note that this function cannot 319 * distinguish between a key that is not present and one which is present 320 * and has the value %NULL. If you need this distinction, use 321 * g_hash_table_lookup_extended(). 322 * 323 * Params: 324 * key = the key to look up 325 * 326 * Returns: the associated value, or %NULL if the key is not found 327 */ 328 public void* lookup(void* key) 329 { 330 return g_hash_table_lookup(gHashTable, key); 331 } 332 333 /** 334 * Looks up a key in the #GHashTable, returning the original key and the 335 * associated value and a #gboolean which is %TRUE if the key was found. This 336 * is useful if you need to free the memory allocated for the original key, 337 * for example before calling g_hash_table_remove(). 338 * 339 * You can actually pass %NULL for @lookup_key to test 340 * whether the %NULL key exists, provided the hash and equal functions 341 * of @hash_table are %NULL-safe. 342 * 343 * Params: 344 * lookupKey = the key to look up 345 * origKey = return location for the original key 346 * value = return location for the value associated 347 * with the key 348 * 349 * Returns: %TRUE if the key was found in the #GHashTable 350 */ 351 public bool lookupExtended(void* lookupKey, out void* origKey, out void* value) 352 { 353 return g_hash_table_lookup_extended(gHashTable, lookupKey, &origKey, &value) != 0; 354 } 355 356 /** 357 * Creates a new #GHashTable with a reference count of 1. 358 * 359 * Hash values returned by @hash_func are used to determine where keys 360 * are stored within the #GHashTable data structure. The g_direct_hash(), 361 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash() 362 * functions are provided for some common types of keys. 363 * If @hash_func is %NULL, g_direct_hash() is used. 364 * 365 * @key_equal_func is used when looking up keys in the #GHashTable. 366 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal() 367 * and g_str_equal() functions are provided for the most common types 368 * of keys. If @key_equal_func is %NULL, keys are compared directly in 369 * a similar fashion to g_direct_equal(), but without the overhead of 370 * a function call. @key_equal_func is called with the key from the hash table 371 * as its first parameter, and the user-provided key to check against as 372 * its second. 373 * 374 * Params: 375 * hashFunc = a function to create a hash value from a key 376 * keyEqualFunc = a function to check two keys for equality 377 * 378 * Returns: a new #GHashTable 379 * 380 * Throws: ConstructionException GTK+ fails to create the object. 381 */ 382 public this(GHashFunc hashFunc, GEqualFunc keyEqualFunc) 383 { 384 auto p = g_hash_table_new(hashFunc, keyEqualFunc); 385 386 if(p is null) 387 { 388 throw new ConstructionException("null returned by new"); 389 } 390 391 this(cast(GHashTable*) p); 392 } 393 394 /** 395 * Creates a new #GHashTable like g_hash_table_new() with a reference 396 * count of 1 and allows to specify functions to free the memory 397 * allocated for the key and value that get called when removing the 398 * entry from the #GHashTable. 399 * 400 * Since version 2.42 it is permissible for destroy notify functions to 401 * recursively remove further items from the hash table. This is only 402 * permissible if the application still holds a reference to the hash table. 403 * This means that you may need to ensure that the hash table is empty by 404 * calling g_hash_table_remove_all() before releasing the last reference using 405 * g_hash_table_unref(). 406 * 407 * Params: 408 * hashFunc = a function to create a hash value from a key 409 * keyEqualFunc = a function to check two keys for equality 410 * keyDestroyFunc = a function to free the memory allocated for the key 411 * used when removing the entry from the #GHashTable, or %NULL 412 * if you don't want to supply such a function. 413 * valueDestroyFunc = a function to free the memory allocated for the 414 * value used when removing the entry from the #GHashTable, or %NULL 415 * if you don't want to supply such a function. 416 * 417 * Returns: a new #GHashTable 418 * 419 * Throws: ConstructionException GTK+ fails to create the object. 420 */ 421 public this(GHashFunc hashFunc, GEqualFunc keyEqualFunc, GDestroyNotify keyDestroyFunc, GDestroyNotify valueDestroyFunc) 422 { 423 auto p = g_hash_table_new_full(hashFunc, keyEqualFunc, keyDestroyFunc, valueDestroyFunc); 424 425 if(p is null) 426 { 427 throw new ConstructionException("null returned by new_full"); 428 } 429 430 this(cast(GHashTable*) p); 431 } 432 433 /** 434 * Atomically increments the reference count of @hash_table by one. 435 * This function is MT-safe and may be called from any thread. 436 * 437 * Returns: the passed in #GHashTable 438 * 439 * Since: 2.10 440 */ 441 public HashTable doref() 442 { 443 auto p = g_hash_table_ref(gHashTable); 444 445 if(p is null) 446 { 447 return null; 448 } 449 450 return new HashTable(cast(GHashTable*) p); 451 } 452 453 /** 454 * Removes a key and its associated value from a #GHashTable. 455 * 456 * If the #GHashTable was created using g_hash_table_new_full(), the 457 * key and value are freed using the supplied destroy functions, otherwise 458 * you have to make sure that any dynamically allocated values are freed 459 * yourself. 460 * 461 * Params: 462 * key = the key to remove 463 * 464 * Returns: %TRUE if the key was found and removed from the #GHashTable 465 */ 466 public bool remove(void* key) 467 { 468 return g_hash_table_remove(gHashTable, key) != 0; 469 } 470 471 /** 472 * Removes all keys and their associated values from a #GHashTable. 473 * 474 * If the #GHashTable was created using g_hash_table_new_full(), 475 * the keys and values are freed using the supplied destroy functions, 476 * otherwise you have to make sure that any dynamically allocated 477 * values are freed yourself. 478 * 479 * Since: 2.12 480 */ 481 public void removeAll() 482 { 483 g_hash_table_remove_all(gHashTable); 484 } 485 486 /** 487 * Inserts a new key and value into a #GHashTable similar to 488 * g_hash_table_insert(). The difference is that if the key 489 * already exists in the #GHashTable, it gets replaced by the 490 * new key. If you supplied a @value_destroy_func when creating 491 * the #GHashTable, the old value is freed using that function. 492 * If you supplied a @key_destroy_func when creating the 493 * #GHashTable, the old key is freed using that function. 494 * 495 * Params: 496 * key = a key to insert 497 * value = the value to associate with the key 498 * 499 * Returns: %TRUE if the key did not exist yet 500 */ 501 public bool replace(void* key, void* value) 502 { 503 return g_hash_table_replace(gHashTable, key, value) != 0; 504 } 505 506 /** 507 * Returns the number of elements contained in the #GHashTable. 508 * 509 * Returns: the number of key/value pairs in the #GHashTable. 510 */ 511 public uint size() 512 { 513 return g_hash_table_size(gHashTable); 514 } 515 516 /** 517 * Removes a key and its associated value from a #GHashTable without 518 * calling the key and value destroy functions. 519 * 520 * Params: 521 * key = the key to remove 522 * 523 * Returns: %TRUE if the key was found and removed from the #GHashTable 524 */ 525 public bool steal(void* key) 526 { 527 return g_hash_table_steal(gHashTable, key) != 0; 528 } 529 530 /** 531 * Removes all keys and their associated values from a #GHashTable 532 * without calling the key and value destroy functions. 533 * 534 * Since: 2.12 535 */ 536 public void stealAll() 537 { 538 g_hash_table_steal_all(gHashTable); 539 } 540 541 /** 542 * Atomically decrements the reference count of @hash_table by one. 543 * If the reference count drops to 0, all keys and values will be 544 * destroyed, and all memory allocated by the hash table is released. 545 * This function is MT-safe and may be called from any thread. 546 * 547 * Since: 2.10 548 */ 549 public void unref() 550 { 551 g_hash_table_unref(gHashTable); 552 } 553 554 /** 555 * Compares two #gpointer arguments and returns %TRUE if they are equal. 556 * It can be passed to g_hash_table_new() as the @key_equal_func 557 * parameter, when using opaque pointers compared by pointer value as 558 * keys in a #GHashTable. 559 * 560 * This equality function is also appropriate for keys that are integers 561 * stored in pointers, such as `GINT_TO_POINTER (n)`. 562 * 563 * Params: 564 * v1 = a key 565 * v2 = a key to compare with @v1 566 * 567 * Returns: %TRUE if the two keys match. 568 */ 569 public static bool directEqual(void* v1, void* v2) 570 { 571 return g_direct_equal(v1, v2) != 0; 572 } 573 574 /** 575 * Converts a gpointer to a hash value. 576 * It can be passed to g_hash_table_new() as the @hash_func parameter, 577 * when using opaque pointers compared by pointer value as keys in a 578 * #GHashTable. 579 * 580 * This hash function is also appropriate for keys that are integers 581 * stored in pointers, such as `GINT_TO_POINTER (n)`. 582 * 583 * Params: 584 * v = a #gpointer key 585 * 586 * Returns: a hash value corresponding to the key. 587 */ 588 public static uint directHash(void* v) 589 { 590 return g_direct_hash(v); 591 } 592 593 /** 594 * Compares the two #gdouble values being pointed to and returns 595 * %TRUE if they are equal. 596 * It can be passed to g_hash_table_new() as the @key_equal_func 597 * parameter, when using non-%NULL pointers to doubles as keys in a 598 * #GHashTable. 599 * 600 * Params: 601 * v1 = a pointer to a #gdouble key 602 * v2 = a pointer to a #gdouble key to compare with @v1 603 * 604 * Returns: %TRUE if the two keys match. 605 * 606 * Since: 2.22 607 */ 608 public static bool doubleEqual(void* v1, void* v2) 609 { 610 return g_double_equal(v1, v2) != 0; 611 } 612 613 /** 614 * Converts a pointer to a #gdouble to a hash value. 615 * It can be passed to g_hash_table_new() as the @hash_func parameter, 616 * It can be passed to g_hash_table_new() as the @hash_func parameter, 617 * when using non-%NULL pointers to doubles as keys in a #GHashTable. 618 * 619 * Params: 620 * v = a pointer to a #gdouble key 621 * 622 * Returns: a hash value corresponding to the key. 623 * 624 * Since: 2.22 625 */ 626 public static uint doubleHash(void* v) 627 { 628 return g_double_hash(v); 629 } 630 631 /** 632 * Compares the two #gint64 values being pointed to and returns 633 * %TRUE if they are equal. 634 * It can be passed to g_hash_table_new() as the @key_equal_func 635 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a 636 * #GHashTable. 637 * 638 * Params: 639 * v1 = a pointer to a #gint64 key 640 * v2 = a pointer to a #gint64 key to compare with @v1 641 * 642 * Returns: %TRUE if the two keys match. 643 * 644 * Since: 2.22 645 */ 646 public static bool int64Equal(void* v1, void* v2) 647 { 648 return g_int64_equal(v1, v2) != 0; 649 } 650 651 /** 652 * Converts a pointer to a #gint64 to a hash value. 653 * 654 * It can be passed to g_hash_table_new() as the @hash_func parameter, 655 * when using non-%NULL pointers to 64-bit integer values as keys in a 656 * #GHashTable. 657 * 658 * Params: 659 * v = a pointer to a #gint64 key 660 * 661 * Returns: a hash value corresponding to the key. 662 * 663 * Since: 2.22 664 */ 665 public static uint int64Hash(void* v) 666 { 667 return g_int64_hash(v); 668 } 669 670 /** 671 * Compares the two #gint values being pointed to and returns 672 * %TRUE if they are equal. 673 * It can be passed to g_hash_table_new() as the @key_equal_func 674 * parameter, when using non-%NULL pointers to integers as keys in a 675 * #GHashTable. 676 * 677 * Note that this function acts on pointers to #gint, not on #gint 678 * directly: if your hash table's keys are of the form 679 * `GINT_TO_POINTER (n)`, use g_direct_equal() instead. 680 * 681 * Params: 682 * v1 = a pointer to a #gint key 683 * v2 = a pointer to a #gint key to compare with @v1 684 * 685 * Returns: %TRUE if the two keys match. 686 */ 687 public static bool intEqual(void* v1, void* v2) 688 { 689 return g_int_equal(v1, v2) != 0; 690 } 691 692 /** 693 * Converts a pointer to a #gint to a hash value. 694 * It can be passed to g_hash_table_new() as the @hash_func parameter, 695 * when using non-%NULL pointers to integer values as keys in a #GHashTable. 696 * 697 * Note that this function acts on pointers to #gint, not on #gint 698 * directly: if your hash table's keys are of the form 699 * `GINT_TO_POINTER (n)`, use g_direct_hash() instead. 700 * 701 * Params: 702 * v = a pointer to a #gint key 703 * 704 * Returns: a hash value corresponding to the key. 705 */ 706 public static uint intHash(void* v) 707 { 708 return g_int_hash(v); 709 } 710 711 /** 712 * Compares two strings for byte-by-byte equality and returns %TRUE 713 * if they are equal. It can be passed to g_hash_table_new() as the 714 * @key_equal_func parameter, when using non-%NULL strings as keys in a 715 * #GHashTable. 716 * 717 * Note that this function is primarily meant as a hash table comparison 718 * function. For a general-purpose, %NULL-safe string comparison function, 719 * see g_strcmp0(). 720 * 721 * Params: 722 * v1 = a key 723 * v2 = a key to compare with @v1 724 * 725 * Returns: %TRUE if the two keys match 726 */ 727 public static bool strEqual(void* v1, void* v2) 728 { 729 return g_str_equal(v1, v2) != 0; 730 } 731 732 /** 733 * Converts a string to a hash value. 734 * 735 * This function implements the widely used "djb" hash apparently 736 * posted by Daniel Bernstein to comp.lang.c some time ago. The 32 737 * bit unsigned hash value starts at 5381 and for each byte 'c' in 738 * the string, is updated: `hash = hash * 33 + c`. This function 739 * uses the signed value of each byte. 740 * 741 * It can be passed to g_hash_table_new() as the @hash_func parameter, 742 * when using non-%NULL strings as keys in a #GHashTable. 743 * 744 * Note that this function may not be a perfect fit for all use cases. 745 * For example, it produces some hash collisions with strings as short 746 * as 2. 747 * 748 * Params: 749 * v = a string key 750 * 751 * Returns: a hash value corresponding to the key 752 */ 753 public static uint strHash(void* v) 754 { 755 return g_str_hash(v); 756 } 757 }