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