| 1 |
/* |
| 2 |
Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com) |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
|
| 16 |
For more information please visit: http://bitmagic.io |
| 17 |
*/ |
| 18 |
#include "bmserial.h" |
| 19 |
#include "bmalgo.h" |
| 20 |
|
| 21 |
|
| 22 |
typedef bm::bvector<libbm::standard_allocator>::enumerator TBM_bvector_enumerator; |
| 23 |
|
| 24 |
#define BM_CATCH_ALL \ |
| 25 |
CATCH (BM_ERR_BADALLOC) { return BM_ERR_BADALLOC; } \ |
| 26 |
CATCH (BM_ERR_BADARG) { return BM_ERR_BADARG; } \ |
| 27 |
CATCH (BM_ERR_RANGE) { return BM_ERR_RANGE; } \ |
| 28 |
CATCH (BM_ERR_CPU) { return BM_ERR_CPU; } \ |
| 29 |
CATCH (BM_ERR_SERIALFORMAT) { return BM_ERR_SERIALFORMAT; } |
| 30 |
|
| 31 |
|
| 32 |
// ------------------------------------------------------------------- |
| 33 |
// |
| 34 |
// code credit for CPU caps identification: |
| 35 |
// https://attractivechaos.wordpress.com/2017/09/04/on-cpu-dispatch/ |
| 36 |
// |
| 37 |
// ------------------------------------------------------------------- |
| 38 |
|
| 39 |
#define SIMD_SSE 0x1 |
| 40 |
#define SIMD_SSE2 0x2 |
| 41 |
#define SIMD_SSE3 0x4 |
| 42 |
#define SIMD_SSE4_1 0x8 |
| 43 |
#define SIMD_SSE4_2 0x10 |
| 44 |
#define SIMD_AVX 0x20 |
| 45 |
#define SIMD_AVX2 0x40 |
| 46 |
#define SIMD_AVX512F 0x80 |
| 47 |
|
| 48 |
static |
| 49 |
unsigned x86_simd(void) |
| 50 |
{ |
| 51 |
unsigned flag = 0; |
| 52 |
|
| 53 |
#ifdef BM_x86 |
| 54 |
unsigned eax, ebx, ecx, edx; |
| 55 |
#ifdef _MSC_VER |
| 56 |
int cpuid[4]; |
| 57 |
__cpuid(cpuid, 1); |
| 58 |
eax = cpuid[0], ebx = cpuid[1], ecx = cpuid[2], edx = cpuid[3]; |
| 59 |
#else |
| 60 |
asm volatile("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (1)); |
| 61 |
#endif // _MSC_VER |
| 62 |
if (edx>>25&1) flag |= SIMD_SSE; |
| 63 |
if (edx>>26&1) flag |= SIMD_SSE2; |
| 64 |
if (ecx>>0 &1) flag |= SIMD_SSE3; |
| 65 |
if (ecx>>19&1) flag |= SIMD_SSE4_1; |
| 66 |
if (ecx>>20&1) flag |= SIMD_SSE4_2; |
| 67 |
if (ecx>>28&1) flag |= SIMD_AVX; |
| 68 |
if (ebx>>5 &1) flag |= SIMD_AVX2; |
| 69 |
if (ebx>>16&1) flag |= SIMD_AVX512F; |
| 70 |
#endif // BM_x86 |
| 71 |
|
| 72 |
return flag; |
| 73 |
} |
| 74 |
|
| 75 |
// ----------------------------------------------------------------- |
| 76 |
|
| 77 |
int BM_init(void*) |
| 78 |
{ |
| 79 |
int bm_simd; |
| 80 |
unsigned cpu_simd; |
| 81 |
|
| 82 |
bm_simd = BM_simd_version(); |
| 83 |
|
| 84 |
if (bm_simd != BM_SIMD_NO) // BM compiled for some SIMD optimization |
| 85 |
{ |
| 86 |
cpu_simd = x86_simd(); |
| 87 |
switch (bm_simd) |
| 88 |
{ |
| 89 |
case BM_SIMD_NO: |
| 90 |
break; |
| 91 |
case BM_SIMD_SSE2: |
| 92 |
if (!(cpu_simd & SIMD_SSE2)) |
| 93 |
return BM_ERR_CPU; |
| 94 |
break; |
| 95 |
case BM_SIMD_SSE42: |
| 96 |
if (!(cpu_simd & SIMD_SSE4_2)) |
| 97 |
return BM_ERR_CPU; |
| 98 |
break; |
| 99 |
case BM_SIMD_AVX2: |
| 100 |
if (!(cpu_simd & SIMD_AVX2)) |
| 101 |
return BM_ERR_CPU; |
| 102 |
break; |
| 103 |
default: |
| 104 |
return BM_ERR_CPU; // ?! |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return BM_OK; |
| 109 |
} |
| 110 |
|
| 111 |
// ----------------------------------------------------------------- |
| 112 |
|
| 113 |
int BM_simd_version(void) |
| 114 |
{ |
| 115 |
int ret = bm::simd_version(); |
| 116 |
return ret; |
| 117 |
} |
| 118 |
|
| 119 |
// ----------------------------------------------------------------- |
| 120 |
|
| 121 |
const char* BM_version(int* major, int* minor, int* patch) |
| 122 |
{ |
| 123 |
if (major) |
| 124 |
*major = bm::_copyright<true>::_v[0]; |
| 125 |
if (minor) |
| 126 |
*minor = bm::_copyright<true>::_v[1]; |
| 127 |
if (patch) |
| 128 |
*patch = bm::_copyright<true>::_v[2]; |
| 129 |
|
| 130 |
return bm::_copyright<true>::_p; |
| 131 |
} |
| 132 |
|
| 133 |
// ----------------------------------------------------------------- |
| 134 |
|
| 135 |
const char* BM_error_msg(int errcode) |
| 136 |
{ |
| 137 |
switch (errcode) |
| 138 |
{ |
| 139 |
case BM_OK: |
| 140 |
return BM_OK_MSG; |
| 141 |
case BM_ERR_BADALLOC: |
| 142 |
return BM_ERR_BADALLOC_MSG; |
| 143 |
case BM_ERR_BADARG: |
| 144 |
return BM_ERR_BADARG_MSG; |
| 145 |
case BM_ERR_RANGE: |
| 146 |
return BM_ERR_RANGE_MSG; |
| 147 |
case BM_ERR_CPU: |
| 148 |
return BM_ERR_CPU_MSG; |
| 149 |
case BM_ERR_SERIALFORMAT: |
| 150 |
return BM_ERR_SERIALFORMAT_MSG; |
| 151 |
} |
| 152 |
return BM_UNK_MSG; |
| 153 |
} |
| 154 |
|
| 155 |
// ----------------------------------------------------------------- |
| 156 |
|
| 157 |
|
| 158 |
int BM_bvector_construct(BM_BVHANDLE* h, unsigned int bv_max) |
| 159 |
{ |
| 160 |
if (h == 0) |
| 161 |
return BM_ERR_BADARG; |
| 162 |
BM_TRY |
| 163 |
{ |
| 164 |
void* mem = ::malloc(sizeof(TBM_bvector)); |
| 165 |
if (mem == 0) |
| 166 |
{ |
| 167 |
*h = 0; |
| 168 |
return BM_ERR_BADALLOC; |
| 169 |
} |
| 170 |
if (bv_max == 0) |
| 171 |
{ |
| 172 |
bv_max = bm::id_max; |
| 173 |
} |
| 174 |
// placement new just to call the constructor |
| 175 |
TBM_bvector* bv = new(mem) TBM_bvector(bm::BM_BIT, |
| 176 |
bm::gap_len_table<true>::_len, |
| 177 |
bv_max, |
| 178 |
TBM_Alloc()); |
| 179 |
*h = bv; |
| 180 |
} |
| 181 |
CATCH (BM_ERR_BADALLOC) |
| 182 |
{ |
| 183 |
*h = 0; |
| 184 |
return BM_ERR_BADALLOC; |
| 185 |
} |
| 186 |
ETRY; |
| 187 |
|
| 188 |
return BM_OK; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
// ----------------------------------------------------------------- |
| 194 |
|
| 195 |
int BM_bvector_init(BM_BVHANDLE h) |
| 196 |
{ |
| 197 |
if (!h) |
| 198 |
return BM_ERR_BADARG; |
| 199 |
BM_TRY |
| 200 |
{ |
| 201 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 202 |
bv->init(); |
| 203 |
} |
| 204 |
BM_CATCH_ALL |
| 205 |
ETRY; |
| 206 |
return BM_OK; |
| 207 |
} |
| 208 |
|
| 209 |
// ----------------------------------------------------------------- |
| 210 |
|
| 211 |
int BM_bvector_construct_copy(BM_BVHANDLE* h, BM_BVHANDLE hfrom) |
| 212 |
{ |
| 213 |
if (h == 0 || !hfrom) |
| 214 |
return BM_ERR_BADARG; |
| 215 |
BM_TRY |
| 216 |
{ |
| 217 |
void* mem = ::malloc(sizeof(TBM_bvector)); |
| 218 |
if (mem == 0) |
| 219 |
{ |
| 220 |
*h = 0; |
| 221 |
return BM_ERR_BADALLOC; |
| 222 |
} |
| 223 |
|
| 224 |
const TBM_bvector* bv_from = (TBM_bvector*)hfrom; |
| 225 |
|
| 226 |
// placement new just to call the copy constructor |
| 227 |
TBM_bvector* bv = new(mem) TBM_bvector(*bv_from); |
| 228 |
*h = bv; |
| 229 |
} |
| 230 |
CATCH (BM_ERR_BADALLOC) |
| 231 |
{ |
| 232 |
*h = 0; |
| 233 |
return BM_ERR_BADALLOC; |
| 234 |
} |
| 235 |
ETRY; |
| 236 |
|
| 237 |
return BM_OK; |
| 238 |
} |
| 239 |
|
| 240 |
// ----------------------------------------------------------------- |
| 241 |
|
| 242 |
BM_API_EXPORT int BM_bvector_construct_copy_ro(BM_BVHANDLE* h, BM_BVHANDLE hfrom) |
| 243 |
{ |
| 244 |
if (h == 0 || !hfrom) |
| 245 |
return BM_ERR_BADARG; |
| 246 |
BM_TRY |
| 247 |
{ |
| 248 |
void* mem = ::malloc(sizeof(TBM_bvector)); |
| 249 |
if (mem == 0) |
| 250 |
{ |
| 251 |
*h = 0; |
| 252 |
return BM_ERR_BADALLOC; |
| 253 |
} |
| 254 |
const TBM_bvector* bv_from = (TBM_bvector*)hfrom; |
| 255 |
|
| 256 |
// placement new just to call the constructor |
| 257 |
TBM_bvector* bv = new(mem) TBM_bvector(*bv_from, bm::finalization::READONLY); |
| 258 |
*h = bv; |
| 259 |
} |
| 260 |
CATCH (BM_ERR_BADALLOC) |
| 261 |
{ |
| 262 |
*h = 0; |
| 263 |
return BM_ERR_BADALLOC; |
| 264 |
} |
| 265 |
ETRY; |
| 266 |
return BM_OK; |
| 267 |
} |
| 268 |
|
| 269 |
// ----------------------------------------------------------------- |
| 270 |
|
| 271 |
BM_API_EXPORT int BM_bvector_construct_copy_rw(BM_BVHANDLE* h, BM_BVHANDLE hfrom) |
| 272 |
{ |
| 273 |
if (h == 0 || !hfrom) |
| 274 |
return BM_ERR_BADARG; |
| 275 |
BM_TRY |
| 276 |
{ |
| 277 |
void* mem = ::malloc(sizeof(TBM_bvector)); |
| 278 |
if (mem == 0) |
| 279 |
{ |
| 280 |
*h = 0; |
| 281 |
return BM_ERR_BADALLOC; |
| 282 |
} |
| 283 |
const TBM_bvector* bv_from = (TBM_bvector*)hfrom; |
| 284 |
|
| 285 |
// placement new just to call the constructor |
| 286 |
TBM_bvector* bv = new(mem) TBM_bvector(*bv_from, |
| 287 |
bm::finalization::READWRITE); |
| 288 |
*h = bv; |
| 289 |
} |
| 290 |
CATCH (BM_ERR_BADALLOC) |
| 291 |
{ |
| 292 |
*h = 0; |
| 293 |
return BM_ERR_BADALLOC; |
| 294 |
} |
| 295 |
ETRY; |
| 296 |
return BM_OK; |
| 297 |
} |
| 298 |
|
| 299 |
// ----------------------------------------------------------------- |
| 300 |
|
| 301 |
int BM_bvector_free(BM_BVHANDLE h) |
| 302 |
{ |
| 303 |
if (!h) |
| 304 |
return BM_ERR_BADARG; |
| 305 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 306 |
bv->~TBM_bvector(); |
| 307 |
::free(h); |
| 308 |
|
| 309 |
return BM_OK; |
| 310 |
} |
| 311 |
|
| 312 |
// ----------------------------------------------------------------- |
| 313 |
|
| 314 |
int BM_bvector_swap(BM_BVHANDLE h1, BM_BVHANDLE h2) |
| 315 |
{ |
| 316 |
if (!h1 || !h2) |
| 317 |
return BM_ERR_BADARG; |
| 318 |
BM_TRY |
| 319 |
{ |
| 320 |
TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 321 |
TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 322 |
bv1->swap(*bv2); |
| 323 |
} |
| 324 |
BM_CATCH_ALL |
| 325 |
ETRY; |
| 326 |
return BM_OK; |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
// ----------------------------------------------------------------- |
| 331 |
|
| 332 |
|
| 333 |
int BM_bvector_get_size(BM_BVHANDLE h, unsigned int* psize) |
| 334 |
{ |
| 335 |
if (!h || !psize) |
| 336 |
return BM_ERR_BADARG; |
| 337 |
BM_TRY |
| 338 |
{ |
| 339 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 340 |
*psize = bv->size(); |
| 341 |
} |
| 342 |
BM_CATCH_ALL |
| 343 |
ETRY; |
| 344 |
return BM_OK; |
| 345 |
} |
| 346 |
|
| 347 |
// ----------------------------------------------------------------- |
| 348 |
/* |
| 349 |
int BM_bvector_get_capacity(BM_BVHANDLE h, unsigned int* pcap) |
| 350 |
{ |
| 351 |
if (!h) |
| 352 |
return BM_ERR_BADARG; |
| 353 |
BM_TRY |
| 354 |
{ |
| 355 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 356 |
if (pcap) |
| 357 |
{ |
| 358 |
*pcap = bv->capacity(); |
| 359 |
} |
| 360 |
} |
| 361 |
BM_CATCH_ALL |
| 362 |
ETRY; |
| 363 |
return BM_OK; |
| 364 |
} |
| 365 |
*/ |
| 366 |
// ----------------------------------------------------------------- |
| 367 |
|
| 368 |
|
| 369 |
int BM_bvector_set_size(BM_BVHANDLE h, unsigned int new_size) |
| 370 |
{ |
| 371 |
if (!h) |
| 372 |
return BM_ERR_BADARG; |
| 373 |
BM_TRY |
| 374 |
{ |
| 375 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 376 |
bv->resize(new_size); |
| 377 |
} |
| 378 |
BM_CATCH_ALL |
| 379 |
ETRY; |
| 380 |
return BM_OK; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
// ----------------------------------------------------------------- |
| 385 |
|
| 386 |
int BM_bvector_set_bit(BM_BVHANDLE h, unsigned int i, int val) |
| 387 |
{ |
| 388 |
if (!h) |
| 389 |
return BM_ERR_BADARG; |
| 390 |
BM_TRY |
| 391 |
{ |
| 392 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 393 |
bv->set(i, val); |
| 394 |
} |
| 395 |
BM_CATCH_ALL |
| 396 |
ETRY; |
| 397 |
return BM_OK; |
| 398 |
} |
| 399 |
|
| 400 |
// ----------------------------------------------------------------- |
| 401 |
|
| 402 |
int BM_bvector_set_bits(BM_BVHANDLE h, unsigned int* idx, unsigned int idx_size) |
| 403 |
{ |
| 404 |
if (!h || !idx) |
| 405 |
return BM_ERR_BADARG; |
| 406 |
BM_TRY |
| 407 |
{ |
| 408 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 409 |
bv->set(idx, idx_size); |
| 410 |
} |
| 411 |
BM_CATCH_ALL |
| 412 |
ETRY; |
| 413 |
return BM_OK; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
// ----------------------------------------------------------------- |
| 418 |
|
| 419 |
int BM_bvector_flip_bit(BM_BVHANDLE h, unsigned int i) |
| 420 |
{ |
| 421 |
if (!h) |
| 422 |
return BM_ERR_BADARG; |
| 423 |
BM_TRY |
| 424 |
{ |
| 425 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 426 |
bv->flip(i); |
| 427 |
} |
| 428 |
BM_CATCH_ALL |
| 429 |
ETRY; |
| 430 |
return BM_OK; |
| 431 |
} |
| 432 |
|
| 433 |
// ----------------------------------------------------------------- |
| 434 |
|
| 435 |
int BM_bvector_swap_bits(BM_BVHANDLE h, unsigned int i1, unsigned int i2) |
| 436 |
{ |
| 437 |
if (!h) |
| 438 |
return BM_ERR_BADARG; |
| 439 |
BM_TRY |
| 440 |
{ |
| 441 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 442 |
bv->swap(i1, i2); |
| 443 |
} |
| 444 |
BM_CATCH_ALL |
| 445 |
ETRY; |
| 446 |
return BM_OK; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
// ----------------------------------------------------------------- |
| 451 |
|
| 452 |
int BM_bvector_inc_bit(BM_BVHANDLE h, unsigned int i, int* carry_over) |
| 453 |
{ |
| 454 |
if (!h || !carry_over) |
| 455 |
return BM_ERR_BADARG; |
| 456 |
BM_TRY |
| 457 |
{ |
| 458 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 459 |
*carry_over = bv->inc(i); |
| 460 |
} |
| 461 |
BM_CATCH_ALL |
| 462 |
ETRY; |
| 463 |
|
| 464 |
return BM_OK; |
| 465 |
} |
| 466 |
|
| 467 |
// ----------------------------------------------------------------- |
| 468 |
|
| 469 |
int BM_bvector_set_bit_conditional(BM_BVHANDLE h, |
| 470 |
unsigned int i, |
| 471 |
int val, |
| 472 |
int condition, |
| 473 |
int* pchanged) |
| 474 |
{ |
| 475 |
unsigned int sz; |
| 476 |
if (!h) |
| 477 |
return BM_ERR_BADARG; |
| 478 |
|
| 479 |
BM_TRY |
| 480 |
{ |
| 481 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 482 |
sz = bv->size(); |
| 483 |
if (i >= sz) |
| 484 |
return BM_ERR_RANGE; |
| 485 |
|
| 486 |
bool b_changed = bv->set_bit_conditional(i, val, condition); |
| 487 |
if (pchanged) |
| 488 |
{ |
| 489 |
*pchanged = b_changed; |
| 490 |
} |
| 491 |
} |
| 492 |
BM_CATCH_ALL |
| 493 |
ETRY; |
| 494 |
return BM_OK; |
| 495 |
} |
| 496 |
|
| 497 |
// ----------------------------------------------------------------- |
| 498 |
|
| 499 |
|
| 500 |
int BM_bvector_set_bit_no_check(BM_BVHANDLE h, unsigned int i) |
| 501 |
{ |
| 502 |
if (!h) |
| 503 |
return BM_ERR_BADARG; |
| 504 |
BM_TRY |
| 505 |
{ |
| 506 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 507 |
bv->set_bit_no_check(i); |
| 508 |
} |
| 509 |
BM_CATCH_ALL |
| 510 |
ETRY; |
| 511 |
return BM_OK; |
| 512 |
|
| 513 |
} |
| 514 |
|
| 515 |
// ----------------------------------------------------------------- |
| 516 |
|
| 517 |
int BM_bvector_set(BM_BVHANDLE h) |
| 518 |
{ |
| 519 |
if (!h) |
| 520 |
return BM_ERR_BADARG; |
| 521 |
BM_TRY |
| 522 |
{ |
| 523 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 524 |
bv->set(); |
| 525 |
} |
| 526 |
BM_CATCH_ALL |
| 527 |
ETRY; |
| 528 |
return BM_OK; |
| 529 |
} |
| 530 |
|
| 531 |
// ----------------------------------------------------------------- |
| 532 |
|
| 533 |
int BM_bvector_set_range(BM_BVHANDLE h, |
| 534 |
unsigned int left, |
| 535 |
unsigned int right, |
| 536 |
int value) |
| 537 |
{ |
| 538 |
if (!h) |
| 539 |
return BM_ERR_BADARG; |
| 540 |
if (left > right) |
| 541 |
return BM_ERR_BADARG; |
| 542 |
|
| 543 |
BM_TRY |
| 544 |
{ |
| 545 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 546 |
bv->set_range(left, right, value); |
| 547 |
} |
| 548 |
BM_CATCH_ALL |
| 549 |
ETRY; |
| 550 |
return BM_OK; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
// ----------------------------------------------------------------- |
| 555 |
|
| 556 |
int BM_bvector_clear(BM_BVHANDLE h, int free_mem) |
| 557 |
{ |
| 558 |
if (!h) |
| 559 |
return BM_ERR_BADARG; |
| 560 |
BM_TRY |
| 561 |
{ |
| 562 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 563 |
bv->clear(free_mem); |
| 564 |
} |
| 565 |
BM_CATCH_ALL |
| 566 |
ETRY; |
| 567 |
return BM_OK; |
| 568 |
} |
| 569 |
|
| 570 |
// ----------------------------------------------------------------- |
| 571 |
|
| 572 |
int BM_bvector_invert(BM_BVHANDLE h) |
| 573 |
{ |
| 574 |
if (!h) |
| 575 |
return BM_ERR_BADARG; |
| 576 |
BM_TRY |
| 577 |
{ |
| 578 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 579 |
bv->invert(); |
| 580 |
} |
| 581 |
BM_CATCH_ALL |
| 582 |
ETRY; |
| 583 |
return BM_OK; |
| 584 |
} |
| 585 |
|
| 586 |
// ----------------------------------------------------------------- |
| 587 |
|
| 588 |
int BM_bvector_any(BM_BVHANDLE h, int* pval) |
| 589 |
{ |
| 590 |
if (!h || !pval) |
| 591 |
return BM_ERR_BADARG; |
| 592 |
BM_TRY |
| 593 |
{ |
| 594 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 595 |
*pval = bv->any(); |
| 596 |
} |
| 597 |
BM_CATCH_ALL |
| 598 |
ETRY; |
| 599 |
return BM_OK; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
// ----------------------------------------------------------------- |
| 605 |
|
| 606 |
int BM_bvector_get_bit(BM_BVHANDLE h, unsigned int i, int* pval) |
| 607 |
{ |
| 608 |
if (!h || !pval) |
| 609 |
return BM_ERR_BADARG; |
| 610 |
BM_TRY |
| 611 |
{ |
| 612 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 613 |
*pval = bv->test(i); |
| 614 |
} |
| 615 |
BM_CATCH_ALL |
| 616 |
ETRY; |
| 617 |
return BM_OK; |
| 618 |
} |
| 619 |
|
| 620 |
// ----------------------------------------------------------------- |
| 621 |
|
| 622 |
int BM_bvector_count(BM_BVHANDLE h, unsigned int* pcount) |
| 623 |
{ |
| 624 |
if (!h || !pcount) |
| 625 |
return BM_ERR_BADARG; |
| 626 |
BM_TRY |
| 627 |
{ |
| 628 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 629 |
*pcount = bv->count(); |
| 630 |
} |
| 631 |
BM_CATCH_ALL |
| 632 |
ETRY; |
| 633 |
return BM_OK; |
| 634 |
} |
| 635 |
|
| 636 |
// ----------------------------------------------------------------- |
| 637 |
|
| 638 |
|
| 639 |
int BM_bvector_count_range(BM_BVHANDLE h, |
| 640 |
unsigned int left, |
| 641 |
unsigned int right, |
| 642 |
unsigned int* pcount) |
| 643 |
{ |
| 644 |
if (!h || !pcount) |
| 645 |
return BM_ERR_BADARG; |
| 646 |
if (left > right) |
| 647 |
return BM_ERR_BADARG; |
| 648 |
|
| 649 |
BM_TRY |
| 650 |
{ |
| 651 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 652 |
*pcount = bv->count_range(left, right); |
| 653 |
} |
| 654 |
BM_CATCH_ALL |
| 655 |
ETRY; |
| 656 |
return BM_OK; |
| 657 |
} |
| 658 |
|
| 659 |
// ----------------------------------------------------------------- |
| 660 |
|
| 661 |
int BM_bvector_find(BM_BVHANDLE h, |
| 662 |
unsigned int from, unsigned int* ppos, int* pfound) |
| 663 |
{ |
| 664 |
if (!h || !ppos || !pfound) |
| 665 |
return BM_ERR_BADARG; |
| 666 |
|
| 667 |
BM_TRY |
| 668 |
{ |
| 669 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 670 |
*pfound = bv->find(from, *ppos); |
| 671 |
} |
| 672 |
BM_CATCH_ALL |
| 673 |
ETRY; |
| 674 |
return BM_OK; |
| 675 |
} |
| 676 |
|
| 677 |
// ----------------------------------------------------------------- |
| 678 |
|
| 679 |
int BM_bvector_find_reverse(BM_BVHANDLE h, |
| 680 |
unsigned int* ppos, int* pfound) |
| 681 |
{ |
| 682 |
if (!h || !ppos || !pfound) |
| 683 |
return BM_ERR_BADARG; |
| 684 |
|
| 685 |
BM_TRY |
| 686 |
{ |
| 687 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 688 |
*pfound = bv->find_reverse(*ppos); |
| 689 |
} |
| 690 |
BM_CATCH_ALL |
| 691 |
ETRY; |
| 692 |
return BM_OK; |
| 693 |
} |
| 694 |
|
| 695 |
// ----------------------------------------------------------------- |
| 696 |
|
| 697 |
int BM_bvector_find_rank(BM_BVHANDLE h, unsigned int rank, |
| 698 |
unsigned int from, |
| 699 |
unsigned int* pidx, |
| 700 |
int* pfound) |
| 701 |
{ |
| 702 |
if (!h || !pidx || !pfound) |
| 703 |
return BM_ERR_BADARG; |
| 704 |
|
| 705 |
BM_TRY |
| 706 |
{ |
| 707 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 708 |
*pfound = bv->find_rank(rank, from, *pidx); |
| 709 |
} |
| 710 |
BM_CATCH_ALL |
| 711 |
ETRY; |
| 712 |
return BM_OK; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
// ----------------------------------------------------------------- |
| 717 |
|
| 718 |
int BM_bvector_get_first(BM_BVHANDLE h, unsigned int* pi, int* pfound) |
| 719 |
{ |
| 720 |
if (!h || !pi || !pfound) |
| 721 |
return BM_ERR_BADARG; |
| 722 |
|
| 723 |
BM_TRY |
| 724 |
{ |
| 725 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 726 |
*pi = bv->get_first(); |
| 727 |
if (*pi == 0) |
| 728 |
{ |
| 729 |
if (!(bv->test(0))) |
| 730 |
{ |
| 731 |
*pfound = 0; |
| 732 |
return BM_OK; |
| 733 |
} |
| 734 |
} |
| 735 |
*pfound = 1; |
| 736 |
} |
| 737 |
BM_CATCH_ALL |
| 738 |
ETRY; |
| 739 |
return BM_OK; |
| 740 |
} |
| 741 |
|
| 742 |
// ----------------------------------------------------------------- |
| 743 |
|
| 744 |
|
| 745 |
int BM_bvector_get_next(BM_BVHANDLE h, unsigned int i, unsigned int* pnext) |
| 746 |
{ |
| 747 |
if (!h || !pnext) |
| 748 |
return BM_ERR_BADARG; |
| 749 |
|
| 750 |
BM_TRY |
| 751 |
{ |
| 752 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 753 |
*pnext = bv->get_next(i); |
| 754 |
} |
| 755 |
BM_CATCH_ALL |
| 756 |
ETRY; |
| 757 |
return BM_OK; |
| 758 |
} |
| 759 |
|
| 760 |
// ----------------------------------------------------------------- |
| 761 |
|
| 762 |
int BM_bvector_extract_next(BM_BVHANDLE h, unsigned int i, unsigned int* pnext) |
| 763 |
{ |
| 764 |
if (!h || !pnext) |
| 765 |
return BM_ERR_BADARG; |
| 766 |
|
| 767 |
BM_TRY |
| 768 |
{ |
| 769 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 770 |
*pnext = bv->extract_next(i); |
| 771 |
} |
| 772 |
BM_CATCH_ALL |
| 773 |
ETRY; |
| 774 |
return BM_OK; |
| 775 |
} |
| 776 |
|
| 777 |
// ----------------------------------------------------------------- |
| 778 |
|
| 779 |
|
| 780 |
int BM_bvector_compare(BM_BVHANDLE h1, BM_BVHANDLE h2, int* pres) |
| 781 |
{ |
| 782 |
if (!h1 || !h2 || !pres) |
| 783 |
return BM_ERR_BADARG; |
| 784 |
|
| 785 |
BM_TRY |
| 786 |
{ |
| 787 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 788 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 789 |
|
| 790 |
*pres = bv1->compare(*bv2); |
| 791 |
} |
| 792 |
BM_CATCH_ALL |
| 793 |
ETRY; |
| 794 |
return BM_OK; |
| 795 |
|
| 796 |
} |
| 797 |
|
| 798 |
// ----------------------------------------------------------------- |
| 799 |
|
| 800 |
BM_API_EXPORT int BM_bvector_find_first_mismatch(BM_BVHANDLE h1, BM_BVHANDLE h2, |
| 801 |
unsigned int* pi, |
| 802 |
int* pfound) |
| 803 |
{ |
| 804 |
if (!h1 || !h2 || !pfound || !pi) |
| 805 |
return BM_ERR_BADARG; |
| 806 |
|
| 807 |
BM_TRY |
| 808 |
{ |
| 809 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 810 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 811 |
|
| 812 |
*pfound = bv1->find_first_mismatch(*bv2, *pi); |
| 813 |
} |
| 814 |
BM_CATCH_ALL |
| 815 |
ETRY; |
| 816 |
return BM_OK; |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
// ----------------------------------------------------------------- |
| 821 |
|
| 822 |
|
| 823 |
int BM_bvector_optimize(BM_BVHANDLE h, |
| 824 |
int opt_mode, |
| 825 |
struct BM_bvector_statistics* pstat) |
| 826 |
{ |
| 827 |
TBM_bvector::statistics stat; |
| 828 |
TBM_bvector::optmode omode; |
| 829 |
if (!h) |
| 830 |
return BM_ERR_BADARG; |
| 831 |
omode = TBM_bvector::opt_compress; |
| 832 |
|
| 833 |
switch (opt_mode) |
| 834 |
{ |
| 835 |
case 1: omode = TBM_bvector::opt_free_0; break; |
| 836 |
case 2: omode = TBM_bvector::opt_free_01; break; |
| 837 |
} |
| 838 |
|
| 839 |
BM_TRY |
| 840 |
{ |
| 841 |
BM_DECLARE_TEMP_BLOCK(tb) |
| 842 |
|
| 843 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 844 |
bv->optimize(tb, omode, &stat); |
| 845 |
|
| 846 |
if (pstat) |
| 847 |
{ |
| 848 |
pstat->bit_blocks = stat.bit_blocks; |
| 849 |
pstat->gap_blocks = stat.gap_blocks; |
| 850 |
pstat->max_serialize_mem = stat.max_serialize_mem; |
| 851 |
pstat->memory_used = stat.memory_used; |
| 852 |
} |
| 853 |
|
| 854 |
} |
| 855 |
BM_CATCH_ALL |
| 856 |
ETRY; |
| 857 |
return BM_OK; |
| 858 |
} |
| 859 |
|
| 860 |
// ----------------------------------------------------------------- |
| 861 |
|
| 862 |
BM_API_EXPORT int BM_bvector_freeze(BM_BVHANDLE h) |
| 863 |
{ |
| 864 |
if (!h) |
| 865 |
return BM_ERR_BADARG; |
| 866 |
BM_TRY |
| 867 |
{ |
| 868 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 869 |
bv->freeze(); |
| 870 |
} |
| 871 |
BM_CATCH_ALL |
| 872 |
ETRY; |
| 873 |
return BM_OK; |
| 874 |
} |
| 875 |
|
| 876 |
// ----------------------------------------------------------------- |
| 877 |
|
| 878 |
BM_API_EXPORT int BM_bvector_is_ro(BM_BVHANDLE h, int* pval) |
| 879 |
{ |
| 880 |
if (!h || !pval) |
| 881 |
return BM_ERR_BADARG; |
| 882 |
BM_TRY |
| 883 |
{ |
| 884 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 885 |
*pval = bv->is_ro(); |
| 886 |
} |
| 887 |
BM_CATCH_ALL |
| 888 |
ETRY; |
| 889 |
return BM_OK; |
| 890 |
} |
| 891 |
|
| 892 |
// ----------------------------------------------------------------- |
| 893 |
|
| 894 |
|
| 895 |
int BM_bvector_calc_stat(BM_BVHANDLE h, |
| 896 |
struct BM_bvector_statistics* pstat) |
| 897 |
{ |
| 898 |
if (!h || !pstat) |
| 899 |
return BM_ERR_BADARG; |
| 900 |
TBM_bvector::statistics stat; |
| 901 |
|
| 902 |
BM_TRY |
| 903 |
{ |
| 904 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 905 |
bv->calc_stat(&stat); |
| 906 |
|
| 907 |
pstat->bit_blocks = stat.bit_blocks; |
| 908 |
pstat->gap_blocks = stat.gap_blocks; |
| 909 |
pstat->max_serialize_mem = stat.max_serialize_mem; |
| 910 |
pstat->memory_used = stat.memory_used; |
| 911 |
} |
| 912 |
BM_CATCH_ALL |
| 913 |
ETRY; |
| 914 |
return BM_OK; |
| 915 |
} |
| 916 |
|
| 917 |
// ----------------------------------------------------------------- |
| 918 |
|
| 919 |
int BM_bvector_merge(BM_BVHANDLE hdst, BM_BVHANDLE hsrc) |
| 920 |
{ |
| 921 |
if (!hdst || !hsrc) |
| 922 |
return BM_ERR_BADARG; |
| 923 |
|
| 924 |
BM_TRY |
| 925 |
{ |
| 926 |
TBM_bvector* bv1 = (TBM_bvector*)hdst; |
| 927 |
TBM_bvector* bv2 = (TBM_bvector*)hsrc; |
| 928 |
|
| 929 |
bv1->merge(*bv2); |
| 930 |
} |
| 931 |
BM_CATCH_ALL |
| 932 |
ETRY; |
| 933 |
return BM_OK; |
| 934 |
} |
| 935 |
|
| 936 |
// ----------------------------------------------------------------- |
| 937 |
|
| 938 |
int BM_bvector_rshift1(BM_BVHANDLE hdst) |
| 939 |
{ |
| 940 |
if (!hdst) |
| 941 |
return BM_ERR_BADARG; |
| 942 |
|
| 943 |
BM_TRY |
| 944 |
{ |
| 945 |
TBM_bvector* bv = (TBM_bvector*)hdst; |
| 946 |
bv->shift_right(); |
| 947 |
} |
| 948 |
BM_CATCH_ALL |
| 949 |
ETRY; |
| 950 |
return BM_OK; |
| 951 |
} |
| 952 |
|
| 953 |
// ----------------------------------------------------------------- |
| 954 |
|
| 955 |
|
| 956 |
int BM_bvector_combine_operation(BM_BVHANDLE hdst, BM_BVHANDLE hsrc, int opcode) |
| 957 |
{ |
| 958 |
if (!hdst || !hsrc) |
| 959 |
return BM_ERR_BADARG; |
| 960 |
|
| 961 |
bm::operation opc; |
| 962 |
switch (opcode) |
| 963 |
{ |
| 964 |
case 0: opc = bm::BM_AND; break; |
| 965 |
case 1: opc = bm::BM_OR; break; |
| 966 |
case 2: opc = bm::BM_SUB; break; |
| 967 |
case 3: opc = bm::BM_XOR; break; |
| 968 |
default: |
| 969 |
return BM_ERR_BADARG; |
| 970 |
} |
| 971 |
|
| 972 |
BM_TRY |
| 973 |
{ |
| 974 |
TBM_bvector* bv1 = (TBM_bvector*)hdst; |
| 975 |
const TBM_bvector* bv2 = (TBM_bvector*)hsrc; |
| 976 |
|
| 977 |
bv1->combine_operation(*bv2, opc); |
| 978 |
} |
| 979 |
BM_CATCH_ALL |
| 980 |
ETRY; |
| 981 |
return BM_OK; |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
int BM_bvector_combine_AND(BM_BVHANDLE hdst, BM_BVHANDLE hsrc) |
| 986 |
{ |
| 987 |
if (!hdst || !hsrc) |
| 988 |
return BM_ERR_BADARG; |
| 989 |
|
| 990 |
BM_TRY |
| 991 |
{ |
| 992 |
TBM_bvector* bvt = (TBM_bvector*)hdst; |
| 993 |
const TBM_bvector* bv1 = (TBM_bvector*)hsrc; |
| 994 |
|
| 995 |
bvt->bit_and(*bv1); |
| 996 |
} |
| 997 |
BM_CATCH_ALL |
| 998 |
ETRY; |
| 999 |
|
| 1000 |
return BM_OK; |
| 1001 |
} |
| 1002 |
|
| 1003 |
int BM_bvector_combine_AND_2sc(BM_BVHANDLE hdst, BM_BVHANDLE hsrc1, BM_BVHANDLE hsrc2, int compress) |
| 1004 |
{ |
| 1005 |
if (!hdst || !hsrc1 || !hsrc2) |
| 1006 |
return BM_ERR_BADARG; |
| 1007 |
|
| 1008 |
TBM_bvector::optmode opt_mode = compress ? TBM_bvector::opt_compress : TBM_bvector::opt_none; |
| 1009 |
|
| 1010 |
BM_TRY |
| 1011 |
{ |
| 1012 |
TBM_bvector* bvt = (TBM_bvector*)hdst; |
| 1013 |
const TBM_bvector* bv1 = (TBM_bvector*)hsrc1; |
| 1014 |
const TBM_bvector* bv2 = (TBM_bvector*)hsrc2; |
| 1015 |
|
| 1016 |
bvt->bit_and(*bv1, *bv2, opt_mode); |
| 1017 |
} |
| 1018 |
BM_CATCH_ALL |
| 1019 |
ETRY; |
| 1020 |
|
| 1021 |
return BM_OK; |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
int BM_bvector_combine_OR(BM_BVHANDLE hdst, BM_BVHANDLE hsrc) |
| 1026 |
{ |
| 1027 |
if (!hdst || !hsrc) |
| 1028 |
return BM_ERR_BADARG; |
| 1029 |
|
| 1030 |
BM_TRY |
| 1031 |
{ |
| 1032 |
TBM_bvector* bvt = (TBM_bvector*)hdst; |
| 1033 |
const TBM_bvector* bv1 = (TBM_bvector*)hsrc; |
| 1034 |
|
| 1035 |
bvt->bit_or(*bv1); |
| 1036 |
} |
| 1037 |
BM_CATCH_ALL |
| 1038 |
ETRY; |
| 1039 |
|
| 1040 |
return BM_OK; |
| 1041 |
} |
| 1042 |
|
| 1043 |
int BM_bvector_combine_OR_2sc(BM_BVHANDLE hdst, BM_BVHANDLE hsrc1, BM_BVHANDLE hsrc2, int compress) |
| 1044 |
{ |
| 1045 |
if (!hdst || !hsrc1 || !hsrc2) |
| 1046 |
return BM_ERR_BADARG; |
| 1047 |
|
| 1048 |
TBM_bvector::optmode opt_mode = compress ? TBM_bvector::opt_compress : TBM_bvector::opt_none; |
| 1049 |
|
| 1050 |
BM_TRY |
| 1051 |
{ |
| 1052 |
TBM_bvector* bvt = (TBM_bvector*)hdst; |
| 1053 |
const TBM_bvector* bv1 = (TBM_bvector*)hsrc1; |
| 1054 |
const TBM_bvector* bv2 = (TBM_bvector*)hsrc2; |
| 1055 |
|
| 1056 |
bvt->bit_or(*bv1, *bv2, opt_mode); |
| 1057 |
} |
| 1058 |
BM_CATCH_ALL |
| 1059 |
ETRY; |
| 1060 |
|
| 1061 |
return BM_OK; |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
int BM_bvector_combine_SUB(BM_BVHANDLE hdst, BM_BVHANDLE hsrc) |
| 1066 |
{ |
| 1067 |
if (!hdst || !hsrc) |
| 1068 |
return BM_ERR_BADARG; |
| 1069 |
|
| 1070 |
BM_TRY |
| 1071 |
{ |
| 1072 |
TBM_bvector* bvt = (TBM_bvector*)hdst; |
| 1073 |
const TBM_bvector* bv1 = (TBM_bvector*)hsrc; |
| 1074 |
|
| 1075 |
bvt->bit_sub(*bv1); |
| 1076 |
} |
| 1077 |
BM_CATCH_ALL |
| 1078 |
ETRY; |
| 1079 |
|
| 1080 |
return BM_OK; |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
int BM_bvector_combine_SUB_2sc(BM_BVHANDLE hdst, BM_BVHANDLE hsrc1, BM_BVHANDLE hsrc2, int compress) |
| 1085 |
{ |
| 1086 |
if (!hdst || !hsrc1 || !hsrc2) |
| 1087 |
return BM_ERR_BADARG; |
| 1088 |
|
| 1089 |
TBM_bvector::optmode opt_mode = compress ? TBM_bvector::opt_compress : TBM_bvector::opt_none; |
| 1090 |
|
| 1091 |
BM_TRY |
| 1092 |
{ |
| 1093 |
TBM_bvector* bvt = (TBM_bvector*)hdst; |
| 1094 |
const TBM_bvector* bv1 = (TBM_bvector*)hsrc1; |
| 1095 |
const TBM_bvector* bv2 = (TBM_bvector*)hsrc2; |
| 1096 |
|
| 1097 |
bvt->bit_sub(*bv1, *bv2, opt_mode); |
| 1098 |
} |
| 1099 |
BM_CATCH_ALL |
| 1100 |
ETRY; |
| 1101 |
|
| 1102 |
return BM_OK; |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
int BM_bvector_combine_XOR(BM_BVHANDLE hdst, BM_BVHANDLE hsrc) |
| 1107 |
{ |
| 1108 |
if (!hdst || !hsrc) |
| 1109 |
return BM_ERR_BADARG; |
| 1110 |
|
| 1111 |
BM_TRY |
| 1112 |
{ |
| 1113 |
TBM_bvector* bvt = (TBM_bvector*)hdst; |
| 1114 |
const TBM_bvector* bv1 = (TBM_bvector*)hsrc; |
| 1115 |
|
| 1116 |
bvt->bit_xor(*bv1); |
| 1117 |
} |
| 1118 |
BM_CATCH_ALL |
| 1119 |
ETRY; |
| 1120 |
|
| 1121 |
return BM_OK; |
| 1122 |
} |
| 1123 |
|
| 1124 |
int BM_bvector_combine_XOR_2sc(BM_BVHANDLE hdst, BM_BVHANDLE hsrc1, BM_BVHANDLE hsrc2, int compress) |
| 1125 |
{ |
| 1126 |
if (!hdst || !hsrc1 || !hsrc2) |
| 1127 |
return BM_ERR_BADARG; |
| 1128 |
|
| 1129 |
TBM_bvector::optmode opt_mode = compress ? TBM_bvector::opt_compress : TBM_bvector::opt_none; |
| 1130 |
|
| 1131 |
BM_TRY |
| 1132 |
{ |
| 1133 |
TBM_bvector* bvt = (TBM_bvector*)hdst; |
| 1134 |
const TBM_bvector* bv1 = (TBM_bvector*)hsrc1; |
| 1135 |
const TBM_bvector* bv2 = (TBM_bvector*)hsrc2; |
| 1136 |
|
| 1137 |
bvt->bit_xor(*bv1, *bv2, opt_mode); |
| 1138 |
} |
| 1139 |
BM_CATCH_ALL |
| 1140 |
ETRY; |
| 1141 |
|
| 1142 |
return BM_OK; |
| 1143 |
} |
| 1144 |
|
| 1145 |
// ----------------------------------------------------------------- |
| 1146 |
|
| 1147 |
|
| 1148 |
int BM_bvector_combine_AND_arr(BM_BVHANDLE hdst, |
| 1149 |
const unsigned int* arr_begin, |
| 1150 |
const unsigned int* arr_end) |
| 1151 |
{ |
| 1152 |
if (!hdst) |
| 1153 |
return BM_ERR_BADARG; |
| 1154 |
|
| 1155 |
BM_TRY |
| 1156 |
{ |
| 1157 |
TBM_bvector* bv = (TBM_bvector*)hdst; |
| 1158 |
bm::combine_and(*bv, arr_begin, arr_end); |
| 1159 |
} |
| 1160 |
BM_CATCH_ALL |
| 1161 |
ETRY; |
| 1162 |
return BM_OK; |
| 1163 |
} |
| 1164 |
|
| 1165 |
// ----------------------------------------------------------------- |
| 1166 |
|
| 1167 |
|
| 1168 |
int BM_bvector_combine_AND_arr_sorted(BM_BVHANDLE hdst, |
| 1169 |
const unsigned int* arr_begin, |
| 1170 |
const unsigned int* arr_end) |
| 1171 |
{ |
| 1172 |
if (!hdst) |
| 1173 |
return BM_ERR_BADARG; |
| 1174 |
|
| 1175 |
BM_TRY |
| 1176 |
{ |
| 1177 |
TBM_bvector* bv = (TBM_bvector*)hdst; |
| 1178 |
bm::combine_and_sorted(*bv, arr_begin, arr_end); |
| 1179 |
} |
| 1180 |
BM_CATCH_ALL |
| 1181 |
ETRY; |
| 1182 |
return BM_OK; |
| 1183 |
} |
| 1184 |
|
| 1185 |
// ----------------------------------------------------------------- |
| 1186 |
|
| 1187 |
|
| 1188 |
int BM_bvector_combine_OR_arr(BM_BVHANDLE hdst, |
| 1189 |
const unsigned int* arr_begin, |
| 1190 |
const unsigned int* arr_end) |
| 1191 |
{ |
| 1192 |
if (!hdst) |
| 1193 |
return BM_ERR_BADARG; |
| 1194 |
|
| 1195 |
BM_TRY |
| 1196 |
{ |
| 1197 |
TBM_bvector* bv = (TBM_bvector*)hdst; |
| 1198 |
bv->set(arr_begin, arr_end - arr_begin); |
| 1199 |
} |
| 1200 |
BM_CATCH_ALL |
| 1201 |
ETRY; |
| 1202 |
return BM_OK; |
| 1203 |
} |
| 1204 |
|
| 1205 |
// ----------------------------------------------------------------- |
| 1206 |
|
| 1207 |
int BM_bvector_combine_XOR_arr(BM_BVHANDLE hdst, |
| 1208 |
const unsigned int* arr_begin, |
| 1209 |
const unsigned int* arr_end) |
| 1210 |
{ |
| 1211 |
if (!hdst) |
| 1212 |
return BM_ERR_BADARG; |
| 1213 |
|
| 1214 |
BM_TRY |
| 1215 |
{ |
| 1216 |
TBM_bvector* bv = (TBM_bvector*)hdst; |
| 1217 |
bm::combine_xor(*bv, arr_begin, arr_end); |
| 1218 |
} |
| 1219 |
BM_CATCH_ALL |
| 1220 |
ETRY; |
| 1221 |
return BM_OK; |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
// ----------------------------------------------------------------- |
| 1226 |
|
| 1227 |
int BM_bvector_combine_SUB_arr(BM_BVHANDLE hdst, |
| 1228 |
const unsigned int* arr_begin, |
| 1229 |
const unsigned int* arr_end) |
| 1230 |
{ |
| 1231 |
if (!hdst) |
| 1232 |
return BM_ERR_BADARG; |
| 1233 |
|
| 1234 |
BM_TRY |
| 1235 |
{ |
| 1236 |
TBM_bvector* bv = (TBM_bvector*)hdst; |
| 1237 |
bv->clear(arr_begin, arr_end - arr_begin); |
| 1238 |
} |
| 1239 |
BM_CATCH_ALL |
| 1240 |
ETRY; |
| 1241 |
return BM_OK; |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
// ----------------------------------------------------------------- |
| 1246 |
|
| 1247 |
|
| 1248 |
int BM_bvector_serialize(BM_BVHANDLE h, |
| 1249 |
char* buf, |
| 1250 |
size_t buf_size, |
| 1251 |
size_t* pblob_size) |
| 1252 |
{ |
| 1253 |
if (!h || !pblob_size) |
| 1254 |
return BM_ERR_BADARG; |
| 1255 |
|
| 1256 |
BM_TRY |
| 1257 |
{ |
| 1258 |
BM_DECLARE_TEMP_BLOCK(tb) |
| 1259 |
|
| 1260 |
const TBM_bvector* bv = (TBM_bvector*)h; |
| 1261 |
|
| 1262 |
bm::serializer<TBM_bvector> bvs(TBM_bvector::allocator_type(), tb); |
| 1263 |
bvs.set_compression_level(4); |
| 1264 |
|
| 1265 |
*pblob_size = bvs.serialize(*bv, (unsigned char*)buf, (unsigned)buf_size); |
| 1266 |
} |
| 1267 |
BM_CATCH_ALL |
| 1268 |
ETRY; |
| 1269 |
return BM_OK; |
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
// ----------------------------------------------------------------- |
| 1274 |
|
| 1275 |
int BM_bvector_deserialize(BM_BVHANDLE h, |
| 1276 |
const char* buf, |
| 1277 |
size_t /*buf_size*/) |
| 1278 |
{ |
| 1279 |
if (!h) |
| 1280 |
return BM_ERR_BADARG; |
| 1281 |
|
| 1282 |
BM_TRY |
| 1283 |
{ |
| 1284 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 1285 |
bm::deserialize(*bv, (const unsigned char*)buf); |
| 1286 |
} |
| 1287 |
BM_CATCH_ALL |
| 1288 |
ETRY; |
| 1289 |
return BM_OK; |
| 1290 |
} |
| 1291 |
|
| 1292 |
// ----------------------------------------------------------------- |
| 1293 |
|
| 1294 |
int BM_bvector_enumerator_construct(BM_BVHANDLE h, BM_BVEHANDLE* peh) |
| 1295 |
{ |
| 1296 |
return BM_bvector_enumerator_construct_from(h, peh, 0); |
| 1297 |
} |
| 1298 |
|
| 1299 |
// ----------------------------------------------------------------- |
| 1300 |
|
| 1301 |
|
| 1302 |
int BM_bvector_enumerator_construct_from(BM_BVHANDLE h, |
| 1303 |
BM_BVEHANDLE* peh, |
| 1304 |
unsigned int pos) |
| 1305 |
{ |
| 1306 |
if (h == 0 || peh == 0) |
| 1307 |
return BM_ERR_BADARG; |
| 1308 |
|
| 1309 |
BM_TRY |
| 1310 |
{ |
| 1311 |
TBM_bvector* bv = (TBM_bvector*)h; |
| 1312 |
|
| 1313 |
void* mem = ::malloc(sizeof(TBM_bvector_enumerator)); |
| 1314 |
if (mem == 0) |
| 1315 |
{ |
| 1316 |
*peh = 0; |
| 1317 |
return BM_ERR_BADALLOC; |
| 1318 |
} |
| 1319 |
// placement new just to call the constructor |
| 1320 |
TBM_bvector_enumerator* bvenum = new(mem) TBM_bvector_enumerator(bv, pos); |
| 1321 |
*peh = bvenum; |
| 1322 |
|
| 1323 |
} |
| 1324 |
CATCH (BM_ERR_BADALLOC) |
| 1325 |
{ |
| 1326 |
*peh = 0; |
| 1327 |
return BM_ERR_BADALLOC; |
| 1328 |
} |
| 1329 |
CATCH (BM_ERR_BADARG) { return BM_ERR_BADARG; } |
| 1330 |
CATCH (BM_ERR_RANGE) { return BM_ERR_RANGE; } |
| 1331 |
|
| 1332 |
ETRY; |
| 1333 |
|
| 1334 |
return BM_OK; |
| 1335 |
} |
| 1336 |
|
| 1337 |
// ----------------------------------------------------------------- |
| 1338 |
|
| 1339 |
int BM_bvector_enumerator_free(BM_BVEHANDLE eh) |
| 1340 |
{ |
| 1341 |
if (!eh) |
| 1342 |
return BM_ERR_BADARG; |
| 1343 |
TBM_bvector_enumerator* bvenum = (TBM_bvector_enumerator*)eh; |
| 1344 |
bvenum->~TBM_bvector_enumerator(); |
| 1345 |
::free(eh); |
| 1346 |
|
| 1347 |
return BM_OK; |
| 1348 |
} |
| 1349 |
|
| 1350 |
// ----------------------------------------------------------------- |
| 1351 |
|
| 1352 |
int BM_bvector_enumerator_is_valid(BM_BVEHANDLE eh, int* valid) |
| 1353 |
{ |
| 1354 |
if (!eh || !valid) |
| 1355 |
return BM_ERR_BADARG; |
| 1356 |
|
| 1357 |
BM_TRY |
| 1358 |
{ |
| 1359 |
TBM_bvector_enumerator* bvenum = (TBM_bvector_enumerator*)eh; |
| 1360 |
*valid = bvenum->valid(); |
| 1361 |
} |
| 1362 |
BM_CATCH_ALL |
| 1363 |
ETRY; |
| 1364 |
return BM_OK; |
| 1365 |
} |
| 1366 |
|
| 1367 |
// ----------------------------------------------------------------- |
| 1368 |
|
| 1369 |
int BM_bvector_enumerator_get_value(BM_BVEHANDLE eh, unsigned int* pvalue) |
| 1370 |
{ |
| 1371 |
if (!eh || !pvalue) |
| 1372 |
return BM_ERR_BADARG; |
| 1373 |
|
| 1374 |
BM_TRY |
| 1375 |
{ |
| 1376 |
TBM_bvector_enumerator* bvenum = (TBM_bvector_enumerator*)eh; |
| 1377 |
*pvalue = bvenum->value(); |
| 1378 |
} |
| 1379 |
BM_CATCH_ALL |
| 1380 |
ETRY; |
| 1381 |
return BM_OK; |
| 1382 |
} |
| 1383 |
|
| 1384 |
// ----------------------------------------------------------------- |
| 1385 |
|
| 1386 |
int BM_bvector_enumerator_next(BM_BVEHANDLE eh, |
| 1387 |
int* pvalid, unsigned int* pvalue) |
| 1388 |
{ |
| 1389 |
if (!eh) |
| 1390 |
return BM_ERR_BADARG; |
| 1391 |
|
| 1392 |
BM_TRY |
| 1393 |
{ |
| 1394 |
TBM_bvector_enumerator* bvenum = (TBM_bvector_enumerator*)eh; |
| 1395 |
bvenum->go_up(); |
| 1396 |
if (pvalid) |
| 1397 |
{ |
| 1398 |
*pvalid = bvenum->valid(); |
| 1399 |
} |
| 1400 |
if (pvalue) |
| 1401 |
{ |
| 1402 |
*pvalue = bvenum->value(); |
| 1403 |
} |
| 1404 |
} |
| 1405 |
BM_CATCH_ALL |
| 1406 |
ETRY; |
| 1407 |
return BM_OK; |
| 1408 |
} |
| 1409 |
|
| 1410 |
// ----------------------------------------------------------------- |
| 1411 |
|
| 1412 |
int BM_bvector_enumerator_goto(BM_BVEHANDLE eh, unsigned int pos, |
| 1413 |
int* pvalid, unsigned int* pvalue) |
| 1414 |
{ |
| 1415 |
if (!eh) |
| 1416 |
return BM_ERR_BADARG; |
| 1417 |
|
| 1418 |
BM_TRY |
| 1419 |
{ |
| 1420 |
TBM_bvector_enumerator* bvenum = (TBM_bvector_enumerator*)eh; |
| 1421 |
bvenum->go_to(pos); |
| 1422 |
if (pvalid) |
| 1423 |
{ |
| 1424 |
*pvalid = bvenum->valid(); |
| 1425 |
} |
| 1426 |
if (pvalue) |
| 1427 |
{ |
| 1428 |
*pvalue = bvenum->value(); |
| 1429 |
} |
| 1430 |
} |
| 1431 |
BM_CATCH_ALL |
| 1432 |
ETRY; |
| 1433 |
return BM_OK; |
| 1434 |
} |
| 1435 |
|
| 1436 |
|
| 1437 |
// ----------------------------------------------------------------- |
| 1438 |
|
| 1439 |
int BM_bvector_count_AND(BM_BVHANDLE h1, BM_BVHANDLE h2, unsigned int* pcount) |
| 1440 |
{ |
| 1441 |
if (!h1 || !h2 || !pcount) |
| 1442 |
return BM_ERR_BADARG; |
| 1443 |
BM_TRY |
| 1444 |
{ |
| 1445 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 1446 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 1447 |
*pcount = bm::count_and(*bv1, *bv2); |
| 1448 |
} |
| 1449 |
BM_CATCH_ALL |
| 1450 |
ETRY; |
| 1451 |
return BM_OK; |
| 1452 |
} |
| 1453 |
|
| 1454 |
// ----------------------------------------------------------------- |
| 1455 |
|
| 1456 |
int BM_bvector_any_AND(BM_BVHANDLE h1, BM_BVHANDLE h2, unsigned int* pany) |
| 1457 |
{ |
| 1458 |
if (!h1 || !h2 || !pany) |
| 1459 |
return BM_ERR_BADARG; |
| 1460 |
BM_TRY |
| 1461 |
{ |
| 1462 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 1463 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 1464 |
*pany = bm::any_and(*bv1, *bv2); |
| 1465 |
} |
| 1466 |
BM_CATCH_ALL |
| 1467 |
ETRY; |
| 1468 |
return BM_OK; |
| 1469 |
} |
| 1470 |
|
| 1471 |
// ----------------------------------------------------------------- |
| 1472 |
|
| 1473 |
int BM_bvector_count_XOR(BM_BVHANDLE h1, BM_BVHANDLE h2, unsigned int* pcount) |
| 1474 |
{ |
| 1475 |
if (!h1 || !h2 || !pcount) |
| 1476 |
return BM_ERR_BADARG; |
| 1477 |
BM_TRY |
| 1478 |
{ |
| 1479 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 1480 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 1481 |
*pcount = bm::count_xor(*bv1, *bv2); |
| 1482 |
} |
| 1483 |
BM_CATCH_ALL |
| 1484 |
ETRY; |
| 1485 |
return BM_OK; |
| 1486 |
} |
| 1487 |
|
| 1488 |
// ----------------------------------------------------------------- |
| 1489 |
|
| 1490 |
int BM_bvector_any_XOR(BM_BVHANDLE h1, BM_BVHANDLE h2, unsigned int* pany) |
| 1491 |
{ |
| 1492 |
if (!h1 || !h2 || !pany) |
| 1493 |
return BM_ERR_BADARG; |
| 1494 |
BM_TRY |
| 1495 |
{ |
| 1496 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 1497 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 1498 |
*pany = bm::any_xor(*bv1, *bv2); |
| 1499 |
} |
| 1500 |
BM_CATCH_ALL |
| 1501 |
ETRY; |
| 1502 |
return BM_OK; |
| 1503 |
} |
| 1504 |
|
| 1505 |
// ----------------------------------------------------------------- |
| 1506 |
|
| 1507 |
int BM_bvector_count_SUB(BM_BVHANDLE h1, BM_BVHANDLE h2, unsigned int* pcount) |
| 1508 |
{ |
| 1509 |
if (!h1 || !h2 || !pcount) |
| 1510 |
return BM_ERR_BADARG; |
| 1511 |
BM_TRY |
| 1512 |
{ |
| 1513 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 1514 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 1515 |
*pcount = bm::count_sub(*bv1, *bv2); |
| 1516 |
} |
| 1517 |
BM_CATCH_ALL |
| 1518 |
ETRY; |
| 1519 |
return BM_OK; |
| 1520 |
} |
| 1521 |
|
| 1522 |
// ----------------------------------------------------------------- |
| 1523 |
|
| 1524 |
int BM_bvector_any_SUB(BM_BVHANDLE h1, BM_BVHANDLE h2, unsigned int* pany) |
| 1525 |
{ |
| 1526 |
if (!h1 || !h2 || !pany) |
| 1527 |
return BM_ERR_BADARG; |
| 1528 |
BM_TRY |
| 1529 |
{ |
| 1530 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 1531 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 1532 |
*pany = bm::any_sub(*bv1, *bv2); |
| 1533 |
} |
| 1534 |
BM_CATCH_ALL |
| 1535 |
ETRY; |
| 1536 |
return BM_OK; |
| 1537 |
} |
| 1538 |
|
| 1539 |
// ----------------------------------------------------------------- |
| 1540 |
|
| 1541 |
int BM_bvector_count_OR(BM_BVHANDLE h1, BM_BVHANDLE h2, unsigned int* pcount) |
| 1542 |
{ |
| 1543 |
if (!h1 || !h2 || !pcount) |
| 1544 |
return BM_ERR_BADARG; |
| 1545 |
BM_TRY |
| 1546 |
{ |
| 1547 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 1548 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 1549 |
*pcount = bm::count_or(*bv1, *bv2); |
| 1550 |
} |
| 1551 |
BM_CATCH_ALL |
| 1552 |
ETRY; |
| 1553 |
return BM_OK; |
| 1554 |
} |
| 1555 |
|
| 1556 |
// ----------------------------------------------------------------- |
| 1557 |
|
| 1558 |
int BM_bvector_any_OR(BM_BVHANDLE h1, BM_BVHANDLE h2, unsigned int* pany) |
| 1559 |
{ |
| 1560 |
if (!h1 || !h2 || !pany) |
| 1561 |
return BM_ERR_BADARG; |
| 1562 |
BM_TRY |
| 1563 |
{ |
| 1564 |
const TBM_bvector* bv1 = (TBM_bvector*)h1; |
| 1565 |
const TBM_bvector* bv2 = (TBM_bvector*)h2; |
| 1566 |
*pany = bm::any_or(*bv1, *bv2); |
| 1567 |
} |
| 1568 |
BM_CATCH_ALL |
| 1569 |
ETRY; |
| 1570 |
return BM_OK; |
| 1571 |
} |
| 1572 |
|
| 1573 |
// ----------------------------------------------------------------- |
| 1574 |
|