forked from google/mozc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.cc
214 lines (182 loc) · 7.2 KB
/
connector.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2010-2016, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "converter/connector.h"
#include <algorithm>
#include "base/logging.h"
#include "base/port.h"
#include "base/stl_util.h"
#include "data_manager/data_manager_interface.h"
#include "storage/louds/simple_succinct_bit_vector_index.h"
using mozc::storage::louds::SimpleSuccinctBitVectorIndex;
namespace mozc {
namespace {
const uint32 kInvalidCacheKey = 0xFFFFFFFF;
const uint16 kConnectorMagicNumber = 0xCDAB;
const uint8 kInvalid1ByteCostValue = 255;
inline uint32 GetHashValue(uint16 rid, uint16 lid, uint32 hash_mask) {
return (3 * static_cast<uint32>(rid) + lid) & hash_mask;
// Note: The above value is equivalent to
// return (3 * rid + lid) % cache_size_
// because cache_size_ is the power of 2.
// Multiplying '3' makes the conversion speed faster.
// The result hash value becomes reasonalbly random.
}
inline uint32 EncodeKey(uint16 rid, uint16 lid) {
return (static_cast<uint32>(rid) << 16) | lid;
}
} // namespace
class Connector::Row {
public:
Row() : chunk_bits_index_(sizeof(uint32)),
compact_bits_index_(sizeof(uint32)) {}
void Init(const uint8 *chunk_bits, size_t chunk_bits_size,
const uint8 *compact_bits, size_t compact_bits_size,
const uint8 *values, bool use_1byte_value) {
chunk_bits_index_.Init(chunk_bits, chunk_bits_size);
compact_bits_index_.Init(compact_bits, compact_bits_size);
values_ = values;
use_1byte_value_ = use_1byte_value;
}
// Returns true if the value is found in the row and then store the found
// value into |value|. Otherwise returns false.
bool GetValue(uint16 index, uint16 *value) const {
int chunk_bit_position = index / 8;
if (!chunk_bits_index_.Get(chunk_bit_position)) {
return false;
}
int compact_bit_position =
chunk_bits_index_.Rank1(chunk_bit_position) * 8 + index % 8;
if (!compact_bits_index_.Get(compact_bit_position)) {
return false;
}
int value_position = compact_bits_index_.Rank1(compact_bit_position);
if (use_1byte_value_) {
*value = values_[value_position];
if (*value == kInvalid1ByteCostValue) {
*value = kInvalidCost;
}
} else {
*value = reinterpret_cast<const uint16 *>(values_)[value_position];
}
return true;
}
private:
SimpleSuccinctBitVectorIndex chunk_bits_index_;
SimpleSuccinctBitVectorIndex compact_bits_index_;
const uint8 *values_;
bool use_1byte_value_;
DISALLOW_COPY_AND_ASSIGN(Row);
};
Connector *Connector::CreateFromDataManager(
const DataManagerInterface &data_manager) {
#ifdef OS_ANDROID
const int kCacheSize = 256;
#else
const int kCacheSize = 1024;
#endif // OS_ANDROID
const char *connection_data = nullptr;
size_t connection_data_size = 0;
data_manager.GetConnectorData(&connection_data, &connection_data_size);
return new Connector(connection_data, connection_data_size, kCacheSize);
}
Connector::Connector(const char *connection_data,
size_t connection_size,
int cache_size)
: default_cost_(nullptr),
cache_size_(cache_size),
cache_hash_mask_(cache_size - 1),
cache_key_(new uint32[cache_size]),
cache_value_(new int[cache_size]) {
const uint16 *ptr = reinterpret_cast<const uint16 *>(connection_data);
CHECK_EQ(kConnectorMagicNumber, ptr[0]);
resolution_ = ptr[1];
const uint16 rsize = ptr[2];
const uint16 lsize = ptr[3];
CHECK_EQ(rsize, lsize) << "The connector matrix should be square.";
default_cost_ = ptr + 4;
// Calculate the row's beginning position. Note that it should be aligned to
// 32-bits boundary.
size_t offset = 8 + (rsize + (rsize & 1)) * 2;
// The number of valid bits in a chunk. Each bit is bitwise-or of consecutive
// 8-bits.
const size_t num_chunk_bits = (lsize + 7) / 8;
// Then calculate the actual size of chunk in bytes, which is aligned to
// 32-bits boundary.
const size_t chunk_bits_size = (num_chunk_bits + 31) / 32 * 4;
const bool use_1byte_value = resolution_ != 1;
rows_.reserve(rsize);
for (size_t i = 0; i < rsize; ++i) {
const uint16 *size_data =
reinterpret_cast<const uint16 *>(connection_data + offset);
Row *row = new Row;
const uint16 compact_bits_size = size_data[0];
CHECK_EQ(compact_bits_size % 4, 0) << compact_bits_size;
const uint16 values_size = size_data[1];
CHECK_EQ(values_size % 4, 0) << values_size;
const uint8 *chunk_bits =
reinterpret_cast<const uint8 *>(connection_data + offset + 4);
const uint8 *compact_bits = chunk_bits + chunk_bits_size;
const uint8 *values = compact_bits + compact_bits_size;
row->Init(chunk_bits, chunk_bits_size, compact_bits, compact_bits_size,
values, use_1byte_value);
rows_.push_back(row);
offset += 4 + chunk_bits_size + compact_bits_size + values_size;
}
// Check if the cache_size is the power of 2 and clear cache.
DCHECK_EQ(0, cache_size & (cache_size - 1));
ClearCache();
}
Connector::~Connector() {
STLDeleteElements(&rows_);
}
int Connector::GetTransitionCost(uint16 rid, uint16 lid) const {
const uint32 index = EncodeKey(rid, lid);
const uint32 bucket = GetHashValue(rid, lid, cache_hash_mask_);
if (cache_key_[bucket] == index) {
return cache_value_[bucket];
}
const int value = LookupCost(rid, lid);
cache_key_[bucket] = index;
cache_value_[bucket] = value;
return value;
}
int Connector::GetResolution() const {
return resolution_;
}
void Connector::ClearCache() {
std::fill(cache_key_.get(), cache_key_.get() + cache_size_, kInvalidCacheKey);
}
int Connector::LookupCost(uint16 rid, uint16 lid) const {
uint16 value;
if (!rows_[rid]->GetValue(lid, &value)) {
return default_cost_[rid];
}
return value * resolution_;
}
} // namespace mozc