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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
|
/*
Copyright (C) 2018 Brazil
Copyright (C) 2020-2023 Sutou Kouhei <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "grn.h"
#include "grn_ctx.h"
#include "grn_float.h"
#include "grn_io.h"
#include "grn_vector.h"
#include <groonga/bulk.hpp>
#include <cstring>
namespace grn {
namespace {
template <typename NUMERIC, typename FLOAT>
NUMERIC
cast_value(const char *raw_value)
{
return static_cast<NUMERIC>(*reinterpret_cast<const FLOAT *>(raw_value));
}
template <>
bool
cast_value<bool, float>(const char *raw_value)
{
float value = *reinterpret_cast<const float *>(raw_value);
return grn_float32_is_zero(value);
}
template <>
bool
cast_value<bool, double>(const char *raw_value)
{
double value = *reinterpret_cast<const double *>(raw_value);
return grn_float_is_zero(value);
}
} // namespace
template <typename NUMERIC>
NUMERIC
vector_get_element(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
NUMERIC default_value)
{
const char *raw_value = NULL;
grn_id domain;
uint32_t length;
NUMERIC value = default_value;
GRN_API_ENTER;
length =
grn_vector_get_element(ctx, vector, offset, &raw_value, NULL, &domain);
if (length > 0) {
switch (domain) {
case GRN_DB_BOOL:
value = *reinterpret_cast<const bool *>(raw_value);
break;
case GRN_DB_INT8:
value =
static_cast<NUMERIC>(*reinterpret_cast<const int8_t *>(raw_value));
break;
case GRN_DB_UINT8:
value =
static_cast<NUMERIC>(*reinterpret_cast<const uint8_t *>(raw_value));
break;
case GRN_DB_INT16:
value =
static_cast<NUMERIC>(*reinterpret_cast<const int16_t *>(raw_value));
break;
case GRN_DB_UINT16:
value =
static_cast<NUMERIC>(*reinterpret_cast<const uint16_t *>(raw_value));
break;
case GRN_DB_INT32:
value =
static_cast<NUMERIC>(*reinterpret_cast<const int32_t *>(raw_value));
break;
case GRN_DB_UINT32:
value =
static_cast<NUMERIC>(*reinterpret_cast<const uint32_t *>(raw_value));
break;
case GRN_DB_INT64:
value =
static_cast<NUMERIC>(*reinterpret_cast<const int64_t *>(raw_value));
break;
case GRN_DB_UINT64:
value =
static_cast<NUMERIC>(*reinterpret_cast<const uint64_t *>(raw_value));
break;
case GRN_DB_FLOAT32:
value = cast_value<NUMERIC, float>(raw_value);
break;
case GRN_DB_FLOAT:
value = cast_value<NUMERIC, double>(raw_value);
break;
default:
break;
}
}
GRN_API_RETURN(value);
}
} // namespace grn
extern "C" {
bool
grn_vector_get_element_bool(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
bool default_value)
{
return grn::vector_get_element<bool>(ctx, vector, offset, default_value);
}
int8_t
grn_vector_get_element_int8(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
int8_t default_value)
{
return grn::vector_get_element<int8_t>(ctx, vector, offset, default_value);
}
uint8_t
grn_vector_get_element_uint8(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
uint8_t default_value)
{
return grn::vector_get_element<uint8_t>(ctx, vector, offset, default_value);
}
int16_t
grn_vector_get_element_int16(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
int16_t default_value)
{
return grn::vector_get_element<int16_t>(ctx, vector, offset, default_value);
}
uint16_t
grn_vector_get_element_uint16(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
uint16_t default_value)
{
return grn::vector_get_element<uint16_t>(ctx, vector, offset, default_value);
}
int32_t
grn_vector_get_element_int32(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
int32_t default_value)
{
return grn::vector_get_element<int32_t>(ctx, vector, offset, default_value);
}
uint32_t
grn_vector_get_element_uint32(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
uint32_t default_value)
{
return grn::vector_get_element<uint32_t>(ctx, vector, offset, default_value);
}
int64_t
grn_vector_get_element_int64(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
int64_t default_value)
{
return grn::vector_get_element<int64_t>(ctx, vector, offset, default_value);
}
uint64_t
grn_vector_get_element_uint64(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
uint64_t default_value)
{
return grn::vector_get_element<uint64_t>(ctx, vector, offset, default_value);
}
float
grn_vector_get_element_float32(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
float default_value)
{
return grn::vector_get_element<float>(ctx, vector, offset, default_value);
}
double
grn_vector_get_element_float64(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
double default_value)
{
return grn::vector_get_element<double>(ctx, vector, offset, default_value);
}
static uint32_t
grn_uvector_element_size_internal(grn_ctx *ctx, grn_obj *uvector)
{
size_t element_size = grn_type_id_size(ctx, uvector->header.domain);
if (grn_obj_is_weight_uvector(ctx, uvector)) {
#ifdef GRN_HAVE_BFLOAT16
if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) {
element_size += sizeof(grn_bfloat16);
} else {
element_size += sizeof(float);
}
#else
element_size += sizeof(float);
#endif
}
return static_cast<uint32_t>(element_size);
}
static uint32_t
grn_uvector_size_internal(grn_ctx *ctx, grn_obj *uvector)
{
uint32_t element_size = grn_uvector_element_size_internal(ctx, uvector);
return static_cast<uint32_t>(GRN_BULK_VSIZE(uvector) / element_size);
}
uint32_t
grn_vector_size(grn_ctx *ctx, grn_obj *vector)
{
uint32_t size;
if (!vector) {
ERR(GRN_INVALID_ARGUMENT, "[vector][size] vector is null");
return 0;
}
GRN_API_ENTER;
switch (vector->header.type) {
case GRN_BULK:
size = static_cast<uint32_t>(GRN_BULK_VSIZE(vector));
break;
case GRN_UVECTOR:
size = grn_uvector_size_internal(ctx, vector);
break;
case GRN_PVECTOR:
size = static_cast<uint32_t>(GRN_BULK_VSIZE(vector) / sizeof(grn_obj *));
break;
case GRN_VECTOR:
size = vector->u.v.n_sections;
break;
default:
{
grn_obj inspected;
GRN_TEXT_INIT(&inspected, 0);
grn_inspect(ctx, &inspected, vector);
ERR(GRN_INVALID_ARGUMENT,
"[vector][size] not vector: %.*s",
(int)GRN_TEXT_LEN(&inspected),
GRN_TEXT_VALUE(&inspected));
GRN_OBJ_FIN(ctx, &inspected);
}
size = 0;
break;
}
GRN_API_RETURN(size);
}
grn_obj *
grn_vector_body(grn_ctx *ctx, grn_obj *v)
{
if (!v) {
ERR(GRN_INVALID_ARGUMENT, "invalid argument");
return NULL;
}
switch (v->header.type) {
case GRN_VECTOR:
if (!v->u.v.body) {
v->u.v.body = grn_obj_open(ctx, GRN_BULK, 0, v->header.domain);
}
return v->u.v.body;
case GRN_BULK:
case GRN_UVECTOR:
return v;
default:
return NULL;
}
}
uint32_t
grn_vector_get_element(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
const char **str,
uint32_t *weight,
grn_id *domain)
{
float weight_float;
uint32_t length = grn_vector_get_element_float(ctx,
vector,
offset,
str,
&weight_float,
domain);
if (weight) {
*weight = static_cast<uint32_t>(weight_float);
}
return length;
}
uint32_t
grn_vector_get_element_float(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
const char **str,
float *weight,
grn_id *domain)
{
uint32_t length = 0;
GRN_API_ENTER;
if (!vector || vector->header.type != GRN_VECTOR) {
ERR(GRN_INVALID_ARGUMENT, "invalid vector");
goto exit;
}
if (vector->u.v.n_sections <= offset) {
ERR(GRN_RANGE_ERROR, "offset out of range");
goto exit;
}
{
grn_section *vp = &vector->u.v.sections[offset];
grn_obj *body = grn_vector_body(ctx, vector);
*str = GRN_BULK_HEAD(body) + vp->offset;
if (weight) {
*weight = vp->weight;
}
if (domain) {
*domain = vp->domain;
}
length = vp->length;
}
exit:
GRN_API_RETURN(length);
}
uint32_t
grn_vector_pop_element(grn_ctx *ctx,
grn_obj *vector,
const char **str,
uint32_t *weight,
grn_id *domain)
{
float weight_float;
uint32_t length =
grn_vector_pop_element_float(ctx, vector, str, &weight_float, domain);
if (weight) {
*weight = static_cast<uint32_t>(weight_float);
}
return length;
}
uint32_t
grn_vector_pop_element_float(grn_ctx *ctx,
grn_obj *vector,
const char **str,
float *weight,
grn_id *domain)
{
uint32_t offset, length = 0;
GRN_API_ENTER;
if (!vector || vector->header.type != GRN_VECTOR) {
ERR(GRN_INVALID_ARGUMENT, "invalid vector");
goto exit;
}
if (!vector->u.v.n_sections) {
ERR(GRN_RANGE_ERROR, "offset out of range");
goto exit;
}
offset = --vector->u.v.n_sections;
{
grn_section *vp = &vector->u.v.sections[offset];
grn_obj *body = grn_vector_body(ctx, vector);
*str = GRN_BULK_HEAD(body) + vp->offset;
if (weight) {
*weight = vp->weight;
}
if (domain) {
*domain = vp->domain;
}
length = vp->length;
grn_bulk_truncate(ctx, body, vp->offset);
}
exit:
GRN_API_RETURN(length);
}
#define W_SECTIONS_UNIT 8
#define S_SECTIONS_UNIT (1 << W_SECTIONS_UNIT)
#define M_SECTIONS_UNIT (S_SECTIONS_UNIT - 1)
grn_rc
grn_vector_delimit(grn_ctx *ctx, grn_obj *v, float weight, grn_id domain)
{
if (v->header.type != GRN_VECTOR) {
return GRN_INVALID_ARGUMENT;
}
if (!(v->u.v.n_sections & M_SECTIONS_UNIT)) {
size_t size = sizeof(grn_section) * (v->u.v.n_sections + S_SECTIONS_UNIT);
grn_section *vp =
static_cast<grn_section *>(GRN_REALLOC(v->u.v.sections, size));
if (!vp) {
return GRN_NO_MEMORY_AVAILABLE;
}
v->u.v.sections = vp;
}
{
grn_obj *body = grn_vector_body(ctx, v);
grn_section *vp = &v->u.v.sections[v->u.v.n_sections];
vp->offset = v->u.v.n_sections ? vp[-1].offset + vp[-1].length : 0;
vp->length = static_cast<uint32_t>(GRN_BULK_VSIZE(body) - vp->offset);
vp->weight = weight;
vp->domain = domain;
}
v->u.v.n_sections++;
return GRN_SUCCESS;
}
grn_obj *
grn_vector_pack(grn_ctx *ctx,
grn_obj *vector,
uint32_t offset,
uint32_t n,
grn_vector_pack_flags flags,
grn_obj *header,
grn_obj *footer)
{
bool need_footer = false;
grn_text_benc(ctx, header, n);
for (uint32_t i = 0; i < n; ++i) {
grn_section *section = &(vector->u.v.sections[i + offset]);
grn_text_benc(ctx, header, section->length);
if (section->weight > 0 || section->domain != GRN_ID_NIL) {
need_footer = true;
}
}
if (need_footer) {
for (uint32_t i = 0; i < n; ++i) {
grn_section *section = &(vector->u.v.sections[i + offset]);
if (flags & GRN_VECTOR_PACK_WEIGHT_FLOAT32) {
GRN_FLOAT32_PUT(ctx, footer, section->weight);
} else if (flags & GRN_VECTOR_PACK_WEIGHT_BFLOAT16) {
#ifdef GRN_HAVE_BFLOAT16
GRN_BFLOAT16_PUT(ctx,
footer,
grn::numeric::to_bfloat16(section->weight));
#else
GRN_FLOAT32_PUT(ctx, footer, section->weight);
#endif
} else {
grn_text_benc(ctx, footer, static_cast<unsigned int>(section->weight));
}
grn_text_benc(ctx, footer, section->domain);
}
}
return grn_vector_body(ctx, vector);
}
grn_rc
grn_vector_unpack(grn_ctx *ctx,
grn_obj *vector,
const uint8_t *data,
uint32_t data_size,
grn_vector_pack_flags flags,
uint32_t *used_size)
{
auto start = data;
auto p = start;
auto pe = p + data_size;
uint32_t n0 = vector->u.v.n_sections;
uint32_t n;
GRN_B_DEC(n, p);
if (((n0 + M_SECTIONS_UNIT) >> W_SECTIONS_UNIT) !=
((n0 + n + M_SECTIONS_UNIT) >> W_SECTIONS_UNIT)) {
size_t size =
sizeof(grn_section) * ((n0 + n + M_SECTIONS_UNIT) & ~M_SECTIONS_UNIT);
grn_section *sections =
static_cast<grn_section *>(GRN_REALLOC(vector->u.v.sections, size));
if (!sections) {
return GRN_NO_MEMORY_AVAILABLE;
}
vector->u.v.sections = sections;
}
{
grn_obj *body = grn_vector_body(ctx, vector);
uint32_t offset = static_cast<uint32_t>(GRN_BULK_VSIZE(body));
uint32_t o = 0;
for (uint32_t i = 0; i < n; ++i) {
grn_section *section = vector->u.v.sections + n0 + i;
if (pe <= p) {
return GRN_INVALID_ARGUMENT;
}
uint32_t length;
GRN_B_DEC(length, p);
section->length = length;
section->offset = offset + o;
section->weight = 0;
section->domain = 0;
o += length;
}
if (pe < p + o) {
return GRN_INVALID_ARGUMENT;
}
grn_bulk_write(ctx, body, (char *)p, o);
p += o;
if (p < pe) {
for (uint32_t i = 0; i < n; ++i) {
grn_section *section = vector->u.v.sections + n0 + i;
if (pe <= p) {
return GRN_INVALID_ARGUMENT;
}
if (flags & GRN_VECTOR_PACK_WEIGHT_FLOAT32) {
grn_memcpy(&(section->weight), p, sizeof(float));
p += sizeof(float);
} else if (flags & GRN_VECTOR_PACK_WEIGHT_BFLOAT16) {
#ifdef GRN_HAVE_BFLOAT16
grn_bfloat16 weight_bfloat16;
grn_memcpy(&weight_bfloat16, p, sizeof(grn_bfloat16));
p += sizeof(grn_bfloat16);
section->weight = grn_bfloat16_to_float32(weight_bfloat16);
#else
grn_memcpy(&(section->weight), p, sizeof(float));
p += sizeof(float);
#endif
} else {
uint32_t weight;
GRN_B_DEC(weight, p);
section->weight = static_cast<float>(weight);
}
GRN_B_DEC(section->domain, p);
}
}
}
vector->u.v.n_sections += n;
if (used_size) {
*used_size = p - start;
}
return GRN_SUCCESS;
}
grn_rc
grn_vector_add_element(grn_ctx *ctx,
grn_obj *vector,
const char *str,
uint32_t str_len,
uint32_t weight,
grn_id domain)
{
return grn_vector_add_element_float(ctx,
vector,
str,
str_len,
static_cast<float>(weight),
domain);
}
grn_rc
grn_vector_add_element_float(grn_ctx *ctx,
grn_obj *vector,
const char *str,
uint32_t str_len,
float weight,
grn_id domain)
{
grn_obj *body;
GRN_API_ENTER;
if (!vector) {
ERR(GRN_INVALID_ARGUMENT, "vector is null");
goto exit;
}
if ((body = grn_vector_body(ctx, vector))) {
grn_bulk_write(ctx, body, str, str_len);
grn_vector_delimit(ctx, vector, weight, domain);
}
exit:
GRN_API_RETURN(ctx->rc);
}
grn_rc
grn_vector_copy(grn_ctx *ctx, grn_obj *src, grn_obj *dest)
{
GRN_API_ENTER;
uint32_t n_elements = grn_vector_size(ctx, src);
uint32_t i;
for (i = 0; i < n_elements; i++) {
const char *content;
float weight;
grn_id domain;
uint32_t content_size =
grn_vector_get_element_float(ctx, src, i, &content, &weight, &domain);
grn_vector_add_element_float(ctx,
dest,
content,
content_size,
weight,
domain);
}
GRN_API_RETURN(ctx->rc);
}
grn_obj *
grn_vector_join(grn_ctx *ctx,
grn_obj *vector,
const char *separator,
int separator_length,
grn_obj *destination)
{
GRN_API_ENTER;
if (!vector) {
ERR(GRN_INVALID_ARGUMENT, "[vector][join] vector is NULL");
GRN_API_RETURN(NULL);
}
if (vector->header.type != GRN_VECTOR) {
grn_obj type_name;
GRN_TEXT_INIT(&type_name, 0);
grn_inspect_type(ctx, &type_name, vector->header.type);
ERR(GRN_INVALID_ARGUMENT,
"[vector][join] must be GRN_UVECTOR: %.*s",
(int)GRN_TEXT_LEN(&type_name),
GRN_TEXT_VALUE(&type_name));
GRN_OBJ_FIN(ctx, &type_name);
GRN_API_RETURN(NULL);
}
if (separator_length < 0) {
separator_length = (int)strlen(separator);
}
if (!destination) {
destination = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_TEXT);
}
uint32_t n_elements = grn_vector_size(ctx, vector);
uint32_t i;
grn_obj element;
GRN_TEXT_INIT(&element, GRN_OBJ_DO_SHALLOW_COPY);
for (i = 0; i < n_elements; i++) {
if (i > 0 && separator_length > 0) {
GRN_TEXT_PUT(ctx, destination, separator, separator_length);
}
const char *content;
grn_id domain;
uint32_t content_size =
grn_vector_get_element_float(ctx, vector, i, &content, NULL, &domain);
GRN_TEXT_SET(ctx, &element, content, content_size);
element.header.domain = domain;
grn_obj_cast(ctx, &element, destination, false);
element.header.domain = GRN_DB_TEXT;
}
GRN_OBJ_FIN(ctx, &element);
GRN_API_RETURN(destination);
}
uint32_t
grn_uvector_size(grn_ctx *ctx, grn_obj *uvector)
{
if (!uvector) {
ERR(GRN_INVALID_ARGUMENT, "uvector must not be NULL");
return 0;
}
if (uvector->header.type != GRN_UVECTOR) {
grn_obj type_name;
GRN_TEXT_INIT(&type_name, 0);
grn_inspect_type(ctx, &type_name, uvector->header.type);
ERR(GRN_INVALID_ARGUMENT,
"must be GRN_UVECTOR: %.*s",
(int)GRN_TEXT_LEN(&type_name),
GRN_TEXT_VALUE(&type_name));
GRN_OBJ_FIN(ctx, &type_name);
return 0;
}
GRN_API_ENTER;
size_t size = grn_uvector_size_internal(ctx, uvector);
GRN_API_RETURN(static_cast<uint32_t>(size));
}
uint32_t
grn_uvector_element_size(grn_ctx *ctx, grn_obj *uvector)
{
if (!uvector) {
ERR(GRN_INVALID_ARGUMENT, "uvector must not be NULL");
return 0;
}
if (uvector->header.type != GRN_UVECTOR) {
grn_obj type_name;
GRN_TEXT_INIT(&type_name, 0);
grn_inspect_type(ctx, &type_name, uvector->header.type);
ERR(GRN_INVALID_ARGUMENT,
"must be GRN_UVECTOR: %.*s",
(int)GRN_TEXT_LEN(&type_name),
GRN_TEXT_VALUE(&type_name));
GRN_OBJ_FIN(ctx, &type_name);
return 0;
}
GRN_API_ENTER;
uint32_t element_size = grn_uvector_element_size_internal(ctx, uvector);
GRN_API_RETURN(element_size);
}
/* For reference uvector. We can't use this for integer family uvector
such as Int64 uvector. */
grn_rc
grn_uvector_add_element(grn_ctx *ctx,
grn_obj *uvector,
grn_id id,
uint32_t weight)
{
return grn_uvector_add_element_record(ctx,
uvector,
id,
static_cast<float>(weight));
}
grn_rc
grn_uvector_add_element_record(grn_ctx *ctx,
grn_obj *uvector,
grn_id id,
float weight)
{
GRN_API_ENTER;
if (!uvector) {
ERR(GRN_INVALID_ARGUMENT, "[uvector][add-element][record] uvector is null");
goto exit;
}
GRN_RECORD_PUT(ctx, uvector, id);
if (grn_obj_is_weight_uvector(ctx, uvector)) {
if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) {
#ifdef GRN_HAVE_BFLOAT16
GRN_BFLOAT16_PUT(ctx, uvector, grn::numeric::to_bfloat16(weight));
#else
GRN_FLOAT32_PUT(ctx, uvector, weight);
#endif
} else {
GRN_FLOAT32_PUT(ctx, uvector, weight);
}
}
exit:
GRN_API_RETURN(ctx->rc);
}
/* For reference uvector. We can't use this for integer family uvector
such as Int64 uvector. */
grn_id
grn_uvector_get_element(grn_ctx *ctx,
grn_obj *uvector,
uint32_t offset,
uint32_t *weight)
{
float weight_float;
grn_id id =
grn_uvector_get_element_record(ctx, uvector, offset, &weight_float);
if (weight) {
*weight = static_cast<uint32_t>(weight_float);
}
return id;
}
grn_id
grn_uvector_get_element_record(grn_ctx *ctx,
grn_obj *uvector,
uint32_t offset,
float *weight)
{
grn_id id = GRN_ID_NIL;
GRN_API_ENTER;
if (!uvector) {
ERR(GRN_INVALID_ARGUMENT, "[uvector][get-element][record] uvector is null");
goto exit;
}
if (uvector->header.type != GRN_UVECTOR) {
ERR(GRN_INVALID_ARGUMENT, "[uvector][get-element][record] invalid uvector");
goto exit;
}
{
size_t element_value_size = sizeof(grn_id);
size_t element_size = element_value_size;
if (grn_obj_is_weight_uvector(ctx, uvector)) {
if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) {
#ifdef GRN_HAVE_BFLOAT16
element_size += sizeof(grn_bfloat16);
#else
element_size += sizeof(float);
#endif
} else {
element_size += sizeof(float);
}
}
const char *elements_start = GRN_BULK_HEAD(uvector);
const char *elements_end = GRN_BULK_CURR(uvector);
if (offset > elements_end - elements_start) {
ERR(GRN_RANGE_ERROR,
"[uvector][get-element][record] "
"offset out of range: <%u>/<%" GRN_FMT_SIZE ">",
offset,
elements_end - elements_start);
goto exit;
}
id = *((grn_id *)(elements_start + (element_size * offset)));
if (weight) {
if (grn_obj_is_weight_uvector(ctx, uvector)) {
if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) {
#ifdef GRN_HAVE_BFLOAT16
grn_bfloat16 weight_bfloat16 =
*reinterpret_cast<const grn_bfloat16 *>(
elements_start + (element_size * offset) + element_value_size);
*weight = grn_bfloat16_to_float32(weight_bfloat16);
#else
*weight = *reinterpret_cast<const float *>(
elements_start + (element_size * offset) + element_value_size);
#endif
} else {
*weight = *reinterpret_cast<const float *>(
elements_start + (element_size * offset) + element_value_size);
}
} else {
*weight = 0.0;
}
}
}
exit:
GRN_API_RETURN(id);
}
grn_rc
grn_uvector_copy(grn_ctx *ctx, grn_obj *src, grn_obj *dest)
{
GRN_API_ENTER;
if (src->header.domain != dest->header.domain) {
ERR(GRN_INVALID_ARGUMENT,
"[uvector][copy] different domain: "
"source=<%u> "
"destination=<%u>",
src->header.domain,
dest->header.domain);
GRN_API_RETURN(ctx->rc);
}
bool src_have_weight = grn_obj_is_weight_uvector(ctx, src);
bool dest_have_weight = grn_obj_is_weight_uvector(ctx, dest);
if (src_have_weight != dest_have_weight) {
ERR(GRN_INVALID_ARGUMENT,
"[uvector][copy] different weight availability: "
"source=<%s> "
"destination=<%s>",
src_have_weight ? "true" : "false",
dest_have_weight ? "true" : "false");
GRN_API_RETURN(ctx->rc);
}
grn_bulk_write(ctx, dest, GRN_BULK_HEAD(src), GRN_BULK_VSIZE(src));
GRN_API_RETURN(ctx->rc);
}
grn_obj *
grn_uvector_join(grn_ctx *ctx,
grn_obj *uvector,
const char *separator,
int separator_length,
grn_obj *destination)
{
GRN_API_ENTER;
if (!uvector) {
ERR(GRN_INVALID_ARGUMENT, "[uvector][join] uvector is NULL");
GRN_API_RETURN(NULL);
}
if (uvector->header.type != GRN_UVECTOR) {
grn_obj type_name;
GRN_TEXT_INIT(&type_name, 0);
grn_inspect_type(ctx, &type_name, uvector->header.type);
ERR(GRN_INVALID_ARGUMENT,
"[uvector][join] must be GRN_UVECTOR: %.*s",
(int)GRN_TEXT_LEN(&type_name),
GRN_TEXT_VALUE(&type_name));
GRN_OBJ_FIN(ctx, &type_name);
GRN_API_RETURN(NULL);
}
if (separator_length < 0) {
separator_length = (int)strlen(separator);
}
if (!destination) {
destination = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_TEXT);
}
uint32_t element_size = grn_uvector_element_size_internal(ctx, uvector);
uint32_t element_content_size = element_size;
if (grn_obj_is_weight_uvector(ctx, uvector)) {
if (uvector->header.flags & GRN_OBJ_WEIGHT_BFLOAT16) {
#ifdef GRN_HAVE_BFLOAT16
element_content_size -= static_cast<uint32_t>(sizeof(grn_bfloat16));
#else
element_content_size -= static_cast<uint32_t>(sizeof(float));
#endif
} else {
element_content_size -= static_cast<uint32_t>(sizeof(float));
}
}
uint32_t n_elements = grn_uvector_size_internal(ctx, uvector);
uint32_t i;
grn_obj element;
GRN_VALUE_FIX_SIZE_INIT(&element,
GRN_OBJ_DO_SHALLOW_COPY,
uvector->header.domain);
for (i = 0; i < n_elements; i++) {
if (i > 0 && separator_length > 0) {
GRN_TEXT_PUT(ctx, destination, separator, separator_length);
}
GRN_TEXT_SET(ctx,
&element,
GRN_BULK_HEAD(uvector) + (element_size * i),
element_content_size);
grn_obj_cast(ctx, &element, destination, false);
}
GRN_OBJ_FIN(ctx, &element);
GRN_API_RETURN(destination);
}
grn_obj *
grn_pvector_join(grn_ctx *ctx,
grn_obj *pvector,
const char *separator,
int separator_length,
grn_obj *destination)
{
GRN_API_ENTER;
if (!pvector) {
ERR(GRN_INVALID_ARGUMENT, "[pvector][join] pvector is NULL");
GRN_API_RETURN(NULL);
}
if (pvector->header.type != GRN_PVECTOR) {
grn_obj type_name;
GRN_TEXT_INIT(&type_name, 0);
grn_inspect_type(ctx, &type_name, pvector->header.type);
ERR(GRN_INVALID_ARGUMENT,
"[pvector][join] must be GRN_PVECTOR: %.*s",
(int)GRN_TEXT_LEN(&type_name),
GRN_TEXT_VALUE(&type_name));
GRN_OBJ_FIN(ctx, &type_name);
GRN_API_RETURN(NULL);
}
if (separator_length < 0) {
separator_length = (int)strlen(separator);
}
if (!destination) {
destination = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_TEXT);
}
size_t n_elements = GRN_PTR_VECTOR_SIZE(pvector);
size_t i;
for (i = 0; i < n_elements; i++) {
if (i > 0 && separator_length > 0) {
GRN_TEXT_PUT(ctx, destination, separator, separator_length);
}
grn_obj *element = GRN_PTR_VALUE_AT(pvector, i);
grn_obj_cast(ctx, element, destination, false);
}
GRN_API_RETURN(destination);
}
}
|