// --------------------------------------------------------------------------- // RangeToken: Range manipulation methods // --------------------------------------------------------------------------- void RangeToken::addRange(const XMLInt32 start, const XMLInt32 end) { XMLInt32 val1, val2; fCaseIToken = 0; if (start <= end) { val1 = start; val2 = end; } else { val1 = end; val2 = start; } if (fRanges == 0) { fRanges = (XMLInt32*) fMemoryManager->allocate ( fMaxCount * sizeof(XMLInt32) );//new XMLInt32[fMaxCount]; fRanges[0] = val1; fRanges[1] = val2; fElemCount = 2; fSorted = true; } else { if (fRanges[fElemCount-1] + 1 == val1) { fRanges[fElemCount-1] = val2; return; } if (fElemCount + 2 >= fMaxCount) { expand(2); } if (fRanges[fElemCount-1] >= val1) fSorted = false; fRanges[fElemCount++] = val1; fRanges[fElemCount++] = val2; if (!fSorted) { sortRanges(); } } }
/** * Ignore whether 'tok' is NRANGE or not. */ void RangeToken::intersectRanges(RangeToken* const tok) { if (fRanges == 0 || tok->fRanges == 0) return; fCaseIToken = 0; sortRanges(); compactRanges(); tok->sortRanges(); tok->compactRanges(); unsigned int newMax = (fElemCount + tok->fElemCount >= fMaxCount) ? fMaxCount + tok->fMaxCount : fMaxCount; XMLInt32* result = (XMLInt32*) fMemoryManager->allocate ( newMax * sizeof(XMLInt32) );//new XMLInt32[newMax]; unsigned int newElemCount = 0; unsigned int srcCount = 0; unsigned int tokCount = 0; while (srcCount < fElemCount && tokCount < tok->fElemCount) { XMLInt32 srcBegin = fRanges[srcCount]; XMLInt32 srcEnd = fRanges[srcCount + 1]; XMLInt32 tokBegin = tok->fRanges[tokCount]; XMLInt32 tokEnd = tok->fRanges[tokCount + 1]; if (srcEnd < tokBegin) { srcCount += 2; } else if (srcEnd >= tokBegin && srcBegin <= tokEnd) { if (tokBegin <= srcBegin && srcEnd <= tokEnd) { result[newElemCount++] = srcBegin; result[newElemCount++] = srcEnd; srcCount += 2; } else if (tokBegin <= srcBegin) { result[newElemCount++] = srcBegin; result[newElemCount++] = tokEnd; tokCount += 2; if (tokCount < tok->fElemCount) fRanges[srcCount] = tokEnd + 1; else srcCount += 2; } else if (srcEnd <= tokEnd) { result[newElemCount++] = tokBegin; result[newElemCount++] = srcEnd; srcCount += 2; } else { result[newElemCount++] = tokBegin; result[newElemCount++] = tokEnd; tokCount += 2; if (tokCount < tok->fElemCount) fRanges[srcCount] = tokEnd + 1; else srcCount += 2; } } else if (tokEnd < srcBegin) { tokCount += 2; if (tokCount >= tok->fElemCount) srcCount += 2; } else { fMemoryManager->deallocate(result);//delete [] result; ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Regex_IntersectRangesError, fMemoryManager); } } //end while fMemoryManager->deallocate(fRanges);//delete [] fRanges; fRanges = result; fElemCount = newElemCount; fMaxCount = newMax; }
void RangeToken::subtractRanges(RangeToken* const tok) { if (fRanges == 0 || tok->fRanges == 0) return; if (tok->getTokenType() == T_NRANGE) { intersectRanges(tok); return; } fCaseIToken = 0; sortRanges(); compactRanges(); tok->sortRanges(); tok->compactRanges(); unsigned int newMax = (fElemCount + tok->fElemCount >= fMaxCount) ? fMaxCount + tok->fMaxCount : fMaxCount; XMLInt32* result = (XMLInt32*) fMemoryManager->allocate ( newMax * sizeof(XMLInt32) );//new XMLInt32[newMax]; unsigned int newElemCount = 0; unsigned int srcCount = 0; unsigned int subCount = 0; while (srcCount < fElemCount && subCount < tok->fElemCount) { XMLInt32 srcBegin = fRanges[srcCount]; XMLInt32 srcEnd = fRanges[srcCount + 1]; XMLInt32 subBegin = tok->fRanges[subCount]; XMLInt32 subEnd = tok->fRanges[subCount + 1]; if (srcEnd < subBegin) { // no overlap result[newElemCount++] = fRanges[srcCount++]; result[newElemCount++] = fRanges[srcCount++]; } else if (srcEnd >= subBegin && srcBegin <= subEnd) { if (subBegin <= srcBegin && srcEnd <= subEnd) { srcCount += 2; } else if (subBegin <= srcBegin) { fRanges[srcCount] = subEnd + 1; subCount += 2; } else if (srcEnd <= subEnd) { result[newElemCount++] = srcBegin; result[newElemCount++] = subBegin - 1; srcCount += 2; } else { result[newElemCount++] = srcBegin; result[newElemCount++] = subBegin - 1; fRanges[srcCount] = subEnd + 1; subCount += 2; } } else if (subEnd < srcBegin) { subCount += 2; } else { fMemoryManager->deallocate(result);//delete [] result; ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::Regex_SubtractRangesError, fMemoryManager); } } //end while while (srcCount < fElemCount) { result[newElemCount++] = fRanges[srcCount++]; result[newElemCount++] = fRanges[srcCount++]; } fMemoryManager->deallocate(fRanges);//delete [] fRanges; fRanges = result; fElemCount = newElemCount; fMaxCount = newMax; }
void RangeToken::mergeRanges(const Token *const tok) { if (tok->getTokenType() != this->getTokenType()) ThrowXMLwithMemMgr(IllegalArgumentException, XMLExcepts::Regex_MergeRangesTypeMismatch, fMemoryManager); RangeToken* rangeTok = (RangeToken *) tok; if (rangeTok->fRanges == 0) return; fCaseIToken = 0; sortRanges(); rangeTok->sortRanges(); if (fRanges == 0) { fMaxCount = rangeTok->fMaxCount; fRanges = (XMLInt32*) fMemoryManager->allocate ( fMaxCount * sizeof(XMLInt32) );//new XMLInt32[fMaxCount]; for (unsigned int index = 0; index < rangeTok->fElemCount; index++) { fRanges[index] = rangeTok->fRanges[index]; } fElemCount = rangeTok->fElemCount; return; } unsigned int newMaxCount = (fElemCount + rangeTok->fElemCount >= fMaxCount) ? fMaxCount + rangeTok->fMaxCount : fMaxCount; XMLInt32* result = (XMLInt32*) fMemoryManager->allocate ( newMaxCount * sizeof(XMLInt32) );//new XMLInt32[newMaxCount]; for (unsigned int i=0, j=0, k=0; i < fElemCount || j < rangeTok->fElemCount;) { if (i >= fElemCount) { for (int count = 0; count < 2; count++) { result[k++] = rangeTok->fRanges[j++]; } } else if (j >= rangeTok->fElemCount) { for (int count = 0; count < 2; count++) { result[k++] = fRanges[i++]; } } else if (rangeTok->fRanges[j] < fRanges[i] || (rangeTok->fRanges[j] == fRanges[i] && rangeTok->fRanges[j+1] < fRanges[i+1])) { for (int count = 0; count < 2; count++) { result[k++] = rangeTok->fRanges[j++]; } } else { for (int count = 0; count < 2; count++) { result[k++] = fRanges[i++]; } } } fMemoryManager->deallocate(fRanges);//delete [] fRanges; fElemCount += rangeTok->fElemCount; fRanges = result; fMaxCount = newMaxCount; }
// --------------------------------------------------------------------------- // RangeToken: Range manipulation methods // --------------------------------------------------------------------------- void RangeToken::addRange(const XMLInt32 start, const XMLInt32 end) { XMLInt32 val1, val2; fCaseIToken = 0; if (start <= end) { val1 = start; val2 = end; } else { val1 = end; val2 = start; } if (fRanges == 0) { fRanges = (XMLInt32*) fMemoryManager->allocate ( fMaxCount * sizeof(XMLInt32) );//new XMLInt32[fMaxCount]; fRanges[0] = val1; fRanges[1] = val2; fElemCount = 2; fSorted = true; } else { if (fRanges[fElemCount-1] + 1 == val1) { fRanges[fElemCount-1] = val2; return; } if (fElemCount + 2 >= fMaxCount) { expand(2); } if(fSorted && fRanges[fElemCount-1] >= val1) { for (int i = 0; i < (int)fElemCount; i +=2) { // check if this range is already part of this one if (fRanges[i] <= val1 && fRanges[i+1] >= val2) break; // or if the new one extends the old one else if(fRanges[i]==val1 && fRanges[i+1] < val2) { fRanges[i+1]=val2; break; } else if (fRanges[i] > val1 || (fRanges[i]==val1 && fRanges[i+1] > val2)) { for(int j=fElemCount-1;j>=i;j--) fRanges[j+2]=fRanges[j]; fRanges[i] = val1; fRanges[i+1] = val2; fElemCount += 2; break; } } } else { if (fRanges[fElemCount-1] >= val1) fSorted = false; fRanges[fElemCount++] = val1; fRanges[fElemCount++] = val2; if (!fSorted) { sortRanges(); } } } }