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

Last change on this file was 5267, checked in by nmedfort, 8 months ago

Code clean-up. Removed Pablo Call, SetIthBit? and Prototype.

File size: 2.0 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 <llvm/Support/Casting.h>
12
13namespace re {
14
15class Alt : public Vector {
16public:
17    static inline bool classof(const RE * re) {
18        return re->getClassTypeId() == ClassTypeId::Alt;
19    }
20    static inline bool classof(const void *) {
21        return false;
22    }
23protected:
24    friend Alt * makeAlt();
25    template<typename iterator> friend RE * makeAlt(iterator, iterator);
26    Alt()
27    : Vector(ClassTypeId::Alt) {
28
29    }
30    Alt(iterator begin, iterator end)
31    : Vector(ClassTypeId::Alt, begin, end) {
32
33    }
34private:
35    template<typename iterator>
36    void flatten(iterator begin, iterator end) {
37        for (auto i = begin; i != end; ++i) {
38            if (LLVM_UNLIKELY(llvm::isa<Alt>(*i))) {
39                flatten<Alt::iterator>(llvm::cast<Alt>(*i)->begin(), llvm::cast<Alt>(*i)->end());
40            } else {
41                push_back(*i);
42            }
43        }
44    }
45};
46
47/**
48 * @brief makeAlt
49 *
50 * Build an Alt, flattening alternative subgroups, and combining character classes and
51 * move character classes towards the end of the list to ensure that all combinations are found.
52 *
53 * @param list
54 * @return simplified RE representing the Alt
55 */
56
57inline Alt * makeAlt() {
58    return new Alt();
59}
60
61template<typename iterator>
62RE * makeAlt(iterator begin, iterator end) {
63    if (LLVM_UNLIKELY(std::distance(begin, end) == 0)) {
64        throw std::runtime_error("Alt objects cannot be empty!");
65    } else if (std::distance(begin, end) == 1) {
66        return *begin;
67    } else {
68        Alt * alt = makeAlt();
69        alt->flatten(begin, end);
70        if (alt->size() == 1) {
71            return alt->front();
72        }
73        return alt;
74    }
75}
76
77inline RE * makeAlt(RE::InitializerList list) {
78    return makeAlt(list.begin(), list.end());
79}
80
81}
82
83#endif // ALT_H
84
Note: See TracBrowser for help on using the repository browser.