1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
// Copyright (c) 1994 James Clark
// See the file COPYING for copying permission.
#ifdef __GNUG__
#pragma implementation
#endif
#include "splib.h"
#include "Partition.h"
#include "ISet.h"
#include "ISetIter.h"
#include "SubstTable.h"
#include "Link.h"
#include "IList.h"
#include "IListIter.h"
#include "Owner.h"
#include "macros.h"
#include "EquivClass.h"
#include "constant.h"
#include "StringC.h"
#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif
static void refineByChar(IList<EquivClass> *, Char);
static void refineBySet(IList<EquivClass> *, const ISet<Char> &, unsigned);
#if _MSC_VER == 900
// Work around MSVC 2.0 bug.
typedef SubstTable _msvc_dummy;
#endif
Partition::Partition(const ISet<Char> &chars,
const ISet<Char> **sets,
int nSets,
const SubstTable &subst)
: map_(0) // eE gets code 0
{
IList<EquivClass> classes;
classes.insert(new EquivClass);
classes.head()->set.addRange(0, charMax);
{
ISetIter<Char> iter(chars);
Char min, max;
while (iter.next(min, max)) {
do {
refineByChar(&classes, subst[min]);
} while (min++ != max);
}
}
int i;
for (i = 0; i < nSets; i++)
refineBySet(&classes, *sets[i], (1 << i));
maxCode_ = 0;
setCodes_.resize(nSets);
for (IListIter<EquivClass> listIter(classes);
!listIter.done();
listIter.next()) {
++maxCode_;
ASSERT(maxCode_ != 0);
EquivClass *p = listIter.cur();
for (i = 0; i < nSets; i++)
if ((1 << i) & p->inSets)
setCodes_[i] += maxCode_;
ISetIter<Char> setIter(p->set);
Char min, max;
while (setIter.next(min, max))
map_.setRange(min, max, maxCode_);
}
{
ISetIter<Char> iter(chars);
Char min, max;
while (iter.next(min, max)) {
do {
StringC str(subst.inverse(min));
EquivCode code = map_[min];
for (size_t i = 0; i < str.size(); i++)
map_.setChar(str[i], code);
} while (min++ != max);
}
}
}
static
void refineByChar(IList<EquivClass> *classes, Char c)
{
// Avoid modifying *classes, while there's an active iter on it.
EquivClass *found = 0;
{
for (IListIter<EquivClass> iter(*classes); !iter.done(); iter.next()) {
if (iter.cur()->set.contains(c)) {
found = iter.cur();
break;
}
}
}
if (found && !found->set.isSingleton()) {
found->set.remove(c);
classes->insert(new EquivClass(found->inSets));
classes->head()->set.add(c);
}
}
static
void addUpTo(ISet<Char> *to, Char limit, const ISet<Char> &from)
{
ISetIter<Char> iter(from);
Char min, max;
while (iter.next(min, max) && min < limit)
to->addRange(min, max >= limit ? limit - 1 : max);
}
enum RefineResult { allIn, allOut, someInSomeOut };
static
RefineResult refine(const ISet<Char> &set, const ISet<Char> &refiner,
ISet<Char> *inp, ISet<Char> *outp)
{
Char setMin, setMax, refMin, refMax;
ISetIter<Char> refIter(refiner);
ISetIter<Char> setIter(set);
Boolean oneIn = 0;
Boolean oneOut = 0;
if (!refIter.next(refMin, refMax))
return allOut;
while (setIter.next(setMin, setMax)) {
while (setMin <= setMax) {
while (refMax < setMin && refIter.next(refMin, refMax))
;
if (refMax < setMin || setMin < refMin) {
if (!oneOut) {
if (oneIn)
addUpTo(inp, setMin, set);
oneOut = 1;
}
if (refMax < setMin || refMin > setMax) {
if (oneIn)
outp->addRange(setMin, setMax);
break;
}
else {
if (oneIn)
outp->addRange(setMin, refMin - 1);
setMin = refMin;
}
}
else {
if (!oneIn) {
if (oneOut)
addUpTo(outp, setMin, set);
oneIn = 1;
}
if (setMax <= refMax) {
if (oneOut)
inp->addRange(setMin, setMax);
break;
}
else {
// refMax < setMax
if (oneOut)
inp->addRange(setMin, refMax);
// avoid wrapping round
if (refMax == charMax)
break;
setMin = refMax + 1;
}
}
}
}
if (oneIn)
return oneOut ? someInSomeOut : allIn;
else
return allOut;
}
static
void refineBySet(IList<EquivClass> *classes, const ISet<Char> &set,
unsigned setFlag)
{
Owner<EquivClass> in(new EquivClass), out(new EquivClass);
IList<EquivClass> newClasses;
for (;;) {
EquivClass *p = classes->head();
if (!p)
break;
if (!out)
out = new EquivClass;
switch (refine(p->set, set, &in->set, &out->set)) {
case someInSomeOut:
in->inSets = p->inSets | setFlag;
newClasses.insert(in.extract());
out->inSets = p->inSets;
newClasses.insert(out.extract());
in = classes->get();
in->set.clear();
in->inSets = 0;
break;
case allIn:
p->inSets |= setFlag;
newClasses.insert(classes->get());
break;
case allOut:
newClasses.insert(classes->get());
break;
}
}
classes->swap(newClasses);
}
#ifdef SP_NAMESPACE
}
#endif
|