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