// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4* Copyright (C) 2001-2014, International Business Machines5* Corporation and others. All Rights Reserved.6*******************************************************************************7* file name: bocsu.h8* encoding: UTF-89* tab size: 8 (not used)10* indentation:411*12* Author: Markus W. Scherer13*14* Modification history:15* 05/18/2001 weiv Made into separate module16*/1718#ifndef BOCSU_H19#define BOCSU_H2021#include "unicode/utypes.h"2223#if !UCONFIG_NO_COLLATION2425U_NAMESPACE_BEGIN2627class ByteSink;2829U_NAMESPACE_END3031/*32* "BOCSU"33* Binary Ordered Compression Scheme for Unicode34*35* Specific application:36*37* Encode a Unicode string for the identical level of a sort key.38* Restrictions:39* - byte stream (unsigned 8-bit bytes)40* - lexical order of the identical-level run must be41* the same as code point order for the string42* - avoid byte values 0, 1, 243*44* Method: Slope Detection45* Remember the previous code point (initial 0).46* For each cp in the string, encode the difference to the previous one.47*48* With a compact encoding of differences, this yields good results for49* small scripts and UTF-like results otherwise.50*51* Encoding of differences:52* - Similar to a UTF, encoding the length of the byte sequence in the lead bytes.53* - Does not need to be friendly for decoding or random access54* (trail byte values may overlap with lead/single byte values).55* - The signedness must be encoded as the most significant part.56*57* We encode differences with few bytes if their absolute values are small.58* For correct ordering, we must treat the entire value range -10ffff..+10ffff59* in ascending order, which forbids encoding the sign and the absolute value separately.60* Instead, we split the lead byte range in the middle and encode non-negative values61* going up and negative values going down.62*63* For very small absolute values, the difference is added to a middle byte value64* for single-byte encoded differences.65* For somewhat larger absolute values, the difference is divided by the number66* of byte values available, the modulo is used for one trail byte, and the remainder67* is added to a lead byte avoiding the single-byte range.68* For large absolute values, the difference is similarly encoded in three bytes.69*70* This encoding does not use byte values 0, 1, 2, but uses all other byte values71* for lead/single bytes so that the middle range of single bytes is as large72* as possible.73* Note that the lead byte ranges overlap some, but that the sequences as a whole74* are well ordered. I.e., even if the lead byte is the same for sequences of different75* lengths, the trail bytes establish correct order.76* It would be possible to encode slightly larger ranges for each length (>1) by77* subtracting the lower bound of the range. However, that would also slow down the78* calculation.79*80* For the actual string encoding, an optimization moves the previous code point value81* to the middle of its Unicode script block to minimize the differences in82* same-script text runs.83*/8485/* Do not use byte values 0, 1, 2 because they are separators in sort keys. */86#define SLOPE_MIN 387#define SLOPE_MAX 0xff88#define SLOPE_MIDDLE 0x818990#define SLOPE_TAIL_COUNT (SLOPE_MAX-SLOPE_MIN+1)9192#define SLOPE_MAX_BYTES 49394/*95* Number of lead bytes:96* 1 middle byte for 097* 2*80=160 single bytes for !=098* 2*42=84 for double-byte values99* 2*3=6 for 3-byte values100* 2*1=2 for 4-byte values101*102* The sum must be <=SLOPE_TAIL_COUNT.103*104* Why these numbers?105* - There should be >=128 single-byte values to cover 128-blocks106* with small scripts.107* - There should be >=20902 single/double-byte values to cover Unihan.108* - It helps CJK Extension B some if there are 3-byte values that cover109* the distance between them and Unihan.110* This also helps to jump among distant places in the BMP.111* - Four-byte values are necessary to cover the rest of Unicode.112*113* Symmetrical lead byte counts are for convenience.114* With an equal distribution of even and odd differences there is also115* no advantage to asymmetrical lead byte counts.116*/117#define SLOPE_SINGLE 80118#define SLOPE_LEAD_2 42119#define SLOPE_LEAD_3 3120#define SLOPE_LEAD_4 1121122/* The difference value range for single-byters. */123#define SLOPE_REACH_POS_1 SLOPE_SINGLE124#define SLOPE_REACH_NEG_1 (-SLOPE_SINGLE)125126/* The difference value range for double-byters. */127#define SLOPE_REACH_POS_2 (SLOPE_LEAD_2*SLOPE_TAIL_COUNT+(SLOPE_LEAD_2-1))128#define SLOPE_REACH_NEG_2 (-SLOPE_REACH_POS_2-1)129130/* The difference value range for 3-byters. */131#define SLOPE_REACH_POS_3 (SLOPE_LEAD_3*SLOPE_TAIL_COUNT*SLOPE_TAIL_COUNT+(SLOPE_LEAD_3-1)*SLOPE_TAIL_COUNT+(SLOPE_TAIL_COUNT-1))132#define SLOPE_REACH_NEG_3 (-SLOPE_REACH_POS_3-1)133134/* The lead byte start values. */135#define SLOPE_START_POS_2 (SLOPE_MIDDLE+SLOPE_SINGLE+1)136#define SLOPE_START_POS_3 (SLOPE_START_POS_2+SLOPE_LEAD_2)137138#define SLOPE_START_NEG_2 (SLOPE_MIDDLE+SLOPE_REACH_NEG_1)139#define SLOPE_START_NEG_3 (SLOPE_START_NEG_2-SLOPE_LEAD_2)140141/*142* Integer division and modulo with negative numerators143* yields negative modulo results and quotients that are one more than144* what we need here.145*/146#define NEGDIVMOD(n, d, m) UPRV_BLOCK_MACRO_BEGIN { \147(m)=(n)%(d); \148(n)/=(d); \149if((m)<0) { \150--(n); \151(m)+=(d); \152} \153} UPRV_BLOCK_MACRO_END154155U_CFUNC UChar32156u_writeIdenticalLevelRun(UChar32 prev, const UChar *s, int32_t length, icu::ByteSink &sink);157158#endif /* #if !UCONFIG_NO_COLLATION */159160#endif161162163