Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 2002-2011, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: propsvec.c |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:4 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 2002feb22 |
michael@0 | 14 | * created by: Markus W. Scherer |
michael@0 | 15 | * |
michael@0 | 16 | * Store bits (Unicode character properties) in bit set vectors. |
michael@0 | 17 | */ |
michael@0 | 18 | |
michael@0 | 19 | #include <stdlib.h> |
michael@0 | 20 | #include "unicode/utypes.h" |
michael@0 | 21 | #include "cmemory.h" |
michael@0 | 22 | #include "utrie.h" |
michael@0 | 23 | #include "utrie2.h" |
michael@0 | 24 | #include "uarrsort.h" |
michael@0 | 25 | #include "propsvec.h" |
michael@0 | 26 | #include "uassert.h" |
michael@0 | 27 | |
michael@0 | 28 | struct UPropsVectors { |
michael@0 | 29 | uint32_t *v; |
michael@0 | 30 | int32_t columns; /* number of columns, plus two for start & limit values */ |
michael@0 | 31 | int32_t maxRows; |
michael@0 | 32 | int32_t rows; |
michael@0 | 33 | int32_t prevRow; /* search optimization: remember last row seen */ |
michael@0 | 34 | UBool isCompacted; |
michael@0 | 35 | }; |
michael@0 | 36 | |
michael@0 | 37 | #define UPVEC_INITIAL_ROWS (1<<12) |
michael@0 | 38 | #define UPVEC_MEDIUM_ROWS ((int32_t)1<<16) |
michael@0 | 39 | #define UPVEC_MAX_ROWS (UPVEC_MAX_CP+1) |
michael@0 | 40 | |
michael@0 | 41 | U_CAPI UPropsVectors * U_EXPORT2 |
michael@0 | 42 | upvec_open(int32_t columns, UErrorCode *pErrorCode) { |
michael@0 | 43 | UPropsVectors *pv; |
michael@0 | 44 | uint32_t *v, *row; |
michael@0 | 45 | uint32_t cp; |
michael@0 | 46 | |
michael@0 | 47 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 48 | return NULL; |
michael@0 | 49 | } |
michael@0 | 50 | if(columns<1) { |
michael@0 | 51 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 52 | return NULL; |
michael@0 | 53 | } |
michael@0 | 54 | columns+=2; /* count range start and limit columns */ |
michael@0 | 55 | |
michael@0 | 56 | pv=(UPropsVectors *)uprv_malloc(sizeof(UPropsVectors)); |
michael@0 | 57 | v=(uint32_t *)uprv_malloc(UPVEC_INITIAL_ROWS*columns*4); |
michael@0 | 58 | if(pv==NULL || v==NULL) { |
michael@0 | 59 | uprv_free(pv); |
michael@0 | 60 | uprv_free(v); |
michael@0 | 61 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 62 | return NULL; |
michael@0 | 63 | } |
michael@0 | 64 | uprv_memset(pv, 0, sizeof(UPropsVectors)); |
michael@0 | 65 | pv->v=v; |
michael@0 | 66 | pv->columns=columns; |
michael@0 | 67 | pv->maxRows=UPVEC_INITIAL_ROWS; |
michael@0 | 68 | pv->rows=2+(UPVEC_MAX_CP-UPVEC_FIRST_SPECIAL_CP); |
michael@0 | 69 | |
michael@0 | 70 | /* set the all-Unicode row and the special-value rows */ |
michael@0 | 71 | row=pv->v; |
michael@0 | 72 | uprv_memset(row, 0, pv->rows*columns*4); |
michael@0 | 73 | row[0]=0; |
michael@0 | 74 | row[1]=0x110000; |
michael@0 | 75 | row+=columns; |
michael@0 | 76 | for(cp=UPVEC_FIRST_SPECIAL_CP; cp<=UPVEC_MAX_CP; ++cp) { |
michael@0 | 77 | row[0]=cp; |
michael@0 | 78 | row[1]=cp+1; |
michael@0 | 79 | row+=columns; |
michael@0 | 80 | } |
michael@0 | 81 | return pv; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | U_CAPI void U_EXPORT2 |
michael@0 | 85 | upvec_close(UPropsVectors *pv) { |
michael@0 | 86 | if(pv!=NULL) { |
michael@0 | 87 | uprv_free(pv->v); |
michael@0 | 88 | uprv_free(pv); |
michael@0 | 89 | } |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | static uint32_t * |
michael@0 | 93 | _findRow(UPropsVectors *pv, UChar32 rangeStart) { |
michael@0 | 94 | uint32_t *row; |
michael@0 | 95 | int32_t columns, i, start, limit, prevRow; |
michael@0 | 96 | |
michael@0 | 97 | columns=pv->columns; |
michael@0 | 98 | limit=pv->rows; |
michael@0 | 99 | prevRow=pv->prevRow; |
michael@0 | 100 | |
michael@0 | 101 | /* check the vicinity of the last-seen row (start searching with an unrolled loop) */ |
michael@0 | 102 | row=pv->v+prevRow*columns; |
michael@0 | 103 | if(rangeStart>=(UChar32)row[0]) { |
michael@0 | 104 | if(rangeStart<(UChar32)row[1]) { |
michael@0 | 105 | /* same row as last seen */ |
michael@0 | 106 | return row; |
michael@0 | 107 | } else if(rangeStart<(UChar32)(row+=columns)[1]) { |
michael@0 | 108 | /* next row after the last one */ |
michael@0 | 109 | pv->prevRow=prevRow+1; |
michael@0 | 110 | return row; |
michael@0 | 111 | } else if(rangeStart<(UChar32)(row+=columns)[1]) { |
michael@0 | 112 | /* second row after the last one */ |
michael@0 | 113 | pv->prevRow=prevRow+2; |
michael@0 | 114 | return row; |
michael@0 | 115 | } else if((rangeStart-(UChar32)row[1])<10) { |
michael@0 | 116 | /* we are close, continue looping */ |
michael@0 | 117 | prevRow+=2; |
michael@0 | 118 | do { |
michael@0 | 119 | ++prevRow; |
michael@0 | 120 | row+=columns; |
michael@0 | 121 | } while(rangeStart>=(UChar32)row[1]); |
michael@0 | 122 | pv->prevRow=prevRow; |
michael@0 | 123 | return row; |
michael@0 | 124 | } |
michael@0 | 125 | } else if(rangeStart<(UChar32)pv->v[1]) { |
michael@0 | 126 | /* the very first row */ |
michael@0 | 127 | pv->prevRow=0; |
michael@0 | 128 | return pv->v; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | /* do a binary search for the start of the range */ |
michael@0 | 132 | start=0; |
michael@0 | 133 | while(start<limit-1) { |
michael@0 | 134 | i=(start+limit)/2; |
michael@0 | 135 | row=pv->v+i*columns; |
michael@0 | 136 | if(rangeStart<(UChar32)row[0]) { |
michael@0 | 137 | limit=i; |
michael@0 | 138 | } else if(rangeStart<(UChar32)row[1]) { |
michael@0 | 139 | pv->prevRow=i; |
michael@0 | 140 | return row; |
michael@0 | 141 | } else { |
michael@0 | 142 | start=i; |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | /* must be found because all ranges together always cover all of Unicode */ |
michael@0 | 147 | pv->prevRow=start; |
michael@0 | 148 | return pv->v+start*columns; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | U_CAPI void U_EXPORT2 |
michael@0 | 152 | upvec_setValue(UPropsVectors *pv, |
michael@0 | 153 | UChar32 start, UChar32 end, |
michael@0 | 154 | int32_t column, |
michael@0 | 155 | uint32_t value, uint32_t mask, |
michael@0 | 156 | UErrorCode *pErrorCode) { |
michael@0 | 157 | uint32_t *firstRow, *lastRow; |
michael@0 | 158 | int32_t columns; |
michael@0 | 159 | UChar32 limit; |
michael@0 | 160 | UBool splitFirstRow, splitLastRow; |
michael@0 | 161 | |
michael@0 | 162 | /* argument checking */ |
michael@0 | 163 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 164 | return; |
michael@0 | 165 | } |
michael@0 | 166 | if( pv==NULL || |
michael@0 | 167 | start<0 || start>end || end>UPVEC_MAX_CP || |
michael@0 | 168 | column<0 || column>=(pv->columns-2) |
michael@0 | 169 | ) { |
michael@0 | 170 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 171 | return; |
michael@0 | 172 | } |
michael@0 | 173 | if(pv->isCompacted) { |
michael@0 | 174 | *pErrorCode=U_NO_WRITE_PERMISSION; |
michael@0 | 175 | return; |
michael@0 | 176 | } |
michael@0 | 177 | limit=end+1; |
michael@0 | 178 | |
michael@0 | 179 | /* initialize */ |
michael@0 | 180 | columns=pv->columns; |
michael@0 | 181 | column+=2; /* skip range start and limit columns */ |
michael@0 | 182 | value&=mask; |
michael@0 | 183 | |
michael@0 | 184 | /* find the rows whose ranges overlap with the input range */ |
michael@0 | 185 | |
michael@0 | 186 | /* find the first and last rows, always successful */ |
michael@0 | 187 | firstRow=_findRow(pv, start); |
michael@0 | 188 | lastRow=_findRow(pv, end); |
michael@0 | 189 | |
michael@0 | 190 | /* |
michael@0 | 191 | * Rows need to be split if they partially overlap with the |
michael@0 | 192 | * input range (only possible for the first and last rows) |
michael@0 | 193 | * and if their value differs from the input value. |
michael@0 | 194 | */ |
michael@0 | 195 | splitFirstRow= (UBool)(start!=(UChar32)firstRow[0] && value!=(firstRow[column]&mask)); |
michael@0 | 196 | splitLastRow= (UBool)(limit!=(UChar32)lastRow[1] && value!=(lastRow[column]&mask)); |
michael@0 | 197 | |
michael@0 | 198 | /* split first/last rows if necessary */ |
michael@0 | 199 | if(splitFirstRow || splitLastRow) { |
michael@0 | 200 | int32_t count, rows; |
michael@0 | 201 | |
michael@0 | 202 | rows=pv->rows; |
michael@0 | 203 | if((rows+splitFirstRow+splitLastRow)>pv->maxRows) { |
michael@0 | 204 | uint32_t *newVectors; |
michael@0 | 205 | int32_t newMaxRows; |
michael@0 | 206 | |
michael@0 | 207 | if(pv->maxRows<UPVEC_MEDIUM_ROWS) { |
michael@0 | 208 | newMaxRows=UPVEC_MEDIUM_ROWS; |
michael@0 | 209 | } else if(pv->maxRows<UPVEC_MAX_ROWS) { |
michael@0 | 210 | newMaxRows=UPVEC_MAX_ROWS; |
michael@0 | 211 | } else { |
michael@0 | 212 | /* Implementation bug, or UPVEC_MAX_ROWS too low. */ |
michael@0 | 213 | *pErrorCode=U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 214 | return; |
michael@0 | 215 | } |
michael@0 | 216 | newVectors=(uint32_t *)uprv_malloc(newMaxRows*columns*4); |
michael@0 | 217 | if(newVectors==NULL) { |
michael@0 | 218 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 219 | return; |
michael@0 | 220 | } |
michael@0 | 221 | uprv_memcpy(newVectors, pv->v, rows*columns*4); |
michael@0 | 222 | firstRow=newVectors+(firstRow-pv->v); |
michael@0 | 223 | lastRow=newVectors+(lastRow-pv->v); |
michael@0 | 224 | uprv_free(pv->v); |
michael@0 | 225 | pv->v=newVectors; |
michael@0 | 226 | pv->maxRows=newMaxRows; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | /* count the number of row cells to move after the last row, and move them */ |
michael@0 | 230 | count = (int32_t)((pv->v+rows*columns)-(lastRow+columns)); |
michael@0 | 231 | if(count>0) { |
michael@0 | 232 | uprv_memmove( |
michael@0 | 233 | lastRow+(1+splitFirstRow+splitLastRow)*columns, |
michael@0 | 234 | lastRow+columns, |
michael@0 | 235 | count*4); |
michael@0 | 236 | } |
michael@0 | 237 | pv->rows=rows+splitFirstRow+splitLastRow; |
michael@0 | 238 | |
michael@0 | 239 | /* split the first row, and move the firstRow pointer to the second part */ |
michael@0 | 240 | if(splitFirstRow) { |
michael@0 | 241 | /* copy all affected rows up one and move the lastRow pointer */ |
michael@0 | 242 | count = (int32_t)((lastRow-firstRow)+columns); |
michael@0 | 243 | uprv_memmove(firstRow+columns, firstRow, count*4); |
michael@0 | 244 | lastRow+=columns; |
michael@0 | 245 | |
michael@0 | 246 | /* split the range and move the firstRow pointer */ |
michael@0 | 247 | firstRow[1]=firstRow[columns]=(uint32_t)start; |
michael@0 | 248 | firstRow+=columns; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | /* split the last row */ |
michael@0 | 252 | if(splitLastRow) { |
michael@0 | 253 | /* copy the last row data */ |
michael@0 | 254 | uprv_memcpy(lastRow+columns, lastRow, columns*4); |
michael@0 | 255 | |
michael@0 | 256 | /* split the range and move the firstRow pointer */ |
michael@0 | 257 | lastRow[1]=lastRow[columns]=(uint32_t)limit; |
michael@0 | 258 | } |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | /* set the "row last seen" to the last row for the range */ |
michael@0 | 262 | pv->prevRow=(int32_t)((lastRow-(pv->v))/columns); |
michael@0 | 263 | |
michael@0 | 264 | /* set the input value in all remaining rows */ |
michael@0 | 265 | firstRow+=column; |
michael@0 | 266 | lastRow+=column; |
michael@0 | 267 | mask=~mask; |
michael@0 | 268 | for(;;) { |
michael@0 | 269 | *firstRow=(*firstRow&mask)|value; |
michael@0 | 270 | if(firstRow==lastRow) { |
michael@0 | 271 | break; |
michael@0 | 272 | } |
michael@0 | 273 | firstRow+=columns; |
michael@0 | 274 | } |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | U_CAPI uint32_t U_EXPORT2 |
michael@0 | 278 | upvec_getValue(const UPropsVectors *pv, UChar32 c, int32_t column) { |
michael@0 | 279 | uint32_t *row; |
michael@0 | 280 | UPropsVectors *ncpv; |
michael@0 | 281 | |
michael@0 | 282 | if(pv->isCompacted || c<0 || c>UPVEC_MAX_CP || column<0 || column>=(pv->columns-2)) { |
michael@0 | 283 | return 0; |
michael@0 | 284 | } |
michael@0 | 285 | ncpv=(UPropsVectors *)pv; |
michael@0 | 286 | row=_findRow(ncpv, c); |
michael@0 | 287 | return row[2+column]; |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | U_CAPI uint32_t * U_EXPORT2 |
michael@0 | 291 | upvec_getRow(const UPropsVectors *pv, int32_t rowIndex, |
michael@0 | 292 | UChar32 *pRangeStart, UChar32 *pRangeEnd) { |
michael@0 | 293 | uint32_t *row; |
michael@0 | 294 | int32_t columns; |
michael@0 | 295 | |
michael@0 | 296 | if(pv->isCompacted || rowIndex<0 || rowIndex>=pv->rows) { |
michael@0 | 297 | return NULL; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | columns=pv->columns; |
michael@0 | 301 | row=pv->v+rowIndex*columns; |
michael@0 | 302 | if(pRangeStart!=NULL) { |
michael@0 | 303 | *pRangeStart=(UChar32)row[0]; |
michael@0 | 304 | } |
michael@0 | 305 | if(pRangeEnd!=NULL) { |
michael@0 | 306 | *pRangeEnd=(UChar32)row[1]-1; |
michael@0 | 307 | } |
michael@0 | 308 | return row+2; |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | static int32_t U_CALLCONV |
michael@0 | 312 | upvec_compareRows(const void *context, const void *l, const void *r) { |
michael@0 | 313 | const uint32_t *left=(const uint32_t *)l, *right=(const uint32_t *)r; |
michael@0 | 314 | const UPropsVectors *pv=(const UPropsVectors *)context; |
michael@0 | 315 | int32_t i, count, columns; |
michael@0 | 316 | |
michael@0 | 317 | count=columns=pv->columns; /* includes start/limit columns */ |
michael@0 | 318 | |
michael@0 | 319 | /* start comparing after start/limit but wrap around to them */ |
michael@0 | 320 | i=2; |
michael@0 | 321 | do { |
michael@0 | 322 | if(left[i]!=right[i]) { |
michael@0 | 323 | return left[i]<right[i] ? -1 : 1; |
michael@0 | 324 | } |
michael@0 | 325 | if(++i==columns) { |
michael@0 | 326 | i=0; |
michael@0 | 327 | } |
michael@0 | 328 | } while(--count>0); |
michael@0 | 329 | |
michael@0 | 330 | return 0; |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | U_CAPI void U_EXPORT2 |
michael@0 | 334 | upvec_compact(UPropsVectors *pv, UPVecCompactHandler *handler, void *context, UErrorCode *pErrorCode) { |
michael@0 | 335 | uint32_t *row; |
michael@0 | 336 | int32_t i, columns, valueColumns, rows, count; |
michael@0 | 337 | UChar32 start, limit; |
michael@0 | 338 | |
michael@0 | 339 | /* argument checking */ |
michael@0 | 340 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 341 | return; |
michael@0 | 342 | } |
michael@0 | 343 | if(handler==NULL) { |
michael@0 | 344 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 345 | return; |
michael@0 | 346 | } |
michael@0 | 347 | if(pv->isCompacted) { |
michael@0 | 348 | return; |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | /* Set the flag now: Sorting and compacting destroys the builder data structure. */ |
michael@0 | 352 | pv->isCompacted=TRUE; |
michael@0 | 353 | |
michael@0 | 354 | rows=pv->rows; |
michael@0 | 355 | columns=pv->columns; |
michael@0 | 356 | U_ASSERT(columns>=3); /* upvec_open asserts this */ |
michael@0 | 357 | valueColumns=columns-2; /* not counting start & limit */ |
michael@0 | 358 | |
michael@0 | 359 | /* sort the properties vectors to find unique vector values */ |
michael@0 | 360 | uprv_sortArray(pv->v, rows, columns*4, |
michael@0 | 361 | upvec_compareRows, pv, FALSE, pErrorCode); |
michael@0 | 362 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 363 | return; |
michael@0 | 364 | } |
michael@0 | 365 | |
michael@0 | 366 | /* |
michael@0 | 367 | * Find and set the special values. |
michael@0 | 368 | * This has to do almost the same work as the compaction below, |
michael@0 | 369 | * to find the indexes where the special-value rows will move. |
michael@0 | 370 | */ |
michael@0 | 371 | row=pv->v; |
michael@0 | 372 | count=-valueColumns; |
michael@0 | 373 | for(i=0; i<rows; ++i) { |
michael@0 | 374 | start=(UChar32)row[0]; |
michael@0 | 375 | |
michael@0 | 376 | /* count a new values vector if it is different from the current one */ |
michael@0 | 377 | if(count<0 || 0!=uprv_memcmp(row+2, row-valueColumns, valueColumns*4)) { |
michael@0 | 378 | count+=valueColumns; |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | if(start>=UPVEC_FIRST_SPECIAL_CP) { |
michael@0 | 382 | handler(context, start, start, count, row+2, valueColumns, pErrorCode); |
michael@0 | 383 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 384 | return; |
michael@0 | 385 | } |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | row+=columns; |
michael@0 | 389 | } |
michael@0 | 390 | |
michael@0 | 391 | /* count is at the beginning of the last vector, add valueColumns to include that last vector */ |
michael@0 | 392 | count+=valueColumns; |
michael@0 | 393 | |
michael@0 | 394 | /* Call the handler once more to signal the start of delivering real values. */ |
michael@0 | 395 | handler(context, UPVEC_START_REAL_VALUES_CP, UPVEC_START_REAL_VALUES_CP, |
michael@0 | 396 | count, row-valueColumns, valueColumns, pErrorCode); |
michael@0 | 397 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 398 | return; |
michael@0 | 399 | } |
michael@0 | 400 | |
michael@0 | 401 | /* |
michael@0 | 402 | * Move vector contents up to a contiguous array with only unique |
michael@0 | 403 | * vector values, and call the handler function for each vector. |
michael@0 | 404 | * |
michael@0 | 405 | * This destroys the Properties Vector structure and replaces it |
michael@0 | 406 | * with an array of just vector values. |
michael@0 | 407 | */ |
michael@0 | 408 | row=pv->v; |
michael@0 | 409 | count=-valueColumns; |
michael@0 | 410 | for(i=0; i<rows; ++i) { |
michael@0 | 411 | /* fetch these first before memmove() may overwrite them */ |
michael@0 | 412 | start=(UChar32)row[0]; |
michael@0 | 413 | limit=(UChar32)row[1]; |
michael@0 | 414 | |
michael@0 | 415 | /* add a new values vector if it is different from the current one */ |
michael@0 | 416 | if(count<0 || 0!=uprv_memcmp(row+2, pv->v+count, valueColumns*4)) { |
michael@0 | 417 | count+=valueColumns; |
michael@0 | 418 | uprv_memmove(pv->v+count, row+2, valueColumns*4); |
michael@0 | 419 | } |
michael@0 | 420 | |
michael@0 | 421 | if(start<UPVEC_FIRST_SPECIAL_CP) { |
michael@0 | 422 | handler(context, start, limit-1, count, pv->v+count, valueColumns, pErrorCode); |
michael@0 | 423 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 424 | return; |
michael@0 | 425 | } |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | row+=columns; |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | /* count is at the beginning of the last vector, add one to include that last vector */ |
michael@0 | 432 | pv->rows=count/valueColumns+1; |
michael@0 | 433 | } |
michael@0 | 434 | |
michael@0 | 435 | U_CAPI const uint32_t * U_EXPORT2 |
michael@0 | 436 | upvec_getArray(const UPropsVectors *pv, int32_t *pRows, int32_t *pColumns) { |
michael@0 | 437 | if(!pv->isCompacted) { |
michael@0 | 438 | return NULL; |
michael@0 | 439 | } |
michael@0 | 440 | if(pRows!=NULL) { |
michael@0 | 441 | *pRows=pv->rows; |
michael@0 | 442 | } |
michael@0 | 443 | if(pColumns!=NULL) { |
michael@0 | 444 | *pColumns=pv->columns-2; |
michael@0 | 445 | } |
michael@0 | 446 | return pv->v; |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | U_CAPI uint32_t * U_EXPORT2 |
michael@0 | 450 | upvec_cloneArray(const UPropsVectors *pv, |
michael@0 | 451 | int32_t *pRows, int32_t *pColumns, UErrorCode *pErrorCode) { |
michael@0 | 452 | uint32_t *clonedArray; |
michael@0 | 453 | int32_t byteLength; |
michael@0 | 454 | |
michael@0 | 455 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 456 | return NULL; |
michael@0 | 457 | } |
michael@0 | 458 | if(!pv->isCompacted) { |
michael@0 | 459 | *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 460 | return NULL; |
michael@0 | 461 | } |
michael@0 | 462 | byteLength=pv->rows*(pv->columns-2)*4; |
michael@0 | 463 | clonedArray=(uint32_t *)uprv_malloc(byteLength); |
michael@0 | 464 | if(clonedArray==NULL) { |
michael@0 | 465 | *pErrorCode=U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 466 | return NULL; |
michael@0 | 467 | } |
michael@0 | 468 | uprv_memcpy(clonedArray, pv->v, byteLength); |
michael@0 | 469 | if(pRows!=NULL) { |
michael@0 | 470 | *pRows=pv->rows; |
michael@0 | 471 | } |
michael@0 | 472 | if(pColumns!=NULL) { |
michael@0 | 473 | *pColumns=pv->columns-2; |
michael@0 | 474 | } |
michael@0 | 475 | return clonedArray; |
michael@0 | 476 | } |
michael@0 | 477 | |
michael@0 | 478 | U_CAPI UTrie2 * U_EXPORT2 |
michael@0 | 479 | upvec_compactToUTrie2WithRowIndexes(UPropsVectors *pv, UErrorCode *pErrorCode) { |
michael@0 | 480 | UPVecToUTrie2Context toUTrie2={ NULL }; |
michael@0 | 481 | upvec_compact(pv, upvec_compactToUTrie2Handler, &toUTrie2, pErrorCode); |
michael@0 | 482 | utrie2_freeze(toUTrie2.trie, UTRIE2_16_VALUE_BITS, pErrorCode); |
michael@0 | 483 | if(U_FAILURE(*pErrorCode)) { |
michael@0 | 484 | utrie2_close(toUTrie2.trie); |
michael@0 | 485 | toUTrie2.trie=NULL; |
michael@0 | 486 | } |
michael@0 | 487 | return toUTrie2.trie; |
michael@0 | 488 | } |
michael@0 | 489 | |
michael@0 | 490 | /* |
michael@0 | 491 | * TODO(markus): Add upvec_16BitsToUTrie2() function that enumerates all rows, extracts |
michael@0 | 492 | * some 16-bit field and builds and returns a UTrie2. |
michael@0 | 493 | */ |
michael@0 | 494 | |
michael@0 | 495 | U_CAPI void U_CALLCONV |
michael@0 | 496 | upvec_compactToUTrie2Handler(void *context, |
michael@0 | 497 | UChar32 start, UChar32 end, |
michael@0 | 498 | int32_t rowIndex, uint32_t *row, int32_t columns, |
michael@0 | 499 | UErrorCode *pErrorCode) { |
michael@0 | 500 | UPVecToUTrie2Context *toUTrie2=(UPVecToUTrie2Context *)context; |
michael@0 | 501 | if(start<UPVEC_FIRST_SPECIAL_CP) { |
michael@0 | 502 | utrie2_setRange32(toUTrie2->trie, start, end, (uint32_t)rowIndex, TRUE, pErrorCode); |
michael@0 | 503 | } else { |
michael@0 | 504 | switch(start) { |
michael@0 | 505 | case UPVEC_INITIAL_VALUE_CP: |
michael@0 | 506 | toUTrie2->initialValue=rowIndex; |
michael@0 | 507 | break; |
michael@0 | 508 | case UPVEC_ERROR_VALUE_CP: |
michael@0 | 509 | toUTrie2->errorValue=rowIndex; |
michael@0 | 510 | break; |
michael@0 | 511 | case UPVEC_START_REAL_VALUES_CP: |
michael@0 | 512 | toUTrie2->maxValue=rowIndex; |
michael@0 | 513 | if(rowIndex>0xffff) { |
michael@0 | 514 | /* too many rows for a 16-bit trie */ |
michael@0 | 515 | *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; |
michael@0 | 516 | } else { |
michael@0 | 517 | toUTrie2->trie=utrie2_open(toUTrie2->initialValue, |
michael@0 | 518 | toUTrie2->errorValue, pErrorCode); |
michael@0 | 519 | } |
michael@0 | 520 | break; |
michael@0 | 521 | default: |
michael@0 | 522 | break; |
michael@0 | 523 | } |
michael@0 | 524 | } |
michael@0 | 525 | } |