melisa 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -0
- data/ext/marisa/bindings/marisa-swig.cxx +253 -0
- data/ext/marisa/bindings/marisa-swig.h +183 -0
- data/ext/marisa/bindings/perl/marisa-swig.cxx +253 -0
- data/ext/marisa/bindings/perl/marisa-swig.h +183 -0
- data/ext/marisa/bindings/perl/marisa-swig_wrap.cxx +5160 -0
- data/ext/marisa/bindings/python/marisa-swig.cxx +253 -0
- data/ext/marisa/bindings/python/marisa-swig.h +183 -0
- data/ext/marisa/bindings/python/marisa-swig_wrap.cxx +6090 -0
- data/ext/marisa/bindings/ruby/extconf.rb +5 -0
- data/ext/marisa/bindings/ruby/marisa-swig.cxx +253 -0
- data/ext/marisa/bindings/ruby/marisa-swig.h +183 -0
- data/ext/marisa/bindings/ruby/marisa-swig_wrap.cxx +4708 -0
- data/ext/marisa/lib/marisa.h +14 -0
- data/ext/marisa/lib/marisa/agent.cc +51 -0
- data/ext/marisa/lib/marisa/agent.h +73 -0
- data/ext/marisa/lib/marisa/base.h +193 -0
- data/ext/marisa/lib/marisa/exception.h +82 -0
- data/ext/marisa/lib/marisa/grimoire/algorithm.h +26 -0
- data/ext/marisa/lib/marisa/grimoire/algorithm/sort.h +196 -0
- data/ext/marisa/lib/marisa/grimoire/intrin.h +115 -0
- data/ext/marisa/lib/marisa/grimoire/io.h +18 -0
- data/ext/marisa/lib/marisa/grimoire/io/mapper.cc +163 -0
- data/ext/marisa/lib/marisa/grimoire/io/mapper.h +67 -0
- data/ext/marisa/lib/marisa/grimoire/io/reader.cc +147 -0
- data/ext/marisa/lib/marisa/grimoire/io/reader.h +66 -0
- data/ext/marisa/lib/marisa/grimoire/io/writer.cc +148 -0
- data/ext/marisa/lib/marisa/grimoire/io/writer.h +65 -0
- data/ext/marisa/lib/marisa/grimoire/trie.h +16 -0
- data/ext/marisa/lib/marisa/grimoire/trie/cache.h +81 -0
- data/ext/marisa/lib/marisa/grimoire/trie/config.h +155 -0
- data/ext/marisa/lib/marisa/grimoire/trie/entry.h +82 -0
- data/ext/marisa/lib/marisa/grimoire/trie/header.h +61 -0
- data/ext/marisa/lib/marisa/grimoire/trie/history.h +65 -0
- data/ext/marisa/lib/marisa/grimoire/trie/key.h +228 -0
- data/ext/marisa/lib/marisa/grimoire/trie/louds-trie.cc +876 -0
- data/ext/marisa/lib/marisa/grimoire/trie/louds-trie.h +134 -0
- data/ext/marisa/lib/marisa/grimoire/trie/range.h +115 -0
- data/ext/marisa/lib/marisa/grimoire/trie/state.h +117 -0
- data/ext/marisa/lib/marisa/grimoire/trie/tail.cc +218 -0
- data/ext/marisa/lib/marisa/grimoire/trie/tail.h +72 -0
- data/ext/marisa/lib/marisa/grimoire/vector.h +18 -0
- data/ext/marisa/lib/marisa/grimoire/vector/bit-vector.cc +826 -0
- data/ext/marisa/lib/marisa/grimoire/vector/bit-vector.h +179 -0
- data/ext/marisa/lib/marisa/grimoire/vector/flat-vector.h +205 -0
- data/ext/marisa/lib/marisa/grimoire/vector/pop-count.h +110 -0
- data/ext/marisa/lib/marisa/grimoire/vector/rank-index.h +82 -0
- data/ext/marisa/lib/marisa/grimoire/vector/vector.h +256 -0
- data/ext/marisa/lib/marisa/iostream.h +18 -0
- data/ext/marisa/lib/marisa/key.h +85 -0
- data/ext/marisa/lib/marisa/keyset.cc +181 -0
- data/ext/marisa/lib/marisa/keyset.h +80 -0
- data/ext/marisa/lib/marisa/query.h +71 -0
- data/ext/marisa/lib/marisa/scoped-array.h +48 -0
- data/ext/marisa/lib/marisa/scoped-ptr.h +52 -0
- data/ext/marisa/lib/marisa/stdio.h +15 -0
- data/ext/marisa/lib/marisa/trie.cc +249 -0
- data/ext/marisa/lib/marisa/trie.h +64 -0
- data/ext/marisa/tests/base-test.cc +309 -0
- data/ext/marisa/tests/io-test.cc +252 -0
- data/ext/marisa/tests/marisa-assert.h +26 -0
- data/ext/marisa/tests/marisa-test.cc +388 -0
- data/ext/marisa/tests/trie-test.cc +507 -0
- data/ext/marisa/tests/vector-test.cc +466 -0
- data/ext/marisa/tools/cmdopt.cc +298 -0
- data/ext/marisa/tools/cmdopt.h +58 -0
- data/ext/marisa/tools/marisa-benchmark.cc +418 -0
- data/ext/marisa/tools/marisa-build.cc +206 -0
- data/ext/marisa/tools/marisa-common-prefix-search.cc +143 -0
- data/ext/marisa/tools/marisa-dump.cc +151 -0
- data/ext/marisa/tools/marisa-lookup.cc +110 -0
- data/ext/marisa/tools/marisa-predictive-search.cc +143 -0
- data/ext/marisa/tools/marisa-reverse-lookup.cc +110 -0
- data/lib/melisa.rb +7 -0
- data/lib/melisa/base_config_flags.rb +76 -0
- data/lib/melisa/bytes_trie.rb +55 -0
- data/lib/melisa/int_trie.rb +14 -0
- data/lib/melisa/search.rb +55 -0
- data/lib/melisa/trie.rb +96 -0
- data/lib/melisa/version.rb +3 -0
- data/melisa.gemspec +36 -0
- data/spec/base_config_flags_spec.rb +73 -0
- data/spec/bytes_trie_spec.rb +16 -0
- data/spec/int_trie_spec.rb +16 -0
- data/spec/search_spec.rb +29 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/trie_spec.rb +30 -0
- metadata +207 -0
@@ -0,0 +1,80 @@
|
|
1
|
+
#ifndef MARISA_KEYSET_H_
|
2
|
+
#define MARISA_KEYSET_H_
|
3
|
+
|
4
|
+
#include "marisa/key.h"
|
5
|
+
|
6
|
+
namespace marisa {
|
7
|
+
|
8
|
+
class Keyset {
|
9
|
+
public:
|
10
|
+
enum {
|
11
|
+
BASE_BLOCK_SIZE = 4096,
|
12
|
+
EXTRA_BLOCK_SIZE = 1024,
|
13
|
+
KEY_BLOCK_SIZE = 256
|
14
|
+
};
|
15
|
+
|
16
|
+
Keyset();
|
17
|
+
|
18
|
+
void push_back(const Key &key);
|
19
|
+
void push_back(const Key &key, char end_marker);
|
20
|
+
|
21
|
+
void push_back(const char *str);
|
22
|
+
void push_back(const char *ptr, std::size_t length, float weight = 1.0);
|
23
|
+
|
24
|
+
const Key &operator[](std::size_t i) const {
|
25
|
+
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
|
26
|
+
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
|
27
|
+
}
|
28
|
+
Key &operator[](std::size_t i) {
|
29
|
+
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
|
30
|
+
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
|
31
|
+
}
|
32
|
+
|
33
|
+
std::size_t num_keys() const {
|
34
|
+
return size_;
|
35
|
+
}
|
36
|
+
|
37
|
+
bool empty() const {
|
38
|
+
return size_ == 0;
|
39
|
+
}
|
40
|
+
std::size_t size() const {
|
41
|
+
return size_;
|
42
|
+
}
|
43
|
+
std::size_t total_length() const {
|
44
|
+
return total_length_;
|
45
|
+
}
|
46
|
+
|
47
|
+
void reset();
|
48
|
+
|
49
|
+
void clear();
|
50
|
+
void swap(Keyset &rhs);
|
51
|
+
|
52
|
+
private:
|
53
|
+
scoped_array<scoped_array<char> > base_blocks_;
|
54
|
+
std::size_t base_blocks_size_;
|
55
|
+
std::size_t base_blocks_capacity_;
|
56
|
+
scoped_array<scoped_array<char> > extra_blocks_;
|
57
|
+
std::size_t extra_blocks_size_;
|
58
|
+
std::size_t extra_blocks_capacity_;
|
59
|
+
scoped_array<scoped_array<Key> > key_blocks_;
|
60
|
+
std::size_t key_blocks_size_;
|
61
|
+
std::size_t key_blocks_capacity_;
|
62
|
+
char *ptr_;
|
63
|
+
std::size_t avail_;
|
64
|
+
std::size_t size_;
|
65
|
+
std::size_t total_length_;
|
66
|
+
|
67
|
+
char *reserve(std::size_t size);
|
68
|
+
|
69
|
+
void append_base_block();
|
70
|
+
void append_extra_block(std::size_t size);
|
71
|
+
void append_key_block();
|
72
|
+
|
73
|
+
// Disallows copy and assignment.
|
74
|
+
Keyset(const Keyset &);
|
75
|
+
Keyset &operator=(const Keyset &);
|
76
|
+
};
|
77
|
+
|
78
|
+
} // namespace marisa
|
79
|
+
|
80
|
+
#endif // MARISA_KEYSET_H_
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#ifndef MARISA_QUERY_H_
|
2
|
+
#define MARISA_QUERY_H_
|
3
|
+
|
4
|
+
#include "marisa/base.h"
|
5
|
+
|
6
|
+
namespace marisa {
|
7
|
+
|
8
|
+
class Query {
|
9
|
+
public:
|
10
|
+
Query() : ptr_(NULL), length_(0), id_(0) {}
|
11
|
+
Query(const Query &query)
|
12
|
+
: ptr_(query.ptr_), length_(query.length_), id_(query.id_) {}
|
13
|
+
|
14
|
+
Query &operator=(const Query &query) {
|
15
|
+
ptr_ = query.ptr_;
|
16
|
+
length_ = query.length_;
|
17
|
+
id_ = query.id_;
|
18
|
+
return *this;
|
19
|
+
}
|
20
|
+
|
21
|
+
char operator[](std::size_t i) const {
|
22
|
+
MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR);
|
23
|
+
return ptr_[i];
|
24
|
+
}
|
25
|
+
|
26
|
+
void set_str(const char *str) {
|
27
|
+
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
|
28
|
+
std::size_t length = 0;
|
29
|
+
while (str[length] != '\0') {
|
30
|
+
++length;
|
31
|
+
}
|
32
|
+
ptr_ = str;
|
33
|
+
length_ = length;
|
34
|
+
}
|
35
|
+
void set_str(const char *ptr, std::size_t length) {
|
36
|
+
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
|
37
|
+
ptr_ = ptr;
|
38
|
+
length_ = length;
|
39
|
+
}
|
40
|
+
void set_id(std::size_t id) {
|
41
|
+
id_ = id;
|
42
|
+
}
|
43
|
+
|
44
|
+
const char *ptr() const {
|
45
|
+
return ptr_;
|
46
|
+
}
|
47
|
+
std::size_t length() const {
|
48
|
+
return length_;
|
49
|
+
}
|
50
|
+
std::size_t id() const {
|
51
|
+
return id_;
|
52
|
+
}
|
53
|
+
|
54
|
+
void clear() {
|
55
|
+
Query().swap(*this);
|
56
|
+
}
|
57
|
+
void swap(Query &rhs) {
|
58
|
+
marisa::swap(ptr_, rhs.ptr_);
|
59
|
+
marisa::swap(length_, rhs.length_);
|
60
|
+
marisa::swap(id_, rhs.id_);
|
61
|
+
}
|
62
|
+
|
63
|
+
private:
|
64
|
+
const char *ptr_;
|
65
|
+
std::size_t length_;
|
66
|
+
std::size_t id_;
|
67
|
+
};
|
68
|
+
|
69
|
+
} // namespace marisa
|
70
|
+
|
71
|
+
#endif // MARISA_QUERY_H_
|
@@ -0,0 +1,48 @@
|
|
1
|
+
#ifndef MARISA_SCOPED_ARRAY_H_
|
2
|
+
#define MARISA_SCOPED_ARRAY_H_
|
3
|
+
|
4
|
+
#include "marisa/base.h"
|
5
|
+
|
6
|
+
namespace marisa {
|
7
|
+
|
8
|
+
template <typename T>
|
9
|
+
class scoped_array {
|
10
|
+
public:
|
11
|
+
scoped_array() : array_(NULL) {}
|
12
|
+
explicit scoped_array(T *array) : array_(array) {}
|
13
|
+
|
14
|
+
~scoped_array() {
|
15
|
+
delete [] array_;
|
16
|
+
}
|
17
|
+
|
18
|
+
void reset(T *array = NULL) {
|
19
|
+
MARISA_THROW_IF((array != NULL) && (array == array_), MARISA_RESET_ERROR);
|
20
|
+
scoped_array(array).swap(*this);
|
21
|
+
}
|
22
|
+
|
23
|
+
T &operator[](std::size_t i) const {
|
24
|
+
MARISA_DEBUG_IF(array_ == NULL, MARISA_STATE_ERROR);
|
25
|
+
return array_[i];
|
26
|
+
}
|
27
|
+
T *get() const {
|
28
|
+
return array_;
|
29
|
+
}
|
30
|
+
|
31
|
+
void clear() {
|
32
|
+
scoped_array().swap(*this);
|
33
|
+
}
|
34
|
+
void swap(scoped_array &rhs) {
|
35
|
+
marisa::swap(array_, rhs.array_);
|
36
|
+
}
|
37
|
+
|
38
|
+
private:
|
39
|
+
T *array_;
|
40
|
+
|
41
|
+
// Disallows copy and assignment.
|
42
|
+
scoped_array(const scoped_array &);
|
43
|
+
scoped_array &operator=(const scoped_array &);
|
44
|
+
};
|
45
|
+
|
46
|
+
} // namespace marisa
|
47
|
+
|
48
|
+
#endif // MARISA_SCOPED_ARRAY_H_
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#ifndef MARISA_SCOPED_PTR_H_
|
2
|
+
#define MARISA_SCOPED_PTR_H_
|
3
|
+
|
4
|
+
#include "marisa/base.h"
|
5
|
+
|
6
|
+
namespace marisa {
|
7
|
+
|
8
|
+
template <typename T>
|
9
|
+
class scoped_ptr {
|
10
|
+
public:
|
11
|
+
scoped_ptr() : ptr_(NULL) {}
|
12
|
+
explicit scoped_ptr(T *ptr) : ptr_(ptr) {}
|
13
|
+
|
14
|
+
~scoped_ptr() {
|
15
|
+
delete ptr_;
|
16
|
+
}
|
17
|
+
|
18
|
+
void reset(T *ptr = NULL) {
|
19
|
+
MARISA_THROW_IF((ptr != NULL) && (ptr == ptr_), MARISA_RESET_ERROR);
|
20
|
+
scoped_ptr(ptr).swap(*this);
|
21
|
+
}
|
22
|
+
|
23
|
+
T &operator*() const {
|
24
|
+
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
|
25
|
+
return *ptr_;
|
26
|
+
}
|
27
|
+
T *operator->() const {
|
28
|
+
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
|
29
|
+
return ptr_;
|
30
|
+
}
|
31
|
+
T *get() const {
|
32
|
+
return ptr_;
|
33
|
+
}
|
34
|
+
|
35
|
+
void clear() {
|
36
|
+
scoped_ptr().swap(*this);
|
37
|
+
}
|
38
|
+
void swap(scoped_ptr &rhs) {
|
39
|
+
marisa::swap(ptr_, rhs.ptr_);
|
40
|
+
}
|
41
|
+
|
42
|
+
private:
|
43
|
+
T *ptr_;
|
44
|
+
|
45
|
+
// Disallows copy and assignment.
|
46
|
+
scoped_ptr(const scoped_ptr &);
|
47
|
+
scoped_ptr &operator=(const scoped_ptr &);
|
48
|
+
};
|
49
|
+
|
50
|
+
} // namespace marisa
|
51
|
+
|
52
|
+
#endif // MARISA_SCOPED_PTR_H_
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#ifndef MARISA_STDIO_H_
|
2
|
+
#define MARISA_STDIO_H_
|
3
|
+
|
4
|
+
#include <cstdio>
|
5
|
+
|
6
|
+
namespace marisa {
|
7
|
+
|
8
|
+
class Trie;
|
9
|
+
|
10
|
+
void fread(std::FILE *file, Trie *trie);
|
11
|
+
void fwrite(std::FILE *file, const Trie &trie);
|
12
|
+
|
13
|
+
} // namespace marisa
|
14
|
+
|
15
|
+
#endif // MARISA_STDIO_H_
|
@@ -0,0 +1,249 @@
|
|
1
|
+
#include "marisa/stdio.h"
|
2
|
+
#include "marisa/iostream.h"
|
3
|
+
#include "marisa/trie.h"
|
4
|
+
#include "marisa/grimoire/trie.h"
|
5
|
+
|
6
|
+
namespace marisa {
|
7
|
+
|
8
|
+
Trie::Trie() : trie_() {}
|
9
|
+
|
10
|
+
Trie::~Trie() {}
|
11
|
+
|
12
|
+
void Trie::build(Keyset &keyset, int config_flags) {
|
13
|
+
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
|
14
|
+
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
|
15
|
+
|
16
|
+
temp->build(keyset, config_flags);
|
17
|
+
trie_.swap(temp);
|
18
|
+
}
|
19
|
+
|
20
|
+
void Trie::mmap(const char *filename) {
|
21
|
+
MARISA_THROW_IF(filename == NULL, MARISA_NULL_ERROR);
|
22
|
+
|
23
|
+
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
|
24
|
+
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
|
25
|
+
|
26
|
+
grimoire::Mapper mapper;
|
27
|
+
mapper.open(filename);
|
28
|
+
temp->map(mapper);
|
29
|
+
trie_.swap(temp);
|
30
|
+
}
|
31
|
+
|
32
|
+
void Trie::map(const void *ptr, std::size_t size) {
|
33
|
+
MARISA_THROW_IF((ptr == NULL) && (size != 0), MARISA_NULL_ERROR);
|
34
|
+
|
35
|
+
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
|
36
|
+
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
|
37
|
+
|
38
|
+
grimoire::Mapper mapper;
|
39
|
+
mapper.open(ptr, size);
|
40
|
+
temp->map(mapper);
|
41
|
+
trie_.swap(temp);
|
42
|
+
}
|
43
|
+
|
44
|
+
void Trie::load(const char *filename) {
|
45
|
+
MARISA_THROW_IF(filename == NULL, MARISA_NULL_ERROR);
|
46
|
+
|
47
|
+
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
|
48
|
+
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
|
49
|
+
|
50
|
+
grimoire::Reader reader;
|
51
|
+
reader.open(filename);
|
52
|
+
temp->read(reader);
|
53
|
+
trie_.swap(temp);
|
54
|
+
}
|
55
|
+
|
56
|
+
void Trie::read(int fd) {
|
57
|
+
MARISA_THROW_IF(fd == -1, MARISA_CODE_ERROR);
|
58
|
+
|
59
|
+
scoped_ptr<grimoire::LoudsTrie> temp(new (std::nothrow) grimoire::LoudsTrie);
|
60
|
+
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
|
61
|
+
|
62
|
+
grimoire::Reader reader;
|
63
|
+
reader.open(fd);
|
64
|
+
temp->read(reader);
|
65
|
+
trie_.swap(temp);
|
66
|
+
}
|
67
|
+
|
68
|
+
void Trie::save(const char *filename) const {
|
69
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
70
|
+
MARISA_THROW_IF(filename == NULL, MARISA_NULL_ERROR);
|
71
|
+
|
72
|
+
grimoire::Writer writer;
|
73
|
+
writer.open(filename);
|
74
|
+
trie_->write(writer);
|
75
|
+
}
|
76
|
+
|
77
|
+
void Trie::write(int fd) const {
|
78
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
79
|
+
MARISA_THROW_IF(fd == -1, MARISA_CODE_ERROR);
|
80
|
+
|
81
|
+
grimoire::Writer writer;
|
82
|
+
writer.open(fd);
|
83
|
+
trie_->write(writer);
|
84
|
+
}
|
85
|
+
|
86
|
+
bool Trie::lookup(Agent &agent) const {
|
87
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
88
|
+
if (!agent.has_state()) {
|
89
|
+
agent.init_state();
|
90
|
+
}
|
91
|
+
return trie_->lookup(agent);
|
92
|
+
}
|
93
|
+
|
94
|
+
void Trie::reverse_lookup(Agent &agent) const {
|
95
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
96
|
+
if (!agent.has_state()) {
|
97
|
+
agent.init_state();
|
98
|
+
}
|
99
|
+
trie_->reverse_lookup(agent);
|
100
|
+
}
|
101
|
+
|
102
|
+
bool Trie::common_prefix_search(Agent &agent) const {
|
103
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
104
|
+
if (!agent.has_state()) {
|
105
|
+
agent.init_state();
|
106
|
+
}
|
107
|
+
return trie_->common_prefix_search(agent);
|
108
|
+
}
|
109
|
+
|
110
|
+
bool Trie::predictive_search(Agent &agent) const {
|
111
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
112
|
+
if (!agent.has_state()) {
|
113
|
+
agent.init_state();
|
114
|
+
}
|
115
|
+
return trie_->predictive_search(agent);
|
116
|
+
}
|
117
|
+
|
118
|
+
std::size_t Trie::num_tries() const {
|
119
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
120
|
+
return trie_->num_tries();
|
121
|
+
}
|
122
|
+
|
123
|
+
std::size_t Trie::num_keys() const {
|
124
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
125
|
+
return trie_->num_keys();
|
126
|
+
}
|
127
|
+
|
128
|
+
std::size_t Trie::num_nodes() const {
|
129
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
130
|
+
return trie_->num_nodes();
|
131
|
+
}
|
132
|
+
|
133
|
+
TailMode Trie::tail_mode() const {
|
134
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
135
|
+
return trie_->tail_mode();
|
136
|
+
}
|
137
|
+
|
138
|
+
NodeOrder Trie::node_order() const {
|
139
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
140
|
+
return trie_->node_order();
|
141
|
+
}
|
142
|
+
|
143
|
+
bool Trie::empty() const {
|
144
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
145
|
+
return trie_->empty();
|
146
|
+
}
|
147
|
+
|
148
|
+
std::size_t Trie::size() const {
|
149
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
150
|
+
return trie_->size();
|
151
|
+
}
|
152
|
+
|
153
|
+
std::size_t Trie::total_size() const {
|
154
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
155
|
+
return trie_->total_size();
|
156
|
+
}
|
157
|
+
|
158
|
+
std::size_t Trie::io_size() const {
|
159
|
+
MARISA_THROW_IF(trie_.get() == NULL, MARISA_STATE_ERROR);
|
160
|
+
return trie_->io_size();
|
161
|
+
}
|
162
|
+
|
163
|
+
void Trie::clear() {
|
164
|
+
Trie().swap(*this);
|
165
|
+
}
|
166
|
+
|
167
|
+
void Trie::swap(Trie &rhs) {
|
168
|
+
trie_.swap(rhs.trie_);
|
169
|
+
}
|
170
|
+
|
171
|
+
} // namespace marisa
|
172
|
+
|
173
|
+
#include <iostream>
|
174
|
+
|
175
|
+
namespace marisa {
|
176
|
+
|
177
|
+
class TrieIO {
|
178
|
+
public:
|
179
|
+
static void fread(std::FILE *file, Trie *trie) {
|
180
|
+
MARISA_THROW_IF(trie == NULL, MARISA_NULL_ERROR);
|
181
|
+
|
182
|
+
scoped_ptr<grimoire::LoudsTrie> temp(
|
183
|
+
new (std::nothrow) grimoire::LoudsTrie);
|
184
|
+
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
|
185
|
+
|
186
|
+
grimoire::Reader reader;
|
187
|
+
reader.open(file);
|
188
|
+
temp->read(reader);
|
189
|
+
trie->trie_.swap(temp);
|
190
|
+
}
|
191
|
+
static void fwrite(std::FILE *file, const Trie &trie) {
|
192
|
+
MARISA_THROW_IF(file == NULL, MARISA_NULL_ERROR);
|
193
|
+
MARISA_THROW_IF(trie.trie_.get() == NULL, MARISA_STATE_ERROR);
|
194
|
+
grimoire::Writer writer;
|
195
|
+
writer.open(file);
|
196
|
+
trie.trie_->write(writer);
|
197
|
+
}
|
198
|
+
|
199
|
+
static std::istream &read(std::istream &stream, Trie *trie) {
|
200
|
+
MARISA_THROW_IF(trie == NULL, MARISA_NULL_ERROR);
|
201
|
+
|
202
|
+
scoped_ptr<grimoire::LoudsTrie> temp(
|
203
|
+
new (std::nothrow) grimoire::LoudsTrie);
|
204
|
+
MARISA_THROW_IF(temp.get() == NULL, MARISA_MEMORY_ERROR);
|
205
|
+
|
206
|
+
grimoire::Reader reader;
|
207
|
+
reader.open(stream);
|
208
|
+
temp->read(reader);
|
209
|
+
trie->trie_.swap(temp);
|
210
|
+
return stream;
|
211
|
+
}
|
212
|
+
static std::ostream &write(std::ostream &stream, const Trie &trie) {
|
213
|
+
MARISA_THROW_IF(trie.trie_.get() == NULL, MARISA_STATE_ERROR);
|
214
|
+
grimoire::Writer writer;
|
215
|
+
writer.open(stream);
|
216
|
+
trie.trie_->write(writer);
|
217
|
+
return stream;
|
218
|
+
}
|
219
|
+
};
|
220
|
+
|
221
|
+
void fread(std::FILE *file, Trie *trie) {
|
222
|
+
MARISA_THROW_IF(file == NULL, MARISA_NULL_ERROR);
|
223
|
+
MARISA_THROW_IF(trie == NULL, MARISA_NULL_ERROR);
|
224
|
+
TrieIO::fread(file, trie);
|
225
|
+
}
|
226
|
+
|
227
|
+
void fwrite(std::FILE *file, const Trie &trie) {
|
228
|
+
MARISA_THROW_IF(file == NULL, MARISA_NULL_ERROR);
|
229
|
+
TrieIO::fwrite(file, trie);
|
230
|
+
}
|
231
|
+
|
232
|
+
std::istream &read(std::istream &stream, Trie *trie) {
|
233
|
+
MARISA_THROW_IF(trie == NULL, MARISA_NULL_ERROR);
|
234
|
+
return TrieIO::read(stream, trie);
|
235
|
+
}
|
236
|
+
|
237
|
+
std::ostream &write(std::ostream &stream, const Trie &trie) {
|
238
|
+
return TrieIO::write(stream, trie);
|
239
|
+
}
|
240
|
+
|
241
|
+
std::istream &operator>>(std::istream &stream, Trie &trie) {
|
242
|
+
return read(stream, &trie);
|
243
|
+
}
|
244
|
+
|
245
|
+
std::ostream &operator<<(std::ostream &stream, const Trie &trie) {
|
246
|
+
return write(stream, trie);
|
247
|
+
}
|
248
|
+
|
249
|
+
} // namespace marisa
|