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
/*
appellation: state <module>
authors: @FL03
*/
use crate::MemoryPosition;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Deserialize, serde_derive::Serialize),
serde(default, rename_all = "snake_case")
)]
#[repr(C)]
pub struct MemoryState {
/// Current epoch of the memory system
pub(crate) epoch: usize,
/// Total number of features in the memory
pub(crate) feature_count: usize,
/// Total number of patterns recognized
pub(crate) pattern_count: usize,
/// a positional tracker for the memory system to maintain consistency
pub(crate) position: MemoryPosition,
/// Total number of relationships between features
pub(crate) relationship_count: usize,
}
impl MemoryState {
/// Creates a new `MemoryState` with default values.
pub fn new() -> Self {
MemoryState {
epoch: 0,
feature_count: 0,
pattern_count: 0,
position: MemoryPosition::zero(),
relationship_count: 0,
}
}
/// returns a copy of the current epoch
pub const fn epoch(&self) -> usize {
self.epoch
}
/// returns a mutable reference to the current epoch
pub const fn epoch_mut(&mut self) -> &mut usize {
&mut self.epoch
}
/// returns a copy of the current feature count
pub const fn feature_count(&self) -> usize {
self.feature_count
}
/// returns a mutable reference to the current feature count
pub const fn feature_count_mut(&mut self) -> &mut usize {
&mut self.feature_count
}
/// returns a copy of the current pattern count
pub const fn pattern_count(&self) -> usize {
self.pattern_count
}
/// returns a mutable reference to the current pattern count
pub const fn pattern_count_mut(&mut self) -> &mut usize {
&mut self.pattern_count
}
/// returns a reference to the current memory position
pub const fn position(&self) -> &MemoryPosition {
&self.position
}
/// returns a mutable reference to the current memory position
pub const fn position_mut(&mut self) -> &mut MemoryPosition {
&mut self.position
}
/// returns a copy of the current relationship count
pub const fn relationship_count(&self) -> usize {
self.relationship_count
}
/// returns a mutable reference to the current relationship count
pub const fn relationship_count_mut(&mut self) -> &mut usize {
&mut self.relationship_count
}
/// update the current epoch and return a mutable reference to the current state
pub fn set_epoch(&mut self, epoch: usize) -> &mut Self {
self.epoch = epoch;
self
}
/// update the current feature count and return a mutable reference to the current state
pub fn set_feature_count(&mut self, count: usize) -> &mut Self {
self.feature_count = count;
self
}
/// update the current pattern count and return a mutable reference to the current state
pub fn set_pattern_count(&mut self, count: usize) -> &mut Self {
self.pattern_count = count;
self
}
/// update the current relationship count and return a mutable reference to the current
/// state
pub fn set_relationship_count(&mut self, count: usize) -> &mut Self {
self.relationship_count = count;
self
}
/// sets the current [`position`](MemoryPosition) to a new value and returns a mutable
/// reference to the current state.
pub fn set_position(&mut self, position: MemoryPosition) -> &mut Self {
self.position = position;
self
}
/// consumes the current instance to create another with the given epoch
pub fn with_epoch(self, epoch: usize) -> Self {
Self { epoch, ..self }
}
/// consumes the current instance to create another with the given feature count
pub fn with_feature_count(self, count: usize) -> Self {
Self {
feature_count: count,
..self
}
}
/// consumes the current instance to create another with the given pattern count
pub fn with_pattern_count(self, count: usize) -> Self {
Self {
pattern_count: count,
..self
}
}
/// consumes the current instance to create another with the given relationship count
pub fn with_relationship_count(self, count: usize) -> Self {
Self {
relationship_count: count,
..self
}
}
/// consumes the current instance to create another with the given position
pub fn with_position(self, position: MemoryPosition) -> Self {
Self { position, ..self }
}
pub const fn replace_epoch(&mut self, epoch: usize) -> usize {
core::mem::replace(self.epoch_mut(), epoch)
}
/// returns the current epoch after replacing it with the next one
pub fn next_epoch(&mut self) -> usize {
self.replace_epoch(self.epoch() + 1)
}
/// increments the current feature index and returns the old value
pub fn next_feature_id(&mut self) -> usize {
self.position_mut().next_feature_id()
}
/// increments the current pattern index and returns the old value
pub fn next_pattern_id(&mut self) -> usize {
self.position_mut().next_pattern_id()
}
}