rsmarisa 0.4.1

Pure Rust port of marisa-trie: a static and space-efficient trie data structure
Documentation
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Agent for trie operations.
//!
//! Ported from:
//! - include/marisa/agent.h
//! - lib/marisa/agent.cc

use crate::grimoire::trie::state::{State, StatusCode};
use crate::key::Key;
use crate::query::Query;
use std::io;

/// Agent encapsulates query, key, and state for trie operations.
///
/// An agent is used for:
/// - Lookup operations (query → key with ID)
/// - Reverse lookup (query with ID → key with string)
/// - Common prefix search
/// - Predictive search
pub struct Agent {
    /// Query for search operations.
    query: Query,
    /// Key result from operations.
    key: Key,
    /// Optional state for complex searches.
    state: Option<Box<State>>,
}

impl Default for Agent {
    fn default() -> Self {
        Self::new()
    }
}

impl Clone for Agent {
    fn clone(&self) -> Self {
        let mut cloned = Agent {
            query: self.query.clone(),
            key: self.key.clone(),
            state: self.state.as_ref().map(|s| Box::new((**s).clone())),
        };

        // Update agent after copying state
        let should_update = cloned.state.is_some();
        if should_update {
            if let Some(ref state) = cloned.state {
                let status = state.status_code();
                match status {
                    StatusCode::ReadyToPredictiveSearch | StatusCode::EndOfPredictiveSearch => {
                        // Key points into state buffer - repoint after copy
                        let key_buf = state.key_buf();
                        cloned.key.set_bytes(key_buf);
                    }
                    _ => {
                        // Key is null or points to query - no update needed
                    }
                }
            }
        }

        cloned
    }
}

impl Agent {
    /// Creates a new empty agent.
    pub fn new() -> Self {
        Agent {
            query: Query::new(),
            key: Key::new(),
            state: None,
        }
    }

    /// Returns a reference to the query.
    pub fn query(&self) -> &Query {
        &self.query
    }

    /// Returns a mutable reference to the query.
    pub fn query_mut(&mut self) -> &mut Query {
        &mut self.query
    }

    /// Returns a reference to the key.
    pub fn key(&self) -> &Key {
        &self.key
    }

    /// Returns a mutable reference to the key.
    pub fn key_mut(&mut self) -> &mut Key {
        &mut self.key
    }

    /// Sets the query from a string slice.
    pub fn set_query_str(&mut self, s: &str) {
        if let Some(ref mut state) = self.state {
            state.reset();
        }
        self.query.set_str(s);
    }

    /// Sets the query from a byte slice.
    pub fn set_query_bytes(&mut self, bytes: &[u8]) {
        if let Some(ref mut state) = self.state {
            state.reset();
        }
        self.query.set_bytes(bytes);
    }

    /// Sets the query from a key ID for reverse lookup.
    pub fn set_query_id(&mut self, key_id: usize) {
        if let Some(ref mut state) = self.state {
            state.reset();
        }
        self.query.set_id(key_id);
    }

    /// Returns a reference to the state if it exists.
    pub fn state(&self) -> Option<&State> {
        self.state.as_deref()
    }

    /// Returns a mutable reference to the state if it exists.
    pub fn state_mut(&mut self) -> Option<&mut State> {
        self.state.as_deref_mut()
    }

    /// Returns the query bytes alongside a mutable reference to the state.
    ///
    /// This is a borrow-splitting helper for inner loops in tail/trie matching
    /// that need to read query bytes while updating the state — calling
    /// `agent.query()` and `agent.state_mut()` separately would force callers
    /// to clone the query into a temporary `Vec` to satisfy the borrow checker.
    ///
    /// # Panics
    ///
    /// Panics if the agent has no state initialized.
    #[inline]
    pub fn query_bytes_and_state_mut(&mut self) -> (&[u8], &mut State) {
        // Borrow each field directly so the compiler treats them as disjoint.
        let bytes = self.query.as_bytes();
        let state = self
            .state
            .as_deref_mut()
            .expect("Agent must have state initialized");
        (bytes, state)
    }

    /// Sets the key from a string slice.
    pub fn set_key_str(&mut self, s: &str) {
        self.key.set_str(s);
    }

    /// Sets the key from a byte slice.
    pub fn set_key_bytes(&mut self, bytes: &[u8]) {
        self.key.set_bytes(bytes);
    }

    /// Sets the key ID.
    pub fn set_key_id(&mut self, id: usize) {
        self.key.set_id(id);
    }

    /// Sets the key to point to the state's key buffer.
    ///
    /// This is used after operations like reverse_lookup that build
    /// the key in the state's buffer.
    pub fn set_key_from_state_buf(&mut self) {
        if let Some(ref state) = self.state {
            let buf = state.key_buf();
            // Set key to point to state's buffer
            // SAFETY: The buffer is owned by state which is owned by self,
            // so it will live as long as self lives.
            self.key.set_bytes(buf);
        } else {
            panic!("Agent must have state to set key from state buffer");
        }
    }

    /// Sets the key to point to the query buffer.
    ///
    /// This is used after a successful lookup to set the result key
    /// to the query string that was searched for.
    pub fn set_key_from_query(&mut self) {
        let bytes = self.query.as_bytes();
        self.key.set_bytes(bytes);
    }

    /// Sets the key to point to a prefix of the query buffer.
    ///
    /// This is used during prefix searches to set the result key
    /// to a prefix of the query string.
    ///
    /// # Arguments
    ///
    /// * `length` - Length of the prefix
    pub fn set_key_from_query_prefix(&mut self, length: usize) {
        let bytes = self.query.as_bytes();
        assert!(length <= bytes.len(), "Prefix length out of bounds");
        self.key.set_bytes(&bytes[..length]);
    }

