michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2010, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: denseranges.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2010sep25 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Helper code for finding a small number of dense ranges. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "denseranges.h" michael@0: michael@0: // Definitions in the anonymous namespace are invisible outside this file. michael@0: namespace { michael@0: michael@0: /** michael@0: * Collect up to 15 range gaps and sort them by ascending gap size. michael@0: */ michael@0: class LargestGaps { michael@0: public: michael@0: LargestGaps(int32_t max) : maxLength(max<=kCapacity ? max : kCapacity), length(0) {} michael@0: michael@0: void add(int32_t gapStart, int64_t gapLength) { michael@0: int32_t i=length; michael@0: while(i>0 && gapLength>gapLengths[i-1]) { michael@0: --i; michael@0: } michael@0: if(ii) { michael@0: gapStarts[j]=gapStarts[j-1]; michael@0: gapLengths[j]=gapLengths[j-1]; michael@0: --j; michael@0: } michael@0: gapStarts[i]=gapStart; michael@0: gapLengths[i]=gapLength; michael@0: } michael@0: } michael@0: michael@0: void truncate(int32_t newLength) { michael@0: if(newLength=(density*maxLength)/0x100) { michael@0: // Use one range. michael@0: ranges[0][0]=minValue; michael@0: ranges[0][1]=maxValue; michael@0: return 1; michael@0: } michael@0: if(length<=4) { michael@0: return 0; michael@0: } michael@0: // See if we can split [minValue, maxValue] into 2..capacity ranges, michael@0: // divided by the 1..(capacity-1) largest gaps. michael@0: LargestGaps gaps(capacity-1); michael@0: int32_t i; michael@0: int32_t expectedValue=minValue; michael@0: for(i=1; i=1 because we have fewer values (length) than michael@0: // the length of the [minValue..maxValue] range (maxLength). michael@0: // (Otherwise we would have returned with the one range above.) michael@0: int32_t num; michael@0: for(i=0, num=2;; ++i, ++num) { michael@0: if(i>=gaps.count()) { michael@0: // The values are too sparse for capacity or fewer ranges michael@0: // of the requested density. michael@0: return 0; michael@0: } michael@0: maxLength-=gaps.gapLength(i); michael@0: if(length>num*2 && length>=(density*maxLength)/0x100) { michael@0: break; michael@0: } michael@0: } michael@0: // Use the num ranges with the num-1 largest gaps. michael@0: gaps.truncate(num-1); michael@0: ranges[0][0]=minValue; michael@0: for(i=0; i<=num-2; ++i) { michael@0: int32_t gapIndex=gaps.firstAfter(minValue); michael@0: int32_t gapStart=gaps.gapStart(gapIndex); michael@0: ranges[i][1]=gapStart-1; michael@0: ranges[i+1][0]=minValue=(int32_t)(gapStart+gaps.gapLength(gapIndex)); michael@0: } michael@0: ranges[num-1][1]=maxValue; michael@0: return num; michael@0: }