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