    /// Returns true if the agent has state.
    pub fn has_state(&self) -> bool {
        self.state.is_some()
    }

    /// Initializes state for complex searches.
    ///
    /// # Errors
    ///
    /// Returns an error if state is already initialized.
    pub fn init_state(&mut self) -> io::Result<()> {
        if self.state.is_some() {
            return Err(io::Error::new(
                io::ErrorKind::AlreadyExists,
                "State already initialized",
            ));
        }
        self.state = Some(Box::new(State::new()));
        Ok(())
    }

    /// Clears the agent to empty state.
    pub fn clear(&mut self) {
        *self = Agent::new();
    }

    /// Swaps with another agent.
    pub fn swap(&mut self, other: &mut Agent) {
        std::mem::swap(self, other);
    }
}

/// Updates agent's key pointer after copying state.
///
/// In predictive search states, the agent's key points into the state's
/// key buffer. After copying state, we need to update the pointer.
#[allow(dead_code)]
fn update_agent_after_copying_state(state: &State, agent: &mut Agent) {
    match state.status_code() {
        StatusCode::ReadyToPredictiveSearch | StatusCode::EndOfPredictiveSearch => {
            // Key points into state buffer - repoint after copy
            let key_buf = state.key_buf();
            agent.key.set_bytes(key_buf);
        }
        _ => {
            // Key is null or points to query - no update needed
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_agent_new() {
        let agent = Agent::new();
        assert_eq!(agent.query().length(), 0);
        assert_eq!(agent.key().length(), 0);
        assert!(!agent.has_state());
    }

    #[test]
    fn test_agent_default() {
        let agent = Agent::default();
        assert_eq!(agent.query().length(), 0);
    }

    #[test]
    fn test_agent_set_query_str() {
        let mut agent = Agent::new();
        agent.set_query_str("hello");

        assert_eq!(agent.query().as_str(), "hello");
        assert_eq!(agent.query().length(), 5);
    }

    #[test]
    fn test_agent_set_query_bytes() {
        let mut agent = Agent::new();
        agent.set_query_bytes(b"world");

        assert_eq!(agent.query().as_bytes(), b"world");
        assert_eq!(agent.query().length(), 5);
    }

    #[test]
    fn test_agent_set_query_id() {
        let mut agent = Agent::new();
        agent.set_query_id(42);

        assert_eq!(agent.query().id(), 42);
    }

    #[test]
    fn test_agent_set_key_str() {
        let mut agent = Agent::new();
        agent.set_key_str("test");

        assert_eq!(agent.key().as_str(), "test");
    }

    #[test]
    fn test_agent_set_key_id() {
        let mut agent = Agent::new();
        agent.set_key_id(100);

        assert_eq!(agent.key().id(), 100);
    }

    #[test]
    fn test_agent_init_state() {
        let mut agent = Agent::new();
        assert!(!agent.has_state());

        agent.init_state().unwrap();
        assert!(agent.has_state());
    }

    #[test]
    fn test_agent_init_state_already_exists() {
        let mut agent = Agent::new();
        agent.init_state().unwrap();

        let result = agent.init_state();
        assert!(result.is_err());
    }

    #[test]
    fn test_agent_state_reset_on_set_query() {
        let mut agent = Agent::new();
        agent.init_state().unwrap();

        {
            let state = agent.state_mut().unwrap();
            state.set_status_code(StatusCode::EndOfPredictiveSearch);
        }

        // Setting query should reset state status code
        agent.set_query_str("new query");

        let state = agent.state().unwrap();
        assert_eq!(state.status_code(), StatusCode::ReadyToAll);
    }

    #[test]
    fn test_agent_clear() {
        let mut agent = Agent::new();
        agent.set_query_str("test");
        agent.set_key_id(10);
        agent.init_state().unwrap();

        agent.clear();

        assert_eq!(agent.query().length(), 0);
        assert_eq!(agent.key().length(), 0);
        assert!(!agent.has_state());
    }

    #[test]
    fn test_agent_swap() {
        let s1 = "query1";
        let s2 = "query2";

        let mut a1 = Agent::new();
        a1.set_query_str(s1);
        a1.set_key_id(1);

        let mut a2 = Agent::new();
        a2.set_query_str(s2);
        a2.set_key_id(2);
        a2.init_state().unwrap();

        a1.swap(&mut a2);

        assert_eq!(a1.query().as_str(), "query2");
        assert_eq!(a1.key().id(), 2);
        assert!(a1.has_state());

        assert_eq!(a2.query().as_str(), "query1");
        assert_eq!(a2.key().id(), 1);
        assert!(!a2.has_state());
    }

    #[test]
    fn test_agent_clone() {
        let mut agent = Agent::new();
        agent.set_query_str("original");
        agent.set_key_id(42);
        agent.init_state().unwrap();

        let cloned = agent.clone();

        assert_eq!(cloned.query().as_str(), "original");
        assert_eq!(cloned.key().id(), 42);
        assert!(cloned.has_state());
    }

    #[test]
    fn test_agent_query_mut() {
        let mut agent = Agent::new();
        agent.query_mut().set_str("mutable");

        assert_eq!(agent.query().as_str(), "mutable");
    }

    #[test]
    fn test_agent_key_mut() {
        let mut agent = Agent::new();
        agent.key_mut().set_id(99);

        assert_eq!(agent.key().id(), 99);
    }
}