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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
Appellation: features <module>
Contrib: @FL03
*/
use num_traits::{Float, FromPrimitive, ToPrimitive};
/// A topological feature that persists across transformations.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(rename_all = "camelCase")
)]
pub struct PersistentFeature<T = f32> {
pub(crate) id: usize,
pub(crate) birth: usize,
pub(crate) content: Vec<usize>,
pub(crate) death: Option<usize>,
pub(crate) dimension: usize,
pub(crate) importance: T,
pub(crate) occurrences: usize,
}
impl<T> PersistentFeature<T> {
pub fn from_parts(
id: usize,
birth: usize,
content: Vec<usize>,
dimension: usize,
importance: T,
) -> Self {
Self {
id,
birth,
content,
death: None,
dimension,
importance,
occurrences: 1,
}
}
/// returns the birth of a feature
pub const fn birth(&self) -> usize {
self.birth
}
/// returns a mutable reference to the birth of a feature
pub const fn birth_mut(&mut self) -> &mut usize {
&mut self.birth
}
/// returns the dimension of the feature
pub const fn dimension(&self) -> usize {
self.dimension
}
/// returns a mutable reference to the dimension of the feature
pub const fn dimension_mut(&mut self) -> &mut usize {
&mut self.dimension
}
/// returns the id of the feature
pub const fn id(&self) -> usize {
self.id
}
/// returns a mutable reference to the id of the feature
pub const fn id_mut(&mut self) -> &mut usize {
&mut self.id
}
/// returns the occurrences of the feature
pub const fn occurrences(&self) -> usize {
self.occurrences
}
/// returns a mutable reference to the occurrences of the feature
pub const fn occurrences_mut(&mut self) -> &mut usize {
&mut self.occurrences
}
/// returns an immutable reference to the importance of the feature
pub const fn importance(&self) -> &T {
&self.importance
}
/// returns a mutable reference to the importance of the feature
pub const fn importance_mut(&mut self) -> &mut T {
&mut self.importance
}
/// returns an immutable reference to the features content
pub const fn content(&self) -> &Vec<usize> {
&self.content
}
/// returns a mutable reference to the feature's content
pub const fn content_mut(&mut self) -> &mut Vec<usize> {
&mut self.content
}
/// get the death time of this feature; None if still active
pub const fn death(&self) -> Option<usize> {
self.death
}
pub const fn death_mut(&mut self) -> Option<&mut usize> {
self.death.as_mut()
}
/// scale the importance of the feature
pub fn importance_to_usize_by(&self, scale: T) -> usize
where
T: Copy + core::ops::Mul<T, Output = T> + ToPrimitive,
{
(self.importance * scale).to_usize().unwrap()
}
/// scale the importance of the feature
pub fn importance_to_usize_by_10(&self) -> usize
where
T: Copy + core::ops::Mul<Output = T> + FromPrimitive + ToPrimitive,
{
Self::importance_to_usize_by(self, T::from_usize(10).unwrap())
}
pub fn update_importance_by_occurrences(&mut self)
where
T: Float + FromPrimitive,
{
let ten = T::from_usize(10).unwrap();
let occurrences = T::from_usize(self.occurrences).unwrap();
self.set_importance(occurrences.min(ten) / ten);
}
/// update the birth of a feature
pub fn set_birth(&mut self, birth: usize) {
self.birth = birth;
}
/// set the content of the feature
pub fn set_content<I>(&mut self, content: I)
where
I: IntoIterator<Item = usize>,
{
self.content = Vec::from_iter(content);
}
/// set the death of the feature
pub fn set_death(&mut self, death: usize) {
self.death = Some(death);
}
/// update the dimension of a feature
pub fn set_dimension(&mut self, dimension: usize) {
self.dimension = dimension;
}
/// update the id of a feature pattern
pub fn set_id(&mut self, id: usize) {
self.id = id;
}
/// update the importance of a feature
pub fn set_importance(&mut self, importance: T) {
self.importance = importance;
}
/// update the occurrences of a feature
pub fn set_occurrences(&mut self, occurrences: usize) {
self.occurrences = occurrences;
}
/// consumes the current instance to create another with the specified birth
pub fn with_birth(self, birth: usize) -> Self {
Self { birth, ..self }
}
/// consumes the current instance to create another with the given content
pub fn with_content<I>(self, iter: I) -> Self
where
I: IntoIterator<Item = usize>,
{
Self {
content: Vec::from_iter(iter),
..self
}
}
/// consumes the current instance to create another with the given death time
pub fn with_death(self, death: Option<usize>) -> Self {
Self { death, ..self }
}
/// consumes the current instance to create another with the specified dimension
pub fn with_dimension(self, dimension: usize) -> Self {
Self { dimension, ..self }
}
/// consumes the current instance to create another with the specified id
pub fn with_id(self, id: usize) -> Self {
Self { id, ..self }
}
/// consumes the current instance to create another with the specified importance
pub fn with_importance(self, importance: T) -> Self {
Self { importance, ..self }
}
/// consumes the current instance to create another with the specified occurrences
pub fn with_occurrences(self, occurrences: usize) -> Self {
Self {
occurrences,
..self
}
}
/// check if the feature contains a given content
pub fn contains(&self, content: &usize) -> bool {
self.content.contains(content)
}
/// check if the feature does not have a death time
pub fn is_alive(&self) -> bool {
self.death.is_none()
}
/// check if a feature has died
pub fn is_dead(&self) -> bool {
self.death.is_some()
}
/// returns the number of elements in the content
pub fn content_len(&self) -> usize {
self.content.len()
}
/// decrement the number of occurrences
pub fn decrement_occurrences(&mut self) {
self.occurrences -= 1;
}
/// Increment the occurrence count
pub fn increment_occurrences(&mut self) {
self.occurrences += 1;
}
}
impl<T> core::ops::Index<usize> for PersistentFeature<T> {
type Output = usize;
fn index(&self, index: usize) -> &Self::Output {
&self.content[index]
}
}
impl<T> core::ops::IndexMut<usize> for PersistentFeature<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.content[index]
}
}