1 /*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <assert.h>
18 #include "../include/lpicache.h"
19
20 namespace ime_pinyin {
21
22 LpiCache* LpiCache::instance_ = NULL;
23
LpiCache()24 LpiCache::LpiCache() {
25 lpi_cache_ = new LmaPsbItem[kFullSplIdStart * kMaxLpiCachePerId];
26 lpi_cache_len_ = new uint16[kFullSplIdStart];
27 assert(NULL != lpi_cache_);
28 assert(NULL != lpi_cache_len_);
29 for (uint16 id = 0; id < kFullSplIdStart; id++)
30 lpi_cache_len_[id] = 0;
31 }
32
~LpiCache()33 LpiCache::~LpiCache() {
34 if (NULL != lpi_cache_)
35 delete [] lpi_cache_;
36
37 if (NULL != lpi_cache_len_)
38 delete [] lpi_cache_len_;
39 }
40
get_instance()41 LpiCache& LpiCache::get_instance() {
42 if (NULL == instance_) {
43 instance_ = new LpiCache();
44 assert(NULL != instance_);
45 }
46 return *instance_;
47 }
48
is_cached(uint16 splid)49 bool LpiCache::is_cached(uint16 splid) {
50 if (splid >= kFullSplIdStart)
51 return false;
52 return lpi_cache_len_[splid] != 0;
53 }
54
put_cache(uint16 splid,LmaPsbItem lpi_items[],size_t lpi_num)55 size_t LpiCache::put_cache(uint16 splid, LmaPsbItem lpi_items[],
56 size_t lpi_num) {
57 uint16 num = kMaxLpiCachePerId;
58 if (num > lpi_num)
59 num = static_cast<uint16>(lpi_num);
60
61 LmaPsbItem *lpi_cache_this = lpi_cache_ + splid * kMaxLpiCachePerId;
62 for (uint16 pos = 0; pos < num; pos++)
63 lpi_cache_this[pos] = lpi_items[pos];
64
65 lpi_cache_len_[splid] = num;
66 return num;
67 }
68
get_cache(uint16 splid,LmaPsbItem lpi_items[],size_t lpi_max)69 size_t LpiCache::get_cache(uint16 splid, LmaPsbItem lpi_items[],
70 size_t lpi_max) {
71 if (lpi_max > lpi_cache_len_[splid])
72 lpi_max = lpi_cache_len_[splid];
73
74 LmaPsbItem *lpi_cache_this = lpi_cache_ + splid * kMaxLpiCachePerId;
75 for (uint16 pos = 0; pos < lpi_max; pos++) {
76 lpi_items[pos] = lpi_cache_this[pos];
77 }
78 return lpi_max;
79 }
80
81 } // namespace ime_pinyin
82