os/textandloc/fontservices/textshaperplugin/IcuSource/common/ucmp8.h
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2  ********************************************************************
     3  * COPYRIGHT: 
     4  * Copyright (c) 1996-2004, International Business Machines Corporation and
     5  * others. All Rights Reserved.
     6  ********************************************************************
     7  */
     8 
     9 
    10 
    11 #ifndef UCMP8_H
    12 #define UCMP8_H
    13 
    14 /* 32-bits.
    15   Bump this whenever the internal structure changes.
    16 */
    17 #define ICU_UCMP8_VERSION 0x01260000
    18 
    19 #include "unicode/utypes.h"
    20 
    21 /*====================================
    22  * class CompactByteArray
    23  * Provides a compact way to store information that is indexed by Unicode values,
    24  * such as character properties, types, keyboard values, etc.
    25  * The ATypes are used by value, so should be small, integers or pointers.
    26  *====================================
    27  */
    28 
    29 U_CAPI int32_t U_EXPORT2 ucmp8_getkUnicodeCount(void);
    30 U_CAPI int32_t U_EXPORT2 ucmp8_getkBlockCount(void);
    31 
    32 typedef struct CompactByteArray {
    33   uint32_t fStructSize;
    34   int8_t* fArray;
    35   uint16_t* fIndex;
    36   int32_t fCount;
    37   UBool fCompact;
    38   UBool fBogus;
    39   UBool fAlias;
    40   UBool fIAmOwned; /* don't free CBA on close */
    41 } CompactByteArray;
    42 
    43 #define UCMP8_kUnicodeCount 65536
    44 #define UCMP8_kBlockShift 7
    45 #define UCMP8_kBlockCount (1<<UCMP8_kBlockShift)
    46 #define UCMP8_kIndexShift (16-UCMP8_kBlockShift)
    47 #define UCMP8_kIndexCount (1<<UCMP8_kIndexShift)
    48 #define UCMP8_kBlockMask (UCMP8_kBlockCount-1)
    49 
    50 
    51 /**
    52  * Construct an empty CompactByteArray with uprv_malloc(). Do not call any of the
    53  * ucmp8_init*() functions after using this function. They will cause a memory
    54  * leak.
    55  *
    56  * @param defaultValue the default value for all characters not explicitly in the array
    57  * @see ucmp8_init
    58  * @see ucmp8_initBogus
    59  * @return The initialized array.
    60  */
    61 U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_open(int8_t defaultValue);
    62 
    63 /**
    64  * Construct a CompactByteArray from a pre-computed index and values array. The values
    65  * will be adopted by the CompactByteArray. Memory is allocated with uprv_malloc.
    66  * Note: for speed, the compact method will only re-use blocks in the values array
    67  * that are on a block boundary. The pre-computed arrays passed in to this constructor
    68  * may re-use blocks at any position in the values array. The indexArray and newValues
    69  * will be uprv_free'd when ucmp16_close() is called.
    70  *
    71  * @param indexArray the index array to be adopted
    72  * @param newValues the value array to be adopted
    73  * @param count the number of entries in the value array
    74  * @return the newly constructed ComapctByteArray
    75  * @see compact
    76  */
    77 U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_openAdopt(uint16_t* indexArray, 
    78                                int8_t* newValues,
    79                                int32_t count);
    80 
    81 /**
    82  * Construct a CompactByteArray from a pre-computed index and values array. The values
    83  * will be aliased by the CompactByteArray. Memory is allocated with uprv_malloc.
    84  * Note: for speed, the compact method will only re-use blocks in the values array
    85  * that are on a block boundary. The pre-computed arrays passed in to this constructor
    86  * may re-use blocks at any position in the values array.
    87  *
    88  * @param indexArray the index array to be adopted
    89  * @param newValues the value array to be adopted
    90  * @param count the number of entries in the value array
    91  * @return the newly constructed CompactByteArray
    92  * @see compact
    93  */
    94 U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_openAlias(uint16_t* indexArray, 
    95                                int8_t* newValues,
    96                                int32_t count);
    97 
    98 
    99 /**
   100  * Initialize an empty CompactByteArray. Do not call this function
   101  * if you created the array with ucmp8_open() because it will cause a memory
   102  * leak.
   103  *
   104  * @param defaultValue the default value for all characters not explicitly in the array
   105  * @param array An uninitialized CompactByteArray
   106  * @see ucmp8_open
   107  */
   108 U_CAPI  void U_EXPORT2 ucmp8_init(CompactByteArray* array, int8_t defaultValue);
   109 
   110 /**
   111  * Initialize an empty CompactByteArray to the bogus value. Do not call this
   112  * function if you created the array with ucmp8_open() because it will cause
   113  * a memory leak.
   114  *
   115  * @param array An uninitialized CompactByteArray
   116  * @see ucmp8_open
   117  * @see ucmp8_isBogus
   118  */
   119 U_CAPI  void U_EXPORT2 ucmp8_initBogus(CompactByteArray* array);
   120 
   121 /**
   122  * Initialize a CompactByteArray from a pre-computed index and values array. The values
   123  * will be adopted by the CompactByteArray. Memory is allocated with uprv_malloc.
   124  * Note: for speed, the compact method will only re-use blocks in the values array
   125  * that are on a block boundary. The pre-computed arrays passed in to this constructor
   126  * may re-use blocks at any position in the values array. The indexArray and newValues
   127  * will be uprv_free'd when ucmp16_close() is called.
   128  *
   129  * @param this_obj An uninitialized CompactByteArray
   130  * @param indexArray the index array to be adopted
   131  * @param newValues the value array to be adopted
   132  * @param count the number of entries in the value array
   133  * @return the pointer refers to the CompactByteArray
   134  * @see compact
   135  */
   136 U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_initAdopt(CompactByteArray *this_obj,
   137                                uint16_t* indexArray, 
   138                                int8_t* newValues,
   139                                int32_t count);
   140 
   141 /**
   142  * Initialize a CompactByteArray from a pre-computed index and values array. The values
   143  * will be aliased by the CompactByteArray. Memory is allocated with uprv_malloc.
   144  * Note: for speed, the compact method will only re-use blocks in the values array
   145  * that are on a block boundary. The pre-computed arrays passed in to this constructor
   146  * may re-use blocks at any position in the values array.
   147  *
   148  * @param this_obj An uninitialized CompactByteArray
   149  * @param indexArray the index array to be adopted
   150  * @param newValues the value array to be adopted
   151  * @param count the number of entries in the value array
   152  * @return the pointer refers to the CompactByteArray
   153  * @see compact
   154  */
   155 U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_initAlias(CompactByteArray *this_obj,
   156                                uint16_t* indexArray, 
   157                                int8_t* newValues,
   158                                int32_t count);
   159 
   160 /**
   161  * Free up any allocated memory associated with this compact array.
   162  * The memory that is uprv_free'd depends on how the array was initialized
   163  * or opened.
   164  * 
   165  * @param array The compact array to close
   166  */
   167 U_CAPI  void U_EXPORT2 ucmp8_close(CompactByteArray* array);
   168 
   169 /**
   170  * Returns TRUE if the creation of the compact array fails.
   171  * @param array The CompactByteArray to be created.
   172  * @return TRUE if the creation of the compact array fails.
   173  */
   174 U_CAPI  UBool U_EXPORT2 ucmp8_isBogus(const CompactByteArray* array);
   175 
   176 /**
   177  * Get the mapped value of a Unicode character.
   178  *
   179  * @param index the character to get the mapped value with
   180  * @return the mapped value of the given character
   181  */
   182 #define ucmp8_get(array, index)  (array->fArray[(array->fIndex[index >> UCMP8_kBlockShift] & 0xFFFF) + (index & UCMP8_kBlockMask)])
   183 
   184 #define ucmp8_getu(array,index) (uint8_t)ucmp8_get(array,index)
   185 
   186 
   187 /**
   188  * Set a new value for a Unicode character.
   189  * Set automatically expands the array if it is compacted.
   190  *
   191  * @param array the CompactByteArray to be set
   192  * @param character the character to set the mapped value with
   193  * @param value the new mapped value
   194  */
   195 U_CAPI  void U_EXPORT2 ucmp8_set(CompactByteArray* array,
   196                  UChar character,
   197                  int8_t value);
   198 
   199 /**
   200  * Set new values for a range of Unicode character.
   201  *
   202  * @param array the CompactByteArray to be set
   203  * @param start the starting offset of the range
   204  * @param end the ending offset of the range
   205  * @param value the new mapped value
   206  */
   207 U_CAPI  void U_EXPORT2 ucmp8_setRange(CompactByteArray* array, 
   208                   UChar start,
   209                   UChar end, 
   210                   int8_t value);
   211 
   212 U_CAPI  int32_t U_EXPORT2 ucmp8_getCount(const CompactByteArray* array);
   213 U_CAPI  const int8_t* U_EXPORT2 ucmp8_getArray(const CompactByteArray* array);
   214 U_CAPI  const uint16_t* U_EXPORT2 ucmp8_getIndex(const CompactByteArray* array);
   215 
   216 /**
   217  * Compact the array.
   218  * The value of cycle determines how large the overlap can be.
   219  * A cycle of 1 is the most compacted, but takes the most time to do.
   220  * If values stored in the array tend to repeat in cycles of, say, 16,
   221  * then using that will be faster than cycle = 1, and get almost the
   222  * same compression.
   223  * @param array The CompactByteArray to be compacted
   224  * @param cycle The value determines how large the overlap can be.
   225  */
   226 U_CAPI  void U_EXPORT2 ucmp8_compact(CompactByteArray* array, 
   227                  uint32_t cycle);
   228 
   229 /** Expanded takes the array back to a 65536 element array
   230  *  @param array The CompactByteArray to be expanded
   231  */
   232 U_CAPI  void U_EXPORT2 ucmp8_expand(CompactByteArray* array);
   233 
   234 /** 
   235  * Flatten into a memory structure. Pass in NULL to pre-flight to get the required size.
   236  * @internal
   237  */
   238 U_CAPI  uint32_t U_EXPORT2 ucmp8_flattenMem(const CompactByteArray* array, uint8_t *MS);
   239 
   240 /* initializes an existing CBA from memory.  Will cause ucmp8_close() to not deallocate anything. */
   241 U_CAPI  void U_EXPORT2 ucmp8_initFromData(CompactByteArray* array, const uint8_t **source, UErrorCode *status);
   242 
   243 #endif
   244