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