1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/*
*******************************************************************************
*
* Copyright (C) 2002, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: propsvec.c
* encoding: US-ASCII
* tab size: 8 (not used)
* indentation:4
*
* created on: 2002feb22
* created by: Markus W. Scherer
*
* Store additional Unicode character properties in bit set vectors.
*/
#include <stdlib.h>
#include "unicode/utypes.h"
#include "cmemory.h"
#include "utrie.h"
#include "uprops.h"
#include "propsvec.h"
static uint32_t *
_findRow(uint32_t *pv, uint32_t rangeStart) {
uint32_t *row;
int32_t columns, i, start, limit;
columns=(int32_t)pv[UPVEC_COLUMNS];
limit=(int32_t)pv[UPVEC_ROWS];
pv+=UPVEC_HEADER_LENGTH;
/* do a binary search for the start of the range */
start=0;
while(start<limit-1) {
i=(start+limit)/2;
row=pv+i*columns;
if(rangeStart<row[0]) {
limit=i;
} else if(rangeStart<row[1]) {
return row;
} else {
start=i;
}
}
/* must be found because all ranges together always cover all of Unicode */
return pv+start*columns;
}
U_CFUNC uint32_t *
upvec_open(int32_t columns, int32_t maxRows) {
uint32_t *pv, *row;
int32_t length;
if(columns<1 || maxRows<1) {
return NULL;
}
columns+=2; /* count range start and limit columns */
length=UPVEC_HEADER_LENGTH+maxRows*columns;
pv=(uint32_t *)uprv_malloc(length*4);
if(pv!=NULL) {
/* set header */
pv[UPVEC_COLUMNS]=(uint32_t)columns;
pv[UPVEC_MAXROWS]=(uint32_t)maxRows;
pv[UPVEC_ROWS]=1;
pv[UPVEC_RESERVED]=0;
/* set initial row */
row=pv+UPVEC_HEADER_LENGTH;
*row++=0;
*row++=0x110000;
columns-=2;
do {
*row++=0;
} while(--columns>0);
}
return pv;
}
U_CFUNC void
upvec_close(uint32_t *pv) {
if(pv==NULL) {
uprv_free(pv);
}
}
U_CFUNC UBool
upvec_setValue(uint32_t *pv,
uint32_t start, uint32_t limit,
int32_t column,
uint32_t value, uint32_t mask,
UErrorCode *pErrorCode) {
uint32_t *firstRow, *lastRow;
int32_t columns;
UBool splitFirstRow, splitLastRow;
/* argument checking */
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return FALSE;
}
if( pv==NULL ||
start>limit || limit>0x110000 ||
column<0 || (uint32_t)(column+1)>=pv[UPVEC_COLUMNS]
) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return FALSE;
}
if(start==limit) {
/* empty range, nothing to do */
return TRUE;
}
/* initialize */
columns=pv[UPVEC_COLUMNS];
column+=2; /* skip range start and limit columns */
value&=mask;
/* find the rows whose ranges overlap with the input range */
/* find the first row, always successful */
firstRow=_findRow(pv, start);
/* find the last row, always successful */
lastRow=firstRow;
while(limit>lastRow[1]) {
lastRow+=columns;
}
/*
* Rows need to be split if they partially overlap with the
* input range (only possible for the first and last rows)
* and if their value differs from the input value.
*/
splitFirstRow= (UBool)(start!=firstRow[0] && value!=(firstRow[column]&mask));
splitLastRow= (UBool)(limit!=lastRow[1] && value!=(lastRow[column]&mask));
/* split first/last rows if necessary */
if(splitFirstRow || splitLastRow) {
int32_t count, rows;
rows=(int32_t)pv[UPVEC_ROWS];
if((rows+splitFirstRow+splitLastRow)>(int32_t)pv[UPVEC_MAXROWS]) {
*pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR;
return FALSE;
}
/* count the number of row cells to move after the last row, and move them */
count=(pv+UPVEC_HEADER_LENGTH+rows*columns)-(lastRow+columns);
if(count>0) {
uprv_memmove(
lastRow+(1+splitFirstRow+splitLastRow)*columns,
lastRow+columns,
count*4);
}
pv[UPVEC_ROWS]=rows+splitFirstRow+splitLastRow;
/* split the first row, and move the firstRow pointer to the second part */
if(splitFirstRow) {
/* copy all affected rows up one and move the lastRow pointer */
count=(lastRow-firstRow)+columns;
uprv_memmove(firstRow+columns, firstRow, count*4);
lastRow+=columns;
/* split the range and move the firstRow pointer */
firstRow[1]=firstRow[columns]=start;
firstRow+=columns;
}
/* split the last row */
if(splitLastRow) {
/* copy the last row data */
uprv_memcpy(lastRow+columns, lastRow, columns*4);
/* split the range and move the firstRow pointer */
lastRow[1]=lastRow[columns]=limit;
}
}
/* set the input value in all remaining rows */
firstRow+=column;
lastRow+=column;
mask=~mask;
for(;;) {
*firstRow=(*firstRow&mask)|value;
if(firstRow==lastRow) {
break;
}
firstRow+=columns;
}
return TRUE;
}
U_CFUNC uint32_t *
upvec_getRow(uint32_t *pv, int32_t rowIndex,
uint32_t *pRangeStart, uint32_t *pRangeLimit) {
uint32_t *row;
int32_t columns;
if(pv==NULL || rowIndex<0 || rowIndex>=(int32_t)pv[UPVEC_ROWS]) {
return NULL;
}
columns=pv[UPVEC_COLUMNS];
row=pv+UPVEC_HEADER_LENGTH+rowIndex*columns;
if(pRangeStart!=NULL) {
*pRangeStart=row[0];
}
if(pRangeLimit!=NULL) {
*pRangeLimit=row[1];
}
return row+2;
}
static int
upvec_compareRows(const void *l, const void *r) {
const uint32_t *left=(const uint32_t *)l, *right=(const uint32_t *)r;
int32_t i, count, columns;
count=columns=2+UPROPS_VECTOR_WORDS;
/* start comparing after start/limit but wrap around to them */
i=2;
do {
if(left[i]!=right[i]) {
return left[i]<right[i] ? -1 : 1;
}
if(++i==columns) {
i=0;
}
} while(--count>0);
return 0;
}
U_CFUNC int32_t
upvec_toTrie(uint32_t *pv, UNewTrie *trie, UErrorCode *pErrorCode) {
uint32_t *row;
int32_t columns, valueColumns, rows, count;
/* argument checking */
if(pErrorCode==NULL || U_FAILURE(*pErrorCode)) {
return 0;
}
if(pv==NULL || trie==NULL) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
row=pv+UPVEC_HEADER_LENGTH;
columns=(int32_t)pv[UPVEC_COLUMNS];
rows=(int32_t)pv[UPVEC_ROWS];
/* sort the properties vectors to find unique vector values */
if(rows>1) {
qsort(pv+UPVEC_HEADER_LENGTH, rows, columns*4, upvec_compareRows);
}
/*
* Move vector contents up to a contiguous array with only unique
* vector values, and set indexes to those values into the trie.
*
* This destroys the Properties Vector structure and replaces it
* with an array of just vector values.
*/
valueColumns=columns-2; /* not counting start & limit */
count=-valueColumns;
do {
/* add a new values vector if it is different from the current one */
if(count<0 || 0!=uprv_memcmp(row+2, pv+count, valueColumns*4)) {
count+=valueColumns;
uprv_memmove(pv+count, row+2, valueColumns*4);
}
if(count>0 && !utrie_setRange32(trie, (UChar32)row[0], (UChar32)row[1], (uint32_t)count, FALSE)) {
*pErrorCode=U_BUFFER_OVERFLOW_ERROR;
return 0;
}
row+=columns;
} while(--rows>0);
return count+valueColumns;
}
|