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 protected bool ownedRef; 43 44 /** Get the main Gtk struct */ 45 public GHashTable* getHashTableStruct() 46 { 47 return gHashTable; 48 } 49 50 /** the main Gtk struct as a void* */ 51 protected void* getStruct() 52 { 53 return cast(void*)gHashTable; 54 } 55 56 /** 57 * Sets our main struct and passes it to the parent class. 58 */ 59 public this (GHashTable* gHashTable, bool ownedRef = false) 60 { 61 this.gHashTable = gHashTable; 62 this.ownedRef = ownedRef; 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 * Return: a 253 * %NULL-terminated array containing each key from the table. 254 * 255 * Since: 2.40 256 */ 257 public void*[] getKeysAsArray() 258 { 259 uint length; 260 261 auto p = g_hash_table_get_keys_as_array(gHashTable, &length); 262 263 return p[0 .. length]; 264 } 265 266 /** 267 * Retrieves every value inside @hash_table. The returned data 268 * is valid until @hash_table is modified. 269 * 270 * This iterates over every entry in the hash table to build its return value. 271 * To iterate over the entries in a #GHashTable more efficiently, use a 272 * #GHashTableIter. 273 * 274 * Return: a #GList containing all the values inside the hash 275 * table. The content of the list is owned by the hash table and 276 * should not be modified or freed. Use g_list_free() when done 277 * using the list. 278 * 279 * Since: 2.14 280 */ 281 public ListG getValues() 282 { 283 auto p = g_hash_table_get_values(gHashTable); 284 285 if(p is null) 286 { 287 return null; 288 } 289 290 return new ListG(cast(GList*) p); 291 } 292 293 /** 294 * Inserts a new key and value into a #GHashTable. 295 * 296 * If the key already exists in the #GHashTable its current 297 * value is replaced with the new value. If you supplied a 298 * @value_destroy_func when creating the #GHashTable, the old 299 * value is freed using that function. If you supplied a 300 * @key_destroy_func when creating the #GHashTable, the passed 301 * key is freed using that function. 302 * 303 * Params: 304 * key = a key to insert 305 * value = the value to associate with the key 306 * 307 * Return: %TRUE if the key did not exist yet 308 */ 309 public bool insert(void* key, void* value) 310 { 311 return g_hash_table_insert(gHashTable, key, value) != 0; 312 } 313 314 /** 315 * Looks up a key in a #GHashTable. Note that this function cannot 316 * distinguish between a key that is not present and one which is present 317 * and has the value %NULL. If you need this distinction, use 318 * g_hash_table_lookup_extended(). 319 * 320 * Params: 321 * key = the key to look up 322 * 323 * Return: the associated value, or %NULL if the key is not found 324 */ 325 public void* lookup(void* key) 326 { 327 return g_hash_table_lookup(gHashTable, key); 328 } 329 330 /** 331 * Looks up a key in the #GHashTable, returning the original key and the 332 * associated value and a #gboolean which is %TRUE if the key was found. This 333 * is useful if you need to free the memory allocated for the original key, 334 * for example before calling g_hash_table_remove(). 335 * 336 * You can actually pass %NULL for @lookup_key to test 337 * whether the %NULL key exists, provided the hash and equal functions 338 * of @hash_table are %NULL-safe. 339 * 340 * Params: 341 * lookupKey = the key to look up 342 * origKey = return location for the original key, or %NULL 343 * value = return location for the value associated with the key, or %NULL 344 * 345 * Return: %TRUE if the key was found in the #GHashTable 346 */ 347 public bool lookupExtended(void* lookupKey, void** origKey, void** value) 348 { 349 return g_hash_table_lookup_extended(gHashTable, lookupKey, origKey, value) != 0; 350 } 351 352 /** 353 * Creates a new #GHashTable with a reference count of 1. 354 * 355 * Hash values returned by @hash_func are used to determine where keys 356 * are stored within the #GHashTable data structure. The g_direct_hash(), 357 * g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash() 358 * functions are provided for some common types of keys. 359 * If @hash_func is %NULL, g_direct_hash() is used. 360 * 361 * @key_equal_func is used when looking up keys in the #GHashTable. 362 * The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal() 363 * and g_str_equal() functions are provided for the most common types 364 * of keys. If @key_equal_func is %NULL, keys are compared directly in 365 * a similar fashion to g_direct_equal(), but without the overhead of 366 * a function call. 367 * 368 * Params: 369 * hashFunc = a function to create a hash value from a key 370 * keyEqualFunc = a function to check two keys for equality 371 * 372 * Return: a new #GHashTable 373 * 374 * Throws: ConstructionException GTK+ fails to create the object. 375 */ 376 public this(GHashFunc hashFunc, GEqualFunc keyEqualFunc) 377 { 378 auto p = g_hash_table_new(hashFunc, keyEqualFunc); 379 380 if(p is null) 381 { 382 throw new ConstructionException("null returned by new"); 383 } 384 385 this(cast(GHashTable*) p); 386 } 387 388 /** 389 * Creates a new #GHashTable like g_hash_table_new() with a reference 390 * count of 1 and allows to specify functions to free the memory 391 * allocated for the key and value that get called when removing the 392 * entry from the #GHashTable. 393 * 394 * Since version 2.42 it is permissible for destroy notify functions to 395 * recursively remove further items from the hash table. This is only 396 * permissible if the application still holds a reference to the hash table. 397 * This means that you may need to ensure that the hash table is empty by 398 * calling g_hash_table_remove_all before releasing the last reference using 399 * g_hash_table_unref(). 400 * 401 * Params: 402 * hashFunc = a function to create a hash value from a key 403 * keyEqualFunc = a function to check two keys for equality 404 * keyDestroyFunc = a function to free the memory allocated for the key 405 * used when removing the entry from the #GHashTable, or %NULL 406 * if you don't want to supply such a function. 407 * valueDestroyFunc = a function to free the memory allocated for the 408 * value used when removing the entry from the #GHashTable, or %NULL 409 * if you don't want to supply such a function. 410 * 411 * Return: a new #GHashTable 412 * 413 * Throws: ConstructionException GTK+ fails to create the object. 414 */ 415 public this(GHashFunc hashFunc, GEqualFunc keyEqualFunc, GDestroyNotify keyDestroyFunc, GDestroyNotify valueDestroyFunc) 416 { 417 auto p = g_hash_table_new_full(hashFunc, keyEqualFunc, keyDestroyFunc, valueDestroyFunc); 418 419 if(p is null) 420 { 421 throw new ConstructionException("null returned by new_full"); 422 } 423 424 this(cast(GHashTable*) p); 425 } 426 427 /** 428 * Atomically increments the reference count of @hash_table by one. 429 * This function is MT-safe and may be called from any thread. 430 * 431 * Return: the passed in #GHashTable 432 * 433 * Since: 2.10 434 */ 435 public HashTable doref() 436 { 437 auto p = g_hash_table_ref(gHashTable); 438 439 if(p is null) 440 { 441 return null; 442 } 443 444 return new HashTable(cast(GHashTable*) p); 445 } 446 447 /** 448 * Removes a key and its associated value from a #GHashTable. 449 * 450 * If the #GHashTable was created using g_hash_table_new_full(), the 451 * key and value are freed using the supplied destroy functions, otherwise 452 * you have to make sure that any dynamically allocated values are freed 453 * yourself. 454 * 455 * Params: 456 * key = the key to remove 457 * 458 * Return: %TRUE if the key was found and removed from the #GHashTable 459 */ 460 public bool remove(void* key) 461 { 462 return g_hash_table_remove(gHashTable, key) != 0; 463 } 464 465 /** 466 * Removes all keys and their associated values from a #GHashTable. 467 * 468 * If the #GHashTable was created using g_hash_table_new_full(), 469 * the keys and values are freed using the supplied destroy functions, 470 * otherwise you have to make sure that any dynamically allocated 471 * values are freed yourself. 472 * 473 * Since: 2.12 474 */ 475 public void removeAll() 476 { 477 g_hash_table_remove_all(gHashTable); 478 } 479 480 /** 481 * Inserts a new key and value into a #GHashTable similar to 482 * g_hash_table_insert(). The difference is that if the key 483 * already exists in the #GHashTable, it gets replaced by the 484 * new key. If you supplied a @value_destroy_func when creating 485 * the #GHashTable, the old value is freed using that function. 486 * If you supplied a @key_destroy_func when creating the 487 * #GHashTable, the old key is freed using that function. 488 * 489 * Params: 490 * key = a key to insert 491 * value = the value to associate with the key 492 * 493 * Return: %TRUE if the key did not exist yet 494 */ 495 public bool replace(void* key, void* value) 496 { 497 return g_hash_table_replace(gHashTable, key, value) != 0; 498 } 499 500 /** 501 * Returns the number of elements contained in the #GHashTable. 502 * 503 * Return: the number of key/value pairs in the #GHashTable. 504 */ 505 public uint size() 506 { 507 return g_hash_table_size(gHashTable); 508 } 509 510 /** 511 * Removes a key and its associated value from a #GHashTable without 512 * calling the key and value destroy functions. 513 * 514 * Params: 515 * key = the key to remove 516 * 517 * Return: %TRUE if the key was found and removed from the #GHashTable 518 */ 519 public bool steal(void* key) 520 { 521 return g_hash_table_steal(gHashTable, key) != 0; 522 } 523 524 /** 525 * Removes all keys and their associated values from a #GHashTable 526 * without calling the key and value destroy functions. 527 * 528 * Since: 2.12 529 */ 530 public void stealAll() 531 { 532 g_hash_table_steal_all(gHashTable); 533 } 534 535 /** 536 * Atomically decrements the reference count of @hash_table by one. 537 * If the reference count drops to 0, all keys and values will be 538 * destroyed, and all memory allocated by the hash table is released. 539 * This function is MT-safe and may be called from any thread. 540 * 541 * Since: 2.10 542 */ 543 public void unref() 544 { 545 g_hash_table_unref(gHashTable); 546 } 547 548 /** 549 * Compares two #gpointer arguments and returns %TRUE if they are equal. 550 * It can be passed to g_hash_table_new() as the @key_equal_func 551 * parameter, when using opaque pointers compared by pointer value as 552 * keys in a #GHashTable. 553 * 554 * This equality function is also appropriate for keys that are integers 555 * stored in pointers, such as `GINT_TO_POINTER (n)`. 556 * 557 * Params: 558 * v1 = a key 559 * v2 = a key to compare with @v1 560 * 561 * Return: %TRUE if the two keys match. 562 */ 563 public static bool directEqual(void* v1, void* v2) 564 { 565 return g_direct_equal(v1, v2) != 0; 566 } 567 568 /** 569 * Converts a gpointer to a hash value. 570 * It can be passed to g_hash_table_new() as the @hash_func parameter, 571 * when using opaque pointers compared by pointer value as keys in a 572 * #GHashTable. 573 * 574 * This hash function is also appropriate for keys that are integers 575 * stored in pointers, such as `GINT_TO_POINTER (n)`. 576 * 577 * Params: 578 * v = a #gpointer key 579 * 580 * Return: a hash value corresponding to the key. 581 */ 582 public static uint directHash(void* v) 583 { 584 return g_direct_hash(v); 585 } 586 587 /** 588 * Compares the two #gdouble values being pointed to and returns 589 * %TRUE if they are equal. 590 * It can be passed to g_hash_table_new() as the @key_equal_func 591 * parameter, when using non-%NULL pointers to doubles as keys in a 592 * #GHashTable. 593 * 594 * Params: 595 * v1 = a pointer to a #gdouble key 596 * v2 = a pointer to a #gdouble key to compare with @v1 597 * 598 * Return: %TRUE if the two keys match. 599 * 600 * Since: 2.22 601 */ 602 public static bool doubleEqual(void* v1, void* v2) 603 { 604 return g_double_equal(v1, v2) != 0; 605 } 606 607 /** 608 * Converts a pointer to a #gdouble to a hash value. 609 * It can be passed to g_hash_table_new() as the @hash_func parameter, 610 * It can be passed to g_hash_table_new() as the @hash_func parameter, 611 * when using non-%NULL pointers to doubles as keys in a #GHashTable. 612 * 613 * Params: 614 * v = a pointer to a #gdouble key 615 * 616 * Return: a hash value corresponding to the key. 617 * 618 * Since: 2.22 619 */ 620 public static uint doubleHash(void* v) 621 { 622 return g_double_hash(v); 623 } 624 625 /** 626 * Compares the two #gint64 values being pointed to and returns 627 * %TRUE if they are equal. 628 * It can be passed to g_hash_table_new() as the @key_equal_func 629 * parameter, when using non-%NULL pointers to 64-bit integers as keys in a 630 * #GHashTable. 631 * 632 * Params: 633 * v1 = a pointer to a #gint64 key 634 * v2 = a pointer to a #gint64 key to compare with @v1 635 * 636 * Return: %TRUE if the two keys match. 637 * 638 * Since: 2.22 639 */ 640 public static bool int64Equal(void* v1, void* v2) 641 { 642 return g_int64_equal(v1, v2) != 0; 643 } 644 645 /** 646 * Converts a pointer to a #gint64 to a hash value. 647 * 648 * It can be passed to g_hash_table_new() as the @hash_func parameter, 649 * when using non-%NULL pointers to 64-bit integer values as keys in a 650 * #GHashTable. 651 * 652 * Params: 653 * v = a pointer to a #gint64 key 654 * 655 * Return: a hash value corresponding to the key. 656 * 657 * Since: 2.22 658 */ 659 public static uint int64Hash(void* v) 660 { 661 return g_int64_hash(v); 662 } 663 664 /** 665 * Compares the two #gint values being pointed to and returns 666 * %TRUE if they are equal. 667 * It can be passed to g_hash_table_new() as the @key_equal_func 668 * parameter, when using non-%NULL pointers to integers as keys in a 669 * #GHashTable. 670 * 671 * Note that this function acts on pointers to #gint, not on #gint 672 * directly: if your hash table's keys are of the form 673 * `GINT_TO_POINTER (n)`, use g_direct_equal() instead. 674 * 675 * Params: 676 * v1 = a pointer to a #gint key 677 * v2 = a pointer to a #gint key to compare with @v1 678 * 679 * Return: %TRUE if the two keys match. 680 */ 681 public static bool intEqual(void* v1, void* v2) 682 { 683 return g_int_equal(v1, v2) != 0; 684 } 685 686 /** 687 * Converts a pointer to a #gint to a hash value. 688 * It can be passed to g_hash_table_new() as the @hash_func parameter, 689 * when using non-%NULL pointers to integer values as keys in a #GHashTable. 690 * 691 * Note that this function acts on pointers to #gint, not on #gint 692 * directly: if your hash table's keys are of the form 693 * `GINT_TO_POINTER (n)`, use g_direct_hash() instead. 694 * 695 * Params: 696 * v = a pointer to a #gint key 697 * 698 * Return: a hash value corresponding to the key. 699 */ 700 public static uint intHash(void* v) 701 { 702 return g_int_hash(v); 703 } 704 705 /** 706 * Compares two strings for byte-by-byte equality and returns %TRUE 707 * if they are equal. It can be passed to g_hash_table_new() as the 708 * @key_equal_func parameter, when using non-%NULL strings as keys in a 709 * #GHashTable. 710 * 711 * Note that this function is primarily meant as a hash table comparison 712 * function. For a general-purpose, %NULL-safe string comparison function, 713 * see g_strcmp0(). 714 * 715 * Params: 716 * v1 = a key 717 * v2 = a key to compare with @v1 718 * 719 * Return: %TRUE if the two keys match 720 */ 721 public static bool strEqual(void* v1, void* v2) 722 { 723 return g_str_equal(v1, v2) != 0; 724 } 725 726 /** 727 * Converts a string to a hash value. 728 * 729 * This function implements the widely used "djb" hash apparently 730 * posted by Daniel Bernstein to comp.lang.c some time ago. The 32 731 * bit unsigned hash value starts at 5381 and for each byte 'c' in 732 * the string, is updated: `hash = hash * 33 + c`. This function 733 * uses the signed value of each byte. 734 * 735 * It can be passed to g_hash_table_new() as the @hash_func parameter, 736 * when using non-%NULL strings as keys in a #GHashTable. 737 * 738 * Note that this function may not be a perfect fit for all use cases. 739 * For example, it produces some hash collisions with strings as short 740 * as 2. 741 * 742 * Params: 743 * v = a string key 744 * 745 * Return: a hash value corresponding to the key 746 */ 747 public static uint strHash(void* v) 748 { 749 return g_str_hash(v); 750 } 751 }