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