sl@0: /*
sl@0:  ********************************************************************
sl@0:  * COPYRIGHT: 
sl@0:  * Copyright (c) 1996-2004, International Business Machines Corporation and
sl@0:  * others. All Rights Reserved.
sl@0:  ********************************************************************
sl@0:  */
sl@0: 
sl@0: 
sl@0: 
sl@0: #ifndef UCMP8_H
sl@0: #define UCMP8_H
sl@0: 
sl@0: /* 32-bits.
sl@0:   Bump this whenever the internal structure changes.
sl@0: */
sl@0: #define ICU_UCMP8_VERSION 0x01260000
sl@0: 
sl@0: #include "unicode/utypes.h"
sl@0: 
sl@0: /*====================================
sl@0:  * class CompactByteArray
sl@0:  * Provides a compact way to store information that is indexed by Unicode values,
sl@0:  * such as character properties, types, keyboard values, etc.
sl@0:  * The ATypes are used by value, so should be small, integers or pointers.
sl@0:  *====================================
sl@0:  */
sl@0: 
sl@0: U_CAPI int32_t U_EXPORT2 ucmp8_getkUnicodeCount(void);
sl@0: U_CAPI int32_t U_EXPORT2 ucmp8_getkBlockCount(void);
sl@0: 
sl@0: typedef struct CompactByteArray {
sl@0:   uint32_t fStructSize;
sl@0:   int8_t* fArray;
sl@0:   uint16_t* fIndex;
sl@0:   int32_t fCount;
sl@0:   UBool fCompact;
sl@0:   UBool fBogus;
sl@0:   UBool fAlias;
sl@0:   UBool fIAmOwned; /* don't free CBA on close */
sl@0: } CompactByteArray;
sl@0: 
sl@0: #define UCMP8_kUnicodeCount 65536
sl@0: #define UCMP8_kBlockShift 7
sl@0: #define UCMP8_kBlockCount (1<<UCMP8_kBlockShift)
sl@0: #define UCMP8_kIndexShift (16-UCMP8_kBlockShift)
sl@0: #define UCMP8_kIndexCount (1<<UCMP8_kIndexShift)
sl@0: #define UCMP8_kBlockMask (UCMP8_kBlockCount-1)
sl@0: 
sl@0: 
sl@0: /**
sl@0:  * Construct an empty CompactByteArray with uprv_malloc(). Do not call any of the
sl@0:  * ucmp8_init*() functions after using this function. They will cause a memory
sl@0:  * leak.
sl@0:  *
sl@0:  * @param defaultValue the default value for all characters not explicitly in the array
sl@0:  * @see ucmp8_init
sl@0:  * @see ucmp8_initBogus
sl@0:  * @return The initialized array.
sl@0:  */
sl@0: U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_open(int8_t defaultValue);
sl@0: 
sl@0: /**
sl@0:  * Construct a CompactByteArray from a pre-computed index and values array. The values
sl@0:  * will be adopted by the CompactByteArray. Memory is allocated with uprv_malloc.
sl@0:  * Note: for speed, the compact method will only re-use blocks in the values array
sl@0:  * that are on a block boundary. The pre-computed arrays passed in to this constructor
sl@0:  * may re-use blocks at any position in the values array. The indexArray and newValues
sl@0:  * will be uprv_free'd when ucmp16_close() is called.
sl@0:  *
sl@0:  * @param indexArray the index array to be adopted
sl@0:  * @param newValues the value array to be adopted
sl@0:  * @param count the number of entries in the value array
sl@0:  * @return the newly constructed ComapctByteArray
sl@0:  * @see compact
sl@0:  */
sl@0: U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_openAdopt(uint16_t* indexArray, 
sl@0:                                int8_t* newValues,
sl@0:                                int32_t count);
sl@0: 
sl@0: /**
sl@0:  * Construct a CompactByteArray from a pre-computed index and values array. The values
sl@0:  * will be aliased by the CompactByteArray. Memory is allocated with uprv_malloc.
sl@0:  * Note: for speed, the compact method will only re-use blocks in the values array
sl@0:  * that are on a block boundary. The pre-computed arrays passed in to this constructor
sl@0:  * may re-use blocks at any position in the values array.
sl@0:  *
sl@0:  * @param indexArray the index array to be adopted
sl@0:  * @param newValues the value array to be adopted
sl@0:  * @param count the number of entries in the value array
sl@0:  * @return the newly constructed CompactByteArray
sl@0:  * @see compact
sl@0:  */
sl@0: U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_openAlias(uint16_t* indexArray, 
sl@0:                                int8_t* newValues,
sl@0:                                int32_t count);
sl@0: 
sl@0: 
sl@0: /**
sl@0:  * Initialize an empty CompactByteArray. Do not call this function
sl@0:  * if you created the array with ucmp8_open() because it will cause a memory
sl@0:  * leak.
sl@0:  *
sl@0:  * @param defaultValue the default value for all characters not explicitly in the array
sl@0:  * @param array An uninitialized CompactByteArray
sl@0:  * @see ucmp8_open
sl@0:  */
sl@0: U_CAPI  void U_EXPORT2 ucmp8_init(CompactByteArray* array, int8_t defaultValue);
sl@0: 
sl@0: /**
sl@0:  * Initialize an empty CompactByteArray to the bogus value. Do not call this
sl@0:  * function if you created the array with ucmp8_open() because it will cause
sl@0:  * a memory leak.
sl@0:  *
sl@0:  * @param array An uninitialized CompactByteArray
sl@0:  * @see ucmp8_open
sl@0:  * @see ucmp8_isBogus
sl@0:  */
sl@0: U_CAPI  void U_EXPORT2 ucmp8_initBogus(CompactByteArray* array);
sl@0: 
sl@0: /**
sl@0:  * Initialize a CompactByteArray from a pre-computed index and values array. The values
sl@0:  * will be adopted by the CompactByteArray. Memory is allocated with uprv_malloc.
sl@0:  * Note: for speed, the compact method will only re-use blocks in the values array
sl@0:  * that are on a block boundary. The pre-computed arrays passed in to this constructor
sl@0:  * may re-use blocks at any position in the values array. The indexArray and newValues
sl@0:  * will be uprv_free'd when ucmp16_close() is called.
sl@0:  *
sl@0:  * @param this_obj An uninitialized CompactByteArray
sl@0:  * @param indexArray the index array to be adopted
sl@0:  * @param newValues the value array to be adopted
sl@0:  * @param count the number of entries in the value array
sl@0:  * @return the pointer refers to the CompactByteArray
sl@0:  * @see compact
sl@0:  */
sl@0: U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_initAdopt(CompactByteArray *this_obj,
sl@0:                                uint16_t* indexArray, 
sl@0:                                int8_t* newValues,
sl@0:                                int32_t count);
sl@0: 
sl@0: /**
sl@0:  * Initialize a CompactByteArray from a pre-computed index and values array. The values
sl@0:  * will be aliased by the CompactByteArray. Memory is allocated with uprv_malloc.
sl@0:  * Note: for speed, the compact method will only re-use blocks in the values array
sl@0:  * that are on a block boundary. The pre-computed arrays passed in to this constructor
sl@0:  * may re-use blocks at any position in the values array.
sl@0:  *
sl@0:  * @param this_obj An uninitialized CompactByteArray
sl@0:  * @param indexArray the index array to be adopted
sl@0:  * @param newValues the value array to be adopted
sl@0:  * @param count the number of entries in the value array
sl@0:  * @return the pointer refers to the CompactByteArray
sl@0:  * @see compact
sl@0:  */
sl@0: U_CAPI  CompactByteArray* U_EXPORT2 ucmp8_initAlias(CompactByteArray *this_obj,
sl@0:                                uint16_t* indexArray, 
sl@0:                                int8_t* newValues,
sl@0:                                int32_t count);
sl@0: 
sl@0: /**
sl@0:  * Free up any allocated memory associated with this compact array.
sl@0:  * The memory that is uprv_free'd depends on how the array was initialized
sl@0:  * or opened.
sl@0:  * 
sl@0:  * @param array The compact array to close
sl@0:  */
sl@0: U_CAPI  void U_EXPORT2 ucmp8_close(CompactByteArray* array);
sl@0: 
sl@0: /**
sl@0:  * Returns TRUE if the creation of the compact array fails.
sl@0:  * @param array The CompactByteArray to be created.
sl@0:  * @return TRUE if the creation of the compact array fails.
sl@0:  */
sl@0: U_CAPI  UBool U_EXPORT2 ucmp8_isBogus(const CompactByteArray* array);
sl@0: 
sl@0: /**
sl@0:  * Get the mapped value of a Unicode character.
sl@0:  *
sl@0:  * @param index the character to get the mapped value with
sl@0:  * @return the mapped value of the given character
sl@0:  */
sl@0: #define ucmp8_get(array, index)  (array->fArray[(array->fIndex[index >> UCMP8_kBlockShift] & 0xFFFF) + (index & UCMP8_kBlockMask)])
sl@0: 
sl@0: #define ucmp8_getu(array,index) (uint8_t)ucmp8_get(array,index)
sl@0: 
sl@0: 
sl@0: /**
sl@0:  * Set a new value for a Unicode character.
sl@0:  * Set automatically expands the array if it is compacted.
sl@0:  *
sl@0:  * @param array the CompactByteArray to be set
sl@0:  * @param character the character to set the mapped value with
sl@0:  * @param value the new mapped value
sl@0:  */
sl@0: U_CAPI  void U_EXPORT2 ucmp8_set(CompactByteArray* array,
sl@0:                  UChar character,
sl@0:                  int8_t value);
sl@0: 
sl@0: /**
sl@0:  * Set new values for a range of Unicode character.
sl@0:  *
sl@0:  * @param array the CompactByteArray to be set
sl@0:  * @param start the starting offset of the range
sl@0:  * @param end the ending offset of the range
sl@0:  * @param value the new mapped value
sl@0:  */
sl@0: U_CAPI  void U_EXPORT2 ucmp8_setRange(CompactByteArray* array, 
sl@0:                   UChar start,
sl@0:                   UChar end, 
sl@0:                   int8_t value);
sl@0: 
sl@0: U_CAPI  int32_t U_EXPORT2 ucmp8_getCount(const CompactByteArray* array);
sl@0: U_CAPI  const int8_t* U_EXPORT2 ucmp8_getArray(const CompactByteArray* array);
sl@0: U_CAPI  const uint16_t* U_EXPORT2 ucmp8_getIndex(const CompactByteArray* array);
sl@0: 
sl@0: /**
sl@0:  * Compact the array.
sl@0:  * The value of cycle determines how large the overlap can be.
sl@0:  * A cycle of 1 is the most compacted, but takes the most time to do.
sl@0:  * If values stored in the array tend to repeat in cycles of, say, 16,
sl@0:  * then using that will be faster than cycle = 1, and get almost the
sl@0:  * same compression.
sl@0:  * @param array The CompactByteArray to be compacted
sl@0:  * @param cycle The value determines how large the overlap can be.
sl@0:  */
sl@0: U_CAPI  void U_EXPORT2 ucmp8_compact(CompactByteArray* array, 
sl@0:                  uint32_t cycle);
sl@0: 
sl@0: /** Expanded takes the array back to a 65536 element array
sl@0:  *  @param array The CompactByteArray to be expanded
sl@0:  */
sl@0: U_CAPI  void U_EXPORT2 ucmp8_expand(CompactByteArray* array);
sl@0: 
sl@0: /** 
sl@0:  * Flatten into a memory structure. Pass in NULL to pre-flight to get the required size.
sl@0:  * @internal
sl@0:  */
sl@0: U_CAPI  uint32_t U_EXPORT2 ucmp8_flattenMem(const CompactByteArray* array, uint8_t *MS);
sl@0: 
sl@0: /* initializes an existing CBA from memory.  Will cause ucmp8_close() to not deallocate anything. */
sl@0: U_CAPI  void U_EXPORT2 ucmp8_initFromData(CompactByteArray* array, const uint8_t **source, UErrorCode *status);
sl@0: 
sl@0: #endif
sl@0: