1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
// MIT License // // Copyright (c) 2020 Gregory Meyer // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation files // (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, // and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. //! A lockfree concurrent hash map implemented with open addressing and linear //! probing. pub(crate) mod bucket; pub(crate) mod bucket_array_ref; use bucket::BucketArray; use bucket_array_ref::BucketArrayRef; use std::{ borrow::Borrow, hash::{BuildHasher, Hash}, sync::atomic::{self, AtomicUsize, Ordering}, }; use ahash::RandomState; use crossbeam_epoch::{self, Atomic}; /// Default hasher for `HashMap`. /// /// This is currently [aHash], a hashing algorithm designed around acceleration /// by the [AES-NI] instruction set on x86 processors. aHash is not /// cryptographically secure, but is fast and resistant to DoS attacks. Compared /// to [Fx Hash], the previous default hasher, aHash is slower at hashing /// integers, faster at hashing strings, and provides DoS attack resistance. /// /// [aHash]: https://docs.rs/ahash /// [AES-NI]: https://en.wikipedia.org/wiki/AES_instruction_set /// [Fx Hash]: https://docs.rs/fxhash pub type DefaultHashBuilder = RandomState; /// A lockfree concurrent hash map implemented with open addressing and linear /// probing. /// /// The default hashing algorithm is [aHash], a hashing algorithm that is /// accelerated by the [AES-NI] instruction set on x86 proessors. aHash provides /// some resistance to DoS attacks, but will not provide the same level of /// resistance as something like [`RandomState`]. /// /// The hashing algorithm to be used can be chosen on a per-`HashMap` basis /// using the [`with_hasher`] and [`with_capacity_and_hasher`] methods. /// /// Key types must implement [`Hash`] and [`Eq`]. Any operations that return a /// key or value require the return types to implement [`Clone`], as elements /// may be in use by other threads and as such cannot be moved from. /// /// `HashMap` is inspired by Jeff Phreshing's hash tables implemented in /// [Junction], described in [this blog post]. In short, `HashMap` supports /// fully concurrent lookups, insertions, removals, and updates. /// /// [aHash]: https://docs.rs/ahash /// [AES-NI]: https://en.wikipedia.org/wiki/AES_instruction_set /// [`RandomState`]: https://doc.rust-lang.org/std/collections/hash_map/struct.RandomState.html /// [`with_hasher`]: #method.with_hasher /// [`with_capacity_and_hasher`]: #method.with_capacity_and_hasher /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html /// [Junction]: https://github.com/preshing/junction /// [this blog post]: https://preshing.com/20160222/a-resizable-concurrent-map/ #[derive(Default)] pub struct HashMap<K, V, S = DefaultHashBuilder> { bucket_array: Atomic<bucket::BucketArray<K, V>>, build_hasher: S, len: AtomicUsize, } impl<K, V> HashMap<K, V, DefaultHashBuilder> { /// Creates an empty `HashMap`. /// /// The hash map is created with a capacity of 0 and will not allocate any /// space for elements until the first insertion. pub fn new() -> HashMap<K, V, DefaultHashBuilder> { HashMap::with_capacity_and_hasher(0, DefaultHashBuilder::default()) } /// Creates an empty `HashMap` with space for at least `capacity` elements /// without reallocating. /// /// If `capacity == 0`, no allocations will occur. pub fn with_capacity(capacity: usize) -> HashMap<K, V, DefaultHashBuilder> { HashMap::with_capacity_and_hasher(capacity, DefaultHashBuilder::default()) } } impl<K, V, S> HashMap<K, V, S> { /// Creates an empty `HashMap` that will use `build_hasher` to hash keys. /// /// The created map will have a capacity of 0 and as such will not have any /// space for elements allocated until the first insertion. pub fn with_hasher(build_hasher: S) -> HashMap<K, V, S> { HashMap::with_capacity_and_hasher(0, build_hasher) } /// Creates an empty `HashMap` that will hold at least `capacity` elements /// without reallocating and that uses `build_hasher` to hash keys. /// /// If `capacity == 0`, no allocations will occur. pub fn with_capacity_and_hasher(capacity: usize, build_hasher: S) -> HashMap<K, V, S> { let bucket_array = if capacity == 0 { Atomic::null() } else { Atomic::new(BucketArray::with_length( 0, (capacity * 2).next_power_of_two(), )) }; Self { bucket_array, build_hasher, len: AtomicUsize::new(0), } } /// Returns the number of elements that are confirmed to have been inserted /// into this map. /// /// Because `HashMap` can be updated concurrently, this function reflects /// the number of insert operations that have returned to the user. /// In-progress insertions are not counted. pub fn len(&self) -> usize { self.len.load(Ordering::Relaxed) } /// Returns true if this `HashMap` contains no confirmed inserted elements. /// /// In-progress insertions into this `HashMap` are not considered. pub fn is_empty(&self) -> bool { self.len() == 0 } /// Returns the number of elements this `HashMap` can hold without /// reallocating a table. /// /// Note that all mutating operations, with the exception of removing /// elements, incur at least one allocation for the associated bucket. /// /// If there are insertion operations in flight, it is possible that a /// new, larger table has already been allocated. pub fn capacity(&self) -> usize { let guard = &crossbeam_epoch::pin(); let bucket_array_ptr = self.bucket_array.load_consume(guard); unsafe { bucket_array_ptr.as_ref() } .map(BucketArray::capacity) .unwrap_or(0) } } impl<K: Hash + Eq, V, S: BuildHasher> HashMap<K, V, S> { /// Returns a copy of the value corresponding to `key`. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. `V` must implement [`Clone`], as the value may /// be deleted at any moment; the best we can do is to clone them while we /// know they exist. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html /// [`get_and`]: #method.get_and #[inline] pub fn get<Q: Hash + Eq + ?Sized>(&self, key: &Q) -> Option<V> where K: Borrow<Q>, V: Clone, { self.get_key_value_and(key, |_, v| v.clone()) } /// Returns a copy of the key and value corresponding to `key`. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. `K` and `V` must implement [`Clone`], as the /// bucket may be concurrently removed at any time; the best we can do is to /// clone them while we know they exist. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html /// [`get_key_value_and`]: #method.get_key_value_and #[inline] pub fn get_key_value<Q: Hash + Eq + ?Sized>(&self, key: &Q) -> Option<(K, V)> where K: Borrow<Q> + Clone, V: Clone, { self.get_key_value_and(key, |k, v| (k.clone(), v.clone())) } /// Invokes `with_value` with a reference to the value corresponding to `key`. /// /// `with_value` will only be invoked if there is a value associated with /// `key` contained within this hash map. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html #[inline] pub fn get_and<Q: Hash + Eq + ?Sized, F: FnOnce(&V) -> T, T>( &self, key: &Q, with_value: F, ) -> Option<T> where K: Borrow<Q>, { self.get_key_value_and(key, move |_, v| with_value(v)) } /// Invokes `with_entry` with a reference to the key and value corresponding /// to `key`. /// /// `with_entry` will only be invoked if there is a value associated with `key` /// contained within this hash map. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html #[inline] pub fn get_key_value_and<Q: Hash + Eq + ?Sized, F: FnOnce(&K, &V) -> T, T>( &self, key: &Q, with_entry: F, ) -> Option<T> where K: Borrow<Q>, { let hash = bucket::hash(&self.build_hasher, &key); self.bucket_array_ref() .get_key_value_and(key, hash, with_entry) } /// Inserts a key-value pair, then returns a copy of the value previously /// associated with `key`. /// /// If the key was not previously present in this hash map, [`None`] is /// returned. /// /// `V` must implement [`Clone`], as other threads may hold references to /// the associated value. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn insert(&self, key: K, value: V) -> Option<V> where V: Clone, { self.insert_entry_and(key, value, |_, v| v.clone()) } /// Inserts a key-value pair, then returns a copy of the previous entry. /// /// If the key was not previously present in this hash map, [`None`] is /// returned. /// /// `K` and `V` must implement [`Clone`], as other threads may hold /// references to the entry. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn insert_entry(&self, key: K, value: V) -> Option<(K, V)> where K: Clone, V: Clone, { self.insert_entry_and(key, value, |k, v| (k.clone(), v.clone())) } /// Inserts a key-value pair, then invokes `with_previous_value` with the /// value previously associated with `key`. /// /// If the key was not previously present in this hash map, [`None`] is /// returned and `with_previous_value` is not invoked. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None #[inline] pub fn insert_and<F: FnOnce(&V) -> T, T>( &self, key: K, value: V, with_previous_value: F, ) -> Option<T> { self.insert_entry_and(key, value, move |_, v| with_previous_value(v)) } /// Inserts a key-value pair, then invokes `with_previous_entry` with the /// previous entry. /// /// If the key was not previously present in this hash map, [`None`] is /// returned and `with_previous_entry` is not invoked. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None #[inline] pub fn insert_entry_and<F: FnOnce(&K, &V) -> T, T>( &self, key: K, value: V, with_previous_entry: F, ) -> Option<T> { let hash = bucket::hash(&self.build_hasher, &key); self.bucket_array_ref() .insert_entry_and(key, hash, value, with_previous_entry) } /// If there is a value associated with `key`, remove and return a copy of /// it. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. `V` must implement [`Clone`], as other /// threads may hold references to the associated value. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn remove<Q: Hash + Eq + ?Sized>(&self, key: &Q) -> Option<V> where K: Borrow<Q>, V: Clone, { self.remove_entry_if_and(key, |_, _| true, |_, v| v.clone()) } /// If there is a value associated with `key`, remove it and return a copy /// of the previous entity. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. `K` and `V` must implement [`Clone`], as other /// threads may hold references to the entry. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn remove_entry<Q: Hash + Eq + ?Sized>(&self, key: &Q) -> Option<(K, V)> where K: Borrow<Q> + Clone, V: Clone, { self.remove_entry_if_and(key, |_, _| true, |k, v| (k.clone(), v.clone())) } /// If there is a value associated with `key`, remove it and return the /// result of invoking `with_previous_value` with that value. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html #[inline] pub fn remove_and<Q: Hash + Eq + ?Sized, F: FnOnce(&V) -> T, T>( &self, key: &Q, with_previous_value: F, ) -> Option<T> where K: Borrow<Q>, { self.remove_entry_if_and(key, |_, _| true, move |_, v| with_previous_value(v)) } /// If there is a value associated with `key`, remove it and return the /// result of invoking `with_previous_entry` with that entry. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html #[inline] pub fn remove_entry_and<Q: Hash + Eq + ?Sized, F: FnOnce(&K, &V) -> T, T>( &self, key: &Q, with_previous_entry: F, ) -> Option<T> where K: Borrow<Q>, { self.remove_entry_if_and(key, |_, _| true, with_previous_entry) } /// If there is a value associated with `key` and `condition` returns true /// when invoked with the current entry, remove and return a copy of its /// value. /// /// `condition` may be invoked one or more times, even if no entry was /// removed. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. `K` and `V` must implement [`Clone`], as other /// threads may hold references to the entry. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn remove_if<Q: Hash + Eq + ?Sized, F: FnMut(&K, &V) -> bool>( &self, key: &Q, condition: F, ) -> Option<V> where K: Borrow<Q>, V: Clone, { self.remove_entry_if_and(key, condition, move |_, v| v.clone()) } /// If there is a value associated with `key` and `condition` returns true /// when invoked with the current entry, remove and return a copy of it. /// /// `condition` may be invoked one or more times, even if no entry was /// removed. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. `K` and `V` must implement [`Clone`], as other /// threads may hold references to the entry. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn remove_entry_if<Q: Hash + Eq + ?Sized, F: FnMut(&K, &V) -> bool>( &self, key: &Q, condition: F, ) -> Option<(K, V)> where K: Clone + Borrow<Q>, V: Clone, { self.remove_entry_if_and(key, condition, move |k, v| (k.clone(), v.clone())) } /// If there is a value associated with `key` and `condition` returns true /// when invoked with the current entry, remove it and return the result of /// invoking `with_previous_value` with its value. /// /// `condition` may be invoked one or more times, even if no entry was /// removed. If `condition` failed or there was no value associated with /// `key`, `with_previous_entry` is not invoked and [`None`] is returned. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None #[inline] pub fn remove_if_and<Q: Hash + Eq + ?Sized, F: FnMut(&K, &V) -> bool, G: FnOnce(&V) -> T, T>( &self, key: &Q, condition: F, with_previous_value: G, ) -> Option<T> where K: Borrow<Q>, { self.remove_entry_if_and(key, condition, move |_, v| with_previous_value(v)) } /// If there is a value associated with `key` and `condition` returns true /// when invoked with the current entry, remove it and return the result of /// invoking `with_previous_entry` with it. /// /// `condition` may be invoked one or more times, even if no entry was /// removed. If `condition` failed or there was no value associated with /// `key`, `with_previous_entry` is not invoked and [`None`] is returned. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. /// /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None #[inline] pub fn remove_entry_if_and< Q: Hash + Eq + ?Sized, F: FnMut(&K, &V) -> bool, G: FnOnce(&K, &V) -> T, T, >( &self, key: &Q, condition: F, with_previous_entry: G, ) -> Option<T> where K: Borrow<Q>, { let hash = bucket::hash(&self.build_hasher, &key); self.bucket_array_ref() .remove_entry_if_and(key, hash, condition, with_previous_entry) } /// Insert a value if none is associated with `key`. Otherwise, replace the /// value with the result of `on_modify` with the current entry as /// arguments. Finally, return a copy of the previously associated value. /// /// If there is no value associated with `key`, [`None`] will be returned. /// `on_modify` may be invoked multiple times, even if [`None`] is returned. /// /// `V` must implement [`Clone`], as other threads may hold references to /// the associated value. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn insert_or_modify<F: FnMut(&K, &V) -> V>( &self, key: K, value: V, on_modify: F, ) -> Option<V> where V: Clone, { self.insert_with_or_modify_entry_and(key, move || value, on_modify, |_, v| v.clone()) } /// Insert a value if none is associated with `key`. Otherwise, replace the /// value with the result of `on_modify` with the current entry as /// arguments. Finally, return a copy of the previous entry. /// /// If there is no value associated with `key`, [`None`] will be returned. /// `on_modify` may be invoked multiple times, even if [`None`] is returned. /// /// `K` and `V` must implement [`Clone`], as other threads may hold /// references to the entry. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn insert_or_modify_entry<F: FnMut(&K, &V) -> V>( &self, key: K, value: V, on_modify: F, ) -> Option<(K, V)> where K: Clone, V: Clone, { self.insert_with_or_modify_entry_and( key, move || value, on_modify, |k, v| (k.clone(), v.clone()), ) } /// Insert the result of `on_insert` if no value is associated with `key`. /// Otherwise, replace the value with the result of `on_modify` with the /// current entry as arguments. Finally, return a copy of the previously /// associated value. /// /// If there is no value associated with `key`, `on_insert` will be invoked /// and [`None`] will be returned. `on_modify` may be invoked multiple /// times, even if [`None`] is returned. Similarly, `on_insert` may be /// invoked if [`Some`] is returned. /// /// `V` must implement [`Clone`], as other threads may hold references to /// the associated value. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Some`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn insert_with_or_modify<F: FnOnce() -> V, G: FnMut(&K, &V) -> V>( &self, key: K, on_insert: F, on_modify: G, ) -> Option<V> where V: Clone, { self.insert_with_or_modify_entry_and(key, on_insert, on_modify, |_, v| v.clone()) } /// Insert the result of `on_insert` if no value is associated with `key`. /// Otherwise, replace the value with the result of `on_modify` with the /// current entry as arguments. Finally, return a copy of the previous /// entry. /// /// If there is no value associated with `key`, `on_insert` will be invoked /// and [`None`] will be returned. `on_modify` may be invoked multiple /// times, even if [`None`] is returned. Similarly, `on_insert` may be /// invoked if [`Some`] is returned. /// /// `K` and `V` must implement [`Clone`], as other threads may hold /// references to the entry. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Some`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn insert_with_or_modify_entry<F: FnOnce() -> V, G: FnMut(&K, &V) -> V>( &self, key: K, on_insert: F, on_modify: G, ) -> Option<(K, V)> where K: Clone, V: Clone, { self.insert_with_or_modify_entry_and(key, on_insert, on_modify, |k, v| { (k.clone(), v.clone()) }) } /// Insert a value if none is associated with `key`. Otherwise, replace the /// value with the result of `on_modify` with the current entry as /// arguments. Finally, return the result of invoking `with_old_value` with /// the previously associated value. /// /// If there is no value associated with `key`, `with_old_value` will not be /// invoked and [`None`] will be returned. `on_modify` may be invoked /// multiple times, even if [`None`] is returned. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None #[inline] pub fn insert_or_modify_and<F: FnMut(&K, &V) -> V, G: FnOnce(&V) -> T, T>( &self, key: K, value: V, on_modify: F, with_old_value: G, ) -> Option<T> { self.insert_with_or_modify_entry_and( key, move || value, on_modify, move |_, v| with_old_value(v), ) } /// Insert a value if none is associated with `key`. Otherwise, replace the /// value with the result of `on_modify` with the current entry as /// arguments. Finally, return the result of invoking `with_old_entry` with /// the previous entry. /// /// If there is no value associated with `key`, `with_old_value` will not be /// invoked and [`None`] will be returned. `on_modify` may be invoked /// multiple times, even if [`None`] is returned. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None #[inline] pub fn insert_or_modify_entry_and<F: FnMut(&K, &V) -> V, G: FnOnce(&K, &V) -> T, T>( &self, key: K, value: V, on_modify: F, with_old_entry: G, ) -> Option<T> { self.insert_with_or_modify_entry_and(key, move || value, on_modify, with_old_entry) } /// Insert the result of `on_insert` if no value is associated with `key`. /// Otherwise, replace the value with the result of `on_modify` with the /// current entry as arguments. Finally, return the result of invoking /// `with_old_value` with the previously associated value. /// /// If there is no value associated with `key`, `on_insert` will be invoked, /// `with_old_value` will not be invoked, and [`None`] will be returned. /// `on_modify` may be invoked multiple times, even if [`None`] is returned. /// Similarly, `on_insert` may be invoked if [`Some`] is returned. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Some`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some #[inline] pub fn insert_with_or_modify_and< F: FnOnce() -> V, G: FnMut(&K, &V) -> V, H: FnOnce(&V) -> T, T, >( &self, key: K, on_insert: F, on_modify: G, with_old_value: H, ) -> Option<T> { self.insert_with_or_modify_entry_and(key, on_insert, on_modify, move |_, v| { with_old_value(v) }) } /// Insert the result of `on_insert` if no value is associated with `key`. /// Otherwise, replace the value with the result of `on_modify` with the /// current entry as arguments. Finally, return the result of invoking /// `with_old_entry` with the previous entry. /// /// If there is no value associated with `key`, `on_insert` will be invoked, /// `with_old_value` will not be invoked, and [`None`] will be returned. /// `on_modify` may be invoked multiple times, even if [`None`] is returned. /// Similarly, `on_insert` may be invoked if [`Some`] is returned. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Some`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some #[inline] pub fn insert_with_or_modify_entry_and< F: FnOnce() -> V, G: FnMut(&K, &V) -> V, H: FnOnce(&K, &V) -> T, T, >( &self, key: K, on_insert: F, on_modify: G, with_old_entry: H, ) -> Option<T> { let hash = bucket::hash(&self.build_hasher, &key); self.bucket_array_ref().insert_with_or_modify_entry_and( key, hash, on_insert, on_modify, with_old_entry, ) } /// If there is a value associated with `key`, replace it with the result of /// invoking `on_modify` using the current key and value, then return a copy /// of the previously associated value. /// /// If there is no value associated with `key`, [`None`] will be returned. /// `on_modify` may be invoked multiple times, even if [`None`] is returned. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. `V` must implement [`Clone`], as other /// threads may hold references to the associated value. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn modify<F: FnMut(&K, &V) -> V>(&self, key: K, on_modify: F) -> Option<V> where V: Clone, { self.modify_entry_and(key, on_modify, |_, v| v.clone()) } /// If there is a value associated with `key`, replace it with the result of /// invoking `on_modify` using the current key and value, then return a copy /// of the previously entry. /// /// If there is no value associated with `key`, [`None`] will be returned. /// `on_modify` may be invoked multiple times, even if [`None`] is returned. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. `K` and `V` must implement [`Clone`], as other /// threads may hold references to the entry. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html /// [`Clone`]: https://doc.rust-lang.org/std/clone/trait.Clone.html #[inline] pub fn modify_entry<F: FnMut(&K, &V) -> V>(&self, key: K, on_modify: F) -> Option<(K, V)> where K: Clone, V: Clone, { self.modify_entry_and(key, on_modify, |k, v| (k.clone(), v.clone())) } /// If there is a value associated with `key`, replace it with the result of /// invoking `on_modify` using the current key and value, then return the /// result of invoking `with_old_value` with the previously associated /// value. /// /// If there is no value associated with `key`, `with_old_value` will not be /// invoked and [`None`] will be returned. `on_modify` may be invoked /// multiple times, even if [`None`] is returned. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html #[inline] pub fn modify_and<F: FnMut(&K, &V) -> V, G: FnOnce(&V) -> T, T>( &self, key: K, on_modify: F, with_old_value: G, ) -> Option<T> { self.modify_entry_and(key, on_modify, move |_, v| with_old_value(v)) } /// If there is a value associated with `key`, replace it with the result of /// invoking `on_modify` using the current key and value, then return the /// result of invoking `with_old_value` with the previous entry. /// /// If there is no value associated with `key`, `with_old_value` will not be /// invoked and [`None`] will be returned. `on_modify` may be invoked /// multiple times, even if [`None`] is returned. /// /// `Q` can be any borrowed form of `K`, but [`Hash`] and [`Eq`] on `Q` /// *must* match that of `K`. /// /// [`None`]: https://doc.rust-lang.org/std/option/enum.Option.html#variant.None /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html #[inline] pub fn modify_entry_and<F: FnMut(&K, &V) -> V, G: FnOnce(&K, &V) -> T, T>( &self, key: K, on_modify: F, with_old_entry: G, ) -> Option<T> { let hash = bucket::hash(&self.build_hasher, &key); self.bucket_array_ref() .modify_entry_and(key, hash, on_modify, with_old_entry) } } impl<K, V, S> HashMap<K, V, S> { #[inline] fn bucket_array_ref(&'_ self) -> BucketArrayRef<'_, K, V, S> { BucketArrayRef { bucket_array: &self.bucket_array, build_hasher: &self.build_hasher, len: &self.len, } } } impl<K, V, S> Drop for HashMap<K, V, S> { fn drop(&mut self) { let guard = unsafe { &crossbeam_epoch::unprotected() }; atomic::fence(Ordering::Acquire); let mut current_ptr = self.bucket_array.load(Ordering::Relaxed, guard); while let Some(current_ref) = unsafe { current_ptr.as_ref() } { let next_ptr = current_ref.next.load(Ordering::Relaxed, guard); for this_bucket_ptr in current_ref .buckets .iter() .map(|b| b.load(Ordering::Relaxed, guard)) .filter(|p| !p.is_null()) .filter(|p| next_ptr.is_null() || p.tag() & bucket::TOMBSTONE_TAG == 0) { // only delete tombstones from the newest bucket array // the only way this becomes a memory leak is if there was a panic during a rehash, // in which case i'm going to say that running destructors and freeing memory is // best-effort, and my best effort is to not do it unsafe { bucket::defer_acquire_destroy(guard, this_bucket_ptr) }; } unsafe { bucket::defer_acquire_destroy(guard, current_ptr) }; current_ptr = next_ptr; } } } #[cfg(test)] mod tests { use crate::write_test_cases_for_me; use super::*; write_test_cases_for_me!(HashMap); }