Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions docs/readme.en.html
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ <h3><a name="key">class Key</a></h3>
char operator[](std::size_t i) const;
std::string_view str() const;
const char *ptr() const;
std::size_t length() const;
std::size_t id() const;
uint32_t length() const;
uint32_t id() const;
};</pre>
</div><!-- float -->
<p>
Expand All @@ -531,8 +531,8 @@ <h3><a name="query">class Query</a></h3>
char operator[](std::size_t i) const;
std::string_view str() const;
const char *ptr() const;
std::size_t length() const;
std::size_t id() const;
uint32_t length() const;
uint32_t id() const;
};</pre>
</div><!-- float -->
<p>
Expand Down Expand Up @@ -560,11 +560,11 @@ <h3><a name="keyset">class Keyset</a></h3>
const Key &amp;operator[](std::size_t i) const;
Key &amp;operator[](std::size_t i);

std::size_t num_keys();
uint32_t num_keys();

bool empty() const;
std::size_t size() const;
std::size_t total_length() const;
uint32_t size() const;
uint32_t total_length() const;

void reset();

Expand Down Expand Up @@ -615,7 +615,7 @@ <h3><a name="agent">class Agent</a></h3>
void set_query(const char *str);
void set_query(const char *ptr,
std::size_t length);
void set_query(std::size_t key_id);
void set_query(uint32_t key_id);
};</pre>
</div><!-- float -->
<p>
Expand Down Expand Up @@ -652,9 +652,9 @@ <h3><a name="trie">class Trie</a></h3>
bool common_prefix_search(Agent &amp;agent) const;
bool predictive_search(Agent &amp;agent) const;

std::size_t num_tries() const;
std::size_t num_keys() const;
std::size_t num_nodes() const;
uint32_t num_tries() const;
uint32_t num_keys() const;
uint32_t num_nodes() const;

TailMode tail_mode() const;
NodeOrder node_order() const;
Expand Down
22 changes: 11 additions & 11 deletions docs/readme.ja.html
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,8 @@ <h3><a name="key">class Key</a></h3>
char operator[](std::size_t i) const;
std::string_view str() const;
const char *ptr() const;
std::size_t length() const;
std::size_t id() const;
uint32_t length() const;
uint32_t id() const;
};</pre>
</div><!-- float -->
<p>
Expand All @@ -533,8 +533,8 @@ <h3><a name="query">class Query</a></h3>
char operator[](std::size_t i) const;
std::string_view str() const;
const char *ptr() const;
std::size_t length() const;
std::size_t id() const;
uint32_t length() const;
uint32_t id() const;
};</pre>
</div><!-- float -->
<p>
Expand Down Expand Up @@ -562,11 +562,11 @@ <h3><a name="keyset">class Keyset</a></h3>
const Key &amp;operator[](std::size_t i) const;
Key &amp;operator[](std::size_t i);

std::size_t num_keys();
uint32_t num_keys();

bool empty() const;
std::size_t size() const;
std::size_t total_length() const;
uint32_t size() const;
uint32_t total_length() const;

void reset();

Expand Down Expand Up @@ -617,7 +617,7 @@ <h3><a name="agent">class Agent</a></h3>
void set_query(const char *str);
void set_query(const char *ptr,
std::size_t length);
void set_query(std::size_t key_id);
void set_query(uint32_t key_id);
};</pre>
</div><!-- float -->
<p>
Expand Down Expand Up @@ -654,9 +654,9 @@ <h3><a name="trie">class Trie</a></h3>
bool common_prefix_search(Agent &amp;agent) const;
bool predictive_search(Agent &amp;agent) const;

std::size_t num_tries() const;
std::size_t num_keys() const;
std::size_t num_nodes() const;
uint32_t num_tries() const;
uint32_t num_keys() const;
uint32_t num_nodes() const;

