Skip to content

Commit

Permalink
Add contains_key1, contains_key2, contains_keys methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JustForFun88 committed Apr 10, 2022
1 parent 726bf86 commit be7b30d
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "double-map"
version = "0.6.0"
version = "0.7.0"
authors = ["Alisher Galiev <[email protected]>"]
description = "A HashMap with double key to single data/value"
edition = "2021"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
111 changes: 111 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Q: ?Sized>(&self, k1: &Q) -> bool
where
K1: Borrow<Q>,
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<Q: ?Sized>(&self, k2: &Q) -> bool
where
K2: Borrow<Q>,
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<Q1: ?Sized, Q2: ?Sized>(&self, k1: &Q1, k2: &Q2) -> bool
where
K1: Borrow<Q1>,
K2: Borrow<Q2>,
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)`.
///
Expand Down

0 comments on commit be7b30d

Please sign in to comment.