| 1523 | } |
| 1524 | |
| 1525 | Value CreateVector(size_t start, size_t vec_len, size_t step, bool typed, |
| 1526 | bool fixed, const Value *keys = nullptr) { |
| 1527 | FLATBUFFERS_ASSERT( |
| 1528 | !fixed || |
| 1529 | typed); // typed=false, fixed=true combination is not supported. |
| 1530 | // Figure out smallest bit width we can store this vector with. |
| 1531 | auto bit_width = (std::max)(force_min_bit_width_, WidthU(vec_len)); |
| 1532 | auto prefix_elems = 1; |
| 1533 | if (keys) { |
| 1534 | // If this vector is part of a map, we will pre-fix an offset to the keys |
| 1535 | // to this vector. |
| 1536 | bit_width = (std::max)(bit_width, keys->ElemWidth(buf_.size(), 0)); |
| 1537 | prefix_elems += 2; |
| 1538 | } |
| 1539 | Type vector_type = FBT_KEY; |
| 1540 | // Check bit widths and types for all elements. |
| 1541 | for (size_t i = start; i < stack_.size(); i += step) { |
| 1542 | auto elem_width = |
| 1543 | stack_[i].ElemWidth(buf_.size(), i - start + prefix_elems); |
| 1544 | bit_width = (std::max)(bit_width, elem_width); |
| 1545 | if (typed) { |
| 1546 | if (i == start) { |
| 1547 | vector_type = stack_[i].type_; |
| 1548 | } else { |
| 1549 | // If you get this assert, you are writing a typed vector with |
| 1550 | // elements that are not all the same type. |
| 1551 | FLATBUFFERS_ASSERT(vector_type == stack_[i].type_); |
| 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | // If you get this assert, your fixed types are not one of: |
| 1556 | // Int / UInt / Float / Key. |
| 1557 | FLATBUFFERS_ASSERT(!fixed || IsTypedVectorElementType(vector_type)); |
| 1558 | auto byte_width = Align(bit_width); |
| 1559 | // Write vector. First the keys width/offset if available, and size. |
| 1560 | if (keys) { |
| 1561 | WriteOffset(keys->u_, byte_width); |
| 1562 | Write<uint64_t>(1ULL << keys->min_bit_width_, byte_width); |
| 1563 | } |
| 1564 | if (!fixed) Write<uint64_t>(vec_len, byte_width); |
| 1565 | // Then the actual data. |
| 1566 | auto vloc = buf_.size(); |
| 1567 | for (size_t i = start; i < stack_.size(); i += step) { |
| 1568 | WriteAny(stack_[i], byte_width); |
| 1569 | } |
| 1570 | // Then the types. |
| 1571 | if (!typed) { |
| 1572 | for (size_t i = start; i < stack_.size(); i += step) { |
| 1573 | buf_.push_back(stack_[i].StoredPackedType(bit_width)); |
| 1574 | } |
| 1575 | } |
| 1576 | return Value(static_cast<uint64_t>(vloc), |
| 1577 | keys ? FBT_MAP |
| 1578 | : (typed ? ToTypedVector(vector_type, fixed ? vec_len : 0) |
| 1579 | : FBT_VECTOR), |
| 1580 | bit_width); |
| 1581 | } |
| 1582 |
no test coverage detected