TailMode tail_mode() const;
NodeOrder node_order() const;
Expand Down
2 changes: 1 addition & 1 deletion include/marisa/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Agent {
}
void set_query(const char *str);
void set_query(const char *ptr, std::size_t length);
void set_query(std::size_t key_id);
void set_query(uint32_t key_id);

const grimoire::trie::State &state() const {
return *state_;
Expand Down
4 changes: 2 additions & 2 deletions include/marisa/key.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class Key {
const char *ptr() const {
return ptr_;
}
std::size_t length() const {
uint32_t length() const {
return length_;
}
std::size_t id() const {
uint32_t id() const {
return union_.id;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setter on line 41 is void set_id(std::size_t id), but should probably also be a uint32_t.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, #include <cstdint> should probably be added to this file.

}
float weight() const {
Expand Down
28 changes: 14 additions & 14 deletions include/marisa/keyset.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ class Keyset {
return key_blocks_[i / KEY_BLOCK_SIZE][i % KEY_BLOCK_SIZE];
}

std::size_t num_keys() const {
uint32_t num_keys() const {
return size_;
}

bool empty() const {
return size_ == 0;
}
std::size_t size() const {
uint32_t size() const {
return size_;
}
std::size_t total_length() const {
uint32_t total_length() const {
return total_length_;
}

Expand All @@ -61,23 +61,23 @@ class Keyset {

private:
std::unique_ptr<std::unique_ptr<char[]>[]> base_blocks_;
std::size_t base_blocks_size_ = 0;
std::size_t base_blocks_capacity_ = 0;
uint32_t base_blocks_size_ = 0;
uint32_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;
uint32_t extra_blocks_size_ = 0;
uint32_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;
uint32_t key_blocks_size_ = 0;
uint32_t key_blocks_capacity_ = 0;
Comment on lines +64 to +71
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While these should fit in a uint32_t, it might be better to keep this as a size_t since they're primarily used for indexing memory.

char *ptr_ = nullptr;
std::size_t avail_ = 0;
std::size_t size_ = 0;
std::size_t total_length_ = 0;
uint32_t avail_ = 0;
uint32_t size_ = 0;
uint32_t total_length_ = 0;

char *reserve(std::size_t size);
char *reserve(uint32_t size);

void append_base_block();
void append_extra_block(std::size_t size);
void append_extra_block(uint32_t size);
void append_key_block();
};

Expand Down
10 changes: 5 additions & 5 deletions include/marisa/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Query {
ptr_ = ptr;
length_ = length;
}
void set_id(std::size_t id) {
void set_id(uint32_t id) {
id_ = id;
}

Expand All @@ -47,10 +47,10 @@ class Query {
const char *ptr() const {
return ptr_;
}
std::size_t length() const {
uint32_t length() const {
return length_;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an assert(length <= UINT32_MAX); to the set_str methods like the other files.

}
std::size_t id() const {
uint32_t id() const {
return id_;
}

Expand All @@ -65,8 +65,8 @@ class Query {

private:
const char *ptr_ = nullptr;
std::size_t length_ = 0;
std::size_t id_ = 0;
uint32_t length_ = 0;
uint32_t id_ = 0;
};

} // namespace marisa
Expand Down
3 changes: 2 additions & 1 deletion lib/marisa/agent.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "marisa/agent.h"

#include <cstdint>
#include <new>
#include <stdexcept>
#include <utility>
Expand Down Expand Up @@ -73,7 +74,7 @@ void Agent::set_query(const char *ptr, std::size_t length) {
query_.set_str(ptr, length);
}

void Agent::set_query(std::size_t key_id) {
void Agent::set_query(uint32_t key_id) {
if (state_ != nullptr) {
state_->reset();
}
Expand Down
23 changes: 11 additions & 12 deletions lib/marisa/grimoire/trie/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <cassert>
#include <cfloat>
#include <cstdint>

#include "marisa/base.h"

Expand All @@ -14,41 +15,39 @@ class Cache {
Cache(const Cache &cache) = default;
Cache &operator=(const Cache &cache) = default;

void set_parent(std::size_t parent) {
assert(parent <= UINT32_MAX);
parent_ = static_cast<uint32_t>(parent);
void set_parent(uint32_t parent) {
parent_ = parent;
}
void set_child(std::size_t child) {
assert(child <= UINT32_MAX);
child_ = static_cast<uint32_t>(child);
void set_child(uint32_t child) {
child_ = child;
}
void set_base(uint8_t base) {
union_.link = (union_.link & ~0xFFU) | base;
}
void set_extra(std::size_t extra) {
void set_extra(uint32_t extra) {
assert(extra <= (UINT32_MAX >> 8));
union_.link = static_cast<uint32_t>((union_.link & 0xFFU) | (extra << 8));
union_.link = (union_.link & 0xFFU) | (extra << 8);
}
void set_weight(float weight) {
union_.weight = weight;
}

std::size_t parent() const {
uint32_t parent() const {
return parent_;
}
std::size_t child() const {
uint32_t child() const {
return child_;
}
uint8_t base() const {
return static_cast<uint8_t>(union_.link & 0xFFU);
}
std::size_t extra() const {
uint32_t extra() const {
return union_.link >> 8;
}
char label() const {
return static_cast<char>(base());
}
std::size_t link() const {
uint32_t link() const {
return union_.link;
}
float weight() const {
Expand Down
11 changes: 5 additions & 6 deletions lib/marisa/grimoire/trie/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,24 @@ class Entry {
ptr_ = ptr + length - 1;
length_ = static_cast<uint32_t>(length);
}
void set_id(std::size_t id) {
assert(id <= UINT32_MAX);
id_ = static_cast<uint32_t>(id);
void set_id(uint32_t id) {
id_ = id;
}

const char *ptr() const {
return ptr_ - length_ + 1;
}
std::size_t length() const {
uint32_t length() const {
return length_;
}
std::size_t id() const {
uint32_t id() const {
return id_;
}

class StringComparer {
public:
bool operator()(const Entry &lhs, const Entry &rhs) const {
for (std::size_t i = 0; i < lhs.length(); ++i) {
for (uint32_t i = 0; i < lhs.length(); ++i) {
if (i == rhs.length()) {
return true;
}
Expand Down
35 changes: 15 additions & 20 deletions lib/marisa/grimoire/trie/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,35 @@ class History {
public:
History() = default;

void set_node_id(std::size_t node_id) {
assert(node_id <= UINT32_MAX);
node_id_ = static_cast<uint32_t>(node_id);
void set_node_id(uint32_t node_id) {
node_id_ = node_id;
}
void set_louds_pos(std::size_t louds_pos) {
assert(louds_pos <= UINT32_MAX);
louds_pos_ = static_cast<uint32_t>(louds_pos);
void set_louds_pos(uint32_t louds_pos) {
louds_pos_ = louds_pos;
}
void set_key_pos(std::size_t key_pos) {
assert(key_pos <= UINT32_MAX);
key_pos_ = static_cast<uint32_t>(key_pos);
void set_key_pos(uint32_t key_pos) {
key_pos_ = key_pos;
}
void set_link_id(std::size_t link_id) {
assert(link_id <= UINT32_MAX);
link_id_ = static_cast<uint32_t>(link_id);
void set_link_id(uint32_t link_id) {
link_id_ = link_id;
}
void set_key_id(std::size_t key_id) {
assert(key_id <= UINT32_MAX);
key_id_ = static_cast<uint32_t>(key_id);
void set_key_id(uint32_t key_id) {
key_id_ = key_id;
}

std::size_t node_id() const {
uint32_t node_id() const {
return node_id_;
}
std::size_t louds_pos() const {
uint32_t louds_pos() const {
return louds_pos_;
}
std::size_t key_pos() const {
uint32_t key_pos() const {
return key_pos_;
}
std::size_t link_id() const {
uint32_t link_id() const {
return link_id_;
}
std::size_t key_id() const {
uint32_t key_id() const {
return key_id_;
}

Expand Down
Loading