| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2020 Carbo Kuo <[email protected]> |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <atomic> |
| 22 | #include <mutex> |
| 23 | |
| 24 | #include "Common.hpp" |
| 25 | #include "SerializableDict.hpp" |
| 26 | |
| 27 | namespace marisa { |
| 28 | class Trie; |
| 29 | } |
| 30 | |
| 31 | namespace opencc { |
| 32 | /** |
| 33 | * Darts dictionary |
| 34 | * @ingroup opencc_cpp_api |
| 35 | */ |
| 36 | class OPENCC_EXPORT MarisaDict : public Dict, public SerializableDict { |
| 37 | public: |
| 38 | virtual ~MarisaDict() override; |
| 39 | |
| 40 | virtual size_t KeyMaxLength() const override; |
| 41 | |
| 42 | virtual Optional<const DictEntry*> Match(const char* word, |
| 43 | size_t len) const override; |
| 44 | |
| 45 | virtual Optional<const DictEntry*> MatchPrefix(const char* word, |
| 46 | size_t len) const override; |
| 47 | |
| 48 | virtual std::vector<const DictEntry*> MatchAllPrefixes( |
| 49 | const char* word, size_t len) const override; |
| 50 | |
| 51 | virtual LexiconPtr GetLexicon() const override; |
| 52 | |
| 53 | virtual bool SupportsFastPrefixMatch() const override { return true; } |
| 54 | |
| 55 | virtual PrefixMatchView MatchPrefixValue(const char* word, |
| 56 | size_t len) const override; |
| 57 | |
| 58 | virtual void SerializeToFile(FILE* fp) const override; |
| 59 | |
| 60 | /** |
| 61 | * Constructs a MarisaDict from another dictionary. |
| 62 | */ |
| 63 | static MarisaDictPtr NewFromDict(const Dict& thatDict); |
| 64 | |
| 65 | static MarisaDictPtr NewFromFile(FILE* fp); |
| 66 | |
| 67 | static MarisaDictPtr NewFromBuffer(const char* data, size_t size); |
| 68 | |
| 69 | // Exposed for testing only. |
| 70 | bool IsLexiconReconstructed() const { |
| 71 | return lexiconReconstructed.load(std::memory_order_acquire); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | MarisaDict(); |
| 76 | |
| 77 | void LoadFromMappedBuffer(); |
| 78 | void ReconstructLexicon() const; |
| 79 | |
| 80 | mutable size_t maxLength; |
| 81 | mutable LexiconPtr lexicon; |
| 82 | mutable std::mutex lexiconMutex; |
| 83 | mutable std::atomic<bool> lexiconReconstructed; |
| 84 | LexiconPtr valuesLexicon; |
| 85 | |
| 86 | class MarisaInternal; |
| 87 | std::unique_ptr<MarisaInternal> internal; |
| 88 | }; |
| 89 | } // namespace opencc |