Skip to main content

hoomd_spatial/
all_pairs.rs

1// Copyright (c) 2024-2026 The Regents of the University of Michigan.
2// Part of hoomd-rs, released under the BSD 3-Clause License.
3
4//! Implement `AllPairs`
5
6use rustc_hash::FxHashSet;
7use serde::{Deserialize, Serialize};
8use std::{fmt, hash::Hash};
9
10use hoomd_utility::valid::PositiveReal;
11
12use super::{PointUpdate, PointsNearBall, WithSearchRadius};
13
14/// Check all pairs.
15///
16/// Prefer [`VecCell`] or [`HashCell`] when possible. In typical benchmarks,
17/// [`AllPairs`] outperforms [`VecCell`] only when the system size is less
18/// than 32 sites.
19///
20/// [`VecCell`]: crate::VecCell
21/// [`HashCell`]: crate::HashCell
22#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
23pub struct AllPairs<K>
24where
25    K: Eq + Hash,
26{
27    /// Store all keys currently in the spatial data.
28    keys: FxHashSet<K>,
29}
30
31impl<K> Default for AllPairs<K>
32where
33    K: Copy + Eq + Hash,
34{
35    #[inline]
36    fn default() -> Self {
37        Self {
38            keys: FxHashSet::default(),
39        }
40    }
41}
42
43impl<K> WithSearchRadius for AllPairs<K>
44where
45    K: Copy + Eq + Hash,
46{
47    #[inline]
48    fn with_search_radius(_radius: PositiveReal) -> Self {
49        Self::default()
50    }
51}
52
53impl<P, K> PointUpdate<P, K> for AllPairs<K>
54where
55    K: Copy + Eq + Hash,
56{
57    #[inline]
58    fn insert(&mut self, key: K, _position: P) {
59        self.keys.insert(key);
60    }
61
62    #[inline]
63    fn remove(&mut self, key: &K) {
64        self.keys.remove(key);
65    }
66
67    #[inline]
68    fn len(&self) -> usize {
69        self.keys.len()
70    }
71
72    #[inline]
73    fn is_empty(&self) -> bool {
74        self.keys.is_empty()
75    }
76
77    #[inline]
78    fn contains_key(&self, key: &K) -> bool {
79        self.keys.contains(key)
80    }
81
82    #[inline]
83    fn clear(&mut self) {
84        self.keys.clear();
85    }
86}
87
88impl<P, K> PointsNearBall<P, K> for AllPairs<K>
89where
90    K: Copy + Eq + Hash,
91{
92    #[inline]
93    fn points_near_ball(&self, _position: &P, _radius: f64) -> impl Iterator<Item = K> {
94        self.keys.iter().copied()
95    }
96
97    #[inline]
98    fn is_all_pairs() -> bool {
99        true
100    }
101}
102
103impl<K> fmt::Display for AllPairs<K>
104where
105    K: Eq + Hash,
106{
107    #[inline]
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        write!(f, "AllPairs")
110    }
111}