|
1 /* GRAPHITE2 LICENSING |
|
2 |
|
3 Copyright 2010, SIL International |
|
4 All rights reserved. |
|
5 |
|
6 This library is free software; you can redistribute it and/or modify |
|
7 it under the terms of the GNU Lesser General Public License as published |
|
8 by the Free Software Foundation; either version 2.1 of License, or |
|
9 (at your option) any later version. |
|
10 |
|
11 This program is distributed in the hope that it will be useful, |
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 Lesser General Public License for more details. |
|
15 |
|
16 You should also have received a copy of the GNU Lesser General Public |
|
17 License along with this library in the file named "LICENSE". |
|
18 If not, write to the Free Software Foundation, 51 Franklin Street, |
|
19 Suite 500, Boston, MA 02110-1335, USA or visit their web page on the |
|
20 internet at http://www.fsf.org/licenses/lgpl.html. |
|
21 |
|
22 Alternatively, the contents of this file may be used under the terms of the |
|
23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public |
|
24 License, as published by the Free Software Foundation, either version 2 |
|
25 of the License or (at your option) any later version. |
|
26 */ |
|
27 #pragma once |
|
28 |
|
29 #ifndef GRAPHITE2_NSEGCACHE |
|
30 |
|
31 #include "inc/Main.h" |
|
32 #include "inc/Slot.h" |
|
33 |
|
34 namespace graphite2 { |
|
35 |
|
36 class Segment; |
|
37 class Slot; |
|
38 class SegCacheEntry; |
|
39 class SegCachePrefixEntry; |
|
40 |
|
41 enum SegCacheParameters { |
|
42 /** number of characters used in initial prefix tree */ |
|
43 ePrefixLength = 2, |
|
44 /** Segments more recent than maxSegmentCount() / eAgeFactor are kept */ |
|
45 eAgeFactor = 4, |
|
46 /** Segments are purged according to the formular: |
|
47 * accessCount < (totalAccesses)/(ePurgeFactor * maxSegments) */ |
|
48 ePurgeFactor = 5, |
|
49 /** Maximum number of Segments to store which have the same |
|
50 * prefix. Needed to prevent unique identifiers flooding the cache */ |
|
51 eMaxSuffixCount = 15 |
|
52 |
|
53 }; |
|
54 |
|
55 class SegCacheCharInfo |
|
56 { |
|
57 public: |
|
58 uint16 m_unicode; |
|
59 uint16 m_before; |
|
60 uint16 m_after; |
|
61 }; |
|
62 |
|
63 /** |
|
64 * SegCacheEntry stores the result of running the engine for specific unicode |
|
65 * code points in the typical mid-line situation. |
|
66 */ |
|
67 class SegCacheEntry |
|
68 { |
|
69 // Prevent any implict copying; |
|
70 SegCacheEntry(const SegCacheEntry &); |
|
71 SegCacheEntry & operator = (const SegCacheEntry &); |
|
72 |
|
73 friend class SegCachePrefixEntry; |
|
74 public: |
|
75 SegCacheEntry() : |
|
76 m_glyphLength(0), m_unicode(NULL), m_glyph(NULL), m_attr(NULL), m_justs(0), |
|
77 m_accessCount(0), m_lastAccess(0) |
|
78 {} |
|
79 SegCacheEntry(const uint16 * cmapGlyphs, size_t length, Segment * seg, size_t charOffset, long long cacheTime); |
|
80 ~SegCacheEntry() { clear(); }; |
|
81 void clear(); |
|
82 size_t glyphLength() const { return m_glyphLength; } |
|
83 const Slot * first() const { return m_glyph; } |
|
84 const Slot * last() const { return m_glyph + (m_glyphLength - 1); } |
|
85 |
|
86 /** Total number of times this entry has been accessed since creation */ |
|
87 unsigned long long accessCount() const { return m_accessCount; } |
|
88 /** "time" of last access where "time" is measured in accesses to the cache owning this entry */ |
|
89 void accessed(unsigned long long cacheTime) const |
|
90 { |
|
91 m_lastAccess = cacheTime; ++m_accessCount; |
|
92 }; |
|
93 |
|
94 int compareRank(const SegCacheEntry & entry) const |
|
95 { |
|
96 if (m_accessCount > entry.m_accessCount) return 1; |
|
97 else if (m_accessCount < entry.m_accessCount) return 1; |
|
98 else if (m_lastAccess > entry.m_lastAccess) return 1; |
|
99 else if (m_lastAccess < entry.m_lastAccess) return -1; |
|
100 return 0; |
|
101 } |
|
102 unsigned long long lastAccess() const { return m_lastAccess; }; |
|
103 |
|
104 CLASS_NEW_DELETE; |
|
105 private: |
|
106 |
|
107 size_t m_glyphLength; |
|
108 /** glyph ids resulting from cmap mapping from unicode to glyph before substitution |
|
109 * the length of this array is determined by the position in the SegCachePrefixEntry */ |
|
110 uint16 * m_unicode; |
|
111 /** slots after shapping and positioning */ |
|
112 Slot * m_glyph; |
|
113 int16 * m_attr; |
|
114 byte * m_justs; |
|
115 mutable unsigned long long m_accessCount; |
|
116 mutable unsigned long long m_lastAccess; |
|
117 }; |
|
118 |
|
119 } // namespace graphite2 |
|
120 |
|
121 #endif |