addDoubleIndirectly method

void addDoubleIndirectly(
  1. double value, {
  2. bool cache = false,
})

Stores double value indirectly in the buffer.

Double are stored as 8 or 4 byte values in FlexBuffers. If they are stored in a mixed vector, values which are smaller than 4 / 8 bytes will be padded. When we add double indirectly, the vector will contain not the value itself, but only the relative offset to the value. Which could occupy only 1 or 2 bytes, reducing the odds for unnecessary padding. By setting the cache parameter to true, you make sure that the builder tracks already added double value and performs deduplication.

Implementation

void addDoubleIndirectly(double value, {bool cache = false}) {
  _integrityCheckOnValueAddition();
  if (cache && _indirectDoubleCache.containsKey(value)) {
    _stack.add(_indirectDoubleCache[value]!);
    return;
  }
  final stackValue = _StackValue.withDouble(value);
  final byteWidth = _align(stackValue.width);
  final newOffset = _newOffset(byteWidth);
  final valueOffset = _offset;
  _pushBuffer(stackValue.asU8List(stackValue.width));
  final stackOffset = _StackValue.withOffset(
      valueOffset, ValueType.IndirectFloat, stackValue.width);
  _stack.add(stackOffset);
  _offset = newOffset;
  if (cache) {
    _indirectDoubleCache[value] = stackOffset;
  }
}