xref: /aosp_15_r20/external/marisa-trie/lib/marisa/agent.cc (revision ab8db090fce404b23716c4c9194221ee27efe31c)
1 #include <new>
2 
3 #include "marisa/agent.h"
4 #include "marisa/grimoire/trie.h"
5 
6 namespace marisa {
7 
Agent()8 Agent::Agent() : query_(), key_(), state_() {}
9 
~Agent()10 Agent::~Agent() {}
11 
set_query(const char * str)12 void Agent::set_query(const char *str) {
13   MARISA_THROW_IF(str == NULL, MARISA_NULL_ERROR);
14   if (state_.get() != NULL) {
15     state_->reset();
16   }
17   query_.set_str(str);
18 }
19 
set_query(const char * ptr,std::size_t length)20 void Agent::set_query(const char *ptr, std::size_t length) {
21   MARISA_THROW_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
22   if (state_.get() != NULL) {
23     state_->reset();
24   }
25   query_.set_str(ptr, length);
26 }
27 
set_query(std::size_t key_id)28 void Agent::set_query(std::size_t key_id) {
29   if (state_.get() != NULL) {
30     state_->reset();
31   }
32   query_.set_id(key_id);
33 }
34 
init_state()35 void Agent::init_state() {
36   MARISA_THROW_IF(state_.get() != NULL, MARISA_STATE_ERROR);
37   state_.reset(new (std::nothrow) grimoire::State);
38   MARISA_THROW_IF(state_.get() == NULL, MARISA_MEMORY_ERROR);
39 }
40 
clear()41 void Agent::clear() {
42   Agent().swap(*this);
43 }
44 
swap(Agent & rhs)45 void Agent::swap(Agent &rhs) {
46   query_.swap(rhs.query_);
47   key_.swap(rhs.key_);
48   state_.swap(rhs.state_);
49 }
50 
51 }  // namespace marisa
52