aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/hyperscan/src/nfagraph/ng_split.cpp
blob: 91a099fc38abd2f0b39b58bfe937fcaa52b5b2b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright (c) 2015-2017, Intel Corporation
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/** \file
 * \brief Functions for splitting NFAGraphs into LHS and RHS.
 */
#include "ng_split.h"

#include "ng_holder.h"
#include "ng_prune.h"
#include "ng_util.h"
#include "util/container.h"
#include "util/graph.h"
#include "util/graph_range.h"

#include <map>
#include <set>
#include <vector>

using namespace std;

namespace ue2 {

static
void clearAccepts(NGHolder &g) {
    for (auto v : inv_adjacent_vertices_range(g.accept, g)) {
        g[v].reports.clear();
    }

    for (auto v : inv_adjacent_vertices_range(g.acceptEod, g)) {
        g[v].reports.clear();
    }

    clear_in_edges(g.accept, g);
    clear_in_edges(g.acceptEod, g);
    add_edge(g.accept, g.acceptEod, g);
}

static
void filterSplitMap(const NGHolder &g,
                    unordered_map<NFAVertex, NFAVertex> *out_map) {
    unordered_set<NFAVertex> verts;
    insert(&verts, vertices(g));
    auto it = out_map->begin();
    while (it != out_map->end()) {
        auto jt = it;
        ++it;
        if (!contains(verts, jt->second)) {
            out_map->erase(jt);
        }
    }
}

static
void splitLHS(const NGHolder &base, const vector<NFAVertex> &pivots,
              const vector<NFAVertex> &rhs_pivots, NGHolder *lhs,
              unordered_map<NFAVertex, NFAVertex> *lhs_map) {
    assert(lhs && lhs_map);

    cloneHolder(*lhs, base, lhs_map);

    clearAccepts(*lhs);

    for (auto pivot : pivots) {
        DEBUG_PRINTF("pivot is %zu lv %zu lm %zu\n", base[pivot].index,
                     num_vertices(*lhs), lhs_map->size());
        assert(contains(*lhs_map, pivot));

        for (auto v : rhs_pivots) {
            assert(contains(*lhs_map, v));
            remove_edge((*lhs_map)[pivot], (*lhs_map)[v], *lhs);
        }

        (*lhs)[(*lhs_map)[pivot]].reports.insert(0);
        add_edge((*lhs_map)[pivot], lhs->accept, *lhs);
    }

    /* should do the renumbering unconditionally as we know edges are already
     * misnumbered */
    pruneUseless(*lhs, false);
    renumber_edges(*lhs);
    renumber_vertices(*lhs);

    filterSplitMap(*lhs, lhs_map);

    switch (base.kind) {
    case NFA_PREFIX:
    case NFA_OUTFIX:
        lhs->kind = NFA_PREFIX;
        break;
    case NFA_INFIX:
    case NFA_SUFFIX:
        lhs->kind = NFA_INFIX;
        break;
    case NFA_EAGER_PREFIX:
        /* Current code should not be assigning eager until well after all the
         * splitting is done. */
        assert(0);
        lhs->kind = NFA_EAGER_PREFIX;
        break;
    case NFA_REV_PREFIX:
    case NFA_OUTFIX_RAW:
        assert(0);
        break;
    }
}

void splitLHS(const NGHolder &base, NFAVertex pivot,
              NGHolder *lhs, unordered_map<NFAVertex, NFAVertex> *lhs_map) {
    vector<NFAVertex> pivots(1, pivot);
    vector<NFAVertex> rhs_pivots;
    insert(&rhs_pivots, rhs_pivots.end(), adjacent_vertices(pivot, base));
    splitLHS(base, pivots, rhs_pivots, lhs, lhs_map);
}

void splitRHS(const NGHolder &base, const vector<NFAVertex> &pivots,
              NGHolder *rhs, unordered_map<NFAVertex, NFAVertex> *rhs_map) {
    assert(rhs && rhs_map);

    cloneHolder(*rhs, base, rhs_map);

    clear_out_edges(rhs->start, *rhs);
    clear_out_edges(rhs->startDs, *rhs);
    add_edge(rhs->start, rhs->startDs, *rhs);
    add_edge(rhs->startDs, rhs->startDs, *rhs);

    for (auto pivot : pivots) {
        assert(contains(*rhs_map, pivot));
        NFAEdge e = add_edge(rhs->start, (*rhs_map)[pivot], *rhs);
        (*rhs)[e].tops.insert(DEFAULT_TOP);
    }

     /* should do the renumbering unconditionally as we know edges are already
      * misnumbered */
    pruneUseless(*rhs, false);
    renumber_edges(*rhs);
    renumber_vertices(*rhs);
    filterSplitMap(*rhs, rhs_map);

    switch (base.kind) {
    case NFA_PREFIX:
    case NFA_INFIX:
        rhs->kind = NFA_INFIX;
        break;
    case NFA_SUFFIX:
    case NFA_OUTFIX:
        rhs->kind = NFA_SUFFIX;
        break;
    case NFA_EAGER_PREFIX:
        /* Current code should not be assigning eager until well after all the
         * splitting is done. */
        assert(0);
        rhs->kind = NFA_INFIX;
        break;
    case NFA_REV_PREFIX:
    case NFA_OUTFIX_RAW:
        assert(0);
        break;
    }
}

/** \brief Fills \a succ with the common successors of the vertices in \a
 * pivots. */
static
void findCommonSuccessors(const NGHolder &g, const vector<NFAVertex> &pivots,
                          vector<NFAVertex> &succ) {
    assert(!pivots.empty());

    set<NFAVertex> adj;
    set<NFAVertex> adj_temp;

    insert(&adj, adjacent_vertices(pivots.at(0), g));

    for (auto it = pivots.begin() + 1, ite = pivots.end(); it != ite; ++it) {
        NFAVertex pivot = *it;
        adj_temp.clear();
        for (auto v : adjacent_vertices_range(pivot, g)) {
            if (contains(adj, v)) {
                adj_temp.insert(v);
            }
        }
        adj.swap(adj_temp);
    }

    succ.insert(succ.end(), adj.begin(), adj.end());
}

void splitGraph(const NGHolder &base, const vector<NFAVertex> &pivots,
                NGHolder *lhs, unordered_map<NFAVertex, NFAVertex> *lhs_map,
                NGHolder *rhs, unordered_map<NFAVertex, NFAVertex> *rhs_map) {
    DEBUG_PRINTF("splitting graph at %zu vertices\n", pivots.size());

    assert(!has_parallel_edge(base));
    assert(isCorrectlyTopped(base));

    /* RHS pivots are built from the common set of successors of pivots. */
    vector<NFAVertex> rhs_pivots;
    findCommonSuccessors(base, pivots, rhs_pivots);

    /* generate lhs */
    splitLHS(base, pivots, rhs_pivots, lhs, lhs_map);

    /* generate the rhs */
    splitRHS(base, rhs_pivots, rhs, rhs_map);

    assert(!has_parallel_edge(*lhs));
    assert(!has_parallel_edge(*rhs));
    assert(isCorrectlyTopped(*lhs));
    assert(isCorrectlyTopped(*rhs));
}

void splitGraph(const NGHolder &base, NFAVertex pivot,
                NGHolder *lhs, unordered_map<NFAVertex, NFAVertex> *lhs_map,
                NGHolder *rhs, unordered_map<NFAVertex, NFAVertex> *rhs_map) {
    vector<NFAVertex> pivots(1, pivot);
    splitGraph(base, pivots, lhs, lhs_map, rhs, rhs_map);
}

} // namespace ue2