From be7b30db42bb28da78a3c34d906b0a3715f474e7 Mon Sep 17 00:00:00 2001 From: JustForFun88 <100504524+JustForFun88@users.noreply.github.com> Date: Sun, 10 Apr 2022 22:41:35 +0500 Subject: [PATCH] Add contains_key1, contains_key2, contains_keys methods --- CHANGELOG.md | 23 +++++++++++ Cargo.toml | 2 +- README.md | 2 +- src/map.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9df0767..4893ddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,29 @@ All notable changes to this project will be documented in this file. +## [v0.7.0] - 2022-04-11 + +### Added + +- **`contains_key1`** `DHashMap` method that returns true if the map contains a value for the specified primary +key of type `K1`; +- **`contains_key2`** `DHashMap` method that returns true if the map contains a value for the specified secondary +key of type `K2`; +- **`contains_keys`** `DHashMap` method that returns true if the map contains a value for the specified primary +key of type `K1` and secondary key of type `K2` and also they both refer to the same value; + +### Changed + +Nothign + +### Removed + +Nothign + +### Fixed + +Nothing + ## [v0.6.0] - 2022-04-10 ### Added diff --git a/Cargo.toml b/Cargo.toml index 0033104..4047feb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "double-map" -version = "0.6.0" +version = "0.7.0" authors = ["Alisher Galiev "] description = "A HashMap with double key to single data/value" edition = "2021" diff --git a/README.md b/README.md index 8242ac3..71730b7 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ interface. Look at [Change log](CHANGELOG.md) for more information. - [x] `entry`: Done since `v0.1.0` - [x] `get`: Done since `v0.1.0` (`get_key1` and `get_key2` methods) - [ ] `get_key_value`: Under development -- [ ] `contains_key`: Under development +- [x] `contains_key`: Done since `v0.7.0` (`contains_key1`, `contains_key2` and `contains_keys` methods) - [x] `get_mut`: Done since `v0.1.0` (`get_mut_key1` and `get_mut_key2` methods) - [x] `insert`: Done since `v0.1.0` (`insert_unchecked` and `insert` methods) - [x] `try_insert`: Done since `v0.1.0` diff --git a/src/map.rs b/src/map.rs index 9c634ac..0b249eb 100644 --- a/src/map.rs +++ b/src/map.rs @@ -782,6 +782,117 @@ where Some(value) } + /// Returns `true` if the map contains a value for the specified primary key `(key #1)` + /// of type `K1`. + /// + /// The key may be any borrowed form of the map's key type, but + /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for + /// the key type + /// + /// # Example + /// + /// ``` + /// use double_map::{DHashMap, dhashmap}; + /// + /// let map = dhashmap! { + /// 1, "One" => String::from("Eins"), + /// 2, "Two" => String::from("Zwei"), + /// 3, "Three" => String::from("Drei"), + /// }; + /// + /// assert!( map.contains_key1(&1)); + /// assert!(!map.contains_key1(&4)); + /// ``` + #[inline] + pub fn contains_key1(&self, k1: &Q) -> bool + where + K1: Borrow, + Q: Hash + Eq, + { + self.value_map.contains_key(k1) + } + + /// Returns `true` if the map contains a value for the specified secondary key `(key #2)` + /// of type `K2`. + /// + /// The key may be any borrowed form of the map's key type, but + /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for + /// the key type + /// + /// # Example + /// + /// ``` + /// use double_map::{DHashMap, dhashmap}; + /// + /// let map = dhashmap! { + /// 1, "One" => String::from("Eins"), + /// 2, "Two" => String::from("Zwei"), + /// 3, "Three" => String::from("Drei"), + /// }; + /// + /// assert!( map.contains_key2(&"One") ); + /// assert!(!map.contains_key2(&"Four")); + /// ``` + #[inline] + pub fn contains_key2(&self, k2: &Q) -> bool + where + K2: Borrow, + Q: Hash + Eq, + { + self.key_map.contains_key(k2) + } + + /// Returns `true` if the map contains a value for the specified primary key `(key #1)` + /// of type `K1` and secondary key `(key #2)` of type `K2` if they both refer to + /// the same value. + /// + /// The keys may be any borrowed form of the map's key type, but + /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for + /// the keys type. + /// + /// # Note + /// Note that this [`contains_keys`](DHashMap::contains_keys) method return `true` only if two + /// keys exist and refer to the same `value`. + /// + /// # Example + /// + /// ``` + /// use double_map::{DHashMap, dhashmap}; + /// + /// let map = dhashmap! { + /// 1, "One" => String::from("Eins"), + /// 2, "Two" => String::from("Zwei"), + /// 3, "Three" => String::from("Drei"), + /// }; + /// + /// // two keys exist and refer to the same value ("Eins") + /// assert_eq!(map.contains_keys(&1, &"One" ), true ); + /// + /// // Both keys don't exist + /// assert_eq!(map.contains_keys(&4, &"Four"), false); + /// + /// // Both keys exist but refer to the different value (key1: 1 refers to "Eins", + /// // key2: "Two" refers to "Zwei") + /// assert_eq!(map.contains_keys(&1, &"Two" ), false); + /// assert!(map.contains_key1(&1) == true && map.get_key1(&1).unwrap() == "Eins"); + /// assert!(map.contains_key2(&"Two") == true && map.get_key2(&"Two").unwrap() == "Zwei"); + /// ``` + #[inline] + pub fn contains_keys(&self, k1: &Q1, k2: &Q2) -> bool + where + K1: Borrow, + K2: Borrow, + Q1: Hash + Eq, + Q2: Hash + Eq, + { + if let Some((k2_exist, _)) = self.value_map.get(k1) { + if let Some(k1_exist) = self.key_map.get(k2) { + return k1_exist.borrow() == k1 && k2_exist.borrow() == k2; + } + } + false + } + /// Returns a mutable reference to the value corresponding to /// the given primary key `(key #1)`. ///