source: icGREP/icgrep-devel/icgrep/cc/multiplex_CCs.cpp

Last change on this file was 6184, checked in by nmedfort, 6 months ago

Initial version of PipelineKernel? + revised StreamSet? model.

File size: 6.5 KB
Line 
1/*
2 *  Copyright (c) 2017 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6#include <map>
7#include <UCD/unicode_set.h>
8#include <re/re_cc.h>
9#include "boost/dynamic_bitset.hpp"
10#include <cc/multiplex_CCs.h>
11#include <re/printer_re.h>
12#include <llvm/Support/Casting.h>
13#include <llvm/Support/ErrorHandling.h>
14#include <llvm/Support/raw_ostream.h>
15
16namespace cc {
17
18//
19// Breakpoints of a set of character classes (CCs): each codepoint c such that
20// there is some CC in CCs such that either (a) c is in the CC and c-1 is not, or
21// (b) c-1 is in the CC and c is not.  Boundary cases: if codepoint 0 is in
22// some CC, then 0 is a breakpoint (codepoint -1 is not in any CC).  If codepoint
23// 0x10FFFF is in some CC then 0x110000 is a breakpoint.
24//
25// The breakpoints may be determined by iterating through the interval
26// representation of each CC.   For each interval (lo, hi), lo and hi+1
27// are breakpoints.
28//
29// For each breakpoint, a bitset is computed identifying the source CCs whose
30// status changes at the breakpoint.
31//
32
33using bitset = boost::dynamic_bitset<>;
34
35std::map<UCD::codepoint_t, bitset> computeBreakpoints(const std::vector<re::CC *> & CCs) {
36    std::map<UCD::codepoint_t, bitset> breakpoints;
37    for (unsigned i = 0; i < CCs.size(); i++) {
38        for (const auto range : *CCs[i]) {
39            const auto lo = re::lo_codepoint(range);
40            const auto hi = re::hi_codepoint(range);
41            auto f = breakpoints.find(lo);
42            if (f == breakpoints.end()) {
43                breakpoints.emplace(lo, bitset(CCs.size()));
44            }
45            breakpoints[lo].set(i);
46            f = breakpoints.find(hi + 1);
47            if (f == breakpoints.end()) {
48                breakpoints.emplace(hi+1, bitset(CCs.size()));
49            }
50            breakpoints[hi+1].set(i);
51        }
52    }
53    return breakpoints;
54}
55
56void doMultiplexCCs(const std::vector<re::CC *> & CCs,
57                    std::vector<std::vector<unsigned>> & exclusiveSetIDs,
58                    std::vector<re::CC *> & multiplexedCCs) {
59   
60    const auto breakpoints = computeBreakpoints(CCs);
61    // Initialize the exclusiveSetIDs to have one empty vector per source CC.
62    exclusiveSetIDs.clear();
63    exclusiveSetIDs.resize(CCs.size());
64    //
65    // Exclusive set determination.   
66    //
67    // Set up a map from the set of source CCs for each exclusive set to the exclusive set index.
68
69    std::map<bitset, unsigned> CC_set_to_exclusive_set_map;
70
71    // Entry 0 is for the characters not in any of the CCs.
72    CC_set_to_exclusive_set_map.emplace(bitset(CCs.size()), 0);
73
74    unsigned current_exclusive_set_idx = 0;
75    unsigned multiplexed_bit_count = 0;
76    bitset current_set(CCs.size());
77   
78    unsigned range_lo = 0;
79    unsigned next_set_index = 1;
80    for (auto & bkpt_entry : breakpoints) {
81        if (current_exclusive_set_idx > 0) {  // We have a range entry to close for a pending exclusive set.
82            unsigned range_hi = bkpt_entry.first - 1;
83            for (unsigned bit = 0; bit < multiplexed_bit_count; bit++) {
84                if (((current_exclusive_set_idx >> bit) & 1) == 1) {
85                    multiplexedCCs[bit]->insert_range(range_lo, range_hi);
86                }
87            }
88        }
89        // Start a new range.
90        range_lo = bkpt_entry.first;
91        if (range_lo > UCD::UNICODE_MAX) continue; // Nothing to do for bkpt 0x110000
92        current_set ^= bkpt_entry.second;
93        auto idx_iter = CC_set_to_exclusive_set_map.find(current_set);
94        if (idx_iter == CC_set_to_exclusive_set_map.end()) {
95            // New exclusive class; assign the next sequential integer.
96            //current_exclusive_set_idx = exclusiveSetIDs.size();
97            current_exclusive_set_idx = next_set_index;
98            next_set_index++;
99            CC_set_to_exclusive_set_map.emplace(current_set, current_exclusive_set_idx);
100           
101            for (unsigned CC1 = current_set.find_first(); CC1 < CCs.size(); CC1 = current_set.find_next(CC1)) {
102                exclusiveSetIDs[CC1].push_back(current_exclusive_set_idx);
103            }
104            if ((current_exclusive_set_idx & (current_exclusive_set_idx - 1)) == 0) {
105                multiplexed_bit_count++;
106                multiplexedCCs.push_back(re::makeCC());
107            }
108        }
109        else {
110            current_exclusive_set_idx = idx_iter->second;
111        }
112    }
113}
114
115
116
117MultiplexedAlphabet::MultiplexedAlphabet(const std::string alphabetName, const std::vector<re::CC *> CCs)
118: Alphabet(std::move(alphabetName), ClassTypeId::MultiplexedAlphabet)
119, mUnicodeSets(std::move(CCs)) {
120    if (!mUnicodeSets.empty()) {
121        mSourceAlphabet = mUnicodeSets[0]->getAlphabet();
122        for (unsigned i = 1; i < CCs.size(); i++) {
123            if (CCs[i]->getAlphabet() != mSourceAlphabet) {
124                llvm::report_fatal_error("Mismatched source alphabets for Multiplexed Alphabet");
125            }
126        }
127    }
128    doMultiplexCCs(CCs, mExclusiveSetIDs, mMultiplexedCCs);
129}
130
131unsigned long MultiplexedAlphabet::findTargetCCIndex(const re::CC * sourceCC) const {
132    auto f = find(mUnicodeSets.begin(), mUnicodeSets.end(), sourceCC);
133    if (f != mUnicodeSets.end()) {
134        return f - mUnicodeSets.begin();
135    }
136
137    const UCD::UnicodeSet* sourceUcd = sourceCC;
138    for (unsigned long i = 0; i < mUnicodeSets.size(); i++) {
139        UCD::UnicodeSet* t = mUnicodeSets[i];
140        if (*t == *sourceUcd) {
141            return i;
142        }
143    }
144    llvm::report_fatal_error(Printer_RE::PrintRE(sourceCC) + " not found");
145}
146
147re::CC * MultiplexedAlphabet::transformCC(const re::CC * sourceCC) const {
148    if (sourceCC->getAlphabet() != mSourceAlphabet) {
149        llvm::report_fatal_error("Mismatched source alphabets for transformCC");
150    }
151
152    const auto index = findTargetCCIndex(sourceCC);
153    const auto exclusive_IDs = mExclusiveSetIDs[index];
154    re::CC * CC_union = re::makeCC(this);
155    for (auto i : exclusive_IDs) {
156        CC_union = re::makeCC(CC_union, re::makeCC(i, this));
157    }
158    CC_union->sourceCC = sourceCC;
159    return CC_union;
160}
161
162re::CC * MultiplexedAlphabet::invertCC(const re::CC * transformedCC) const {
163    if (transformedCC->getAlphabet() != this) llvm::report_fatal_error("invertCC applied to non-transformed CC");
164    re::CC * CC_union = re::makeCC(mSourceAlphabet);
165    for (const UCD::interval_t i : *transformedCC) {
166        for (unsigned cp = re::lo_codepoint(i); cp <= re::hi_codepoint(i); cp++) {
167            CC_union = re::makeCC(mUnicodeSets[cp], CC_union);
168        }
169    }
170    return CC_union;
171}
172   
173
174   
175}
176
Note: See TracBrowser for help on using the repository browser.