MCPcopy Create free account
hub / github.com/BYVoid/OpenCC / PrefixMatch

Method PrefixMatch

src/PrefixMatch.cpp:329–395  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

327}
328
329PrefixMatch::PrefixMatch(const DictPtr& dict) {
330 // Try to unwrap single dict group
331 DictPtr actualDict = dict;
332 while (actualDict) {
333 const std::list<DictPtr>* items = actualDict->GetDictGroupItems();
334 if (items != nullptr && items->size() == 1) {
335 actualDict = items->front();
336 } else {
337 break;
338 }
339 }
340
341 if (actualDict && actualDict->SupportsFastPrefixMatch()) {
342 singleDict = actualDict;
343 return;
344 }
345
346 static std::mutex cacheMutex;
347 static std::unordered_map<std::string, std::vector<CacheEntry>> cache;
348
349 std::string cacheKey;
350 AppendCacheKey(dict, &cacheKey);
351 std::vector<std::weak_ptr<const Dict>> leafDicts;
352 CollectLeafDicts(dict, &leafDicts);
353
354 {
355 std::lock_guard<std::mutex> lock(cacheMutex);
356 PruneExpiredPrefixMatchCache(&cache);
357 const auto cached = cache.find(cacheKey);
358 if (cached != cache.end()) {
359 for (const CacheEntry& entry : cached->second) {
360 if (SameDicts(entry, leafDicts)) {
361 tables = entry.tables.lock();
362 if (tables != nullptr) {
363 return;
364 }
365 }
366 }
367 }
368 }
369
370 std::shared_ptr<Tables> built(new Tables);
371 built->matcher = BuildMatcher(dict);
372
373 std::lock_guard<std::mutex> lock(cacheMutex);
374 PruneExpiredPrefixMatchCache(&cache);
375 std::vector<CacheEntry>& entries = cache[cacheKey];
376 for (std::vector<CacheEntry>::iterator it = entries.begin();
377 it != entries.end();) {
378 if (HasExpiredDict(*it)) {
379 it = entries.erase(it);
380 } else if (SameDicts(*it, leafDicts)) {
381 tables = it->tables.lock();
382 if (tables != nullptr) {
383 return;
384 }
385 it = entries.erase(it);
386 } else {

Callers

nothing calls this directly

Calls 10

SameDictsFunction · 0.85
BuildMatcherFunction · 0.85
HasExpiredDictFunction · 0.85
moveFunction · 0.85
findMethod · 0.80
sizeMethod · 0.45
endMethod · 0.45
beginMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected