source: icGREP/icgrep-devel/icgrep/re/re_seq.h @ 6237

Last change on this file since 6237 was 6223, checked in by cameron, 10 months ago

Contextual assertion simplifier from Jeremy Schwartz - initial check-in

File size: 2.1 KB
RevLine 
[3850]1/*
[6223]2 *  Copyright (c) 2018 International Characters.
[3850]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
[4194]7#ifndef RE_SEQ_H
8#define RE_SEQ_H
[3850]9
[6138]10#include <string>
[6195]11#include <util/slab_allocator.h>
12#include <vector>
[6138]13#include <re/re_cc.h>
14#include <re/re_re.h>
[6223]15#include <re/re_empty_set.h>
[6138]16#include <UCD/unicode_set.h>
[5267]17#include <llvm/Support/Casting.h>
[3850]18
[4194]19namespace re {
20
[6195]21class Seq : public RE, public std::vector<RE*, ProxyAllocator<RE *>> {
[3850]22public:
[4194]23    static inline bool classof(const RE * re) {
24        return re->getClassTypeId() == ClassTypeId::Seq;
25    }
26    static inline bool classof(const void *) {
27        return false;
28    }
29protected:
[4249]30    friend Seq * makeSeq();
[5835]31    template<typename iterator> friend RE * makeSeq(const iterator, const iterator);
[6195]32    Seq() : RE(ClassTypeId::Seq), std::vector<RE*, ProxyAllocator<RE *>>(mAllocator) {}
[4249]33    Seq(iterator begin, iterator end)
[6195]34    : RE(ClassTypeId::Seq), std::vector<RE*, ProxyAllocator<RE *>>(begin, end, mAllocator) { }
[3850]35};
36
[4249]37inline Seq * makeSeq() {
38    return new Seq();
[4194]39}
40
[5835]41template<typename iterator>
42inline RE * makeSeq(const iterator begin, const iterator end) {
43    Seq * seq = makeSeq();
[4203]44    for (auto i = begin; i != end; ++i) {
[5835]45        RE * const item = *i;
[6223]46        if (LLVM_UNLIKELY(isEmptySet(item))) {
47            return makeEmptySet();
48        } else if (LLVM_UNLIKELY(llvm::isa<Seq>(item))) {
[5835]49            for (RE * const innerItem : *llvm::cast<Seq>(item)) {
50                seq->push_back(innerItem);
51            }
[4852]52        } else {
[5835]53            seq->push_back(item);
[4203]54        }
55    }
[5835]56    if (seq->size() == 1) {
57        return seq->front();
[4203]58    }
[5835]59    return seq;
[4194]60}
61
[6223]62inline RE * makeSeq(std::initializer_list<RE *> list) {
[4249]63    return makeSeq(list.begin(), list.end());
[4203]64}
65
[5810]66inline bool isEmptySeq(RE * s) {
67    return llvm::isa<Seq>(s) && llvm::cast<Seq>(s)->empty();
[4203]68}
[6138]69   
70inline RE * u32string2re(std::u32string s) {
71    std::vector<RE *> ccs;
72    for (auto c : s) {
73        ccs.push_back(makeCC(UCD::UnicodeSet(c)));
74    }
75    return makeSeq(ccs.begin(), ccs.end());
[5810]76}
[6138]77   
78}
[4203]79
[5197]80#endif // RE_SEQ_H
[3850]81
82
83
84
Note: See TracBrowser for help on using the repository browser.