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
/*
* SPDX-FileCopyrightText: 2024 Matteo Dell'Acqua
* SPDX-FileCopyrightText: 2025 Sebastiano Vigna
*
* SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
*/
use Borrow;
/// A kind of cardinality estimator.
///
/// Implementations of this trait describe the behavior of a kind of cardinality
/// estimator. An instance usually contains parameters that further refine the
/// behavior and the precision of the estimator.
///
/// The trait contains the following items:
///
/// * Three associated types:
/// - `Item`: the type of items the estimator accepts.
/// - `Backend`: the type of the estimator backend, that is, the raw,
/// concrete representation of the estimator state.
/// - `Estimator<'a>`: the type of an estimator of this kind.
/// * A method to create a new estimator:
/// [`new_estimator`](EstimationLogic::new_estimator).
/// * A method to add elements to an estimator, given its backend:
/// [`add`](EstimationLogic::add).
/// * Methods to manipulate backends: [`estimate`](EstimationLogic::estimate),
/// [`clear`](EstimationLogic::clear), and [`set`](EstimationLogic::set).
///
/// By providing methods based on backends, an [`EstimationLogic`] can be used
/// to manipulate families of estimators with the same backend and the same
/// configuration (i.e., precision) in a controlled way, and saving space by
/// sharing common parameters. This is particularly useful to build [arrays of
/// cardinality estimators](crate::traits::EstimatorArray), which are arrays of
/// estimators sharing the same logic, but the same technique can be applied
/// to any kind of container (e.g., hash maps or sets of backends).
///
/// If you plan to use a small number of non-related estimators, we suggest you
/// [create](EstimationLogic::new_estimator) them and use their methods. More
/// complex applications, coordinating large numbers of estimators, will find
/// backend-based methods useful.
/// An extension of [`EstimationLogic`] providing methods to merge backends.
///
/// Some kind of estimators make available a *merge* operation, which,
/// given two estimators, returns an estimator with the same state
/// one would obtain by adding to an empty estimator all the elements
/// added to the two estimators, computing, in practice, a set union.
///
/// Since often merging requires additional allocation (usually, to hold
/// temporary results sized like a backend), this trait provides two methods:
/// [`merge`](MergeEstimationLogic::merge) and
/// [`merge_with_helper`](MergeEstimationLogic::merge_with_helper). The first
/// one is simpler to use, but it might cause excessive allocation if used
/// repeatedly. The second one requires a helper to be provided, but it allows
/// to reuse the helper for several merge operations.
/// Trait implemented by [estimation logics](EstimationLogic) whose backend is a
/// slice of elements of some type.
/// Asserts that a backend slice has the expected length for the given
/// [`SliceEstimationLogic`].
pub use assert_backend_len;
/// An immutable estimator.
///
/// Immutable estimators are usually immutable views over some larger structure,
/// or they contain some useful immutable state that can be reused.
///
/// An estimator must implement [`AsRef`] to return a reference to its backend.
/// A mutable estimator.
///
/// A mutable estimator must implement [`AsMut`] to return a mutable
/// reference to its backend.
/// An estimator capable of merging.