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

Last change on this file since 5800 was 5800, checked in by cameron, 16 months ago

isa and dyn_cast for Alphabets

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