Auto update

This commit is contained in:
Fcitx5 Android Bot 2026-04-25 14:35:08 +00:00
parent 642312f996
commit 6672fa19bd
4907 changed files with 206456 additions and 171700 deletions

View file

@ -2,13 +2,13 @@
#define MARISA_H_
// "marisa/stdio.h" includes <cstdio> for I/O using std::FILE.
#include "marisa/stdio.h"
#include "marisa/stdio.h" // IWYU pragma: export
// "marisa/iostream.h" includes <iosfwd> for I/O using std::iostream.
#include "marisa/iostream.h"
#include "marisa/iostream.h" // IWYU pragma: export
// You can use <marisa/trie.h> instead of <marisa.h> if you don't need the
// above I/O interfaces and don't want to include the above I/O headers.
#include "marisa/trie.h"
#include "marisa/trie.h" // IWYU pragma: export
#endif // MARISA_H_

View file

@ -1,27 +1,30 @@
#ifndef MARISA_AGENT_H_
#define MARISA_AGENT_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <memory>
#include <string_view>
#include "marisa/key.h"
#include "marisa/query.h"
namespace marisa {
namespace grimoire {
namespace trie {
namespace grimoire::trie {
class State;
} // namespace trie
} // namespace grimoire
} // namespace grimoire::trie
class Agent {
public:
Agent();
~Agent();
Agent(const Agent &other);
Agent &operator=(const Agent &other);
Agent(Agent &&other) noexcept;
Agent &operator=(Agent &&other) noexcept;
const Query &query() const {
return query_;
}
@ -29,11 +32,9 @@ class Agent {
return key_;
}
#if __cplusplus >= 201703L
void set_query(std::string_view str) {
set_query(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_query(const char *str);
void set_query(const char *ptr, std::size_t length);
void set_query(std::size_t key_id);
@ -45,41 +46,35 @@ class Agent {
return *state_;
}
#if __cplusplus >= 201703L
void set_key(std::string_view str) {
set_key(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_key(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
key_.set_str(str);
}
void set_key(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert((ptr != nullptr) || (length == 0));
assert(length <= UINT32_MAX);
key_.set_str(ptr, length);
}
void set_key(std::size_t id) {
MARISA_DEBUG_IF(id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert(id <= UINT32_MAX);
key_.set_id(id);
}
bool has_state() const {
return state_.get() != NULL;
return state_ != nullptr;
}
void init_state();
void clear();
void swap(Agent &rhs);
void clear() noexcept;
void swap(Agent &rhs) noexcept;
private:
Query query_;
Key key_;
scoped_ptr<grimoire::trie::State> state_;
// Disallows copy and assignment.
Agent(const Agent &);
Agent &operator=(const Agent &);
std::unique_ptr<grimoire::trie::State> state_;
};
} // namespace marisa

View file

@ -1,95 +1,85 @@
#ifndef MARISA_BASE_H_
#define MARISA_BASE_H_
// Old Visual C++ does not provide stdint.h.
#ifndef _MSC_VER
#include <stdint.h>
#endif // _MSC_VER
#include <cstddef>
#include <cstdint>
#include <exception>
#include <system_error>
#include <utility>
#ifdef __cplusplus
#include <cstddef>
#else // __cplusplus
#include <stddef.h>
#endif // __cplusplus
// These aliases are left for backward compatibility.
using marisa_uint8 [[deprecated]] = std::uint8_t;
using marisa_uint16 [[deprecated]] = std::uint16_t;
using marisa_uint32 [[deprecated]] = std::uint32_t;
using marisa_uint64 [[deprecated]] = std::uint64_t;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#ifdef _MSC_VER
typedef unsigned __int8 marisa_uint8;
typedef unsigned __int16 marisa_uint16;
typedef unsigned __int32 marisa_uint32;
typedef unsigned __int64 marisa_uint64;
#else // _MSC_VER
typedef uint8_t marisa_uint8;
typedef uint16_t marisa_uint16;
typedef uint32_t marisa_uint32;
typedef uint64_t marisa_uint64;
#endif // _MSC_VER
#if defined(_WIN64) || defined(__amd64__) || defined(__x86_64__) || \
defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__) || \
defined(__sparc64__) || defined(__mips64__) || defined(__aarch64__) || \
defined(__s390x__)
#if UINTPTR_MAX == UINT64_MAX
#define MARISA_WORD_SIZE 64
#else // defined(_WIN64), etc.
#elif UINTPTR_MAX == UINT32_MAX
#define MARISA_WORD_SIZE 32
#endif // defined(_WIN64), etc.
#else
#error Failed to detect MARISA_WORD_SIZE
#endif
//#define MARISA_WORD_SIZE (sizeof(void *) * 8)
// These constant variables are left for backward compatibility.
[[deprecated]] constexpr auto MARISA_UINT8_MAX = UINT8_MAX;
[[deprecated]] constexpr auto MARISA_UINT16_MAX = UINT16_MAX;
[[deprecated]] constexpr auto MARISA_UINT32_MAX = UINT32_MAX;
[[deprecated]] constexpr auto MARISA_UINT64_MAX = UINT64_MAX;
[[deprecated]] constexpr auto MARISA_SIZE_MAX = SIZE_MAX;
#define MARISA_UINT8_MAX ((marisa_uint8)~(marisa_uint8)0)
#define MARISA_UINT16_MAX ((marisa_uint16)~(marisa_uint16)0)
#define MARISA_UINT32_MAX ((marisa_uint32)~(marisa_uint32)0)
#define MARISA_UINT64_MAX ((marisa_uint64)~(marisa_uint64)0)
#define MARISA_SIZE_MAX ((size_t)~(size_t)0)
#define MARISA_INVALID_LINK_ID MARISA_UINT32_MAX
#define MARISA_INVALID_KEY_ID MARISA_UINT32_MAX
#define MARISA_INVALID_EXTRA (MARISA_UINT32_MAX >> 8)
#define MARISA_INVALID_LINK_ID UINT32_MAX
#define MARISA_INVALID_KEY_ID UINT32_MAX
#define MARISA_INVALID_EXTRA (UINT32_MAX >> 8)
// Error codes are defined as members of marisa_error_code. This library throws
// an exception with one of the error codes when an error occurs.
typedef enum marisa_error_code_ {
enum marisa_error_code {
// MARISA_OK means that a requested operation has succeeded. In practice, an
// exception never has MARISA_OK because it is not an error.
MARISA_OK = 0,
MARISA_OK = 0,
// MARISA_STATE_ERROR means that an object was not ready for a requested
// operation. For example, an operation to modify a fixed vector throws an
// exception with MARISA_STATE_ERROR.
MARISA_STATE_ERROR = 1,
MARISA_STATE_ERROR = 1,
// MARISA_NULL_ERROR means that an invalid NULL pointer has been given.
MARISA_NULL_ERROR = 2,
// MARISA_NULL_ERROR means that an invalid nullptr has been given.
MARISA_NULL_ERROR = 2,
// MARISA_BOUND_ERROR means that an operation has tried to access an out of
// range address.
MARISA_BOUND_ERROR = 3,
MARISA_BOUND_ERROR = 3,
// MARISA_RANGE_ERROR means that an out of range value has appeared in
// operation.
MARISA_RANGE_ERROR = 4,
MARISA_RANGE_ERROR = 4,
// MARISA_CODE_ERROR means that an undefined code has appeared in operation.
MARISA_CODE_ERROR = 5,
MARISA_CODE_ERROR = 5,
// MARISA_RESET_ERROR means that a smart pointer has tried to reset itself.
MARISA_RESET_ERROR = 6,
MARISA_RESET_ERROR = 6,
// MARISA_SIZE_ERROR means that a size has exceeded a library limitation.
MARISA_SIZE_ERROR = 7,
MARISA_SIZE_ERROR = 7,
// MARISA_MEMORY_ERROR means that a memory allocation has failed.
MARISA_MEMORY_ERROR = 8,
// MARISA_IO_ERROR means that an I/O operation has failed.
MARISA_IO_ERROR = 9,
MARISA_IO_ERROR = 9,
// MARISA_FORMAT_ERROR means that input was in invalid format.
MARISA_FORMAT_ERROR = 10,
} marisa_error_code;
};
// Flags for memory mapping are defined as members of marisa_map_flags.
// Trie::open() accepts a combination of these flags.
enum marisa_map_flags {
// MARISA_MAP_POPULATE specifies MAP_POPULATE.
MARISA_MAP_POPULATE = 1 << 0,
};
// Min/max values, flags and masks for dictionary settings are defined below.
// Please note that unspecified settings will be replaced with the default
@ -98,99 +88,136 @@ typedef enum marisa_error_code_ {
// A dictionary consists of 3 tries in default. Usually more tries make a
// dictionary space-efficient but time-inefficient.
typedef enum marisa_num_tries_ {
MARISA_MIN_NUM_TRIES = 0x00001,
MARISA_MAX_NUM_TRIES = 0x0007F,
enum marisa_num_tries {
MARISA_MIN_NUM_TRIES = 0x00001,
MARISA_MAX_NUM_TRIES = 0x0007F,
MARISA_DEFAULT_NUM_TRIES = 0x00003,
} marisa_num_tries;
};
// This library uses a cache technique to accelerate search functions. The
// following enumerated type marisa_cache_level gives a list of available cache
// size options. A larger cache enables faster search but takes a more space.
typedef enum marisa_cache_level_ {
MARISA_HUGE_CACHE = 0x00080,
MARISA_LARGE_CACHE = 0x00100,
MARISA_NORMAL_CACHE = 0x00200,
MARISA_SMALL_CACHE = 0x00400,
MARISA_TINY_CACHE = 0x00800,
MARISA_DEFAULT_CACHE = MARISA_NORMAL_CACHE
} marisa_cache_level;
enum marisa_cache_level {
MARISA_HUGE_CACHE = 0x00080,
MARISA_LARGE_CACHE = 0x00100,
MARISA_NORMAL_CACHE = 0x00200,
MARISA_SMALL_CACHE = 0x00400,
MARISA_TINY_CACHE = 0x00800,
MARISA_DEFAULT_CACHE = MARISA_NORMAL_CACHE
};
// This library provides 2 kinds of TAIL implementations.
typedef enum marisa_tail_mode_ {
enum marisa_tail_mode {
// MARISA_TEXT_TAIL merges last labels as zero-terminated strings. So, it is
// available if and only if the last labels do not contain a NULL character.
// If MARISA_TEXT_TAIL is specified and a NULL character exists in the last
// labels, the setting is automatically switched to MARISA_BINARY_TAIL.
MARISA_TEXT_TAIL = 0x01000,
MARISA_TEXT_TAIL = 0x01000,
// MARISA_BINARY_TAIL also merges last labels but as byte sequences. It uses
// a bit vector to detect the end of a sequence, instead of NULL characters.
// So, MARISA_BINARY_TAIL requires a larger space if the average length of
// labels is greater than 8.
MARISA_BINARY_TAIL = 0x02000,
MARISA_BINARY_TAIL = 0x02000,
MARISA_DEFAULT_TAIL = MARISA_TEXT_TAIL,
} marisa_tail_mode;
MARISA_DEFAULT_TAIL = MARISA_TEXT_TAIL,
};
// The arrangement of nodes affects the time cost of matching and the order of
// predictive search.
typedef enum marisa_node_order_ {
enum marisa_node_order {
// MARISA_LABEL_ORDER arranges nodes in ascending label order.
// MARISA_LABEL_ORDER is useful if an application needs to predict keys in
// label order.
MARISA_LABEL_ORDER = 0x10000,
MARISA_LABEL_ORDER = 0x10000,
// MARISA_WEIGHT_ORDER arranges nodes in descending weight order.
// MARISA_WEIGHT_ORDER is generally a better choice because it enables faster
// matching.
MARISA_WEIGHT_ORDER = 0x20000,
MARISA_WEIGHT_ORDER = 0x20000,
MARISA_DEFAULT_ORDER = MARISA_WEIGHT_ORDER,
} marisa_node_order;
MARISA_DEFAULT_ORDER = MARISA_WEIGHT_ORDER,
};
typedef enum marisa_config_mask_ {
MARISA_NUM_TRIES_MASK = 0x0007F,
MARISA_CACHE_LEVEL_MASK = 0x00F80,
MARISA_TAIL_MODE_MASK = 0x0F000,
MARISA_NODE_ORDER_MASK = 0xF0000,
MARISA_CONFIG_MASK = 0xFFFFF
} marisa_config_mask;
enum marisa_config_mask {
MARISA_NUM_TRIES_MASK = 0x0007F,
MARISA_CACHE_LEVEL_MASK = 0x00F80,
MARISA_TAIL_MODE_MASK = 0x0F000,
MARISA_NODE_ORDER_MASK = 0xF0000,
MARISA_CONFIG_MASK = 0xFFFFF
};
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#ifdef __cplusplus
// `std::swap` is in <utility> since C++ 11 but in <algorithm> in C++ 98:
#if __cplusplus >= 201103L
#include <utility>
#else
#include <algorithm>
#endif
namespace marisa {
typedef ::marisa_uint8 UInt8;
typedef ::marisa_uint16 UInt16;
typedef ::marisa_uint32 UInt32;
typedef ::marisa_uint64 UInt64;
// These aliases are left for backward compatibility.
using UInt8 [[deprecated]] = std::uint8_t;
using UInt16 [[deprecated]] = std::uint16_t;
using UInt32 [[deprecated]] = std::uint32_t;
using UInt64 [[deprecated]] = std::uint64_t;
typedef ::marisa_error_code ErrorCode;
using std::uint16_t;
using std::uint32_t;
using std::uint64_t;
using std::uint8_t;
typedef ::marisa_cache_level CacheLevel;
typedef ::marisa_tail_mode TailMode;
typedef ::marisa_node_order NodeOrder;
using ErrorCode = marisa_error_code;
using CacheLevel = marisa_cache_level;
using TailMode = marisa_tail_mode;
using NodeOrder = marisa_node_order;
// This is left for backward compatibility.
using std::swap;
} // namespace marisa
#endif // __cplusplus
// This is left for backward compatibility.
using Exception = std::exception;
#ifdef __cplusplus
#include "marisa/exception.h"
#include "marisa/scoped-ptr.h"
#include "marisa/scoped-array.h"
#endif // __cplusplus
} // namespace marisa
// These macros are used to convert a line number to a string constant.
#define MARISA_INT_TO_STR(value) #value
#define MARISA_LINE_TO_STR(line) MARISA_INT_TO_STR(line)
#define MARISA_LINE_STR MARISA_LINE_TO_STR(__LINE__)
// MARISA_THROW throws an exception with a filename, a line number, an error
// code and an error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
#define MARISA_THROW(error_type, error_message) \
(throw (error_type)(__FILE__ ":" MARISA_LINE_STR ": " #error_type \
": " error_message))
// MARISA_THROW_IF throws an exception if `condition' is true.
#define MARISA_THROW_IF(condition, error_type) \
(void)((!(condition)) || (MARISA_THROW(error_type, #condition), 0))
// MARISA_THROW_SYSTEM_ERROR_IF throws an exception if `condition` is true.
// ::GetLastError() or errno should be passed as `error_value`.
#define MARISA_THROW_SYSTEM_ERROR_IF(condition, error_value, error_category, \
function_name) \
(void)((!(condition)) || \
(throw std::system_error( \
std::error_code(error_value, error_category), \
__FILE__ ":" MARISA_LINE_STR \
": std::system_error: " function_name ": " #condition), \
false))
// #ifndef MARISA_USE_EXCEPTIONS
// #if defined(__GNUC__) && !defined(__EXCEPTIONS)
// #define MARISA_USE_EXCEPTIONS 0
// #elif defined(__clang__) && !defined(__cpp_exceptions)
// #define MARISA_USE_EXCEPTIONS 0
// #elif defined(_MSC_VER) && !_HAS_EXCEPTIONS
// #define MARISA_USE_EXCEPTIONS 0
// #else
// #define MARISA_USE_EXCEPTIONS 1
// #endif
// #endif
// #if MARISA_USE_EXCEPTIONS
// #define MARISA_TRY try
// #define MARISA_CATCH(x) catch (x)
// #else
// #define MARISA_TRY if (true)
// #define MARISA_CATCH(x) if (false)
// #endif
#endif // MARISA_BASE_H_

View file

@ -1,82 +0,0 @@
#ifndef MARISA_EXCEPTION_H_
#define MARISA_EXCEPTION_H_
#include <exception>
#include "marisa/base.h"
namespace marisa {
// An exception object keeps a filename, a line number, an error code and an
// error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
class Exception : public std::exception {
public:
Exception(const char *filename, int line,
ErrorCode error_code, const char *error_message)
: std::exception(), filename_(filename), line_(line),
error_code_(error_code), error_message_(error_message) {}
Exception(const Exception &ex)
: std::exception(), filename_(ex.filename_), line_(ex.line_),
error_code_(ex.error_code_), error_message_(ex.error_message_) {}
virtual ~Exception() throw() {}
Exception &operator=(const Exception &rhs) {
filename_ = rhs.filename_;
line_ = rhs.line_;
error_code_ = rhs.error_code_;
error_message_ = rhs.error_message_;
return *this;
}
const char *filename() const {
return filename_;
}
int line() const {
return line_;
}
ErrorCode error_code() const {
return error_code_;
}
const char *error_message() const {
return error_message_;
}
virtual const char *what() const throw() {
return error_message_;
}
private:
const char *filename_;
int line_;
ErrorCode error_code_;
const char *error_message_;
};
// These macros are used to convert a line number to a string constant.
#define MARISA_INT_TO_STR(value) #value
#define MARISA_LINE_TO_STR(line) MARISA_INT_TO_STR(line)
#define MARISA_LINE_STR MARISA_LINE_TO_STR(__LINE__)
// MARISA_THROW throws an exception with a filename, a line number, an error
// code and an error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
#define MARISA_THROW(error_code, error_message) \
(throw marisa::Exception(__FILE__, __LINE__, error_code, \
__FILE__ ":" MARISA_LINE_STR ": " #error_code ": " error_message))
// MARISA_THROW_IF throws an exception if `condition' is true.
#define MARISA_THROW_IF(condition, error_code) \
(void)((!(condition)) || (MARISA_THROW(error_code, #condition), 0))
// MARISA_DEBUG_IF is ignored if _DEBUG is undefined. So, it is useful for
// debugging time-critical codes.
#ifdef _DEBUG
#define MARISA_DEBUG_IF(cond, error_code) MARISA_THROW_IF(cond, error_code)
#else
#define MARISA_DEBUG_IF(cond, error_code)
#endif
} // namespace marisa
#endif // MARISA_EXCEPTION_H_

View file

@ -1,9 +1,8 @@
#ifndef MARISA_KEY_H_
#define MARISA_KEY_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <string_view>
#include "marisa/base.h"
@ -11,58 +10,45 @@ namespace marisa {
class Key {
public:
Key() : ptr_(NULL), length_(0), union_() {
union_.id = 0;
}
Key(const Key &key)
: ptr_(key.ptr_), length_(key.length_), union_(key.union_) {}
Key &operator=(const Key &key) {
ptr_ = key.ptr_;
length_ = key.length_;
union_ = key.union_;
return *this;
}
Key() = default;
Key(const Key &key) = default;
Key &operator=(const Key &key) = default;
char operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR);
assert(i < length_);
return ptr_[i];
}
#if __cplusplus >= 201703L
void set_str(std::string_view str) {
set_str(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_str(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
std::size_t length = 0;
while (str[length] != '\0') {
++length;
}
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert(length <= UINT32_MAX);
ptr_ = str;
length_ = (UInt32)length;
length_ = static_cast<uint32_t>(length);
}
void set_str(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert((ptr != nullptr) || (length == 0));
assert(length <= UINT32_MAX);
ptr_ = ptr;
length_ = (UInt32)length;
length_ = static_cast<uint32_t>(length);
}
void set_id(std::size_t id) {
MARISA_DEBUG_IF(id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
union_.id = (UInt32)id;
assert(id <= UINT32_MAX);
union_.id = static_cast<uint32_t>(id);
}
void set_weight(float weight) {
union_.weight = weight;
}
#if __cplusplus >= 201703L
std::string_view str() const {
return std::string_view(ptr_, length_);
}
#endif // __cplusplus >= 201703L
const char *ptr() const {
return ptr_;
}
@ -76,20 +62,20 @@ class Key {
return union_.weight;
}
void clear() {
void clear() noexcept {
Key().swap(*this);
}
void swap(Key &rhs) {
marisa::swap(ptr_, rhs.ptr_);
marisa::swap(length_, rhs.length_);
marisa::swap(union_.id, rhs.union_.id);
void swap(Key &rhs) noexcept {
std::swap(ptr_, rhs.ptr_);
std::swap(length_, rhs.length_);
std::swap(union_.id, rhs.union_.id);
}
private:
const char *ptr_;
UInt32 length_;
const char *ptr_ = nullptr;
uint32_t length_ = 0;
union Union {
UInt32 id;
uint32_t id = 0;
float weight;
} union_;
};

View file

@ -1,9 +1,9 @@
#ifndef MARISA_KEYSET_H_
#define MARISA_KEYSET_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <memory>
#include <string_view>
#include "marisa/key.h"
@ -12,30 +12,31 @@ namespace marisa {
class Keyset {
public:
enum {
BASE_BLOCK_SIZE = 4096,
BASE_BLOCK_SIZE = 4096,
EXTRA_BLOCK_SIZE = 1024,
KEY_BLOCK_SIZE = 256
KEY_BLOCK_SIZE = 256
};
Keyset();
Keyset(const Keyset &) = delete;
Keyset &operator=(const Keyset &) = delete;
void push_back(const Key &key);
void push_back(const Key &key, char end_marker);
#if __cplusplus >= 201703L
void push_back(std::string_view str, float weight = 1.0) {
push_back(str.data(), str.length(), weight);
}
#endif // __cplusplus >= 201703L
void push_back(const char *str);
void push_back(const char *ptr, std::size_t length, float weight = 1.0);
const Key &operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
assert(i < size_);
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
}
Key &operator[](std::size_t i) {
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
assert(i < size_);
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
}
@ -55,33 +56,29 @@ class Keyset {
void reset();
void clear();
void swap(Keyset &rhs);
void clear() noexcept;
void swap(Keyset &rhs) noexcept;
private:
scoped_array<scoped_array<char> > base_blocks_;
std::size_t base_blocks_size_;
std::size_t base_blocks_capacity_;
scoped_array<scoped_array<char> > extra_blocks_;
std::size_t extra_blocks_size_;
std::size_t extra_blocks_capacity_;
scoped_array<scoped_array<Key> > key_blocks_;
std::size_t key_blocks_size_;
std::size_t key_blocks_capacity_;
char *ptr_;
std::size_t avail_;
std::size_t size_;
std::size_t total_length_;
std::unique_ptr<std::unique_ptr<char[]>[]> base_blocks_;
std::size_t base_blocks_size_ = 0;
std::size_t base_blocks_capacity_ = 0;
std::unique_ptr<std::unique_ptr<char[]>[]> extra_blocks_;
std::size_t extra_blocks_size_ = 0;
std::size_t extra_blocks_capacity_ = 0;
std::unique_ptr<std::unique_ptr<Key[]>[]> key_blocks_;
std::size_t key_blocks_size_ = 0;
std::size_t key_blocks_capacity_ = 0;
char *ptr_ = nullptr;
std::size_t avail_ = 0;
std::size_t size_ = 0;
std::size_t total_length_ = 0;
char *reserve(std::size_t size);
void append_base_block();
void append_extra_block(std::size_t size);
void append_key_block();
// Disallows copy and assignment.
Keyset(const Keyset &);
Keyset &operator=(const Keyset &);
};
} // namespace marisa

View file

@ -1,9 +1,8 @@
#ifndef MARISA_QUERY_H_
#define MARISA_QUERY_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <string_view>
#include "marisa/base.h"
@ -11,29 +10,21 @@ namespace marisa {
class Query {
public:
Query() : ptr_(NULL), length_(0), id_(0) {}
Query(const Query &query)
: ptr_(query.ptr_), length_(query.length_), id_(query.id_) {}
Query() = default;
Query(const Query &query) = default;
Query &operator=(const Query &query) {
ptr_ = query.ptr_;
length_ = query.length_;
id_ = query.id_;
return *this;
}
Query &operator=(const Query &query) = default;
char operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR);
assert(i < length_);
return ptr_[i];
}
#if __cplusplus >= 201703L
void set_str(std::string_view str) {
set_str(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_str(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
std::size_t length = 0;
while (str[length] != '\0') {
++length;
@ -42,7 +33,7 @@ class Query {
length_ = length;
}
void set_str(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
assert((ptr != nullptr) || (length == 0));
ptr_ = ptr;
length_ = length;
}
@ -50,11 +41,9 @@ class Query {
id_ = id;
}
#if __cplusplus >= 201703L
std::string_view str() const {
return std::string_view(ptr_, length_);
}
#endif // __cplusplus >= 201703L
const char *ptr() const {
return ptr_;
}
@ -65,19 +54,19 @@ class Query {
return id_;
}
void clear() {
void clear() noexcept {
Query().swap(*this);
}
void swap(Query &rhs) {
marisa::swap(ptr_, rhs.ptr_);
marisa::swap(length_, rhs.length_);
marisa::swap(id_, rhs.id_);
void swap(Query &rhs) noexcept {
std::swap(ptr_, rhs.ptr_);
std::swap(length_, rhs.length_);
std::swap(id_, rhs.id_);
}
private:
const char *ptr_;
std::size_t length_;
std::size_t id_;
const char *ptr_ = nullptr;
std::size_t length_ = 0;
std::size_t id_ = 0;
};
} // namespace marisa

View file

@ -1,48 +0,0 @@
#ifndef MARISA_SCOPED_ARRAY_H_
#define MARISA_SCOPED_ARRAY_H_
#include "marisa/base.h"
namespace marisa {
template <typename T>
class scoped_array {
public:
scoped_array() : array_(NULL) {}
explicit scoped_array(T *array) : array_(array) {}
~scoped_array() {
delete [] array_;
}
void reset(T *array = NULL) {
MARISA_DEBUG_IF((array != NULL) && (array == array_), MARISA_RESET_ERROR);
scoped_array(array).swap(*this);
}
T &operator[](std::size_t i) const {
MARISA_DEBUG_IF(array_ == NULL, MARISA_STATE_ERROR);
return array_[i];
}
T *get() const {
return array_;
}
void clear() {
scoped_array().swap(*this);
}
void swap(scoped_array &rhs) {
marisa::swap(array_, rhs.array_);
}
private:
T *array_;
// Disallows copy and assignment.
scoped_array(const scoped_array &);
scoped_array &operator=(const scoped_array &);
};
} // namespace marisa
#endif // MARISA_SCOPED_ARRAY_H_

View file

@ -1,52 +0,0 @@
#ifndef MARISA_SCOPED_PTR_H_
#define MARISA_SCOPED_PTR_H_
#include "marisa/base.h"
namespace marisa {
template <typename T>
class scoped_ptr {
public:
scoped_ptr() : ptr_(NULL) {}
explicit scoped_ptr(T *ptr) : ptr_(ptr) {}
~scoped_ptr() {
delete ptr_;
}
void reset(T *ptr = NULL) {
MARISA_DEBUG_IF((ptr != NULL) && (ptr == ptr_), MARISA_RESET_ERROR);
scoped_ptr(ptr).swap(*this);
}
T &operator*() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return *ptr_;
}
T *operator->() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return ptr_;
}
T *get() const {
return ptr_;
}
void clear() {
scoped_ptr().swap(*this);
}
void swap(scoped_ptr &rhs) {
marisa::swap(ptr_, rhs.ptr_);
}
private:
T *ptr_;
// Disallows copy and assignment.
scoped_ptr(const scoped_ptr &);
scoped_ptr &operator=(const scoped_ptr &);
};
} // namespace marisa
#endif // MARISA_SCOPED_PTR_H_

View file

@ -1,17 +1,17 @@
#ifndef MARISA_TRIE_H_
#define MARISA_TRIE_H_
#include "marisa/keyset.h"
#include "marisa/agent.h"
#include <memory>
#include "marisa/agent.h" // IWYU pragma: export
#include "marisa/keyset.h" // IWYU pragma: export
namespace marisa {
namespace grimoire {
namespace trie {
namespace grimoire::trie {
class LoudsTrie;
} // namespace trie
} // namespace grimoire
} // namespace grimoire::trie
class Trie {
friend class TrieIO;
@ -20,9 +20,15 @@ class Trie {
Trie();
~Trie();
Trie(const Trie &) = delete;
Trie &operator=(const Trie &) = delete;
Trie(Trie &&) noexcept;
Trie &operator=(Trie &&) noexcept;
void build(Keyset &keyset, int config_flags = 0);
void mmap(const char *filename);
void mmap(const char *filename, int flags = 0);
void map(const void *ptr, std::size_t size);
void load(const char *filename);
@ -48,15 +54,11 @@ class Trie {
std::size_t total_size() const;
std::size_t io_size() const;
void clear();
void swap(Trie &rhs);
void clear() noexcept;
void swap(Trie &rhs) noexcept;
private:
scoped_ptr<grimoire::trie::LoudsTrie> trie_;
// Disallows copy and assignment.
Trie(const Trie &);
Trie &operator=(const Trie &);
std::unique_ptr<grimoire::trie::LoudsTrie> trie_;
};
} // namespace marisa

View file

@ -1,7 +1,7 @@
####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was marisa-config.cmake.in ########
####### The input file was Marisa.cmake.in ########
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
@ -24,8 +24,6 @@ endmacro()
####################################################################################
include(${CMAKE_CURRENT_LIST_DIR}/marisa-targets.cmake)
check_required_components(Marisa)
set_and_check(MARISA_INCLUDE_DIR ${PACKAGE_PREFIX_DIR}/include)
check_required_components(marisa)
include("${CMAKE_CURRENT_LIST_DIR}/MarisaTargets.cmake")

View file

@ -9,19 +9,19 @@
# The variable CVF_VERSION must be set before calling configure_file().
set(PACKAGE_VERSION "0.2.6")
set(PACKAGE_VERSION "0.3.1")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
if("0.2.6" MATCHES "^([0-9]+)\\.")
if("0.3.1" MATCHES "^([0-9]+)\\.")
set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}")
if(NOT CVF_VERSION_MAJOR VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" CVF_VERSION_MAJOR "${CVF_VERSION_MAJOR}")
endif()
else()
set(CVF_VERSION_MAJOR "0.2.6")
set(CVF_VERSION_MAJOR "0.3.1")
endif()
if(PACKAGE_FIND_VERSION_RANGE)

View file

@ -5,15 +5,15 @@
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Import target "marisa" for configuration "Release"
set_property(TARGET marisa APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(marisa PROPERTIES
# Import target "Marisa::marisa" for configuration "Release"
set_property(TARGET Marisa::marisa APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(Marisa::marisa PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libmarisa.a"
)
list(APPEND _cmake_import_check_targets marisa )
list(APPEND _cmake_import_check_files_for_marisa "${_IMPORT_PREFIX}/lib/libmarisa.a" )
list(APPEND _cmake_import_check_targets Marisa::marisa )
list(APPEND _cmake_import_check_files_for_Marisa::marisa "${_IMPORT_PREFIX}/lib/libmarisa.a" )
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)

View file

@ -19,7 +19,7 @@ set(CMAKE_IMPORT_FILE_VERSION 1)
set(_cmake_targets_defined "")
set(_cmake_targets_not_defined "")
set(_cmake_expected_targets "")
foreach(_cmake_expected_target IN ITEMS marisa)
foreach(_cmake_expected_target IN ITEMS Marisa::marisa)
list(APPEND _cmake_expected_targets "${_cmake_expected_target}")
if(TARGET "${_cmake_expected_target}")
list(APPEND _cmake_targets_defined "${_cmake_expected_target}")
@ -55,15 +55,15 @@ if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
# Create imported target marisa
add_library(marisa STATIC IMPORTED)
# Create imported target Marisa::marisa
add_library(Marisa::marisa STATIC IMPORTED)
set_target_properties(marisa PROPERTIES
set_target_properties(Marisa::marisa PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
)
# Load information for each installed configuration.
file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/marisa-targets-*.cmake")
file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/MarisaTargets-*.cmake")
foreach(_cmake_config_file IN LISTS _cmake_config_files)
include("${_cmake_config_file}")
endforeach()

Binary file not shown.

View file

@ -2,13 +2,13 @@
#define MARISA_H_
// "marisa/stdio.h" includes <cstdio> for I/O using std::FILE.
#include "marisa/stdio.h"
#include "marisa/stdio.h" // IWYU pragma: export
// "marisa/iostream.h" includes <iosfwd> for I/O using std::iostream.
#include "marisa/iostream.h"
#include "marisa/iostream.h" // IWYU pragma: export
// You can use <marisa/trie.h> instead of <marisa.h> if you don't need the
// above I/O interfaces and don't want to include the above I/O headers.
#include "marisa/trie.h"
#include "marisa/trie.h" // IWYU pragma: export
#endif // MARISA_H_

View file

@ -1,27 +1,30 @@
#ifndef MARISA_AGENT_H_
#define MARISA_AGENT_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <memory>
#include <string_view>
#include "marisa/key.h"
#include "marisa/query.h"
namespace marisa {
namespace grimoire {
namespace trie {
namespace grimoire::trie {
class State;
} // namespace trie
} // namespace grimoire
} // namespace grimoire::trie
class Agent {
public:
Agent();
~Agent();
Agent(const Agent &other);
Agent &operator=(const Agent &other);
Agent(Agent &&other) noexcept;
Agent &operator=(Agent &&other) noexcept;
const Query &query() const {
return query_;
}
@ -29,11 +32,9 @@ class Agent {
return key_;
}
#if __cplusplus >= 201703L
void set_query(std::string_view str) {
set_query(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_query(const char *str);
void set_query(const char *ptr, std::size_t length);
void set_query(std::size_t key_id);
@ -45,41 +46,35 @@ class Agent {
return *state_;
}
#if __cplusplus >= 201703L
void set_key(std::string_view str) {
set_key(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_key(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
key_.set_str(str);
}
void set_key(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert((ptr != nullptr) || (length == 0));
assert(length <= UINT32_MAX);
key_.set_str(ptr, length);
}
void set_key(std::size_t id) {
MARISA_DEBUG_IF(id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert(id <= UINT32_MAX);
key_.set_id(id);
}
bool has_state() const {
return state_.get() != NULL;
return state_ != nullptr;
}
void init_state();
void clear();
void swap(Agent &rhs);
void clear() noexcept;
void swap(Agent &rhs) noexcept;
private:
Query query_;
Key key_;
scoped_ptr<grimoire::trie::State> state_;
// Disallows copy and assignment.
Agent(const Agent &);
Agent &operator=(const Agent &);
std::unique_ptr<grimoire::trie::State> state_;
};
} // namespace marisa

View file

@ -1,95 +1,85 @@
#ifndef MARISA_BASE_H_
#define MARISA_BASE_H_
// Old Visual C++ does not provide stdint.h.
#ifndef _MSC_VER
#include <stdint.h>
#endif // _MSC_VER
#include <cstddef>
#include <cstdint>
#include <exception>
#include <system_error>
#include <utility>
#ifdef __cplusplus
#include <cstddef>
#else // __cplusplus
#include <stddef.h>
#endif // __cplusplus
// These aliases are left for backward compatibility.
using marisa_uint8 [[deprecated]] = std::uint8_t;
using marisa_uint16 [[deprecated]] = std::uint16_t;
using marisa_uint32 [[deprecated]] = std::uint32_t;
using marisa_uint64 [[deprecated]] = std::uint64_t;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#ifdef _MSC_VER
typedef unsigned __int8 marisa_uint8;
typedef unsigned __int16 marisa_uint16;
typedef unsigned __int32 marisa_uint32;
typedef unsigned __int64 marisa_uint64;
#else // _MSC_VER
typedef uint8_t marisa_uint8;
typedef uint16_t marisa_uint16;
typedef uint32_t marisa_uint32;
typedef uint64_t marisa_uint64;
#endif // _MSC_VER
#if defined(_WIN64) || defined(__amd64__) || defined(__x86_64__) || \
defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__) || \
defined(__sparc64__) || defined(__mips64__) || defined(__aarch64__) || \
defined(__s390x__)
#if UINTPTR_MAX == UINT64_MAX
#define MARISA_WORD_SIZE 64
#else // defined(_WIN64), etc.
#elif UINTPTR_MAX == UINT32_MAX
#define MARISA_WORD_SIZE 32
#endif // defined(_WIN64), etc.
#else
#error Failed to detect MARISA_WORD_SIZE
#endif
//#define MARISA_WORD_SIZE (sizeof(void *) * 8)
// These constant variables are left for backward compatibility.
[[deprecated]] constexpr auto MARISA_UINT8_MAX = UINT8_MAX;
[[deprecated]] constexpr auto MARISA_UINT16_MAX = UINT16_MAX;
[[deprecated]] constexpr auto MARISA_UINT32_MAX = UINT32_MAX;
[[deprecated]] constexpr auto MARISA_UINT64_MAX = UINT64_MAX;
[[deprecated]] constexpr auto MARISA_SIZE_MAX = SIZE_MAX;
#define MARISA_UINT8_MAX ((marisa_uint8)~(marisa_uint8)0)
#define MARISA_UINT16_MAX ((marisa_uint16)~(marisa_uint16)0)
#define MARISA_UINT32_MAX ((marisa_uint32)~(marisa_uint32)0)
#define MARISA_UINT64_MAX ((marisa_uint64)~(marisa_uint64)0)
#define MARISA_SIZE_MAX ((size_t)~(size_t)0)
#define MARISA_INVALID_LINK_ID MARISA_UINT32_MAX
#define MARISA_INVALID_KEY_ID MARISA_UINT32_MAX
#define MARISA_INVALID_EXTRA (MARISA_UINT32_MAX >> 8)
#define MARISA_INVALID_LINK_ID UINT32_MAX
#define MARISA_INVALID_KEY_ID UINT32_MAX
#define MARISA_INVALID_EXTRA (UINT32_MAX >> 8)
// Error codes are defined as members of marisa_error_code. This library throws
// an exception with one of the error codes when an error occurs.
typedef enum marisa_error_code_ {
enum marisa_error_code {
// MARISA_OK means that a requested operation has succeeded. In practice, an
// exception never has MARISA_OK because it is not an error.
MARISA_OK = 0,
MARISA_OK = 0,
// MARISA_STATE_ERROR means that an object was not ready for a requested
// operation. For example, an operation to modify a fixed vector throws an
// exception with MARISA_STATE_ERROR.
MARISA_STATE_ERROR = 1,
MARISA_STATE_ERROR = 1,
// MARISA_NULL_ERROR means that an invalid NULL pointer has been given.
MARISA_NULL_ERROR = 2,
// MARISA_NULL_ERROR means that an invalid nullptr has been given.
MARISA_NULL_ERROR = 2,
// MARISA_BOUND_ERROR means that an operation has tried to access an out of
// range address.
MARISA_BOUND_ERROR = 3,
MARISA_BOUND_ERROR = 3,
// MARISA_RANGE_ERROR means that an out of range value has appeared in
// operation.
MARISA_RANGE_ERROR = 4,
MARISA_RANGE_ERROR = 4,
// MARISA_CODE_ERROR means that an undefined code has appeared in operation.
MARISA_CODE_ERROR = 5,
MARISA_CODE_ERROR = 5,
// MARISA_RESET_ERROR means that a smart pointer has tried to reset itself.
MARISA_RESET_ERROR = 6,
MARISA_RESET_ERROR = 6,
// MARISA_SIZE_ERROR means that a size has exceeded a library limitation.
MARISA_SIZE_ERROR = 7,
MARISA_SIZE_ERROR = 7,
// MARISA_MEMORY_ERROR means that a memory allocation has failed.
MARISA_MEMORY_ERROR = 8,
// MARISA_IO_ERROR means that an I/O operation has failed.
MARISA_IO_ERROR = 9,
MARISA_IO_ERROR = 9,
// MARISA_FORMAT_ERROR means that input was in invalid format.
MARISA_FORMAT_ERROR = 10,
} marisa_error_code;
};
// Flags for memory mapping are defined as members of marisa_map_flags.
// Trie::open() accepts a combination of these flags.
enum marisa_map_flags {
// MARISA_MAP_POPULATE specifies MAP_POPULATE.
MARISA_MAP_POPULATE = 1 << 0,
};
// Min/max values, flags and masks for dictionary settings are defined below.
// Please note that unspecified settings will be replaced with the default
@ -98,99 +88,136 @@ typedef enum marisa_error_code_ {
// A dictionary consists of 3 tries in default. Usually more tries make a
// dictionary space-efficient but time-inefficient.
typedef enum marisa_num_tries_ {
MARISA_MIN_NUM_TRIES = 0x00001,
MARISA_MAX_NUM_TRIES = 0x0007F,
enum marisa_num_tries {
MARISA_MIN_NUM_TRIES = 0x00001,
MARISA_MAX_NUM_TRIES = 0x0007F,
MARISA_DEFAULT_NUM_TRIES = 0x00003,
} marisa_num_tries;
};
// This library uses a cache technique to accelerate search functions. The
// following enumerated type marisa_cache_level gives a list of available cache
// size options. A larger cache enables faster search but takes a more space.
typedef enum marisa_cache_level_ {
MARISA_HUGE_CACHE = 0x00080,
MARISA_LARGE_CACHE = 0x00100,
MARISA_NORMAL_CACHE = 0x00200,
MARISA_SMALL_CACHE = 0x00400,
MARISA_TINY_CACHE = 0x00800,
MARISA_DEFAULT_CACHE = MARISA_NORMAL_CACHE
} marisa_cache_level;
enum marisa_cache_level {
MARISA_HUGE_CACHE = 0x00080,
MARISA_LARGE_CACHE = 0x00100,
MARISA_NORMAL_CACHE = 0x00200,
MARISA_SMALL_CACHE = 0x00400,
MARISA_TINY_CACHE = 0x00800,
MARISA_DEFAULT_CACHE = MARISA_NORMAL_CACHE
};
// This library provides 2 kinds of TAIL implementations.
typedef enum marisa_tail_mode_ {
enum marisa_tail_mode {
// MARISA_TEXT_TAIL merges last labels as zero-terminated strings. So, it is
// available if and only if the last labels do not contain a NULL character.
// If MARISA_TEXT_TAIL is specified and a NULL character exists in the last
// labels, the setting is automatically switched to MARISA_BINARY_TAIL.
MARISA_TEXT_TAIL = 0x01000,
MARISA_TEXT_TAIL = 0x01000,
// MARISA_BINARY_TAIL also merges last labels but as byte sequences. It uses
// a bit vector to detect the end of a sequence, instead of NULL characters.
// So, MARISA_BINARY_TAIL requires a larger space if the average length of
// labels is greater than 8.
MARISA_BINARY_TAIL = 0x02000,
MARISA_BINARY_TAIL = 0x02000,
MARISA_DEFAULT_TAIL = MARISA_TEXT_TAIL,
} marisa_tail_mode;
MARISA_DEFAULT_TAIL = MARISA_TEXT_TAIL,
};
// The arrangement of nodes affects the time cost of matching and the order of
// predictive search.
typedef enum marisa_node_order_ {
enum marisa_node_order {
// MARISA_LABEL_ORDER arranges nodes in ascending label order.
// MARISA_LABEL_ORDER is useful if an application needs to predict keys in
// label order.
MARISA_LABEL_ORDER = 0x10000,
MARISA_LABEL_ORDER = 0x10000,
// MARISA_WEIGHT_ORDER arranges nodes in descending weight order.
// MARISA_WEIGHT_ORDER is generally a better choice because it enables faster
// matching.
MARISA_WEIGHT_ORDER = 0x20000,
MARISA_WEIGHT_ORDER = 0x20000,
MARISA_DEFAULT_ORDER = MARISA_WEIGHT_ORDER,
} marisa_node_order;
MARISA_DEFAULT_ORDER = MARISA_WEIGHT_ORDER,
};
typedef enum marisa_config_mask_ {
MARISA_NUM_TRIES_MASK = 0x0007F,
MARISA_CACHE_LEVEL_MASK = 0x00F80,
MARISA_TAIL_MODE_MASK = 0x0F000,
MARISA_NODE_ORDER_MASK = 0xF0000,
MARISA_CONFIG_MASK = 0xFFFFF
} marisa_config_mask;
enum marisa_config_mask {
MARISA_NUM_TRIES_MASK = 0x0007F,
MARISA_CACHE_LEVEL_MASK = 0x00F80,
MARISA_TAIL_MODE_MASK = 0x0F000,
MARISA_NODE_ORDER_MASK = 0xF0000,
MARISA_CONFIG_MASK = 0xFFFFF
};
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#ifdef __cplusplus
// `std::swap` is in <utility> since C++ 11 but in <algorithm> in C++ 98:
#if __cplusplus >= 201103L
#include <utility>
#else
#include <algorithm>
#endif
namespace marisa {
typedef ::marisa_uint8 UInt8;
typedef ::marisa_uint16 UInt16;
typedef ::marisa_uint32 UInt32;
typedef ::marisa_uint64 UInt64;
// These aliases are left for backward compatibility.
using UInt8 [[deprecated]] = std::uint8_t;
using UInt16 [[deprecated]] = std::uint16_t;
using UInt32 [[deprecated]] = std::uint32_t;
using UInt64 [[deprecated]] = std::uint64_t;
typedef ::marisa_error_code ErrorCode;
using std::uint16_t;
using std::uint32_t;
using std::uint64_t;
using std::uint8_t;
typedef ::marisa_cache_level CacheLevel;
typedef ::marisa_tail_mode TailMode;
typedef ::marisa_node_order NodeOrder;
using ErrorCode = marisa_error_code;
using CacheLevel = marisa_cache_level;
using TailMode = marisa_tail_mode;
using NodeOrder = marisa_node_order;
// This is left for backward compatibility.
using std::swap;
} // namespace marisa
#endif // __cplusplus
// This is left for backward compatibility.
using Exception = std::exception;
#ifdef __cplusplus
#include "marisa/exception.h"
#include "marisa/scoped-ptr.h"
#include "marisa/scoped-array.h"
#endif // __cplusplus
} // namespace marisa
// These macros are used to convert a line number to a string constant.
#define MARISA_INT_TO_STR(value) #value
#define MARISA_LINE_TO_STR(line) MARISA_INT_TO_STR(line)
#define MARISA_LINE_STR MARISA_LINE_TO_STR(__LINE__)
// MARISA_THROW throws an exception with a filename, a line number, an error
// code and an error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
#define MARISA_THROW(error_type, error_message) \
(throw (error_type)(__FILE__ ":" MARISA_LINE_STR ": " #error_type \
": " error_message))
// MARISA_THROW_IF throws an exception if `condition' is true.
#define MARISA_THROW_IF(condition, error_type) \
(void)((!(condition)) || (MARISA_THROW(error_type, #condition), 0))
// MARISA_THROW_SYSTEM_ERROR_IF throws an exception if `condition` is true.
// ::GetLastError() or errno should be passed as `error_value`.
#define MARISA_THROW_SYSTEM_ERROR_IF(condition, error_value, error_category, \
function_name) \
(void)((!(condition)) || \
(throw std::system_error( \
std::error_code(error_value, error_category), \
__FILE__ ":" MARISA_LINE_STR \
": std::system_error: " function_name ": " #condition), \
false))
// #ifndef MARISA_USE_EXCEPTIONS
// #if defined(__GNUC__) && !defined(__EXCEPTIONS)
// #define MARISA_USE_EXCEPTIONS 0
// #elif defined(__clang__) && !defined(__cpp_exceptions)
// #define MARISA_USE_EXCEPTIONS 0
// #elif defined(_MSC_VER) && !_HAS_EXCEPTIONS
// #define MARISA_USE_EXCEPTIONS 0
// #else
// #define MARISA_USE_EXCEPTIONS 1
// #endif
// #endif
// #if MARISA_USE_EXCEPTIONS
// #define MARISA_TRY try
// #define MARISA_CATCH(x) catch (x)
// #else
// #define MARISA_TRY if (true)
// #define MARISA_CATCH(x) if (false)
// #endif
#endif // MARISA_BASE_H_

View file

@ -1,82 +0,0 @@
#ifndef MARISA_EXCEPTION_H_
#define MARISA_EXCEPTION_H_
#include <exception>
#include "marisa/base.h"
namespace marisa {
// An exception object keeps a filename, a line number, an error code and an
// error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
class Exception : public std::exception {
public:
Exception(const char *filename, int line,
ErrorCode error_code, const char *error_message)
: std::exception(), filename_(filename), line_(line),
error_code_(error_code), error_message_(error_message) {}
Exception(const Exception &ex)
: std::exception(), filename_(ex.filename_), line_(ex.line_),
error_code_(ex.error_code_), error_message_(ex.error_message_) {}
virtual ~Exception() throw() {}
Exception &operator=(const Exception &rhs) {
filename_ = rhs.filename_;
line_ = rhs.line_;
error_code_ = rhs.error_code_;
error_message_ = rhs.error_message_;
return *this;
}
const char *filename() const {
return filename_;
}
int line() const {
return line_;
}
ErrorCode error_code() const {
return error_code_;
}
const char *error_message() const {
return error_message_;
}
virtual const char *what() const throw() {
return error_message_;
}
private:
const char *filename_;
int line_;
ErrorCode error_code_;
const char *error_message_;
};
// These macros are used to convert a line number to a string constant.
#define MARISA_INT_TO_STR(value) #value
#define MARISA_LINE_TO_STR(line) MARISA_INT_TO_STR(line)
#define MARISA_LINE_STR MARISA_LINE_TO_STR(__LINE__)
// MARISA_THROW throws an exception with a filename, a line number, an error
// code and an error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
#define MARISA_THROW(error_code, error_message) \
(throw marisa::Exception(__FILE__, __LINE__, error_code, \
__FILE__ ":" MARISA_LINE_STR ": " #error_code ": " error_message))
// MARISA_THROW_IF throws an exception if `condition' is true.
#define MARISA_THROW_IF(condition, error_code) \
(void)((!(condition)) || (MARISA_THROW(error_code, #condition), 0))
// MARISA_DEBUG_IF is ignored if _DEBUG is undefined. So, it is useful for
// debugging time-critical codes.
#ifdef _DEBUG
#define MARISA_DEBUG_IF(cond, error_code) MARISA_THROW_IF(cond, error_code)
#else
#define MARISA_DEBUG_IF(cond, error_code)
#endif
} // namespace marisa
#endif // MARISA_EXCEPTION_H_

View file

@ -1,9 +1,8 @@
#ifndef MARISA_KEY_H_
#define MARISA_KEY_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <string_view>
#include "marisa/base.h"
@ -11,58 +10,45 @@ namespace marisa {
class Key {
public:
Key() : ptr_(NULL), length_(0), union_() {
union_.id = 0;
}
Key(const Key &key)
: ptr_(key.ptr_), length_(key.length_), union_(key.union_) {}
Key &operator=(const Key &key) {
ptr_ = key.ptr_;
length_ = key.length_;
union_ = key.union_;
return *this;
}
Key() = default;
Key(const Key &key) = default;
Key &operator=(const Key &key) = default;
char operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR);
assert(i < length_);
return ptr_[i];
}
#if __cplusplus >= 201703L
void set_str(std::string_view str) {
set_str(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_str(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
std::size_t length = 0;
while (str[length] != '\0') {
++length;
}
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert(length <= UINT32_MAX);
ptr_ = str;
length_ = (UInt32)length;
length_ = static_cast<uint32_t>(length);
}
void set_str(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert((ptr != nullptr) || (length == 0));
assert(length <= UINT32_MAX);
ptr_ = ptr;
length_ = (UInt32)length;
length_ = static_cast<uint32_t>(length);
}
void set_id(std::size_t id) {
MARISA_DEBUG_IF(id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
union_.id = (UInt32)id;
assert(id <= UINT32_MAX);
union_.id = static_cast<uint32_t>(id);
}
void set_weight(float weight) {
union_.weight = weight;
}
#if __cplusplus >= 201703L
std::string_view str() const {
return std::string_view(ptr_, length_);
}
#endif // __cplusplus >= 201703L
const char *ptr() const {
return ptr_;
}
@ -76,20 +62,20 @@ class Key {
return union_.weight;
}
void clear() {
void clear() noexcept {
Key().swap(*this);
}
void swap(Key &rhs) {
marisa::swap(ptr_, rhs.ptr_);
marisa::swap(length_, rhs.length_);
marisa::swap(union_.id, rhs.union_.id);
void swap(Key &rhs) noexcept {
std::swap(ptr_, rhs.ptr_);
std::swap(length_, rhs.length_);
std::swap(union_.id, rhs.union_.id);
}
private:
const char *ptr_;
UInt32 length_;
const char *ptr_ = nullptr;
uint32_t length_ = 0;
union Union {
UInt32 id;
uint32_t id = 0;
float weight;
} union_;
};

View file

@ -1,9 +1,9 @@
#ifndef MARISA_KEYSET_H_
#define MARISA_KEYSET_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <memory>
#include <string_view>
#include "marisa/key.h"
@ -12,30 +12,31 @@ namespace marisa {
class Keyset {
public:
enum {
BASE_BLOCK_SIZE = 4096,
BASE_BLOCK_SIZE = 4096,
EXTRA_BLOCK_SIZE = 1024,
KEY_BLOCK_SIZE = 256
KEY_BLOCK_SIZE = 256
};
Keyset();
Keyset(const Keyset &) = delete;
Keyset &operator=(const Keyset &) = delete;
void push_back(const Key &key);
void push_back(const Key &key, char end_marker);
#if __cplusplus >= 201703L
void push_back(std::string_view str, float weight = 1.0) {
push_back(str.data(), str.length(), weight);
}
#endif // __cplusplus >= 201703L
void push_back(const char *str);
void push_back(const char *ptr, std::size_t length, float weight = 1.0);
const Key &operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
assert(i < size_);
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
}
Key &operator[](std::size_t i) {
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
assert(i < size_);
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
}
@ -55,33 +56,29 @@ class Keyset {
void reset();
void clear();
void swap(Keyset &rhs);
void clear() noexcept;
void swap(Keyset &rhs) noexcept;
private:
scoped_array<scoped_array<char> > base_blocks_;
std::size_t base_blocks_size_;
std::size_t base_blocks_capacity_;
scoped_array<scoped_array<char> > extra_blocks_;
std::size_t extra_blocks_size_;
std::size_t extra_blocks_capacity_;
scoped_array<scoped_array<Key> > key_blocks_;
std::size_t key_blocks_size_;
std::size_t key_blocks_capacity_;
char *ptr_;
std::size_t avail_;
std::size_t size_;
std::size_t total_length_;
std::unique_ptr<std::unique_ptr<char[]>[]> base_blocks_;
std::size_t base_blocks_size_ = 0;
std::size_t base_blocks_capacity_ = 0;
std::unique_ptr<std::unique_ptr<char[]>[]> extra_blocks_;
std::size_t extra_blocks_size_ = 0;
std::size_t extra_blocks_capacity_ = 0;
std::unique_ptr<std::unique_ptr<Key[]>[]> key_blocks_;
std::size_t key_blocks_size_ = 0;
std::size_t key_blocks_capacity_ = 0;
char *ptr_ = nullptr;
std::size_t avail_ = 0;
std::size_t size_ = 0;
std::size_t total_length_ = 0;
char *reserve(std::size_t size);
void append_base_block();
void append_extra_block(std::size_t size);
void append_key_block();
// Disallows copy and assignment.
Keyset(const Keyset &);
Keyset &operator=(const Keyset &);
};
} // namespace marisa

View file

@ -1,9 +1,8 @@
#ifndef MARISA_QUERY_H_
#define MARISA_QUERY_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <string_view>
#include "marisa/base.h"
@ -11,29 +10,21 @@ namespace marisa {
class Query {
public:
Query() : ptr_(NULL), length_(0), id_(0) {}
Query(const Query &query)
: ptr_(query.ptr_), length_(query.length_), id_(query.id_) {}
Query() = default;
Query(const Query &query) = default;
Query &operator=(const Query &query) {
ptr_ = query.ptr_;
length_ = query.length_;
id_ = query.id_;
return *this;
}
Query &operator=(const Query &query) = default;
char operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR);
assert(i < length_);
return ptr_[i];
}
#if __cplusplus >= 201703L
void set_str(std::string_view str) {
set_str(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_str(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
std::size_t length = 0;
while (str[length] != '\0') {
++length;
@ -42,7 +33,7 @@ class Query {
length_ = length;
}
void set_str(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
assert((ptr != nullptr) || (length == 0));
ptr_ = ptr;
length_ = length;
}
@ -50,11 +41,9 @@ class Query {
id_ = id;
}
#if __cplusplus >= 201703L
std::string_view str() const {
return std::string_view(ptr_, length_);
}
#endif // __cplusplus >= 201703L
const char *ptr() const {
return ptr_;
}
@ -65,19 +54,19 @@ class Query {
return id_;
}
void clear() {
void clear() noexcept {
Query().swap(*this);
}
void swap(Query &rhs) {
marisa::swap(ptr_, rhs.ptr_);
marisa::swap(length_, rhs.length_);
marisa::swap(id_, rhs.id_);
void swap(Query &rhs) noexcept {
std::swap(ptr_, rhs.ptr_);
std::swap(length_, rhs.length_);
std::swap(id_, rhs.id_);
}
private:
const char *ptr_;
std::size_t length_;
std::size_t id_;
const char *ptr_ = nullptr;
std::size_t length_ = 0;
std::size_t id_ = 0;
};
} // namespace marisa

View file

@ -1,48 +0,0 @@
#ifndef MARISA_SCOPED_ARRAY_H_
#define MARISA_SCOPED_ARRAY_H_
#include "marisa/base.h"
namespace marisa {
template <typename T>
class scoped_array {
public:
scoped_array() : array_(NULL) {}
explicit scoped_array(T *array) : array_(array) {}
~scoped_array() {
delete [] array_;
}
void reset(T *array = NULL) {
MARISA_DEBUG_IF((array != NULL) && (array == array_), MARISA_RESET_ERROR);
scoped_array(array).swap(*this);
}
T &operator[](std::size_t i) const {
MARISA_DEBUG_IF(array_ == NULL, MARISA_STATE_ERROR);
return array_[i];
}
T *get() const {
return array_;
}
void clear() {
scoped_array().swap(*this);
}
void swap(scoped_array &rhs) {
marisa::swap(array_, rhs.array_);
}
private:
T *array_;
// Disallows copy and assignment.
scoped_array(const scoped_array &);
scoped_array &operator=(const scoped_array &);
};
} // namespace marisa
#endif // MARISA_SCOPED_ARRAY_H_

View file

@ -1,52 +0,0 @@
#ifndef MARISA_SCOPED_PTR_H_
#define MARISA_SCOPED_PTR_H_
#include "marisa/base.h"
namespace marisa {
template <typename T>
class scoped_ptr {
public:
scoped_ptr() : ptr_(NULL) {}
explicit scoped_ptr(T *ptr) : ptr_(ptr) {}
~scoped_ptr() {
delete ptr_;
}
void reset(T *ptr = NULL) {
MARISA_DEBUG_IF((ptr != NULL) && (ptr == ptr_), MARISA_RESET_ERROR);
scoped_ptr(ptr).swap(*this);
}
T &operator*() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return *ptr_;
}
T *operator->() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return ptr_;
}
T *get() const {
return ptr_;
}
void clear() {
scoped_ptr().swap(*this);
}
void swap(scoped_ptr &rhs) {
marisa::swap(ptr_, rhs.ptr_);
}
private:
T *ptr_;
// Disallows copy and assignment.
scoped_ptr(const scoped_ptr &);
scoped_ptr &operator=(const scoped_ptr &);
};
} // namespace marisa
#endif // MARISA_SCOPED_PTR_H_

View file

@ -1,17 +1,17 @@
#ifndef MARISA_TRIE_H_
#define MARISA_TRIE_H_
#include "marisa/keyset.h"
#include "marisa/agent.h"
#include <memory>
#include "marisa/agent.h" // IWYU pragma: export
#include "marisa/keyset.h" // IWYU pragma: export
namespace marisa {
namespace grimoire {
namespace trie {
namespace grimoire::trie {
class LoudsTrie;
} // namespace trie
} // namespace grimoire
} // namespace grimoire::trie
class Trie {
friend class TrieIO;
@ -20,9 +20,15 @@ class Trie {
Trie();
~Trie();
Trie(const Trie &) = delete;
Trie &operator=(const Trie &) = delete;
Trie(Trie &&) noexcept;
Trie &operator=(Trie &&) noexcept;
void build(Keyset &keyset, int config_flags = 0);
void mmap(const char *filename);
void mmap(const char *filename, int flags = 0);
void map(const void *ptr, std::size_t size);
void load(const char *filename);
@ -48,15 +54,11 @@ class Trie {
std::size_t total_size() const;
std::size_t io_size() const;
void clear();
void swap(Trie &rhs);
void clear() noexcept;
void swap(Trie &rhs) noexcept;
private:
scoped_ptr<grimoire::trie::LoudsTrie> trie_;
// Disallows copy and assignment.
Trie(const Trie &);
Trie &operator=(const Trie &);
std::unique_ptr<grimoire::trie::LoudsTrie> trie_;
};
} // namespace marisa

View file

@ -1,7 +1,7 @@
####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was marisa-config.cmake.in ########
####### The input file was Marisa.cmake.in ########
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
@ -24,8 +24,6 @@ endmacro()
####################################################################################
include(${CMAKE_CURRENT_LIST_DIR}/marisa-targets.cmake)
check_required_components(Marisa)
set_and_check(MARISA_INCLUDE_DIR ${PACKAGE_PREFIX_DIR}/include)
check_required_components(marisa)
include("${CMAKE_CURRENT_LIST_DIR}/MarisaTargets.cmake")

View file

@ -9,19 +9,19 @@
# The variable CVF_VERSION must be set before calling configure_file().
set(PACKAGE_VERSION "0.2.6")
set(PACKAGE_VERSION "0.3.1")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
if("0.2.6" MATCHES "^([0-9]+)\\.")
if("0.3.1" MATCHES "^([0-9]+)\\.")
set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}")
if(NOT CVF_VERSION_MAJOR VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" CVF_VERSION_MAJOR "${CVF_VERSION_MAJOR}")
endif()
else()
set(CVF_VERSION_MAJOR "0.2.6")
set(CVF_VERSION_MAJOR "0.3.1")
endif()
if(PACKAGE_FIND_VERSION_RANGE)

View file

@ -5,15 +5,15 @@
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Import target "marisa" for configuration "Release"
set_property(TARGET marisa APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(marisa PROPERTIES
# Import target "Marisa::marisa" for configuration "Release"
set_property(TARGET Marisa::marisa APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(Marisa::marisa PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libmarisa.a"
)
list(APPEND _cmake_import_check_targets marisa )
list(APPEND _cmake_import_check_files_for_marisa "${_IMPORT_PREFIX}/lib/libmarisa.a" )
list(APPEND _cmake_import_check_targets Marisa::marisa )
list(APPEND _cmake_import_check_files_for_Marisa::marisa "${_IMPORT_PREFIX}/lib/libmarisa.a" )
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)

View file

@ -19,7 +19,7 @@ set(CMAKE_IMPORT_FILE_VERSION 1)
set(_cmake_targets_defined "")
set(_cmake_targets_not_defined "")
set(_cmake_expected_targets "")
foreach(_cmake_expected_target IN ITEMS marisa)
foreach(_cmake_expected_target IN ITEMS Marisa::marisa)
list(APPEND _cmake_expected_targets "${_cmake_expected_target}")
if(TARGET "${_cmake_expected_target}")
list(APPEND _cmake_targets_defined "${_cmake_expected_target}")
@ -55,15 +55,15 @@ if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
# Create imported target marisa
add_library(marisa STATIC IMPORTED)
# Create imported target Marisa::marisa
add_library(Marisa::marisa STATIC IMPORTED)
set_target_properties(marisa PROPERTIES
set_target_properties(Marisa::marisa PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
)
# Load information for each installed configuration.
file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/marisa-targets-*.cmake")
file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/MarisaTargets-*.cmake")
foreach(_cmake_config_file IN LISTS _cmake_config_files)
include("${_cmake_config_file}")
endforeach()

Binary file not shown.

View file

@ -2,13 +2,13 @@
#define MARISA_H_
// "marisa/stdio.h" includes <cstdio> for I/O using std::FILE.
#include "marisa/stdio.h"
#include "marisa/stdio.h" // IWYU pragma: export
// "marisa/iostream.h" includes <iosfwd> for I/O using std::iostream.
#include "marisa/iostream.h"
#include "marisa/iostream.h" // IWYU pragma: export
// You can use <marisa/trie.h> instead of <marisa.h> if you don't need the
// above I/O interfaces and don't want to include the above I/O headers.
#include "marisa/trie.h"
#include "marisa/trie.h" // IWYU pragma: export
#endif // MARISA_H_

View file

@ -1,27 +1,30 @@
#ifndef MARISA_AGENT_H_
#define MARISA_AGENT_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <memory>
#include <string_view>
#include "marisa/key.h"
#include "marisa/query.h"
namespace marisa {
namespace grimoire {
namespace trie {
namespace grimoire::trie {
class State;
} // namespace trie
} // namespace grimoire
} // namespace grimoire::trie
class Agent {
public:
Agent();
~Agent();
Agent(const Agent &other);
Agent &operator=(const Agent &other);
Agent(Agent &&other) noexcept;
Agent &operator=(Agent &&other) noexcept;
const Query &query() const {
return query_;
}
@ -29,11 +32,9 @@ class Agent {
return key_;
}
#if __cplusplus >= 201703L
void set_query(std::string_view str) {
set_query(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_query(const char *str);
void set_query(const char *ptr, std::size_t length);
void set_query(std::size_t key_id);
@ -45,41 +46,35 @@ class Agent {
return *state_;
}
#if __cplusplus >= 201703L
void set_key(std::string_view str) {
set_key(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_key(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
key_.set_str(str);
}
void set_key(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert((ptr != nullptr) || (length == 0));
assert(length <= UINT32_MAX);
key_.set_str(ptr, length);
}
void set_key(std::size_t id) {
MARISA_DEBUG_IF(id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert(id <= UINT32_MAX);
key_.set_id(id);
}
bool has_state() const {
return state_.get() != NULL;
return state_ != nullptr;
}
void init_state();
void clear();
void swap(Agent &rhs);
void clear() noexcept;
void swap(Agent &rhs) noexcept;
private:
Query query_;
Key key_;
scoped_ptr<grimoire::trie::State> state_;
// Disallows copy and assignment.
Agent(const Agent &);
Agent &operator=(const Agent &);
std::unique_ptr<grimoire::trie::State> state_;
};
} // namespace marisa

View file

@ -1,95 +1,85 @@
#ifndef MARISA_BASE_H_
#define MARISA_BASE_H_
// Old Visual C++ does not provide stdint.h.
#ifndef _MSC_VER
#include <stdint.h>
#endif // _MSC_VER
#include <cstddef>
#include <cstdint>
#include <exception>
#include <system_error>
#include <utility>
#ifdef __cplusplus
#include <cstddef>
#else // __cplusplus
#include <stddef.h>
#endif // __cplusplus
// These aliases are left for backward compatibility.
using marisa_uint8 [[deprecated]] = std::uint8_t;
using marisa_uint16 [[deprecated]] = std::uint16_t;
using marisa_uint32 [[deprecated]] = std::uint32_t;
using marisa_uint64 [[deprecated]] = std::uint64_t;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#ifdef _MSC_VER
typedef unsigned __int8 marisa_uint8;
typedef unsigned __int16 marisa_uint16;
typedef unsigned __int32 marisa_uint32;
typedef unsigned __int64 marisa_uint64;
#else // _MSC_VER
typedef uint8_t marisa_uint8;
typedef uint16_t marisa_uint16;
typedef uint32_t marisa_uint32;
typedef uint64_t marisa_uint64;
#endif // _MSC_VER
#if defined(_WIN64) || defined(__amd64__) || defined(__x86_64__) || \
defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__) || \
defined(__sparc64__) || defined(__mips64__) || defined(__aarch64__) || \
defined(__s390x__)
#if UINTPTR_MAX == UINT64_MAX
#define MARISA_WORD_SIZE 64
#else // defined(_WIN64), etc.
#elif UINTPTR_MAX == UINT32_MAX
#define MARISA_WORD_SIZE 32
#endif // defined(_WIN64), etc.
#else
#error Failed to detect MARISA_WORD_SIZE
#endif
//#define MARISA_WORD_SIZE (sizeof(void *) * 8)
// These constant variables are left for backward compatibility.
[[deprecated]] constexpr auto MARISA_UINT8_MAX = UINT8_MAX;
[[deprecated]] constexpr auto MARISA_UINT16_MAX = UINT16_MAX;
[[deprecated]] constexpr auto MARISA_UINT32_MAX = UINT32_MAX;
[[deprecated]] constexpr auto MARISA_UINT64_MAX = UINT64_MAX;
[[deprecated]] constexpr auto MARISA_SIZE_MAX = SIZE_MAX;
#define MARISA_UINT8_MAX ((marisa_uint8)~(marisa_uint8)0)
#define MARISA_UINT16_MAX ((marisa_uint16)~(marisa_uint16)0)
#define MARISA_UINT32_MAX ((marisa_uint32)~(marisa_uint32)0)
#define MARISA_UINT64_MAX ((marisa_uint64)~(marisa_uint64)0)
#define MARISA_SIZE_MAX ((size_t)~(size_t)0)
#define MARISA_INVALID_LINK_ID MARISA_UINT32_MAX
#define MARISA_INVALID_KEY_ID MARISA_UINT32_MAX
#define MARISA_INVALID_EXTRA (MARISA_UINT32_MAX >> 8)
#define MARISA_INVALID_LINK_ID UINT32_MAX
#define MARISA_INVALID_KEY_ID UINT32_MAX
#define MARISA_INVALID_EXTRA (UINT32_MAX >> 8)
// Error codes are defined as members of marisa_error_code. This library throws
// an exception with one of the error codes when an error occurs.
typedef enum marisa_error_code_ {
enum marisa_error_code {
// MARISA_OK means that a requested operation has succeeded. In practice, an
// exception never has MARISA_OK because it is not an error.
MARISA_OK = 0,
MARISA_OK = 0,
// MARISA_STATE_ERROR means that an object was not ready for a requested
// operation. For example, an operation to modify a fixed vector throws an
// exception with MARISA_STATE_ERROR.
MARISA_STATE_ERROR = 1,
MARISA_STATE_ERROR = 1,
// MARISA_NULL_ERROR means that an invalid NULL pointer has been given.
MARISA_NULL_ERROR = 2,
// MARISA_NULL_ERROR means that an invalid nullptr has been given.
MARISA_NULL_ERROR = 2,
// MARISA_BOUND_ERROR means that an operation has tried to access an out of
// range address.
MARISA_BOUND_ERROR = 3,
MARISA_BOUND_ERROR = 3,
// MARISA_RANGE_ERROR means that an out of range value has appeared in
// operation.
MARISA_RANGE_ERROR = 4,
MARISA_RANGE_ERROR = 4,
// MARISA_CODE_ERROR means that an undefined code has appeared in operation.
MARISA_CODE_ERROR = 5,
MARISA_CODE_ERROR = 5,
// MARISA_RESET_ERROR means that a smart pointer has tried to reset itself.
MARISA_RESET_ERROR = 6,
MARISA_RESET_ERROR = 6,
// MARISA_SIZE_ERROR means that a size has exceeded a library limitation.
MARISA_SIZE_ERROR = 7,
MARISA_SIZE_ERROR = 7,
// MARISA_MEMORY_ERROR means that a memory allocation has failed.
MARISA_MEMORY_ERROR = 8,
// MARISA_IO_ERROR means that an I/O operation has failed.
MARISA_IO_ERROR = 9,
MARISA_IO_ERROR = 9,
// MARISA_FORMAT_ERROR means that input was in invalid format.
MARISA_FORMAT_ERROR = 10,
} marisa_error_code;
};
// Flags for memory mapping are defined as members of marisa_map_flags.
// Trie::open() accepts a combination of these flags.
enum marisa_map_flags {
// MARISA_MAP_POPULATE specifies MAP_POPULATE.
MARISA_MAP_POPULATE = 1 << 0,
};
// Min/max values, flags and masks for dictionary settings are defined below.
// Please note that unspecified settings will be replaced with the default
@ -98,99 +88,136 @@ typedef enum marisa_error_code_ {
// A dictionary consists of 3 tries in default. Usually more tries make a
// dictionary space-efficient but time-inefficient.
typedef enum marisa_num_tries_ {
MARISA_MIN_NUM_TRIES = 0x00001,
MARISA_MAX_NUM_TRIES = 0x0007F,
enum marisa_num_tries {
MARISA_MIN_NUM_TRIES = 0x00001,
MARISA_MAX_NUM_TRIES = 0x0007F,
MARISA_DEFAULT_NUM_TRIES = 0x00003,
} marisa_num_tries;
};
// This library uses a cache technique to accelerate search functions. The
// following enumerated type marisa_cache_level gives a list of available cache
// size options. A larger cache enables faster search but takes a more space.
typedef enum marisa_cache_level_ {
MARISA_HUGE_CACHE = 0x00080,
MARISA_LARGE_CACHE = 0x00100,
MARISA_NORMAL_CACHE = 0x00200,
MARISA_SMALL_CACHE = 0x00400,
MARISA_TINY_CACHE = 0x00800,
MARISA_DEFAULT_CACHE = MARISA_NORMAL_CACHE
} marisa_cache_level;
enum marisa_cache_level {
MARISA_HUGE_CACHE = 0x00080,
MARISA_LARGE_CACHE = 0x00100,
MARISA_NORMAL_CACHE = 0x00200,
MARISA_SMALL_CACHE = 0x00400,
MARISA_TINY_CACHE = 0x00800,
MARISA_DEFAULT_CACHE = MARISA_NORMAL_CACHE
};
// This library provides 2 kinds of TAIL implementations.
typedef enum marisa_tail_mode_ {
enum marisa_tail_mode {
// MARISA_TEXT_TAIL merges last labels as zero-terminated strings. So, it is
// available if and only if the last labels do not contain a NULL character.
// If MARISA_TEXT_TAIL is specified and a NULL character exists in the last
// labels, the setting is automatically switched to MARISA_BINARY_TAIL.
MARISA_TEXT_TAIL = 0x01000,
MARISA_TEXT_TAIL = 0x01000,
// MARISA_BINARY_TAIL also merges last labels but as byte sequences. It uses
// a bit vector to detect the end of a sequence, instead of NULL characters.
// So, MARISA_BINARY_TAIL requires a larger space if the average length of
// labels is greater than 8.
MARISA_BINARY_TAIL = 0x02000,
MARISA_BINARY_TAIL = 0x02000,
MARISA_DEFAULT_TAIL = MARISA_TEXT_TAIL,
} marisa_tail_mode;
MARISA_DEFAULT_TAIL = MARISA_TEXT_TAIL,
};
// The arrangement of nodes affects the time cost of matching and the order of
// predictive search.
typedef enum marisa_node_order_ {
enum marisa_node_order {
// MARISA_LABEL_ORDER arranges nodes in ascending label order.
// MARISA_LABEL_ORDER is useful if an application needs to predict keys in
// label order.
MARISA_LABEL_ORDER = 0x10000,
MARISA_LABEL_ORDER = 0x10000,
// MARISA_WEIGHT_ORDER arranges nodes in descending weight order.
// MARISA_WEIGHT_ORDER is generally a better choice because it enables faster
// matching.
MARISA_WEIGHT_ORDER = 0x20000,
MARISA_WEIGHT_ORDER = 0x20000,
MARISA_DEFAULT_ORDER = MARISA_WEIGHT_ORDER,
} marisa_node_order;
MARISA_DEFAULT_ORDER = MARISA_WEIGHT_ORDER,
};
typedef enum marisa_config_mask_ {
MARISA_NUM_TRIES_MASK = 0x0007F,
MARISA_CACHE_LEVEL_MASK = 0x00F80,
MARISA_TAIL_MODE_MASK = 0x0F000,
MARISA_NODE_ORDER_MASK = 0xF0000,
MARISA_CONFIG_MASK = 0xFFFFF
} marisa_config_mask;
enum marisa_config_mask {
MARISA_NUM_TRIES_MASK = 0x0007F,
MARISA_CACHE_LEVEL_MASK = 0x00F80,
MARISA_TAIL_MODE_MASK = 0x0F000,
MARISA_NODE_ORDER_MASK = 0xF0000,
MARISA_CONFIG_MASK = 0xFFFFF
};
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#ifdef __cplusplus
// `std::swap` is in <utility> since C++ 11 but in <algorithm> in C++ 98:
#if __cplusplus >= 201103L
#include <utility>
#else
#include <algorithm>
#endif
namespace marisa {
typedef ::marisa_uint8 UInt8;
typedef ::marisa_uint16 UInt16;
typedef ::marisa_uint32 UInt32;
typedef ::marisa_uint64 UInt64;
// These aliases are left for backward compatibility.
using UInt8 [[deprecated]] = std::uint8_t;
using UInt16 [[deprecated]] = std::uint16_t;
using UInt32 [[deprecated]] = std::uint32_t;
using UInt64 [[deprecated]] = std::uint64_t;
typedef ::marisa_error_code ErrorCode;
using std::uint16_t;
using std::uint32_t;
using std::uint64_t;
using std::uint8_t;
typedef ::marisa_cache_level CacheLevel;
typedef ::marisa_tail_mode TailMode;
typedef ::marisa_node_order NodeOrder;
using ErrorCode = marisa_error_code;
using CacheLevel = marisa_cache_level;
using TailMode = marisa_tail_mode;
using NodeOrder = marisa_node_order;
// This is left for backward compatibility.
using std::swap;
} // namespace marisa
#endif // __cplusplus
// This is left for backward compatibility.
using Exception = std::exception;
#ifdef __cplusplus
#include "marisa/exception.h"
#include "marisa/scoped-ptr.h"
#include "marisa/scoped-array.h"
#endif // __cplusplus
} // namespace marisa
// These macros are used to convert a line number to a string constant.
#define MARISA_INT_TO_STR(value) #value
#define MARISA_LINE_TO_STR(line) MARISA_INT_TO_STR(line)
#define MARISA_LINE_STR MARISA_LINE_TO_STR(__LINE__)
// MARISA_THROW throws an exception with a filename, a line number, an error
// code and an error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
#define MARISA_THROW(error_type, error_message) \
(throw (error_type)(__FILE__ ":" MARISA_LINE_STR ": " #error_type \
": " error_message))
// MARISA_THROW_IF throws an exception if `condition' is true.
#define MARISA_THROW_IF(condition, error_type) \
(void)((!(condition)) || (MARISA_THROW(error_type, #condition), 0))
// MARISA_THROW_SYSTEM_ERROR_IF throws an exception if `condition` is true.
// ::GetLastError() or errno should be passed as `error_value`.
#define MARISA_THROW_SYSTEM_ERROR_IF(condition, error_value, error_category, \
function_name) \
(void)((!(condition)) || \
(throw std::system_error( \
std::error_code(error_value, error_category), \
__FILE__ ":" MARISA_LINE_STR \
": std::system_error: " function_name ": " #condition), \
false))
// #ifndef MARISA_USE_EXCEPTIONS
// #if defined(__GNUC__) && !defined(__EXCEPTIONS)
// #define MARISA_USE_EXCEPTIONS 0
// #elif defined(__clang__) && !defined(__cpp_exceptions)
// #define MARISA_USE_EXCEPTIONS 0
// #elif defined(_MSC_VER) && !_HAS_EXCEPTIONS
// #define MARISA_USE_EXCEPTIONS 0
// #else
// #define MARISA_USE_EXCEPTIONS 1
// #endif
// #endif
// #if MARISA_USE_EXCEPTIONS
// #define MARISA_TRY try
// #define MARISA_CATCH(x) catch (x)
// #else
// #define MARISA_TRY if (true)
// #define MARISA_CATCH(x) if (false)
// #endif
#endif // MARISA_BASE_H_

View file

@ -1,82 +0,0 @@
#ifndef MARISA_EXCEPTION_H_
#define MARISA_EXCEPTION_H_
#include <exception>
#include "marisa/base.h"
namespace marisa {
// An exception object keeps a filename, a line number, an error code and an
// error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
class Exception : public std::exception {
public:
Exception(const char *filename, int line,
ErrorCode error_code, const char *error_message)
: std::exception(), filename_(filename), line_(line),
error_code_(error_code), error_message_(error_message) {}
Exception(const Exception &ex)
: std::exception(), filename_(ex.filename_), line_(ex.line_),
error_code_(ex.error_code_), error_message_(ex.error_message_) {}
virtual ~Exception() throw() {}
Exception &operator=(const Exception &rhs) {
filename_ = rhs.filename_;
line_ = rhs.line_;
error_code_ = rhs.error_code_;
error_message_ = rhs.error_message_;
return *this;
}
const char *filename() const {
return filename_;
}
int line() const {
return line_;
}
ErrorCode error_code() const {
return error_code_;
}
const char *error_message() const {
return error_message_;
}
virtual const char *what() const throw() {
return error_message_;
}
private:
const char *filename_;
int line_;
ErrorCode error_code_;
const char *error_message_;
};
// These macros are used to convert a line number to a string constant.
#define MARISA_INT_TO_STR(value) #value
#define MARISA_LINE_TO_STR(line) MARISA_INT_TO_STR(line)
#define MARISA_LINE_STR MARISA_LINE_TO_STR(__LINE__)
// MARISA_THROW throws an exception with a filename, a line number, an error
// code and an error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
#define MARISA_THROW(error_code, error_message) \
(throw marisa::Exception(__FILE__, __LINE__, error_code, \
__FILE__ ":" MARISA_LINE_STR ": " #error_code ": " error_message))
// MARISA_THROW_IF throws an exception if `condition' is true.
#define MARISA_THROW_IF(condition, error_code) \
(void)((!(condition)) || (MARISA_THROW(error_code, #condition), 0))
// MARISA_DEBUG_IF is ignored if _DEBUG is undefined. So, it is useful for
// debugging time-critical codes.
#ifdef _DEBUG
#define MARISA_DEBUG_IF(cond, error_code) MARISA_THROW_IF(cond, error_code)
#else
#define MARISA_DEBUG_IF(cond, error_code)
#endif
} // namespace marisa
#endif // MARISA_EXCEPTION_H_

View file

@ -1,9 +1,8 @@
#ifndef MARISA_KEY_H_
#define MARISA_KEY_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <string_view>
#include "marisa/base.h"
@ -11,58 +10,45 @@ namespace marisa {
class Key {
public:
Key() : ptr_(NULL), length_(0), union_() {
union_.id = 0;
}
Key(const Key &key)
: ptr_(key.ptr_), length_(key.length_), union_(key.union_) {}
Key &operator=(const Key &key) {
ptr_ = key.ptr_;
length_ = key.length_;
union_ = key.union_;
return *this;
}
Key() = default;
Key(const Key &key) = default;
Key &operator=(const Key &key) = default;
char operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR);
assert(i < length_);
return ptr_[i];
}
#if __cplusplus >= 201703L
void set_str(std::string_view str) {
set_str(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_str(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
std::size_t length = 0;
while (str[length] != '\0') {
++length;
}
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert(length <= UINT32_MAX);
ptr_ = str;
length_ = (UInt32)length;
length_ = static_cast<uint32_t>(length);
}
void set_str(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert((ptr != nullptr) || (length == 0));
assert(length <= UINT32_MAX);
ptr_ = ptr;
length_ = (UInt32)length;
length_ = static_cast<uint32_t>(length);
}
void set_id(std::size_t id) {
MARISA_DEBUG_IF(id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
union_.id = (UInt32)id;
assert(id <= UINT32_MAX);
union_.id = static_cast<uint32_t>(id);
}
void set_weight(float weight) {
union_.weight = weight;
}
#if __cplusplus >= 201703L
std::string_view str() const {
return std::string_view(ptr_, length_);
}
#endif // __cplusplus >= 201703L
const char *ptr() const {
return ptr_;
}
@ -76,20 +62,20 @@ class Key {
return union_.weight;
}
void clear() {
void clear() noexcept {
Key().swap(*this);
}
void swap(Key &rhs) {
marisa::swap(ptr_, rhs.ptr_);
marisa::swap(length_, rhs.length_);
marisa::swap(union_.id, rhs.union_.id);
void swap(Key &rhs) noexcept {
std::swap(ptr_, rhs.ptr_);
std::swap(length_, rhs.length_);
std::swap(union_.id, rhs.union_.id);
}
private:
const char *ptr_;
UInt32 length_;
const char *ptr_ = nullptr;
uint32_t length_ = 0;
union Union {
UInt32 id;
uint32_t id = 0;
float weight;
} union_;
};

View file

@ -1,9 +1,9 @@
#ifndef MARISA_KEYSET_H_
#define MARISA_KEYSET_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <memory>
#include <string_view>
#include "marisa/key.h"
@ -12,30 +12,31 @@ namespace marisa {
class Keyset {
public:
enum {
BASE_BLOCK_SIZE = 4096,
BASE_BLOCK_SIZE = 4096,
EXTRA_BLOCK_SIZE = 1024,
KEY_BLOCK_SIZE = 256
KEY_BLOCK_SIZE = 256
};
Keyset();
Keyset(const Keyset &) = delete;
Keyset &operator=(const Keyset &) = delete;
void push_back(const Key &key);
void push_back(const Key &key, char end_marker);
#if __cplusplus >= 201703L
void push_back(std::string_view str, float weight = 1.0) {
push_back(str.data(), str.length(), weight);
}
#endif // __cplusplus >= 201703L
void push_back(const char *str);
void push_back(const char *ptr, std::size_t length, float weight = 1.0);
const Key &operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
assert(i < size_);
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
}
Key &operator[](std::size_t i) {
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
assert(i < size_);
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
}
@ -55,33 +56,29 @@ class Keyset {
void reset();
void clear();
void swap(Keyset &rhs);
void clear() noexcept;
void swap(Keyset &rhs) noexcept;
private:
scoped_array<scoped_array<char> > base_blocks_;
std::size_t base_blocks_size_;
std::size_t base_blocks_capacity_;
scoped_array<scoped_array<char> > extra_blocks_;
std::size_t extra_blocks_size_;
std::size_t extra_blocks_capacity_;
scoped_array<scoped_array<Key> > key_blocks_;
std::size_t key_blocks_size_;
std::size_t key_blocks_capacity_;
char *ptr_;
std::size_t avail_;
std::size_t size_;
std::size_t total_length_;
std::unique_ptr<std::unique_ptr<char[]>[]> base_blocks_;
std::size_t base_blocks_size_ = 0;
std::size_t base_blocks_capacity_ = 0;
std::unique_ptr<std::unique_ptr<char[]>[]> extra_blocks_;
std::size_t extra_blocks_size_ = 0;
std::size_t extra_blocks_capacity_ = 0;
std::unique_ptr<std::unique_ptr<Key[]>[]> key_blocks_;
std::size_t key_blocks_size_ = 0;
std::size_t key_blocks_capacity_ = 0;
char *ptr_ = nullptr;
std::size_t avail_ = 0;
std::size_t size_ = 0;
std::size_t total_length_ = 0;
char *reserve(std::size_t size);
void append_base_block();
void append_extra_block(std::size_t size);
void append_key_block();
// Disallows copy and assignment.
Keyset(const Keyset &);
Keyset &operator=(const Keyset &);
};
} // namespace marisa

View file

@ -1,9 +1,8 @@
#ifndef MARISA_QUERY_H_
#define MARISA_QUERY_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <string_view>
#include "marisa/base.h"
@ -11,29 +10,21 @@ namespace marisa {
class Query {
public:
Query() : ptr_(NULL), length_(0), id_(0) {}
Query(const Query &query)
: ptr_(query.ptr_), length_(query.length_), id_(query.id_) {}
Query() = default;
Query(const Query &query) = default;
Query &operator=(const Query &query) {
ptr_ = query.ptr_;
length_ = query.length_;
id_ = query.id_;
return *this;
}
Query &operator=(const Query &query) = default;
char operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR);
assert(i < length_);
return ptr_[i];
}
#if __cplusplus >= 201703L
void set_str(std::string_view str) {
set_str(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_str(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
std::size_t length = 0;
while (str[length] != '\0') {
++length;
@ -42,7 +33,7 @@ class Query {
length_ = length;
}
void set_str(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
assert((ptr != nullptr) || (length == 0));
ptr_ = ptr;
length_ = length;
}
@ -50,11 +41,9 @@ class Query {
id_ = id;
}
#if __cplusplus >= 201703L
std::string_view str() const {
return std::string_view(ptr_, length_);
}
#endif // __cplusplus >= 201703L
const char *ptr() const {
return ptr_;
}
@ -65,19 +54,19 @@ class Query {
return id_;
}
void clear() {
void clear() noexcept {
Query().swap(*this);
}
void swap(Query &rhs) {
marisa::swap(ptr_, rhs.ptr_);
marisa::swap(length_, rhs.length_);
marisa::swap(id_, rhs.id_);
void swap(Query &rhs) noexcept {
std::swap(ptr_, rhs.ptr_);
std::swap(length_, rhs.length_);
std::swap(id_, rhs.id_);
}
private:
const char *ptr_;
std::size_t length_;
std::size_t id_;
const char *ptr_ = nullptr;
std::size_t length_ = 0;
std::size_t id_ = 0;
};
} // namespace marisa

View file

@ -1,48 +0,0 @@
#ifndef MARISA_SCOPED_ARRAY_H_
#define MARISA_SCOPED_ARRAY_H_
#include "marisa/base.h"
namespace marisa {
template <typename T>
class scoped_array {
public:
scoped_array() : array_(NULL) {}
explicit scoped_array(T *array) : array_(array) {}
~scoped_array() {
delete [] array_;
}
void reset(T *array = NULL) {
MARISA_DEBUG_IF((array != NULL) && (array == array_), MARISA_RESET_ERROR);
scoped_array(array).swap(*this);
}
T &operator[](std::size_t i) const {
MARISA_DEBUG_IF(array_ == NULL, MARISA_STATE_ERROR);
return array_[i];
}
T *get() const {
return array_;
}
void clear() {
scoped_array().swap(*this);
}
void swap(scoped_array &rhs) {
marisa::swap(array_, rhs.array_);
}
private:
T *array_;
// Disallows copy and assignment.
scoped_array(const scoped_array &);
scoped_array &operator=(const scoped_array &);
};
} // namespace marisa
#endif // MARISA_SCOPED_ARRAY_H_

View file

@ -1,52 +0,0 @@
#ifndef MARISA_SCOPED_PTR_H_
#define MARISA_SCOPED_PTR_H_
#include "marisa/base.h"
namespace marisa {
template <typename T>
class scoped_ptr {
public:
scoped_ptr() : ptr_(NULL) {}
explicit scoped_ptr(T *ptr) : ptr_(ptr) {}
~scoped_ptr() {
delete ptr_;
}
void reset(T *ptr = NULL) {
MARISA_DEBUG_IF((ptr != NULL) && (ptr == ptr_), MARISA_RESET_ERROR);
scoped_ptr(ptr).swap(*this);
}
T &operator*() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return *ptr_;
}
T *operator->() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return ptr_;
}
T *get() const {
return ptr_;
}
void clear() {
scoped_ptr().swap(*this);
}
void swap(scoped_ptr &rhs) {
marisa::swap(ptr_, rhs.ptr_);
}
private:
T *ptr_;
// Disallows copy and assignment.
scoped_ptr(const scoped_ptr &);
scoped_ptr &operator=(const scoped_ptr &);
};
} // namespace marisa
#endif // MARISA_SCOPED_PTR_H_

View file

@ -1,17 +1,17 @@
#ifndef MARISA_TRIE_H_
#define MARISA_TRIE_H_
#include "marisa/keyset.h"
#include "marisa/agent.h"
#include <memory>
#include "marisa/agent.h" // IWYU pragma: export
#include "marisa/keyset.h" // IWYU pragma: export
namespace marisa {
namespace grimoire {
namespace trie {
namespace grimoire::trie {
class LoudsTrie;
} // namespace trie
} // namespace grimoire
} // namespace grimoire::trie
class Trie {
friend class TrieIO;
@ -20,9 +20,15 @@ class Trie {
Trie();
~Trie();
Trie(const Trie &) = delete;
Trie &operator=(const Trie &) = delete;
Trie(Trie &&) noexcept;
Trie &operator=(Trie &&) noexcept;
void build(Keyset &keyset, int config_flags = 0);
void mmap(const char *filename);
void mmap(const char *filename, int flags = 0);
void map(const void *ptr, std::size_t size);
void load(const char *filename);
@ -48,15 +54,11 @@ class Trie {
std::size_t total_size() const;
std::size_t io_size() const;
void clear();
void swap(Trie &rhs);
void clear() noexcept;
void swap(Trie &rhs) noexcept;
private:
scoped_ptr<grimoire::trie::LoudsTrie> trie_;
// Disallows copy and assignment.
Trie(const Trie &);
Trie &operator=(const Trie &);
std::unique_ptr<grimoire::trie::LoudsTrie> trie_;
};
} // namespace marisa

View file

@ -1,7 +1,7 @@
####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was marisa-config.cmake.in ########
####### The input file was Marisa.cmake.in ########
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
@ -24,8 +24,6 @@ endmacro()
####################################################################################
include(${CMAKE_CURRENT_LIST_DIR}/marisa-targets.cmake)
check_required_components(Marisa)
set_and_check(MARISA_INCLUDE_DIR ${PACKAGE_PREFIX_DIR}/include)
check_required_components(marisa)
include("${CMAKE_CURRENT_LIST_DIR}/MarisaTargets.cmake")

View file

@ -9,19 +9,19 @@
# The variable CVF_VERSION must be set before calling configure_file().
set(PACKAGE_VERSION "0.2.6")
set(PACKAGE_VERSION "0.3.1")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
if("0.2.6" MATCHES "^([0-9]+)\\.")
if("0.3.1" MATCHES "^([0-9]+)\\.")
set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}")
if(NOT CVF_VERSION_MAJOR VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" CVF_VERSION_MAJOR "${CVF_VERSION_MAJOR}")
endif()
else()
set(CVF_VERSION_MAJOR "0.2.6")
set(CVF_VERSION_MAJOR "0.3.1")
endif()
if(PACKAGE_FIND_VERSION_RANGE)

View file

@ -5,15 +5,15 @@
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Import target "marisa" for configuration "Release"
set_property(TARGET marisa APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(marisa PROPERTIES
# Import target "Marisa::marisa" for configuration "Release"
set_property(TARGET Marisa::marisa APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(Marisa::marisa PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libmarisa.a"
)
list(APPEND _cmake_import_check_targets marisa )
list(APPEND _cmake_import_check_files_for_marisa "${_IMPORT_PREFIX}/lib/libmarisa.a" )
list(APPEND _cmake_import_check_targets Marisa::marisa )
list(APPEND _cmake_import_check_files_for_Marisa::marisa "${_IMPORT_PREFIX}/lib/libmarisa.a" )
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)

View file

@ -19,7 +19,7 @@ set(CMAKE_IMPORT_FILE_VERSION 1)
set(_cmake_targets_defined "")
set(_cmake_targets_not_defined "")
set(_cmake_expected_targets "")
foreach(_cmake_expected_target IN ITEMS marisa)
foreach(_cmake_expected_target IN ITEMS Marisa::marisa)
list(APPEND _cmake_expected_targets "${_cmake_expected_target}")
if(TARGET "${_cmake_expected_target}")
list(APPEND _cmake_targets_defined "${_cmake_expected_target}")
@ -55,15 +55,15 @@ if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
# Create imported target marisa
add_library(marisa STATIC IMPORTED)
# Create imported target Marisa::marisa
add_library(Marisa::marisa STATIC IMPORTED)
set_target_properties(marisa PROPERTIES
set_target_properties(Marisa::marisa PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
)
# Load information for each installed configuration.
file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/marisa-targets-*.cmake")
file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/MarisaTargets-*.cmake")
foreach(_cmake_config_file IN LISTS _cmake_config_files)
include("${_cmake_config_file}")
endforeach()

Binary file not shown.

View file

@ -2,13 +2,13 @@
#define MARISA_H_
// "marisa/stdio.h" includes <cstdio> for I/O using std::FILE.
#include "marisa/stdio.h"
#include "marisa/stdio.h" // IWYU pragma: export
// "marisa/iostream.h" includes <iosfwd> for I/O using std::iostream.
#include "marisa/iostream.h"
#include "marisa/iostream.h" // IWYU pragma: export
// You can use <marisa/trie.h> instead of <marisa.h> if you don't need the
// above I/O interfaces and don't want to include the above I/O headers.
#include "marisa/trie.h"
#include "marisa/trie.h" // IWYU pragma: export
#endif // MARISA_H_

View file

@ -1,27 +1,30 @@
#ifndef MARISA_AGENT_H_
#define MARISA_AGENT_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <memory>
#include <string_view>
#include "marisa/key.h"
#include "marisa/query.h"
namespace marisa {
namespace grimoire {
namespace trie {
namespace grimoire::trie {
class State;
} // namespace trie
} // namespace grimoire
} // namespace grimoire::trie
class Agent {
public:
Agent();
~Agent();
Agent(const Agent &other);
Agent &operator=(const Agent &other);
Agent(Agent &&other) noexcept;
Agent &operator=(Agent &&other) noexcept;
const Query &query() const {
return query_;
}
@ -29,11 +32,9 @@ class Agent {
return key_;
}
#if __cplusplus >= 201703L
void set_query(std::string_view str) {
set_query(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_query(const char *str);
void set_query(const char *ptr, std::size_t length);
void set_query(std::size_t key_id);
@ -45,41 +46,35 @@ class Agent {
return *state_;
}
#if __cplusplus >= 201703L
void set_key(std::string_view str) {
set_key(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_key(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
key_.set_str(str);
}
void set_key(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert((ptr != nullptr) || (length == 0));
assert(length <= UINT32_MAX);
key_.set_str(ptr, length);
}
void set_key(std::size_t id) {
MARISA_DEBUG_IF(id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert(id <= UINT32_MAX);
key_.set_id(id);
}
bool has_state() const {
return state_.get() != NULL;
return state_ != nullptr;
}
void init_state();
void clear();
void swap(Agent &rhs);
void clear() noexcept;
void swap(Agent &rhs) noexcept;
private:
Query query_;
Key key_;
scoped_ptr<grimoire::trie::State> state_;
// Disallows copy and assignment.
Agent(const Agent &);
Agent &operator=(const Agent &);
std::unique_ptr<grimoire::trie::State> state_;
};
} // namespace marisa

View file

@ -1,95 +1,85 @@
#ifndef MARISA_BASE_H_
#define MARISA_BASE_H_
// Old Visual C++ does not provide stdint.h.
#ifndef _MSC_VER
#include <stdint.h>
#endif // _MSC_VER
#include <cstddef>
#include <cstdint>
#include <exception>
#include <system_error>
#include <utility>
#ifdef __cplusplus
#include <cstddef>
#else // __cplusplus
#include <stddef.h>
#endif // __cplusplus
// These aliases are left for backward compatibility.
using marisa_uint8 [[deprecated]] = std::uint8_t;
using marisa_uint16 [[deprecated]] = std::uint16_t;
using marisa_uint32 [[deprecated]] = std::uint32_t;
using marisa_uint64 [[deprecated]] = std::uint64_t;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#ifdef _MSC_VER
typedef unsigned __int8 marisa_uint8;
typedef unsigned __int16 marisa_uint16;
typedef unsigned __int32 marisa_uint32;
typedef unsigned __int64 marisa_uint64;
#else // _MSC_VER
typedef uint8_t marisa_uint8;
typedef uint16_t marisa_uint16;
typedef uint32_t marisa_uint32;
typedef uint64_t marisa_uint64;
#endif // _MSC_VER
#if defined(_WIN64) || defined(__amd64__) || defined(__x86_64__) || \
defined(__ia64__) || defined(__ppc64__) || defined(__powerpc64__) || \
defined(__sparc64__) || defined(__mips64__) || defined(__aarch64__) || \
defined(__s390x__)
#if UINTPTR_MAX == UINT64_MAX
#define MARISA_WORD_SIZE 64
#else // defined(_WIN64), etc.
#elif UINTPTR_MAX == UINT32_MAX
#define MARISA_WORD_SIZE 32
#endif // defined(_WIN64), etc.
#else
#error Failed to detect MARISA_WORD_SIZE
#endif
//#define MARISA_WORD_SIZE (sizeof(void *) * 8)
// These constant variables are left for backward compatibility.
[[deprecated]] constexpr auto MARISA_UINT8_MAX = UINT8_MAX;
[[deprecated]] constexpr auto MARISA_UINT16_MAX = UINT16_MAX;
[[deprecated]] constexpr auto MARISA_UINT32_MAX = UINT32_MAX;
[[deprecated]] constexpr auto MARISA_UINT64_MAX = UINT64_MAX;
[[deprecated]] constexpr auto MARISA_SIZE_MAX = SIZE_MAX;
#define MARISA_UINT8_MAX ((marisa_uint8)~(marisa_uint8)0)
#define MARISA_UINT16_MAX ((marisa_uint16)~(marisa_uint16)0)
#define MARISA_UINT32_MAX ((marisa_uint32)~(marisa_uint32)0)
#define MARISA_UINT64_MAX ((marisa_uint64)~(marisa_uint64)0)
#define MARISA_SIZE_MAX ((size_t)~(size_t)0)
#define MARISA_INVALID_LINK_ID MARISA_UINT32_MAX
#define MARISA_INVALID_KEY_ID MARISA_UINT32_MAX
#define MARISA_INVALID_EXTRA (MARISA_UINT32_MAX >> 8)
#define MARISA_INVALID_LINK_ID UINT32_MAX
#define MARISA_INVALID_KEY_ID UINT32_MAX
#define MARISA_INVALID_EXTRA (UINT32_MAX >> 8)
// Error codes are defined as members of marisa_error_code. This library throws
// an exception with one of the error codes when an error occurs.
typedef enum marisa_error_code_ {
enum marisa_error_code {
// MARISA_OK means that a requested operation has succeeded. In practice, an
// exception never has MARISA_OK because it is not an error.
MARISA_OK = 0,
MARISA_OK = 0,
// MARISA_STATE_ERROR means that an object was not ready for a requested
// operation. For example, an operation to modify a fixed vector throws an
// exception with MARISA_STATE_ERROR.
MARISA_STATE_ERROR = 1,
MARISA_STATE_ERROR = 1,
// MARISA_NULL_ERROR means that an invalid NULL pointer has been given.
MARISA_NULL_ERROR = 2,
// MARISA_NULL_ERROR means that an invalid nullptr has been given.
MARISA_NULL_ERROR = 2,
// MARISA_BOUND_ERROR means that an operation has tried to access an out of
// range address.
MARISA_BOUND_ERROR = 3,
MARISA_BOUND_ERROR = 3,
// MARISA_RANGE_ERROR means that an out of range value has appeared in
// operation.
MARISA_RANGE_ERROR = 4,
MARISA_RANGE_ERROR = 4,
// MARISA_CODE_ERROR means that an undefined code has appeared in operation.
MARISA_CODE_ERROR = 5,
MARISA_CODE_ERROR = 5,
// MARISA_RESET_ERROR means that a smart pointer has tried to reset itself.
MARISA_RESET_ERROR = 6,
MARISA_RESET_ERROR = 6,
// MARISA_SIZE_ERROR means that a size has exceeded a library limitation.
MARISA_SIZE_ERROR = 7,
MARISA_SIZE_ERROR = 7,
// MARISA_MEMORY_ERROR means that a memory allocation has failed.
MARISA_MEMORY_ERROR = 8,
// MARISA_IO_ERROR means that an I/O operation has failed.
MARISA_IO_ERROR = 9,
MARISA_IO_ERROR = 9,
// MARISA_FORMAT_ERROR means that input was in invalid format.
MARISA_FORMAT_ERROR = 10,
} marisa_error_code;
};
// Flags for memory mapping are defined as members of marisa_map_flags.
// Trie::open() accepts a combination of these flags.
enum marisa_map_flags {
// MARISA_MAP_POPULATE specifies MAP_POPULATE.
MARISA_MAP_POPULATE = 1 << 0,
};
// Min/max values, flags and masks for dictionary settings are defined below.
// Please note that unspecified settings will be replaced with the default
@ -98,99 +88,136 @@ typedef enum marisa_error_code_ {
// A dictionary consists of 3 tries in default. Usually more tries make a
// dictionary space-efficient but time-inefficient.
typedef enum marisa_num_tries_ {
MARISA_MIN_NUM_TRIES = 0x00001,
MARISA_MAX_NUM_TRIES = 0x0007F,
enum marisa_num_tries {
MARISA_MIN_NUM_TRIES = 0x00001,
MARISA_MAX_NUM_TRIES = 0x0007F,
MARISA_DEFAULT_NUM_TRIES = 0x00003,
} marisa_num_tries;
};
// This library uses a cache technique to accelerate search functions. The
// following enumerated type marisa_cache_level gives a list of available cache
// size options. A larger cache enables faster search but takes a more space.
typedef enum marisa_cache_level_ {
MARISA_HUGE_CACHE = 0x00080,
MARISA_LARGE_CACHE = 0x00100,
MARISA_NORMAL_CACHE = 0x00200,
MARISA_SMALL_CACHE = 0x00400,
MARISA_TINY_CACHE = 0x00800,
MARISA_DEFAULT_CACHE = MARISA_NORMAL_CACHE
} marisa_cache_level;
enum marisa_cache_level {
MARISA_HUGE_CACHE = 0x00080,
MARISA_LARGE_CACHE = 0x00100,
MARISA_NORMAL_CACHE = 0x00200,
MARISA_SMALL_CACHE = 0x00400,
MARISA_TINY_CACHE = 0x00800,
MARISA_DEFAULT_CACHE = MARISA_NORMAL_CACHE
};
// This library provides 2 kinds of TAIL implementations.
typedef enum marisa_tail_mode_ {
enum marisa_tail_mode {
// MARISA_TEXT_TAIL merges last labels as zero-terminated strings. So, it is
// available if and only if the last labels do not contain a NULL character.
// If MARISA_TEXT_TAIL is specified and a NULL character exists in the last
// labels, the setting is automatically switched to MARISA_BINARY_TAIL.
MARISA_TEXT_TAIL = 0x01000,
MARISA_TEXT_TAIL = 0x01000,
// MARISA_BINARY_TAIL also merges last labels but as byte sequences. It uses
// a bit vector to detect the end of a sequence, instead of NULL characters.
// So, MARISA_BINARY_TAIL requires a larger space if the average length of
// labels is greater than 8.
MARISA_BINARY_TAIL = 0x02000,
MARISA_BINARY_TAIL = 0x02000,
MARISA_DEFAULT_TAIL = MARISA_TEXT_TAIL,
} marisa_tail_mode;
MARISA_DEFAULT_TAIL = MARISA_TEXT_TAIL,
};
// The arrangement of nodes affects the time cost of matching and the order of
// predictive search.
typedef enum marisa_node_order_ {
enum marisa_node_order {
// MARISA_LABEL_ORDER arranges nodes in ascending label order.
// MARISA_LABEL_ORDER is useful if an application needs to predict keys in
// label order.
MARISA_LABEL_ORDER = 0x10000,
MARISA_LABEL_ORDER = 0x10000,
// MARISA_WEIGHT_ORDER arranges nodes in descending weight order.
// MARISA_WEIGHT_ORDER is generally a better choice because it enables faster
// matching.
MARISA_WEIGHT_ORDER = 0x20000,
MARISA_WEIGHT_ORDER = 0x20000,
MARISA_DEFAULT_ORDER = MARISA_WEIGHT_ORDER,
} marisa_node_order;
MARISA_DEFAULT_ORDER = MARISA_WEIGHT_ORDER,
};
typedef enum marisa_config_mask_ {
MARISA_NUM_TRIES_MASK = 0x0007F,
MARISA_CACHE_LEVEL_MASK = 0x00F80,
MARISA_TAIL_MODE_MASK = 0x0F000,
MARISA_NODE_ORDER_MASK = 0xF0000,
MARISA_CONFIG_MASK = 0xFFFFF
} marisa_config_mask;
enum marisa_config_mask {
MARISA_NUM_TRIES_MASK = 0x0007F,
MARISA_CACHE_LEVEL_MASK = 0x00F80,
MARISA_TAIL_MODE_MASK = 0x0F000,
MARISA_NODE_ORDER_MASK = 0xF0000,
MARISA_CONFIG_MASK = 0xFFFFF
};
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
#ifdef __cplusplus
// `std::swap` is in <utility> since C++ 11 but in <algorithm> in C++ 98:
#if __cplusplus >= 201103L
#include <utility>
#else
#include <algorithm>
#endif
namespace marisa {
typedef ::marisa_uint8 UInt8;
typedef ::marisa_uint16 UInt16;
typedef ::marisa_uint32 UInt32;
typedef ::marisa_uint64 UInt64;
// These aliases are left for backward compatibility.
using UInt8 [[deprecated]] = std::uint8_t;
using UInt16 [[deprecated]] = std::uint16_t;
using UInt32 [[deprecated]] = std::uint32_t;
using UInt64 [[deprecated]] = std::uint64_t;
typedef ::marisa_error_code ErrorCode;
using std::uint16_t;
using std::uint32_t;
using std::uint64_t;
using std::uint8_t;
typedef ::marisa_cache_level CacheLevel;
typedef ::marisa_tail_mode TailMode;
typedef ::marisa_node_order NodeOrder;
using ErrorCode = marisa_error_code;
using CacheLevel = marisa_cache_level;
using TailMode = marisa_tail_mode;
using NodeOrder = marisa_node_order;
// This is left for backward compatibility.
using std::swap;
} // namespace marisa
#endif // __cplusplus
// This is left for backward compatibility.
using Exception = std::exception;
#ifdef __cplusplus
#include "marisa/exception.h"
#include "marisa/scoped-ptr.h"
#include "marisa/scoped-array.h"
#endif // __cplusplus
} // namespace marisa
// These macros are used to convert a line number to a string constant.
#define MARISA_INT_TO_STR(value) #value
#define MARISA_LINE_TO_STR(line) MARISA_INT_TO_STR(line)
#define MARISA_LINE_STR MARISA_LINE_TO_STR(__LINE__)
// MARISA_THROW throws an exception with a filename, a line number, an error
// code and an error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
#define MARISA_THROW(error_type, error_message) \
(throw (error_type)(__FILE__ ":" MARISA_LINE_STR ": " #error_type \
": " error_message))
// MARISA_THROW_IF throws an exception if `condition' is true.
#define MARISA_THROW_IF(condition, error_type) \
(void)((!(condition)) || (MARISA_THROW(error_type, #condition), 0))
// MARISA_THROW_SYSTEM_ERROR_IF throws an exception if `condition` is true.
// ::GetLastError() or errno should be passed as `error_value`.
#define MARISA_THROW_SYSTEM_ERROR_IF(condition, error_value, error_category, \
function_name) \
(void)((!(condition)) || \
(throw std::system_error( \
std::error_code(error_value, error_category), \
__FILE__ ":" MARISA_LINE_STR \
": std::system_error: " function_name ": " #condition), \
false))
// #ifndef MARISA_USE_EXCEPTIONS
// #if defined(__GNUC__) && !defined(__EXCEPTIONS)
// #define MARISA_USE_EXCEPTIONS 0
// #elif defined(__clang__) && !defined(__cpp_exceptions)
// #define MARISA_USE_EXCEPTIONS 0
// #elif defined(_MSC_VER) && !_HAS_EXCEPTIONS
// #define MARISA_USE_EXCEPTIONS 0
// #else
// #define MARISA_USE_EXCEPTIONS 1
// #endif
// #endif
// #if MARISA_USE_EXCEPTIONS
// #define MARISA_TRY try
// #define MARISA_CATCH(x) catch (x)
// #else
// #define MARISA_TRY if (true)
// #define MARISA_CATCH(x) if (false)
// #endif
#endif // MARISA_BASE_H_

View file

@ -1,82 +0,0 @@
#ifndef MARISA_EXCEPTION_H_
#define MARISA_EXCEPTION_H_
#include <exception>
#include "marisa/base.h"
namespace marisa {
// An exception object keeps a filename, a line number, an error code and an
// error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
class Exception : public std::exception {
public:
Exception(const char *filename, int line,
ErrorCode error_code, const char *error_message)
: std::exception(), filename_(filename), line_(line),
error_code_(error_code), error_message_(error_message) {}
Exception(const Exception &ex)
: std::exception(), filename_(ex.filename_), line_(ex.line_),
error_code_(ex.error_code_), error_message_(ex.error_message_) {}
virtual ~Exception() throw() {}
Exception &operator=(const Exception &rhs) {
filename_ = rhs.filename_;
line_ = rhs.line_;
error_code_ = rhs.error_code_;
error_message_ = rhs.error_message_;
return *this;
}
const char *filename() const {
return filename_;
}
int line() const {
return line_;
}
ErrorCode error_code() const {
return error_code_;
}
const char *error_message() const {
return error_message_;
}
virtual const char *what() const throw() {
return error_message_;
}
private:
const char *filename_;
int line_;
ErrorCode error_code_;
const char *error_message_;
};
// These macros are used to convert a line number to a string constant.
#define MARISA_INT_TO_STR(value) #value
#define MARISA_LINE_TO_STR(line) MARISA_INT_TO_STR(line)
#define MARISA_LINE_STR MARISA_LINE_TO_STR(__LINE__)
// MARISA_THROW throws an exception with a filename, a line number, an error
// code and an error message. The message format is as follows:
// "__FILE__:__LINE__: error_code: error_message"
#define MARISA_THROW(error_code, error_message) \
(throw marisa::Exception(__FILE__, __LINE__, error_code, \
__FILE__ ":" MARISA_LINE_STR ": " #error_code ": " error_message))
// MARISA_THROW_IF throws an exception if `condition' is true.
#define MARISA_THROW_IF(condition, error_code) \
(void)((!(condition)) || (MARISA_THROW(error_code, #condition), 0))
// MARISA_DEBUG_IF is ignored if _DEBUG is undefined. So, it is useful for
// debugging time-critical codes.
#ifdef _DEBUG
#define MARISA_DEBUG_IF(cond, error_code) MARISA_THROW_IF(cond, error_code)
#else
#define MARISA_DEBUG_IF(cond, error_code)
#endif
} // namespace marisa
#endif // MARISA_EXCEPTION_H_

View file

@ -1,9 +1,8 @@
#ifndef MARISA_KEY_H_
#define MARISA_KEY_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <string_view>
#include "marisa/base.h"
@ -11,58 +10,45 @@ namespace marisa {
class Key {
public:
Key() : ptr_(NULL), length_(0), union_() {
union_.id = 0;
}
Key(const Key &key)
: ptr_(key.ptr_), length_(key.length_), union_(key.union_) {}
Key &operator=(const Key &key) {
ptr_ = key.ptr_;
length_ = key.length_;
union_ = key.union_;
return *this;
}
Key() = default;
Key(const Key &key) = default;
Key &operator=(const Key &key) = default;
char operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR);
assert(i < length_);
return ptr_[i];
}
#if __cplusplus >= 201703L
void set_str(std::string_view str) {
set_str(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_str(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
std::size_t length = 0;
while (str[length] != '\0') {
++length;
}
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert(length <= UINT32_MAX);
ptr_ = str;
length_ = (UInt32)length;
length_ = static_cast<uint32_t>(length);
}
void set_str(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
MARISA_DEBUG_IF(length > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
assert((ptr != nullptr) || (length == 0));
assert(length <= UINT32_MAX);
ptr_ = ptr;
length_ = (UInt32)length;
length_ = static_cast<uint32_t>(length);
}
void set_id(std::size_t id) {
MARISA_DEBUG_IF(id > MARISA_UINT32_MAX, MARISA_SIZE_ERROR);
union_.id = (UInt32)id;
assert(id <= UINT32_MAX);
union_.id = static_cast<uint32_t>(id);
}
void set_weight(float weight) {
union_.weight = weight;
}
#if __cplusplus >= 201703L
std::string_view str() const {
return std::string_view(ptr_, length_);
}
#endif // __cplusplus >= 201703L
const char *ptr() const {
return ptr_;
}
@ -76,20 +62,20 @@ class Key {
return union_.weight;
}
void clear() {
void clear() noexcept {
Key().swap(*this);
}
void swap(Key &rhs) {
marisa::swap(ptr_, rhs.ptr_);
marisa::swap(length_, rhs.length_);
marisa::swap(union_.id, rhs.union_.id);
void swap(Key &rhs) noexcept {
std::swap(ptr_, rhs.ptr_);
std::swap(length_, rhs.length_);
std::swap(union_.id, rhs.union_.id);
}
private:
const char *ptr_;
UInt32 length_;
const char *ptr_ = nullptr;
uint32_t length_ = 0;
union Union {
UInt32 id;
uint32_t id = 0;
float weight;
} union_;
};

View file

@ -1,9 +1,9 @@
#ifndef MARISA_KEYSET_H_
#define MARISA_KEYSET_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <memory>
#include <string_view>
#include "marisa/key.h"
@ -12,30 +12,31 @@ namespace marisa {
class Keyset {
public:
enum {
BASE_BLOCK_SIZE = 4096,
BASE_BLOCK_SIZE = 4096,
EXTRA_BLOCK_SIZE = 1024,
KEY_BLOCK_SIZE = 256
KEY_BLOCK_SIZE = 256
};
Keyset();
Keyset(const Keyset &) = delete;
Keyset &operator=(const Keyset &) = delete;
void push_back(const Key &key);
void push_back(const Key &key, char end_marker);
#if __cplusplus >= 201703L
void push_back(std::string_view str, float weight = 1.0) {
push_back(str.data(), str.length(), weight);
}
#endif // __cplusplus >= 201703L
void push_back(const char *str);
void push_back(const char *ptr, std::size_t length, float weight = 1.0);
const Key &operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
assert(i < size_);
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
}
Key &operator[](std::size_t i) {
MARISA_DEBUG_IF(i >= size_, MARISA_BOUND_ERROR);
assert(i < size_);
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
}
@ -55,33 +56,29 @@ class Keyset {
void reset();
void clear();
void swap(Keyset &rhs);
void clear() noexcept;
void swap(Keyset &rhs) noexcept;
private:
scoped_array<scoped_array<char> > base_blocks_;
std::size_t base_blocks_size_;
std::size_t base_blocks_capacity_;
scoped_array<scoped_array<char> > extra_blocks_;
std::size_t extra_blocks_size_;
std::size_t extra_blocks_capacity_;
scoped_array<scoped_array<Key> > key_blocks_;
std::size_t key_blocks_size_;
std::size_t key_blocks_capacity_;
char *ptr_;
std::size_t avail_;
std::size_t size_;
std::size_t total_length_;
std::unique_ptr<std::unique_ptr<char[]>[]> base_blocks_;
std::size_t base_blocks_size_ = 0;
std::size_t base_blocks_capacity_ = 0;
std::unique_ptr<std::unique_ptr<char[]>[]> extra_blocks_;
std::size_t extra_blocks_size_ = 0;
std::size_t extra_blocks_capacity_ = 0;
std::unique_ptr<std::unique_ptr<Key[]>[]> key_blocks_;
std::size_t key_blocks_size_ = 0;
std::size_t key_blocks_capacity_ = 0;
char *ptr_ = nullptr;
std::size_t avail_ = 0;
std::size_t size_ = 0;
std::size_t total_length_ = 0;
char *reserve(std::size_t size);
void append_base_block();
void append_extra_block(std::size_t size);
void append_key_block();
// Disallows copy and assignment.
Keyset(const Keyset &);
Keyset &operator=(const Keyset &);
};
} // namespace marisa

View file

@ -1,9 +1,8 @@
#ifndef MARISA_QUERY_H_
#define MARISA_QUERY_H_
#if __cplusplus >= 201703L
#include <string_view>
#endif // __cplusplus >= 201703L
#include <cassert>
#include <string_view>
#include "marisa/base.h"
@ -11,29 +10,21 @@ namespace marisa {
class Query {
public:
Query() : ptr_(NULL), length_(0), id_(0) {}
Query(const Query &query)
: ptr_(query.ptr_), length_(query.length_), id_(query.id_) {}
Query() = default;
Query(const Query &query) = default;
Query &operator=(const Query &query) {
ptr_ = query.ptr_;
length_ = query.length_;
id_ = query.id_;
return *this;
}
Query &operator=(const Query &query) = default;
char operator[](std::size_t i) const {
MARISA_DEBUG_IF(i >= length_, MARISA_BOUND_ERROR);
assert(i < length_);
return ptr_[i];
}
#if __cplusplus >= 201703L
void set_str(std::string_view str) {
set_str(str.data(), str.length());
}
#endif // __cplusplus >= 201703L
void set_str(const char *str) {
MARISA_DEBUG_IF(str == NULL, MARISA_NULL_ERROR);
assert(str != nullptr);
std::size_t length = 0;
while (str[length] != '\0') {
++length;
@ -42,7 +33,7 @@ class Query {
length_ = length;
}
void set_str(const char *ptr, std::size_t length) {
MARISA_DEBUG_IF((ptr == NULL) && (length != 0), MARISA_NULL_ERROR);
assert((ptr != nullptr) || (length == 0));
ptr_ = ptr;
length_ = length;
}
@ -50,11 +41,9 @@ class Query {
id_ = id;
}
#if __cplusplus >= 201703L
std::string_view str() const {
return std::string_view(ptr_, length_);
}
#endif // __cplusplus >= 201703L
const char *ptr() const {
return ptr_;
}
@ -65,19 +54,19 @@ class Query {
return id_;
}
void clear() {
void clear() noexcept {
Query().swap(*this);
}
void swap(Query &rhs) {
marisa::swap(ptr_, rhs.ptr_);
marisa::swap(length_, rhs.length_);
marisa::swap(id_, rhs.id_);
void swap(Query &rhs) noexcept {
std::swap(ptr_, rhs.ptr_);
std::swap(length_, rhs.length_);
std::swap(id_, rhs.id_);
}
private:
const char *ptr_;
std::size_t length_;
std::size_t id_;
const char *ptr_ = nullptr;
std::size_t length_ = 0;
std::size_t id_ = 0;
};
} // namespace marisa

View file

@ -1,48 +0,0 @@
#ifndef MARISA_SCOPED_ARRAY_H_
#define MARISA_SCOPED_ARRAY_H_
#include "marisa/base.h"
namespace marisa {
template <typename T>
class scoped_array {
public:
scoped_array() : array_(NULL) {}
explicit scoped_array(T *array) : array_(array) {}
~scoped_array() {
delete [] array_;
}
void reset(T *array = NULL) {
MARISA_DEBUG_IF((array != NULL) && (array == array_), MARISA_RESET_ERROR);
scoped_array(array).swap(*this);
}
T &operator[](std::size_t i) const {
MARISA_DEBUG_IF(array_ == NULL, MARISA_STATE_ERROR);
return array_[i];
}
T *get() const {
return array_;
}
void clear() {
scoped_array().swap(*this);
}
void swap(scoped_array &rhs) {
marisa::swap(array_, rhs.array_);
}
private:
T *array_;
// Disallows copy and assignment.
scoped_array(const scoped_array &);
scoped_array &operator=(const scoped_array &);
};
} // namespace marisa
#endif // MARISA_SCOPED_ARRAY_H_

View file

@ -1,52 +0,0 @@
#ifndef MARISA_SCOPED_PTR_H_
#define MARISA_SCOPED_PTR_H_
#include "marisa/base.h"
namespace marisa {
template <typename T>
class scoped_ptr {
public:
scoped_ptr() : ptr_(NULL) {}
explicit scoped_ptr(T *ptr) : ptr_(ptr) {}
~scoped_ptr() {
delete ptr_;
}
void reset(T *ptr = NULL) {
MARISA_DEBUG_IF((ptr != NULL) && (ptr == ptr_), MARISA_RESET_ERROR);
scoped_ptr(ptr).swap(*this);
}
T &operator*() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return *ptr_;
}
T *operator->() const {
MARISA_DEBUG_IF(ptr_ == NULL, MARISA_STATE_ERROR);
return ptr_;
}
T *get() const {
return ptr_;
}
void clear() {
scoped_ptr().swap(*this);
}
void swap(scoped_ptr &rhs) {
marisa::swap(ptr_, rhs.ptr_);
}
private:
T *ptr_;
// Disallows copy and assignment.
scoped_ptr(const scoped_ptr &);
scoped_ptr &operator=(const scoped_ptr &);
};
} // namespace marisa
#endif // MARISA_SCOPED_PTR_H_

View file

@ -1,17 +1,17 @@
#ifndef MARISA_TRIE_H_
#define MARISA_TRIE_H_
#include "marisa/keyset.h"
#include "marisa/agent.h"
#include <memory>
#include "marisa/agent.h" // IWYU pragma: export
#include "marisa/keyset.h" // IWYU pragma: export
namespace marisa {
namespace grimoire {
namespace trie {
namespace grimoire::trie {
class LoudsTrie;
} // namespace trie
} // namespace grimoire
} // namespace grimoire::trie
class Trie {
friend class TrieIO;
@ -20,9 +20,15 @@ class Trie {
Trie();
~Trie();
Trie(const Trie &) = delete;
Trie &operator=(const Trie &) = delete;
Trie(Trie &&) noexcept;
Trie &operator=(Trie &&) noexcept;
void build(Keyset &keyset, int config_flags = 0);
void mmap(const char *filename);
void mmap(const char *filename, int flags = 0);
void map(const void *ptr, std::size_t size);
void load(const char *filename);
@ -48,15 +54,11 @@ class Trie {
std::size_t total_size() const;
std::size_t io_size() const;
void clear();
void swap(Trie &rhs);
void clear() noexcept;
void swap(Trie &rhs) noexcept;
private:
scoped_ptr<grimoire::trie::LoudsTrie> trie_;
// Disallows copy and assignment.
Trie(const Trie &);
Trie &operator=(const Trie &);
std::unique_ptr<grimoire::trie::LoudsTrie> trie_;
};
} // namespace marisa

View file

@ -1,7 +1,7 @@
####### Expanded from @PACKAGE_INIT@ by configure_package_config_file() #######
####### Any changes to this file will be overwritten by the next CMake run ####
####### The input file was marisa-config.cmake.in ########
####### The input file was Marisa.cmake.in ########
get_filename_component(PACKAGE_PREFIX_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../" ABSOLUTE)
@ -24,8 +24,6 @@ endmacro()
####################################################################################
include(${CMAKE_CURRENT_LIST_DIR}/marisa-targets.cmake)
check_required_components(Marisa)
set_and_check(MARISA_INCLUDE_DIR ${PACKAGE_PREFIX_DIR}/include)
check_required_components(marisa)
include("${CMAKE_CURRENT_LIST_DIR}/MarisaTargets.cmake")

View file

@ -9,19 +9,19 @@
# The variable CVF_VERSION must be set before calling configure_file().
set(PACKAGE_VERSION "0.2.6")
set(PACKAGE_VERSION "0.3.1")
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
if("0.2.6" MATCHES "^([0-9]+)\\.")
if("0.3.1" MATCHES "^([0-9]+)\\.")
set(CVF_VERSION_MAJOR "${CMAKE_MATCH_1}")
if(NOT CVF_VERSION_MAJOR VERSION_EQUAL 0)
string(REGEX REPLACE "^0+" "" CVF_VERSION_MAJOR "${CVF_VERSION_MAJOR}")
endif()
else()
set(CVF_VERSION_MAJOR "0.2.6")
set(CVF_VERSION_MAJOR "0.3.1")
endif()
if(PACKAGE_FIND_VERSION_RANGE)

View file

@ -5,15 +5,15 @@
# Commands may need to know the format version.
set(CMAKE_IMPORT_FILE_VERSION 1)
# Import target "marisa" for configuration "Release"
set_property(TARGET marisa APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(marisa PROPERTIES
# Import target "Marisa::marisa" for configuration "Release"
set_property(TARGET Marisa::marisa APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(Marisa::marisa PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "CXX"
IMPORTED_LOCATION_RELEASE "${_IMPORT_PREFIX}/lib/libmarisa.a"
)
list(APPEND _cmake_import_check_targets marisa )
list(APPEND _cmake_import_check_files_for_marisa "${_IMPORT_PREFIX}/lib/libmarisa.a" )
list(APPEND _cmake_import_check_targets Marisa::marisa )
list(APPEND _cmake_import_check_files_for_Marisa::marisa "${_IMPORT_PREFIX}/lib/libmarisa.a" )
# Commands beyond this point should not need to know the version.
set(CMAKE_IMPORT_FILE_VERSION)

View file

@ -19,7 +19,7 @@ set(CMAKE_IMPORT_FILE_VERSION 1)
set(_cmake_targets_defined "")
set(_cmake_targets_not_defined "")
set(_cmake_expected_targets "")
foreach(_cmake_expected_target IN ITEMS marisa)
foreach(_cmake_expected_target IN ITEMS Marisa::marisa)
list(APPEND _cmake_expected_targets "${_cmake_expected_target}")
if(TARGET "${_cmake_expected_target}")
list(APPEND _cmake_targets_defined "${_cmake_expected_target}")
@ -55,15 +55,15 @@ if(_IMPORT_PREFIX STREQUAL "/")
set(_IMPORT_PREFIX "")
endif()
# Create imported target marisa
add_library(marisa STATIC IMPORTED)
# Create imported target Marisa::marisa
add_library(Marisa::marisa STATIC IMPORTED)
set_target_properties(marisa PROPERTIES
set_target_properties(Marisa::marisa PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
)
# Load information for each installed configuration.
file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/marisa-targets-*.cmake")
file(GLOB _cmake_config_files "${CMAKE_CURRENT_LIST_DIR}/MarisaTargets-*.cmake")
foreach(_cmake_config_file IN LISTS _cmake_config_files)
include("${_cmake_config_file}")
endforeach()

Binary file not shown.