Blame view

morfeusz/segrules/segrules.cpp 3.1 KB
Michał Lenart authored
1
2
3

#include "segrules.hpp"
#include "../fsa/fsa.hpp"
Michał Lenart authored
4
#include "../fsa/const.hpp"
Michał Lenart authored
5
#include "../deserializationUtils.hpp"
Michał Lenart authored
6
Michał Lenart authored
7
8
using namespace std;
Michał Lenart authored
9
static inline void skipSeparatorsList(const unsigned char*& ptr) {
Michał Lenart authored
10
    uint16_t listSize = readInt16(ptr);
Michał Lenart authored
11
12
13
    ptr += 4 * listSize;
}
Michał Lenart authored
14
static inline const unsigned char* getSeparatorsListPtr(const unsigned char* ptr) {
Michał Lenart authored
15
16
    const unsigned char* additionalDataPtr = ptr 
        + FSA_DATA_OFFSET 
Michał Lenart authored
17
18
        + readInt32Const(ptr + FSA_DATA_SIZE_OFFSET);
    const unsigned char* res = additionalDataPtr + readInt32Const(additionalDataPtr) + 4;
Michał Lenart authored
19
20
21
22
23
24
    return res;
}

static inline const unsigned char* getFSAsMapPtr(const unsigned char* ptr) {
    const unsigned char* res = getSeparatorsListPtr(ptr);
    skipSeparatorsList(res);
Michał Lenart authored
25
    return res;
Michał Lenart authored
26
27
28
29
}

static inline SegrulesOptions deserializeOptions(const unsigned char*& ptr) {
    SegrulesOptions res;
Michał Lenart authored
30
31
32
    unsigned char optsNum = *ptr;
    ptr++;
    for (unsigned char i = 0; i < optsNum; i++) {
Michał Lenart authored
33
34
        string key = readString(ptr);
        res[key] = readString(ptr);
Michał Lenart authored
35
    }
Michał Lenart authored
36
37
38
    return res;
}
Michał Lenart authored
39
static inline SegrulesFSA* deserializeFSA(const unsigned char*& ptr) {
Michał Lenart authored
40
    uint32_t fsaSize = readInt32(ptr);
Michał Lenart authored
41
42
//    static SegrulesDeserializer deserializer;
    SegrulesFSA* res = new SegrulesFSA(ptr);
Michał Lenart authored
43
44
45
46
    ptr += fsaSize;
    return res;
}
Michał Lenart authored
47
48
map<SegrulesOptions, SegrulesFSA*> createSegrulesFSAsMap(const unsigned char* analyzerPtr) {
    map<SegrulesOptions, SegrulesFSA*> res;
Michał Lenart authored
49
50
51
52
53
54
    const unsigned char* fsasMapPtr = getFSAsMapPtr(analyzerPtr);
    const unsigned char* currPtr = fsasMapPtr;
    unsigned char fsasNum = *currPtr;
    currPtr++;
    for (unsigned char i = 0; i < fsasNum; i++) {
        SegrulesOptions options = deserializeOptions(currPtr);
Michał Lenart authored
55
        SegrulesFSA* fsa = deserializeFSA(currPtr);
Michał Lenart authored
56
57
58
        res[options] = fsa;
    }
    return res;
Michał Lenart authored
59
}
Michał Lenart authored
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

SegrulesOptions getDefaultSegrulesOptions(const unsigned char* ptr) {
    const unsigned char* fsasMapPtr = getFSAsMapPtr(ptr);
    const unsigned char* currPtr = fsasMapPtr;
    unsigned char fsasNum = *currPtr;
    currPtr++;
    for (unsigned char i = 0; i < fsasNum; i++) {
        deserializeOptions(currPtr);
        deserializeFSA(currPtr);
    }
    return deserializeOptions(currPtr);
}

SegrulesFSA* getDefaultSegrulesFSA(
        const map<SegrulesOptions, SegrulesFSA*>& map, 
        const unsigned char* ptr) {
    SegrulesOptions opts = getDefaultSegrulesOptions(ptr);
    return (*(map.find(opts))).second;
}
Michał Lenart authored
80
81
82
vector<uint32_t> getSeparatorsList(const unsigned char* ptr) {
    ptr = getSeparatorsListPtr(ptr);
    vector<uint32_t> res;
Michał Lenart authored
83
    uint16_t listSize = readInt16(ptr);
Michał Lenart authored
84
    for (unsigned int i = 0; i < listSize; i++) {
Michał Lenart authored
85
        res.push_back(readInt32(ptr));
Michał Lenart authored
86
87
88
89
    }
    return res;
}
Michał Lenart authored
90
91
92
93
94
95
96
97
98
99
100
101
void debugMap(const map<SegrulesOptions, SegrulesFSA*>& res) {
    map<SegrulesOptions, SegrulesFSA*>::const_iterator it = res.begin();
    while (it != res.end()) {
        SegrulesOptions::const_iterator it1 = it->first.begin();
        while (it1 != it->first.end()) {
            cerr << it1->first << " --> " << it1->second << endl;
            it1++;
        }
        cerr << it->second << endl;
        it++;
    }
}