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