~ubuntu-branches/ubuntu/raring/sunpinyin/raring

« back to all changes in this revision

Viewing changes to src/lexicon/pytrie.h

  • Committer: Package Import Robot
  • Author(s): YunQiang Su
  • Date: 2012-03-30 15:31:55 UTC
  • mfrom: (1.1.3) (1.2.7 sid)
  • Revision ID: package-import@ubuntu.com-20120330153155-qgls77sogzgtg9zp
Tags: 2.0.3+git20120222-1
* Team upload: git snapshot 20120222.
   - fix breaks if LDFLAGS in environment contains
       multiple words (Closese #646001).
   - rm patches merged to upstream:
       append-os-environ-toenv.patch
       fix-ftbfs-on-sh.patch
       remove-10-candidate-words-limitation.patch
   - refresh disable-lm-dict-compile.patch.
* Bump stardard version to 3.9.3: no modify needed.
* add libsunpinyin3-dbg and python-sunpinyin packages.
* debian/compat to 9, multiarch it.
* rewrite debian/rules with dh 7 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
    friend class CPinyinTrieMaker;
17
17
 
18
18
    struct TTransUnit {
19
 
        TSyllable       m_Syllable;
20
 
        unsigned        m_Offset;
 
19
        TSyllable m_Syllable;
 
20
        unsigned m_Offset;
21
21
    };
22
22
 
23
23
    struct TWordIdInfo {
24
24
    #ifdef WORDS_BIGENDIAN
25
 
        unsigned        m_bSeen    : 1;
26
 
        unsigned        m_cost     : 5;
27
 
        unsigned        m_csLevel  : 2;
28
 
        unsigned        m_id       : WORD_ID_WIDTH;
 
25
        unsigned m_bSeen    : 1;
 
26
        unsigned m_cost     : 5;
 
27
        unsigned m_csLevel  : 2;
 
28
        unsigned m_id       : WORD_ID_WIDTH;
29
29
    #else
30
 
        unsigned        m_id       : WORD_ID_WIDTH;
31
 
        unsigned        m_csLevel  : 2;
32
 
        unsigned        m_cost     : 5;
33
 
        unsigned        m_bSeen    : 1;
 
30
        unsigned m_id       : WORD_ID_WIDTH;
 
31
        unsigned m_csLevel  : 2;
 
32
        unsigned m_cost     : 5;
 
33
        unsigned m_bSeen    : 1;
34
34
    #endif
35
35
 
36
36
        TWordIdInfo() { memset(this, 0, sizeof(TWordIdInfo)); }
37
37
 
38
 
        TWordIdInfo(unsigned id, unsigned len=0, unsigned seen=0, unsigned cost = 0, unsigned cslvl = 0)
 
38
        TWordIdInfo(unsigned id,
 
39
                    unsigned len = 0,
 
40
                    unsigned seen = 0,
 
41
                    unsigned cost = 0,
 
42
                    unsigned cslvl = 0)
39
43
            : m_id(id), m_csLevel(cslvl), m_cost(cost), m_bSeen(seen) { }
40
44
 
41
45
        operator unsigned int() const { return m_id; }
43
47
 
44
48
    struct TNode {
45
49
    #ifdef WORDS_BIGENDIAN
46
 
        unsigned        m_other      : 5;
47
 
        unsigned        m_bFullSyllableTransfer: 1;
48
 
        unsigned        m_csLevel    : 2;
49
 
        unsigned        m_nTransfer  : 12;
50
 
        unsigned        m_nWordId    : 12;
 
50
        unsigned m_other      : 5;
 
51
        unsigned m_bFullSyllableTransfer : 1;
 
52
        unsigned m_csLevel    : 2;
 
53
        unsigned m_nTransfer  : 12;
 
54
        unsigned m_nWordId    : 12;
51
55
    #else
52
 
        unsigned        m_nWordId    : 12;
53
 
        unsigned        m_nTransfer  : 12;
54
 
        unsigned        m_csLevel    : 2;
55
 
        unsigned        m_bFullSyllableTransfer: 1;
56
 
        unsigned        m_other      : 5;
 
56
        unsigned m_nWordId    : 12;
 
57
        unsigned m_nTransfer  : 12;
 
58
        unsigned m_csLevel    : 2;
 
59
        unsigned m_bFullSyllableTransfer : 1;
 
60
        unsigned m_other      : 5;
57
61
    #endif
58
62
 
59
 
        static unsigned int
60
 
        size_for(unsigned int nTransfer, unsigned int nWordId)
61
 
            { return sizeof(TNode) + sizeof(TTransUnit)*nTransfer + 
62
 
                     sizeof(TWordIdInfo)*nWordId; }
63
 
 
64
 
        TNode() 
65
 
            { *((unsigned*)this) = 0; }
66
 
 
67
 
        bool
68
 
        hasPinyinChild(void) const
69
 
            { return (m_nTransfer > 1);}
70
 
 
71
 
        const TTransUnit*
72
 
        getTrans() const
73
 
            { return (TTransUnit*)(this+1); }
74
 
 
75
 
        const TWordIdInfo*
76
 
        getWordIdPtr() const
77
 
            { return (TWordIdInfo*)(((char*)(this+1))+sizeof(TTransUnit)*m_nTransfer); }
78
 
 
79
 
        unsigned int
80
 
        transfer(unsigned s) const 
81
 
            {
82
 
                unsigned int b = 0, e = m_nTransfer;
83
 
                const TTransUnit* ptrans = getTrans();
84
 
                while (b < e) {
85
 
                    int m = b + (e-b)/2;
86
 
                    if (ptrans[m].m_Syllable == s)
87
 
                        return ptrans[m].m_Offset;
88
 
                    if (ptrans[m].m_Syllable < s)
89
 
                        b = m + 1;
90
 
                    else
91
 
                        e = m;
92
 
                }
93
 
                return 0;
 
63
        static unsigned int size_for(unsigned int nTransfer,
 
64
                                     unsigned int nWordId)                    {
 
65
            return sizeof(TNode) + sizeof(TTransUnit) * nTransfer +
 
66
                   sizeof(TWordIdInfo) * nWordId;
 
67
        }
 
68
 
 
69
        TNode()
 
70
        { *((unsigned *) this) = 0; }
 
71
 
 
72
        bool hasPinyinChild(void) const
 
73
        { return(m_nTransfer > 1); }
 
74
 
 
75
        const TTransUnit*getTrans() const
 
76
        { return (TTransUnit *) (this + 1); }
 
77
 
 
78
        const TWordIdInfo*getWordIdPtr() const
 
79
        { return (TWordIdInfo *) (((char *) (this +
 
80
                                             1)) + sizeof(TTransUnit) *
 
81
                                  m_nTransfer); }
 
82
 
 
83
        unsigned int transfer(unsigned s) const {
 
84
            unsigned int b = 0, e = m_nTransfer;
 
85
            const TTransUnit* ptrans = getTrans();
 
86
            while (b < e) {
 
87
                int m = b + (e - b) / 2;
 
88
                if (ptrans[m].m_Syllable == s)
 
89
                    return ptrans[m].m_Offset;
 
90
                if (ptrans[m].m_Syllable < s)
 
91
                    b = m + 1;
 
92
                else
 
93
                    e = m;
94
94
            }
 
95
            return 0;
 
96
        }
95
97
    };
96
98
 
97
99
public:
98
100
    CPinyinTrie() : m_Size(0), m_mem(NULL), m_words(NULL) { }
99
101
 
100
 
    ~CPinyinTrie() 
101
 
        { free(); }
 
102
    ~CPinyinTrie()
 
103
    { free(); }
102
104
 
103
105
    bool
104
106
    load(const char* fileName);
107
109
    free(void);
108
110
 
109
111
    bool
110
 
    isValid(const TNode* pnode, bool allowNonComplete, unsigned csLevel=0);
111
 
 
112
 
    unsigned int
113
 
    getRootOffset() const 
114
 
        { return 3 * sizeof(unsigned int); }
115
 
 
116
 
    const TNode*
117
 
    getRootNode() const 
118
 
        { return (TNode*)(m_mem+getRootOffset()); }
119
 
 
120
 
    const TNode*
121
 
    nodeFromOffset(unsigned int offset) const
122
 
        { return (offset < getRootOffset())?NULL:((TNode*)(m_mem+offset)); }
123
 
 
124
 
    unsigned int
125
 
    getWordCount(void) const 
126
 
        { return *(unsigned int*)m_mem; }
127
 
 
128
 
    unsigned int
129
 
    getNodeCount(void) const 
130
 
        { return *(unsigned int*)(m_mem+sizeof(unsigned int)); }
131
 
 
132
 
    unsigned int
133
 
    getStringOffset(void) const 
134
 
        { return *(unsigned int*)(m_mem+2*sizeof(unsigned int)); }
135
 
 
136
 
    inline const TNode*
137
 
    transfer(const TNode* pnode, unsigned s) const
138
 
        { return nodeFromOffset(pnode->transfer(s)); }
139
 
 
140
 
    inline const TNode*
141
 
    transfer(unsigned s) const
142
 
        { return transfer(getRootNode(), s); }
 
112
    isValid(const TNode* pnode, bool allowNonComplete, unsigned csLevel = 0);
 
113
 
 
114
    unsigned int getRootOffset() const
 
115
    { return 3 * sizeof(unsigned int); }
 
116
 
 
117
    const TNode*getRootNode() const
 
118
    { return (TNode *) (m_mem + getRootOffset()); }
 
119
 
 
120
    const TNode*nodeFromOffset(unsigned int offset) const
 
121
    { return (offset < getRootOffset()) ? NULL : ((TNode *) (m_mem + offset)); }
 
122
 
 
123
    unsigned int getWordCount(void) const
 
124
    { return *(unsigned int *) m_mem; }
 
125
 
 
126
    unsigned int getNodeCount(void) const
 
127
    { return *(unsigned int *) (m_mem + sizeof(unsigned int)); }
 
128
 
 
129
    unsigned int getStringOffset(void) const
 
130
    { return *(unsigned int *) (m_mem + 2 * sizeof(unsigned int)); }
 
131
 
 
132
    inline const TNode*transfer(const TNode* pnode, unsigned s) const
 
133
    { return nodeFromOffset(pnode->transfer(s)); }
 
134
 
 
135
    inline const TNode*transfer(unsigned s) const
 
136
    { return transfer(getRootNode(), s); }
143
137
 
144
138
    unsigned int
145
139
    getSymbolId(const TWCHAR* wstr);
147
141
    unsigned int
148
142
    getSymbolId(const wstring & wstr);
149
143
 
150
 
    const TWCHAR*
151
 
    operator[](unsigned int idx) const 
152
 
        { return m_words[idx]; }
 
144
    const TWCHAR*operator[](unsigned int idx) const
 
145
    { return m_words[idx]; }
153
146
 
154
147
    int
155
148
    lengthAt(unsigned int idx) const;
158
151
    print(FILE *fp) const;
159
152
 
160
153
protected:
161
 
    unsigned int           m_Size;
 
154
    unsigned int m_Size;
162
155
    char                  *m_mem;
163
156
    TWCHAR               **m_words;
164
157