card_est_array/impls/
default_estimator.rs

1/*
2 * SPDX-FileCopyrightText: 2024 Matteo Dell'Acqua
3 * SPDX-FileCopyrightText: 2025 Sebastiano Vigna
4 *
5 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
6 */
7
8use crate::traits::*;
9use std::borrow::Borrow;
10
11/// A default estimator for generic [`EstimationLogic`] and backends.
12pub struct DefaultEstimator<L: EstimationLogic, BL: Borrow<L>, B> {
13    logic: BL,
14    backend: B,
15    _marker: std::marker::PhantomData<L>,
16}
17
18impl<L: EstimationLogic, BL: Borrow<L>, B> DefaultEstimator<L, BL, B> {
19    /// Creates a new default estimator for the specified logic and backend.
20    ///
21    /// # Arguments
22    /// * `logic`: the estimator logic.
23    /// * `backend`: the estimator's backend.
24    pub fn new(logic: BL, backend: B) -> Self {
25        Self {
26            logic,
27            backend,
28            _marker: std::marker::PhantomData,
29        }
30    }
31}
32
33impl<L: EstimationLogic + Clone, BL: Borrow<L>, B: AsRef<L::Backend>> AsRef<L::Backend>
34    for DefaultEstimator<L, BL, B>
35{
36    fn as_ref(&self) -> &L::Backend {
37        self.backend.as_ref()
38    }
39}
40
41impl<L: EstimationLogic + Clone, BL: Borrow<L>, B: AsMut<L::Backend>> AsMut<L::Backend>
42    for DefaultEstimator<L, BL, B>
43{
44    fn as_mut(&mut self) -> &mut L::Backend {
45        self.backend.as_mut()
46    }
47}
48
49impl<L: EstimationLogic + Clone, BL: Borrow<L>, B: AsRef<L::Backend>> Estimator<L>
50    for DefaultEstimator<L, BL, B>
51{
52    type OwnedEstimator = DefaultEstimator<L, L, Box<L::Backend>>;
53
54    fn logic(&self) -> &L {
55        self.logic.borrow()
56    }
57
58    #[inline(always)]
59    fn estimate(&self) -> f64 {
60        self.logic.borrow().estimate(self.backend.as_ref())
61    }
62    #[inline(always)]
63    fn into_owned(self) -> Self::OwnedEstimator {
64        todo!()
65    }
66}
67
68impl<L: EstimationLogic + Clone, BL: Borrow<L>, B: AsRef<L::Backend> + AsMut<L::Backend>>
69    EstimatorMut<L> for DefaultEstimator<L, BL, B>
70{
71    #[inline(always)]
72    fn add(&mut self, element: impl Borrow<L::Item>) {
73        self.logic.borrow().add(self.backend.as_mut(), element)
74    }
75
76    #[inline(always)]
77    fn clear(&mut self) {
78        self.logic.borrow().clear(self.backend.as_mut())
79    }
80
81    #[inline(always)]
82    fn set(&mut self, backend: &L::Backend) {
83        self.logic.borrow().set(self.backend.as_mut(), backend);
84    }
85}
86
87impl<
88    L: EstimationLogic + MergeEstimationLogic + Clone,
89    BL: Borrow<L>,
90    B: AsRef<L::Backend> + AsMut<L::Backend>,
91> MergeEstimator<L> for DefaultEstimator<L, BL, B>
92{
93    #[inline(always)]
94    fn merge(&mut self, other: &L::Backend) {
95        self.logic.borrow().merge(self.backend.as_mut(), other)
96    }
97
98    #[inline(always)]
99    fn merge_with_helper(
100        &mut self,
101        other: &L::Backend,
102        helper: &mut <L as MergeEstimationLogic>::Helper,
103    ) {
104        self.logic
105            .borrow()
106            .merge_with_helper(self.backend.as_mut(), other, helper)
107    }
108}