michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2001-2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: bocsu.c michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * Author: Markus W. Scherer michael@0: * michael@0: * Modification history: michael@0: * 05/18/2001 weiv Made into separate module michael@0: */ michael@0: michael@0: #ifndef BOCSU_H michael@0: #define BOCSU_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_COLLATION michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class ByteSink; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: /* michael@0: * "BOCSU" michael@0: * Binary Ordered Compression Scheme for Unicode michael@0: * michael@0: * Specific application: michael@0: * michael@0: * Encode a Unicode string for the identical level of a sort key. michael@0: * Restrictions: michael@0: * - byte stream (unsigned 8-bit bytes) michael@0: * - lexical order of the identical-level run must be michael@0: * the same as code point order for the string michael@0: * - avoid byte values 0, 1, 2 michael@0: * michael@0: * Method: Slope Detection michael@0: * Remember the previous code point (initial 0). michael@0: * For each cp in the string, encode the difference to the previous one. michael@0: * michael@0: * With a compact encoding of differences, this yields good results for michael@0: * small scripts and UTF-like results otherwise. michael@0: * michael@0: * Encoding of differences: michael@0: * - Similar to a UTF, encoding the length of the byte sequence in the lead bytes. michael@0: * - Does not need to be friendly for decoding or random access michael@0: * (trail byte values may overlap with lead/single byte values). michael@0: * - The signedness must be encoded as the most significant part. michael@0: * michael@0: * We encode differences with few bytes if their absolute values are small. michael@0: * For correct ordering, we must treat the entire value range -10ffff..+10ffff michael@0: * in ascending order, which forbids encoding the sign and the absolute value separately. michael@0: * Instead, we split the lead byte range in the middle and encode non-negative values michael@0: * going up and negative values going down. michael@0: * michael@0: * For very small absolute values, the difference is added to a middle byte value michael@0: * for single-byte encoded differences. michael@0: * For somewhat larger absolute values, the difference is divided by the number michael@0: * of byte values available, the modulo is used for one trail byte, and the remainder michael@0: * is added to a lead byte avoiding the single-byte range. michael@0: * For large absolute values, the difference is similarly encoded in three bytes. michael@0: * michael@0: * This encoding does not use byte values 0, 1, 2, but uses all other byte values michael@0: * for lead/single bytes so that the middle range of single bytes is as large michael@0: * as possible. michael@0: * Note that the lead byte ranges overlap some, but that the sequences as a whole michael@0: * are well ordered. I.e., even if the lead byte is the same for sequences of different michael@0: * lengths, the trail bytes establish correct order. michael@0: * It would be possible to encode slightly larger ranges for each length (>1) by michael@0: * subtracting the lower bound of the range. However, that would also slow down the michael@0: * calculation. michael@0: * michael@0: * For the actual string encoding, an optimization moves the previous code point value michael@0: * to the middle of its Unicode script block to minimize the differences in michael@0: * same-script text runs. michael@0: */ michael@0: michael@0: /* Do not use byte values 0, 1, 2 because they are separators in sort keys. */ michael@0: #define SLOPE_MIN 3 michael@0: #define SLOPE_MAX 0xff michael@0: #define SLOPE_MIDDLE 0x81 michael@0: michael@0: #define SLOPE_TAIL_COUNT (SLOPE_MAX-SLOPE_MIN+1) michael@0: michael@0: #define SLOPE_MAX_BYTES 4 michael@0: michael@0: /* michael@0: * Number of lead bytes: michael@0: * 1 middle byte for 0 michael@0: * 2*80=160 single bytes for !=0 michael@0: * 2*42=84 for double-byte values michael@0: * 2*3=6 for 3-byte values michael@0: * 2*1=2 for 4-byte values michael@0: * michael@0: * The sum must be <=SLOPE_TAIL_COUNT. michael@0: * michael@0: * Why these numbers? michael@0: * - There should be >=128 single-byte values to cover 128-blocks michael@0: * with small scripts. michael@0: * - There should be >=20902 single/double-byte values to cover Unihan. michael@0: * - It helps CJK Extension B some if there are 3-byte values that cover michael@0: * the distance between them and Unihan. michael@0: * This also helps to jump among distant places in the BMP. michael@0: * - Four-byte values are necessary to cover the rest of Unicode. michael@0: * michael@0: * Symmetrical lead byte counts are for convenience. michael@0: * With an equal distribution of even and odd differences there is also michael@0: * no advantage to asymmetrical lead byte counts. michael@0: */ michael@0: #define SLOPE_SINGLE 80 michael@0: #define SLOPE_LEAD_2 42 michael@0: #define SLOPE_LEAD_3 3 michael@0: #define SLOPE_LEAD_4 1 michael@0: michael@0: /* The difference value range for single-byters. */ michael@0: #define SLOPE_REACH_POS_1 SLOPE_SINGLE michael@0: #define SLOPE_REACH_NEG_1 (-SLOPE_SINGLE) michael@0: michael@0: /* The difference value range for double-byters. */ michael@0: #define SLOPE_REACH_POS_2 (SLOPE_LEAD_2*SLOPE_TAIL_COUNT+(SLOPE_LEAD_2-1)) michael@0: #define SLOPE_REACH_NEG_2 (-SLOPE_REACH_POS_2-1) michael@0: michael@0: /* The difference value range for 3-byters. */ michael@0: #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)) michael@0: #define SLOPE_REACH_NEG_3 (-SLOPE_REACH_POS_3-1) michael@0: michael@0: /* The lead byte start values. */ michael@0: #define SLOPE_START_POS_2 (SLOPE_MIDDLE+SLOPE_SINGLE+1) michael@0: #define SLOPE_START_POS_3 (SLOPE_START_POS_2+SLOPE_LEAD_2) michael@0: michael@0: #define SLOPE_START_NEG_2 (SLOPE_MIDDLE+SLOPE_REACH_NEG_1) michael@0: #define SLOPE_START_NEG_3 (SLOPE_START_NEG_2-SLOPE_LEAD_2) michael@0: michael@0: /* michael@0: * Integer division and modulo with negative numerators michael@0: * yields negative modulo results and quotients that are one more than michael@0: * what we need here. michael@0: */ michael@0: #define NEGDIVMOD(n, d, m) { \ michael@0: (m)=(n)%(d); \ michael@0: (n)/=(d); \ michael@0: if((m)<0) { \ michael@0: --(n); \ michael@0: (m)+=(d); \ michael@0: } \ michael@0: } michael@0: michael@0: U_CFUNC void michael@0: u_writeIdenticalLevelRun(const UChar *s, int32_t length, icu::ByteSink &sink); michael@0: michael@0: U_CFUNC int32_t michael@0: u_writeIdenticalLevelRunTwoChars(UChar32 first, UChar32 second, uint8_t *p); michael@0: michael@0: U_CFUNC uint8_t * michael@0: u_writeDiff(int32_t diff, uint8_t *p); michael@0: michael@0: #endif /* #if !UCONFIG_NO_COLLATION */ michael@0: michael@0: #endif