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