source: icGREP/icgrep-devel/icgrep/re/re_alt.h @ 4405

Last change on this file since 4405 was 4272, checked in by nmedfort, 5 years ago

Implemented slab allocator based on the original Parabix StringPool?; intergrated it with RE and Pablo AST nodes.

File size: 2.2 KB
Line 
1/*
2 *  Copyright (c) 2014 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#ifndef ALT_H
8#define ALT_H
9
10#include "re_re.h"
11#include "re_cc.h"
12#include <queue>
13#include <iostream>
14
15namespace re {
16
17class Alt : public Vector {
18public:
19    static inline bool classof(const RE * re) {
20        return re->getClassTypeId() == ClassTypeId::Alt;
21    }
22    static inline bool classof(const void *) {
23        return false;
24    }
25protected:
26    friend Alt * makeAlt();
27    template<typename iterator> friend RE * makeAlt(iterator, iterator);
28    Alt()
29    : Vector(ClassTypeId::Alt) {
30
31    }
32    Alt(iterator begin, iterator end)
33    : Vector(ClassTypeId::Alt, begin, end) {
34
35    }
36    void* operator new (std::size_t size) noexcept {
37        return mAllocator.allocate(size);
38    }
39private:
40    template<typename iterator>
41    void flatten(iterator begin, iterator end, std::queue<CC*> & ccQ) {
42        for (auto i = begin; i != end; ++i) {
43            if (Alt * alt = dyn_cast<Alt>(*i)) {
44                flatten(alt->begin(), alt->end(), ccQ);
45                continue;
46            }
47            else if (CC * cc = dyn_cast<CC>(*i)) {
48                ccQ.push(cc);
49                continue;
50            }
51            push_back(*i);
52        }
53    }
54};
55
56/**
57 * @brief makeAlt
58 *
59 * Build an Alt, flattening alternative subgroups, and combining character classes and
60 * move character classes towards the end of the list to ensure that all combinations are found.
61 *
62 * @param list
63 * @return simplified RE representing the Alt
64 */
65
66inline Alt * makeAlt() {
67    return new Alt();
68}
69
70template<typename iterator>
71RE * makeAlt(iterator begin, iterator end) {
72    Alt * alt = makeAlt();
73    std::queue<CC*> ccQ;
74    alt->flatten(begin, end, ccQ);
75    if (!ccQ.empty()) {
76        while (ccQ.size() > 1) {
77            CC * a = ccQ.front(); ccQ.pop();
78            CC * b = ccQ.front(); ccQ.pop();
79            ccQ.push(makeCC(a, b));
80        }
81        alt->push_back(ccQ.front());
82    }
83    if (alt->size() == 1) {
84        return alt->back();
85    }
86    return alt;
87}
88
89inline RE * makeAlt(RE::InitializerList list) {
90    return makeAlt(list.begin(), list.end());
91}
92
93}
94
95#endif // ALT_H
96
Note: See TracBrowser for help on using the repository browser.