Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/src/zserio/array.py: 100%

713 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2026-04-23 10:05 +0000

1""" 

2The module implements abstraction for arrays used by Zserio python extension. 

3""" 

4 

5import typing 

6 

7from zserio.bitposition import alignto 

8from zserio.bitsizeof import ( 

9 bitsizeof_varuint16, 

10 bitsizeof_varuint32, 

11 bitsizeof_varuint64, 

12 bitsizeof_varuint, 

13 bitsizeof_varint16, 

14 bitsizeof_varint32, 

15 bitsizeof_varint64, 

16 bitsizeof_varint, 

17 bitsizeof_varsize, 

18 bitsizeof_bytes, 

19 bitsizeof_string, 

20 bitsizeof_bitbuffer, 

21) 

22from zserio.bitreader import BitStreamReader 

23from zserio.bitwriter import BitStreamWriter 

24from zserio.bitbuffer import BitBuffer 

25from zserio.hashcode import ( 

26 HASH_SEED, 

27 calc_hashcode_bool_array, 

28 calc_hashcode_int_array, 

29 calc_hashcode_float32_array, 

30 calc_hashcode_float64_array, 

31 calc_hashcode_bytes_array, 

32 calc_hashcode_string_array, 

33 calc_hashcode_object_array, 

34) 

35from zserio.exception import PythonRuntimeException 

36 

37 

38class Array: 

39 """ 

40 Abstraction for arrays to which Zserio arrays are mapped in python. 

41 """ 

42 

43 def __init__( 

44 self, 

45 array_traits: typing.Any, 

46 raw_array: typing.Optional[typing.List] = None, 

47 *, 

48 is_auto: bool = False, 

49 is_implicit: bool = False, 

50 set_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None, 

51 check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None 

52 ) -> None: 

53 """ 

54 Constructor. 

55 

56 :param array_traits: Array traits which specify the array type. 

57 :param raw_array: Native python list which will be hold by this abstraction. 

58 :param is_auto: True if mapped Zserio array is auto array. 

59 :param is_implicit: True if mapped Zserio array is implicit array. 

60 :param set_offset_method: Set offset method if mapped Zserio array is indexed offset array. 

61 :param check_offset_method: Check offset method if mapped Zserio array is indexed offset array. 

62 """ 

63 

64 self._raw_array: typing.List = [] if raw_array is None else raw_array 

65 self._array_traits: typing.Any = array_traits 

66 self._packed_array_traits: typing.Any = array_traits.packed_traits 

67 self._is_auto: bool = is_auto 

68 self._is_implicit: bool = is_implicit 

69 self._set_offset_method: typing.Optional[typing.Callable[[int, int], None]] = set_offset_method 

70 self._check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = check_offset_method 

71 

72 @classmethod 

73 def from_reader( 

74 cls: typing.Type["Array"], 

75 array_traits: typing.Any, 

76 reader: BitStreamReader, 

77 size: int = 0, 

78 *, 

79 is_auto: bool = False, 

80 is_implicit: bool = False, 

81 set_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None, 

82 check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None 

83 ) -> "Array": 

84 """ 

85 Constructs array and reads elements from the given bit stream reader. 

86 

87 :param array_traits: Array traits which specify the array type. 

88 :param reader: Bit stream from which to read. 

89 :param size: Number of elements to read or None in case of implicit or auto arrays. 

90 :param raw_array: Native python list which will be hold by this abstraction. 

91 :param is_auto: True if mapped Zserio array is auto array. 

92 :param is_implicit: True if mapped Zserio array is implicit array. 

93 :param set_offset_method: Set offset method if mapped Zserio array is indexed offset array. 

94 :param check_offset_method: Check offset method if mapped Zserio array is indexed offset array. 

95 :returns: Array instance filled using given bit stream reader. 

96 """ 

97 

98 instance = cls( 

99 array_traits, 

100 is_auto=is_auto, 

101 is_implicit=is_implicit, 

102 set_offset_method=set_offset_method, 

103 check_offset_method=check_offset_method, 

104 ) 

105 instance.read(reader, size) 

106 

107 return instance 

108 

109 @classmethod 

110 def from_reader_packed( 

111 cls: typing.Type["Array"], 

112 array_traits: typing.Any, 

113 reader: BitStreamReader, 

114 size: int = 0, 

115 *, 

116 is_auto: bool = False, 

117 set_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None, 

118 check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None 

119 ) -> "Array": 

120 """ 

121 Constructs packed array and reads elements from the given bit stream reader. 

122 

123 :param array_traits: Array traits which specify the array type. 

124 :param reader: Bit stream from which to read. 

125 :param size: Number of elements to read or None in case of implicit or auto arrays. 

126 :param raw_array: Native python list which will be hold by this abstraction. 

127 :param is_auto: True if mapped Zserio array is auto array. 

128 :param set_offset_method: Set offset method if mapped Zserio array is indexed offset array. 

129 :param check_offset_method: Check offset method if mapped Zserio array is indexed offset array. 

130 :returns: Array instance filled using given bit stream reader. 

131 """ 

132 

133 instance = cls( 

134 array_traits, 

135 is_auto=is_auto, 

136 is_implicit=False, 

137 set_offset_method=set_offset_method, 

138 check_offset_method=check_offset_method, 

139 ) 

140 instance.read_packed(reader, size) 

141 

142 return instance 

143 

144 def __eq__(self, other: object) -> bool: 

145 # it's enough to check only raw_array because compound types which call this are always the same type 

146 if isinstance(other, Array): 

147 return self._raw_array == other._raw_array 

148 

149 return False 

150 

151 def __hash__(self) -> int: 

152 return self._array_traits.CALC_HASHCODE_FUNC(HASH_SEED, self._raw_array) 

153 

154 def __len__(self) -> int: 

155 return len(self._raw_array) 

156 

157 def __getitem__(self, key: int) -> typing.Any: 

158 return self._raw_array[key] 

159 

160 def __setitem__(self, key: int, value: typing.Any) -> None: 

161 self._raw_array[key] = value 

162 

163 @property 

164 def raw_array(self) -> typing.List: 

165 """ 

166 Gets raw array. 

167 

168 :returns: Native python list which is hold by the array. 

169 """ 

170 

171 return self._raw_array 

172 

173 def bitsizeof(self, bitposition: int) -> int: 

174 """ 

175 Returns length of array stored in the bit stream in bits. 

176 

177 :param bitposition: Current bit stream position. 

178 :returns: Length of the array stored in the bit stream in bits. 

179 """ 

180 

181 end_bitposition = bitposition 

182 size = len(self._raw_array) 

183 if self._is_auto: 

184 end_bitposition += bitsizeof_varsize(size) 

185 

186 if self._array_traits.HAS_BITSIZEOF_CONSTANT and size > 0: 

187 element_size = self._array_traits.bitsizeof() 

188 if self._set_offset_method is None: 

189 end_bitposition += size * element_size 

190 else: 

191 end_bitposition = alignto(8, end_bitposition) 

192 end_bitposition += element_size + (size - 1) * alignto(8, element_size) 

193 else: 

194 array_traits_bitsizeof = self._array_traits.bitsizeof 

195 if self._set_offset_method is not None: 

196 if self._array_traits.NEEDS_BITSIZEOF_POSITION: 

197 for element in self._raw_array: 

198 end_bitposition = alignto(8, end_bitposition) 

199 end_bitposition += array_traits_bitsizeof(end_bitposition, element) 

200 else: 

201 for element in self._raw_array: 

202 end_bitposition = alignto(8, end_bitposition) 

203 end_bitposition += array_traits_bitsizeof(element) 

204 else: 

205 if self._array_traits.NEEDS_BITSIZEOF_POSITION: 

206 for element in self._raw_array: 

207 end_bitposition += array_traits_bitsizeof(end_bitposition, element) 

208 else: 

209 for element in self._raw_array: 

210 end_bitposition += array_traits_bitsizeof(element) 

211 

212 return end_bitposition - bitposition 

213 

214 def bitsizeof_packed(self, bitposition: int) -> int: 

215 """ 

216 Returns length of the packed array stored in the bit stream in bits. 

217 

218 :param bitposition: Current bit stream position. 

219 :returns: Length of the array stored in the bit stream in bits. 

220 """ 

221 

222 end_bitposition = bitposition 

223 size = len(self._raw_array) 

224 if self._is_auto: 

225 end_bitposition += bitsizeof_varsize(size) 

226 

227 if size > 0: 

228 context = self._packed_array_traits.create_context() 

229 packed_array_traits_init_context = self._packed_array_traits.init_context 

230 packed_array_traits_bitsizeof = self._packed_array_traits.bitsizeof 

231 

232 for element in self._raw_array: 

233 packed_array_traits_init_context(context, element) 

234 

235 if self._set_offset_method is not None: 

236 for element in self._raw_array: 

237 end_bitposition = alignto(8, end_bitposition) 

238 end_bitposition += packed_array_traits_bitsizeof(context, end_bitposition, element) 

239 else: 

240 for element in self._raw_array: 

241 end_bitposition += packed_array_traits_bitsizeof(context, end_bitposition, element) 

242 

243 return end_bitposition - bitposition 

244 

245 def initialize_offsets(self, bitposition: int) -> int: 

246 """ 

247 Initializes indexed offsets for the array. 

248 

249 :param bitposition: Current bit stream position. 

250 :returns: Updated bit stream position which points to the first bit after the array. 

251 """ 

252 

253 end_bitposition = bitposition 

254 size = len(self._raw_array) 

255 if self._is_auto: 

256 end_bitposition += bitsizeof_varsize(size) 

257 

258 array_traits_initialize_offsets = self._array_traits.initialize_offsets 

259 if self._set_offset_method is not None: 

260 set_offset = self._set_offset_method 

261 for index in range(size): 

262 end_bitposition = alignto(8, end_bitposition) 

263 set_offset(index, end_bitposition) 

264 end_bitposition = array_traits_initialize_offsets(end_bitposition, self._raw_array[index]) 

265 else: 

266 for element in self._raw_array: 

267 end_bitposition = array_traits_initialize_offsets(end_bitposition, element) 

268 

269 return end_bitposition 

270 

271 def initialize_offsets_packed(self, bitposition: int) -> int: 

272 """ 

273 Initializes indexed offsets for the packed array. 

274 

275 :param bitposition: Current bit stream position. 

276 :returns: Updated bit stream position which points to the first bit after the array. 

277 """ 

278 

279 end_bitposition = bitposition 

280 size = len(self._raw_array) 

281 if self._is_auto: 

282 end_bitposition += bitsizeof_varsize(size) 

283 

284 if size > 0: 

285 context = self._packed_array_traits.create_context() 

286 packed_array_traits_init_context = self._packed_array_traits.init_context 

287 packed_array_traits_initialize_offsets = self._packed_array_traits.initialize_offsets 

288 

289 for element in self._raw_array: 

290 packed_array_traits_init_context(context, element) 

291 

292 if self._set_offset_method is not None: 

293 set_offset = self._set_offset_method 

294 for index in range(size): 

295 end_bitposition = alignto(8, end_bitposition) 

296 set_offset(index, end_bitposition) 

297 end_bitposition = packed_array_traits_initialize_offsets( 

298 context, end_bitposition, self._raw_array[index] 

299 ) 

300 else: 

301 for element in self._raw_array: 

302 end_bitposition = packed_array_traits_initialize_offsets(context, end_bitposition, element) 

303 

304 return end_bitposition 

305 

306 def read(self, reader: BitStreamReader, size: int = 0) -> None: 

307 """ 

308 Reads array from the bit stream. 

309 

310 :param reader: Bit stream from which to read. 

311 :param size: Number of elements to read or None in case of implicit or auto arrays. 

312 

313 :raises PythonRuntimeException: If the implicit array does not have elements with constant bit size. 

314 """ 

315 

316 self._raw_array.clear() 

317 

318 append = self._raw_array.append 

319 array_traits_read = self._array_traits.read 

320 if self._is_implicit: 

321 if not self._array_traits.HAS_BITSIZEOF_CONSTANT: 

322 raise PythonRuntimeException("Array: Implicit array elements must have constant bit size!") 

323 

324 element_size = self._array_traits.bitsizeof() 

325 remaining_bits = reader.buffer_bitsize - reader.bitposition 

326 read_size = remaining_bits // element_size 

327 for _ in range(read_size): 

328 # we know that no traits NEEDS_READ_INDEX here 

329 append(array_traits_read(reader)) 

330 else: 

331 if self._is_auto: 

332 read_size = reader.read_varsize() 

333 else: 

334 read_size = size 

335 

336 if self._array_traits.HAS_BITSIZEOF_CONSTANT: 

337 element_size = self._array_traits.bitsizeof() 

338 if reader.bitposition + read_size * element_size > reader.buffer_bitsize: 

339 raise PythonRuntimeException("BitStreamReader: Reading beyond the stream end!") 

340 

341 if self._check_offset_method is not None: 

342 check_offset = self._check_offset_method 

343 reader_alignto = reader.alignto 

344 if self._array_traits.NEEDS_READ_INDEX: 

345 for index in range(read_size): 

346 reader_alignto(8) 

347 check_offset(index, reader.bitposition) 

348 append(array_traits_read(reader, index)) 

349 else: 

350 for index in range(read_size): 

351 reader_alignto(8) 

352 check_offset(index, reader.bitposition) 

353 append(array_traits_read(reader)) 

354 else: 

355 if self._array_traits.NEEDS_READ_INDEX: 

356 for index in range(read_size): 

357 append(array_traits_read(reader, index)) 

358 else: 

359 for _ in range(read_size): 

360 append(array_traits_read(reader)) 

361 

362 def read_packed(self, reader: BitStreamReader, size: int = 0) -> None: 

363 """ 

364 Reads packed array from the bit stream. 

365 

366 :param reader: Bit stream from which to read. 

367 :param size: Number of elements to read or 0 in case of auto arrays. 

368 """ 

369 

370 self._raw_array.clear() 

371 

372 if self._is_implicit: 

373 raise PythonRuntimeException("Array: Implicit array cannot be packed!") 

374 

375 if self._is_auto: 

376 read_size = reader.read_varsize() 

377 else: 

378 read_size = size 

379 

380 if read_size > 0: 

381 context = self._packed_array_traits.create_context() 

382 

383 append = self._raw_array.append 

384 packed_array_traits_read = self._packed_array_traits.read 

385 

386 if self._check_offset_method is not None: 

387 check_offset = self._check_offset_method 

388 reader_alignto = reader.alignto 

389 for index in range(read_size): 

390 reader_alignto(8) 

391 check_offset(index, reader.bitposition) 

392 append(packed_array_traits_read(context, reader, index)) 

393 else: 

394 for index in range(read_size): 

395 append(packed_array_traits_read(context, reader, index)) 

396 

397 def write(self, writer: BitStreamWriter) -> None: 

398 """ 

399 Writes array to the bit stream. 

400 

401 :param writer: Bit stream where to write. 

402 """ 

403 

404 size = len(self._raw_array) 

405 if self._is_auto: 

406 writer.write_varsize(size) 

407 

408 array_traits_write = self._array_traits.write 

409 if self._check_offset_method is not None: 

410 check_offset = self._check_offset_method 

411 writer_alignto = writer.alignto 

412 for index in range(size): 

413 writer_alignto(8) 

414 check_offset(index, writer.bitposition) 

415 array_traits_write(writer, self._raw_array[index]) 

416 else: 

417 for element in self._raw_array: 

418 array_traits_write(writer, element) 

419 

420 def write_packed(self, writer: BitStreamWriter) -> None: 

421 """ 

422 Writes packed array to the bit stream. 

423 

424 :param writer: Bit stream where to write. 

425 """ 

426 

427 size = len(self._raw_array) 

428 

429 if self._is_auto: 

430 writer.write_varsize(size) 

431 

432 if size > 0: 

433 context = self._packed_array_traits.create_context() 

434 packed_array_traits_init_context = self._packed_array_traits.init_context 

435 packed_array_traits_write = self._packed_array_traits.write 

436 

437 for element in self._raw_array: 

438 packed_array_traits_init_context(context, element) 

439 

440 if self._check_offset_method is not None: 

441 check_offset = self._check_offset_method 

442 writer_alignto = writer.alignto 

443 for index in range(size): 

444 writer_alignto(8) 

445 check_offset(index, writer.bitposition) 

446 packed_array_traits_write(context, writer, self._raw_array[index]) 

447 else: 

448 for element in self._raw_array: 

449 packed_array_traits_write(context, writer, element) 

450 

451 

452class DeltaContext: 

453 """ 

454 Context for delta packing created for each packable field. 

455 

456 Contexts are always newly created for each array operation (bitsizeof, initialize_offsets, read, write). 

457 They must be initialized at first via calling the init method for each packable element present in the 

458 array. After the full initialization, only a single method (bitsizeof, read, write) can be repeatedly 

459 called for exactly the same sequence of packable elements. 

460 

461 Example:: 

462 

463 context = DeltaContext(array_traits) 

464 for element in array: 

465 context.init(element) # initialization step, needed for max_bit_number calculation 

466 context.write_descriptor(writer) # finishes the initialization 

467 for element in array: 

468 context.write(writer, element) 

469 """ 

470 

471 def __init__(self) -> None: 

472 """Constructor.""" 

473 

474 self._is_packed = False 

475 self._max_bit_number = 0 

476 self._previous_element: typing.Optional[int] = None 

477 self._processing_started = False 

478 self._unpacked_bitsize = 0 

479 self._first_element_bitsize = 0 

480 self._num_elements = 0 

481 

482 def init(self, array_traits: typing.Any, element: int) -> None: 

483 """ 

484 Makes initialization step for the provided array element. 

485 

486 :param array_traits: Standard array traits. 

487 :param element: Current element of the array. 

488 """ 

489 

490 self._num_elements += 1 

491 self._unpacked_bitsize += DeltaContext._bitsizeof_unpacked(array_traits, element) 

492 

493 if self._previous_element is None: 

494 self._previous_element = element 

495 self._first_element_bitsize = self._unpacked_bitsize 

496 else: 

497 if self._max_bit_number <= self._MAX_BIT_NUMBER_LIMIT: 

498 self._is_packed = True 

499 

500 delta = element - self._previous_element 

501 max_bit_number = delta.bit_length() 

502 # if delta is negative, we need one bit more because of sign 

503 # if delta is positive, we need one bit more because delta are treated as signed number 

504 # if delta is zero, we need one bit more because bit_length() returned zero 

505 if max_bit_number > self._max_bit_number: 

506 self._max_bit_number = max_bit_number 

507 if max_bit_number > self._MAX_BIT_NUMBER_LIMIT: 

508 self._is_packed = False 

509 

510 self._previous_element = element 

511 

512 def bitsizeof(self, array_traits: typing.Any, element: int) -> int: 

513 """ 

514 Returns length of the element representation stored in the bit stream in bits. 

515 

516 :param array_traits: Standard integral array traits. 

517 :param element: Current element. 

518 

519 :returns: Length of the element representation stored in the bit stream in bits. 

520 """ 

521 

522 if not self._processing_started: 

523 self._processing_started = True 

524 self._finish_init() 

525 

526 return self._bitsizeof_descriptor() + DeltaContext._bitsizeof_unpacked(array_traits, element) 

527 elif not self._is_packed: 

528 return DeltaContext._bitsizeof_unpacked(array_traits, element) 

529 else: 

530 return self._max_bit_number + 1 if self._max_bit_number > 0 else 0 

531 

532 def read(self, array_traits: typing.Any, reader: BitStreamReader) -> int: 

533 """ 

534 Reads the packed element from the bit stream. 

535 

536 :param array_traits: Standard array traits. 

537 :param reader: Bit stream reader. 

538 """ 

539 

540 if not self._processing_started: 

541 self._processing_started = True 

542 self._read_descriptor(reader) 

543 

544 return self._read_unpacked(array_traits, reader) 

545 elif not self._is_packed: 

546 return self._read_unpacked(array_traits, reader) 

547 else: 

548 assert self._previous_element is not None 

549 if self._max_bit_number > 0: 

550 delta = reader.read_signed_bits_unchecked(self._max_bit_number + 1) 

551 self._previous_element += delta 

552 return self._previous_element 

553 

554 def write(self, array_traits: typing.Any, writer: BitStreamWriter, element: int) -> None: 

555 """ 

556 Writes the packed element representation to the bit stream. 

557 

558 :param array_traits: Standard array traits. 

559 :param writer: Bit stream writer. 

560 :param element: Element to write. 

561 """ 

562 

563 if not self._processing_started: 

564 self._processing_started = True 

565 self._finish_init() 

566 self._write_descriptor(writer) 

567 

568 self._write_unpacked(array_traits, writer, element) 

569 elif not self._is_packed: 

570 self._write_unpacked(array_traits, writer, element) 

571 else: # packed and not first 

572 assert self._previous_element is not None 

573 if self._max_bit_number > 0: 

574 delta = element - self._previous_element 

575 writer.write_signed_bits_unchecked(delta, self._max_bit_number + 1) 

576 self._previous_element = element 

577 

578 def _finish_init(self) -> None: 

579 if self._is_packed: 

580 delta_bitsize = self._max_bit_number + 1 if self._max_bit_number > 0 else 0 

581 packed_bitsize_with_descriptor = ( 

582 1 

583 + self._MAX_BIT_NUMBER_BITS # descriptor 

584 + self._first_element_bitsize 

585 + (self._num_elements - 1) * delta_bitsize 

586 ) 

587 unpacked_bitsize_with_descriptor = 1 + self._unpacked_bitsize 

588 if packed_bitsize_with_descriptor >= unpacked_bitsize_with_descriptor: 

589 self._is_packed = False 

590 

591 def _bitsizeof_descriptor(self) -> int: 

592 if self._is_packed: 

593 return 1 + self._MAX_BIT_NUMBER_BITS 

594 else: 

595 return 1 

596 

597 @staticmethod 

598 def _bitsizeof_unpacked(array_traits: typing.Any, element: int) -> int: 

599 if array_traits.HAS_BITSIZEOF_CONSTANT: 

600 return array_traits.bitsizeof() 

601 else: # we know that NEEDS_BITSIZEOF_POSITION is False here 

602 return array_traits.bitsizeof(element) 

603 

604 def _read_descriptor(self, reader: BitStreamReader) -> None: 

605 self._is_packed = reader.read_bool() 

606 if self._is_packed: 

607 self._max_bit_number = reader.read_bits_unchecked(self._MAX_BIT_NUMBER_BITS) 

608 

609 def _read_unpacked(self, array_traits: typing.Any, reader: BitStreamReader) -> int: 

610 element = array_traits.read(reader) 

611 self._previous_element = element 

612 return element 

613 

614 def _write_descriptor(self, writer: BitStreamWriter) -> None: 

615 writer.write_bool(self._is_packed) 

616 if self._is_packed: 

617 writer.write_bits_unchecked(self._max_bit_number, self._MAX_BIT_NUMBER_BITS) 

618 

619 def _write_unpacked(self, array_traits: typing.Any, writer: BitStreamWriter, element: int) -> None: 

620 self._previous_element = element 

621 array_traits.write(writer, element) 

622 

623 _MAX_BIT_NUMBER_BITS = 6 

624 _MAX_BIT_NUMBER_LIMIT = 62 

625 

626 

627class PackedArrayTraits: 

628 """ 

629 Packed array traits. 

630 

631 Packed array traits are used for all built-in types. 

632 """ 

633 

634 def __init__(self, array_traits: typing.Any) -> None: 

635 """ 

636 Constructor. 

637 

638 :param array_traits: Standard array traits. 

639 """ 

640 

641 self._array_traits = array_traits 

642 

643 @staticmethod 

644 def create_context() -> DeltaContext: 

645 """ 

646 Creates new packing context - DeltaContext is used for non-object arrays. 

647 

648 :returns: New packing context. 

649 """ 

650 

651 return DeltaContext() 

652 

653 def init_context(self, delta_context: DeltaContext, element: int) -> None: 

654 """ 

655 Calls context initialization step for the current element. 

656 

657 :param delta_context: Delta context. 

658 :param element: Current element. 

659 """ 

660 

661 delta_context.init(self._array_traits, element) 

662 

663 def bitsizeof(self, delta_context: DeltaContext, _bitposition: int, element: int) -> int: 

664 """ 

665 Returns length of the array element stored in the bit stream in bits. 

666 

667 :param delta_context: Delta context. 

668 :param _bitposition: Current bit stream position (not used). 

669 :param elemnet: Current element. 

670 :returns: Length of the array element stored in the bit stream in bits. 

671 """ 

672 

673 return delta_context.bitsizeof(self._array_traits, element) 

674 

675 def initialize_offsets(self, delta_context: DeltaContext, bitposition: int, element: int) -> int: 

676 """ 

677 Calls indexed offsets initialization for the current element. 

678 

679 :param delta_context: Delta context. 

680 :param _bitposition: Current bit stream position. 

681 :param element: Current element. 

682 :returns: Updated bit stream position which points to the first bit after this element. 

683 """ 

684 

685 return bitposition + delta_context.bitsizeof(self._array_traits, element) 

686 

687 def write(self, delta_context: DeltaContext, writer: BitStreamWriter, element: int) -> None: 

688 """ 

689 Writes the element to the bit stream. 

690 

691 :param delta_context: Delta context. 

692 :param writer: Bit stream writer. 

693 :param element: Element to write. 

694 """ 

695 

696 delta_context.write(self._array_traits, writer, element) 

697 

698 def read(self, delta_context: DeltaContext, reader: BitStreamReader, _index: int) -> int: 

699 """ 

700 Read an element from the bit stream. 

701 

702 :param delta_context: Delta context. 

703 :param reader: Bit stream reader. 

704 :param _index: Not used. 

705 :returns: Read element value. 

706 """ 

707 

708 return delta_context.read(self._array_traits, reader) 

709 

710 

711class ObjectPackedArrayTraits: 

712 """ 

713 Packed array traits for Zserio objects. 

714 

715 This traits are used for Zserio objects which must implement special *_packed* methods to allow itself 

716 to be used in a packed array. 

717 """ 

718 

719 def __init__(self, element_factory: typing.Any): 

720 """ 

721 Constructor. 

722 

723 :param element_factory: Element factory which creates packed object from the element index. 

724 """ 

725 

726 self._element_factory = element_factory 

727 

728 def create_context(self) -> typing.Any: 

729 """ 

730 Creates new packing context - generated ZserioPackingContext is used for object arrays. 

731 

732 :returns: New packing context. 

733 """ 

734 

735 return self._element_factory.create_packing_context() 

736 

737 @staticmethod 

738 def init_context(context: typing.Any, element: typing.Any) -> None: 

739 """ 

740 Calls context initialization step for the current element. 

741 

742 :param context: Packing context. 

743 :param element: Current element. 

744 """ 

745 

746 element.init_packing_context(context) 

747 

748 @staticmethod 

749 def bitsizeof(context: typing.Any, bitposition: int, element: typing.Any) -> int: 

750 """ 

751 Returns length of the array element stored in the bit stream in bits. 

752 

753 :param context: Packing context. 

754 :param bitposition: Current bit stream position. 

755 :param elemnet: Current element. 

756 :returns: Length of the array element stored in the bit stream in bits. 

757 """ 

758 

759 return element.bitsizeof_packed(context, bitposition) 

760 

761 @staticmethod 

762 def initialize_offsets(context: typing.Any, bitposition: int, element: typing.Any) -> int: 

763 """ 

764 Calls indexed offsets initialization for the current element. 

765 

766 :param context: Packing context. 

767 :param bitposition: Current bit stream position. 

768 :param element: Current element. 

769 :returns: Updated bit stream position which points to the first bit after this element. 

770 """ 

771 

772 return element.initialize_offsets_packed(context, bitposition) 

773 

774 @staticmethod 

775 def write(context: typing.Any, writer: BitStreamWriter, element: typing.Any) -> None: 

776 """ 

777 Writes the element to the bit stream. 

778 

779 :param context: Packing context. 

780 :param writer: Bit stream writer. 

781 :param element: Element to write. 

782 """ 

783 

784 element.write_packed(context, writer) 

785 

786 def read(self, context: typing.Any, reader: BitStreamReader, index: int) -> typing.Any: 

787 """ 

788 Read an element from the bit stream. 

789 

790 :param context: Packing context. 

791 :param reader: Bit stream reader. 

792 :param index: Index of the current element. 

793 :returns: Read element value. 

794 """ 

795 

796 return self._element_factory.create_packed(context, reader, index) 

797 

798 

799class BitFieldArrayTraits: 

800 """ 

801 Array traits for unsigned fixed integer Zserio types (uint16, uint32, uint64, bit:5, etc...). 

802 """ 

803 

804 HAS_BITSIZEOF_CONSTANT = True 

805 NEEDS_BITSIZEOF_POSITION = False 

806 NEEDS_READ_INDEX = False 

807 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

808 

809 def __init__(self, numbits: int) -> None: 

810 """ 

811 Constructor. 

812 

813 :param numbits: Number of bits for unsigned fixed integer Zserio type. 

814 """ 

815 

816 self._numbits = numbits 

817 

818 @property 

819 def packed_traits(self) -> PackedArrayTraits: 

820 """ 

821 Gets packed array traits. 

822 

823 :returns: PackedArrayTraits instance. 

824 """ 

825 

826 return PackedArrayTraits(self) 

827 

828 def bitsizeof(self) -> int: 

829 """ 

830 Returns length of unsigned fixed integer Zserio type stored in the bit stream in bits. 

831 

832 :returns: Length of unsigned fixed integer Zserio type in bits. 

833 """ 

834 

835 return self._numbits 

836 

837 def initialize_offsets(self, bitposition: int, _value: int) -> int: 

838 """ 

839 Initializes indexed offsets for unsigned fixed integer Zserio type. 

840 

841 :param bitposition: Current bit stream position. 

842 :param _value: Not used. 

843 :returns: Updated bit stream position which points to the first bit after unsigned fixed integer type. 

844 """ 

845 

846 return bitposition + self.bitsizeof() 

847 

848 def read(self, reader: BitStreamReader) -> int: 

849 """ 

850 Reads unsigned fixed integer Zserio type from the bit stream. 

851 

852 :param reader: Bit stream from which to read. 

853 :returns: Read unsigned int value. 

854 """ 

855 

856 return reader.read_bits(self._numbits) 

857 

858 def write(self, writer: BitStreamWriter, value: int) -> None: 

859 """ 

860 Writes unsigned fixed integer Zserio type to the bit stream. 

861 

862 :param writer: Bit stream where to write. 

863 :param value: Unsigned fixed integer Zserio type to write. 

864 """ 

865 

866 writer.write_bits(value, self._numbits) 

867 

868 

869class SignedBitFieldArrayTraits: 

870 """ 

871 Array traits for signed fixed integer Zserio types (int16, int32, int64, int:5, etc...). 

872 """ 

873 

874 HAS_BITSIZEOF_CONSTANT = True 

875 NEEDS_BITSIZEOF_POSITION = False 

876 NEEDS_READ_INDEX = False 

877 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

878 

879 def __init__(self, numbits: int) -> None: 

880 """ 

881 Constructor. 

882 

883 :param numbits: Number of bits for signed fixed integer Zserio type. 

884 """ 

885 

886 self._numbits = numbits 

887 

888 @property 

889 def packed_traits(self) -> PackedArrayTraits: 

890 """ 

891 Gets packed array traits. 

892 

893 :returns: PackedArrayTraits instance. 

894 """ 

895 

896 return PackedArrayTraits(self) 

897 

898 def bitsizeof(self) -> int: 

899 """ 

900 Returns length of signed fixed integer Zserio type stored in the bit stream in bits. 

901 

902 :returns: Length of signed fixed integer Zserio type in bits. 

903 """ 

904 

905 return self._numbits 

906 

907 def initialize_offsets(self, bitposition: int, _value: int) -> int: 

908 """ 

909 Initializes indexed offsets for signed fixed integer Zserio type. 

910 

911 :param bitposition: Current bit stream position. 

912 :param _value: Not used. 

913 :returns: Updated bit stream position which points to the first bit after signed fixed integer type. 

914 """ 

915 

916 return bitposition + self.bitsizeof() 

917 

918 def read(self, reader: BitStreamReader) -> int: 

919 """ 

920 Reads signed fixed integer Zserio type from the bit stream. 

921 

922 :param reader: Bit stream from which to read. 

923 :returns: Read signed int value. 

924 """ 

925 

926 return reader.read_signed_bits(self._numbits) 

927 

928 def write(self, writer: BitStreamWriter, value: int) -> None: 

929 """ 

930 Writes signed fixed integer Zserio type to the bit stream. 

931 

932 :param writer: Bit stream where to write. 

933 :param value: Signed fixed integer Zserio type to write. 

934 """ 

935 

936 writer.write_signed_bits(value, self._numbits) 

937 

938 

939class VarUInt16ArrayTraits: 

940 """ 

941 Array traits for Zserio varuint16 type. 

942 """ 

943 

944 HAS_BITSIZEOF_CONSTANT = False 

945 NEEDS_BITSIZEOF_POSITION = False 

946 NEEDS_READ_INDEX = False 

947 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

948 

949 @property 

950 def packed_traits(self) -> PackedArrayTraits: 

951 """ 

952 Gets packed array traits. 

953 

954 :returns: PackedArrayTraits instance. 

955 """ 

956 

957 return PackedArrayTraits(self) 

958 

959 @staticmethod 

960 def bitsizeof(value: int) -> int: 

961 """ 

962 Returns length of Zserio varuint16 type stored in the bit stream in bits. 

963 

964 :param value: Zserio varuint16 type value. 

965 :returns: Length of given Zserio varuint16 type in bits. 

966 """ 

967 

968 return bitsizeof_varuint16(value) 

969 

970 @staticmethod 

971 def initialize_offsets(bitposition: int, value: int) -> int: 

972 """ 

973 Initializes indexed offsets for Zserio varuint16 type. 

974 

975 :param bitposition: Current bit stream position. 

976 :param value: Zserio varuint16 type value. 

977 :returns: Updated bit stream position which points to the first bit after Zserio varuint16 type. 

978 """ 

979 

980 return bitposition + VarUInt16ArrayTraits.bitsizeof(value) 

981 

982 @staticmethod 

983 def read(reader: BitStreamReader) -> int: 

984 """ 

985 Reads Zserio varuint16 type from the bit stream. 

986 

987 :param reader: Bit stream from which to read. 

988 """ 

989 

990 return reader.read_varuint16() 

991 

992 @staticmethod 

993 def write(writer: BitStreamWriter, value: int) -> None: 

994 """ 

995 Writes Zserio varuint16 type to the bit stream. 

996 

997 :param writer: Bit stream where to write. 

998 :param value: Zserio varuint16 type to write. 

999 :returns: Read varuint16 value. 

1000 """ 

1001 

1002 writer.write_varuint16(value) 

1003 

1004 

1005class VarUInt32ArrayTraits: 

1006 """ 

1007 Array traits for Zserio varuint32 type. 

1008 """ 

1009 

1010 HAS_BITSIZEOF_CONSTANT = False 

1011 NEEDS_BITSIZEOF_POSITION = False 

1012 NEEDS_READ_INDEX = False 

1013 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1014 

1015 @property 

1016 def packed_traits(self) -> PackedArrayTraits: 

1017 """ 

1018 Gets packed array traits. 

1019 

1020 :returns: PackedArrayTraits instance. 

1021 """ 

1022 

1023 return PackedArrayTraits(self) 

1024 

1025 @staticmethod 

1026 def bitsizeof(value: int) -> int: 

1027 """ 

1028 Returns length of Zserio varuint32 type stored in the bit stream in bits. 

1029 

1030 :param value: Zserio varuint32 type value. 

1031 :returns: Length of given Zserio varuint32 type in bits. 

1032 """ 

1033 

1034 return bitsizeof_varuint32(value) 

1035 

1036 @staticmethod 

1037 def initialize_offsets(bitposition: int, value: int) -> int: 

1038 """ 

1039 Initializes indexed offsets for Zserio varuint32 type. 

1040 

1041 :param bitposition: Current bit stream position. 

1042 :param value: Zserio varuint32 type value. 

1043 :returns: Updated bit stream position which points to the first bit after Zserio varuint32 type. 

1044 """ 

1045 

1046 return bitposition + VarUInt32ArrayTraits.bitsizeof(value) 

1047 

1048 @staticmethod 

1049 def read(reader: BitStreamReader) -> int: 

1050 """ 

1051 Reads Zserio varuint32 type from the bit stream. 

1052 

1053 :param reader: Bit stream from which to read. 

1054 :returns: Read varuint32 value. 

1055 """ 

1056 

1057 return reader.read_varuint32() 

1058 

1059 @staticmethod 

1060 def write(writer: BitStreamWriter, value: int) -> None: 

1061 """ 

1062 Writes Zserio varuint32 type to the bit stream. 

1063 

1064 :param writer: Bit stream where to write. 

1065 :param value: Zserio varuint32 type to write. 

1066 """ 

1067 

1068 writer.write_varuint32(value) 

1069 

1070 

1071class VarUInt64ArrayTraits: 

1072 """ 

1073 Array traits for Zserio varuint64 type. 

1074 """ 

1075 

1076 HAS_BITSIZEOF_CONSTANT = False 

1077 NEEDS_BITSIZEOF_POSITION = False 

1078 NEEDS_READ_INDEX = False 

1079 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1080 

1081 @property 

1082 def packed_traits(self) -> PackedArrayTraits: 

1083 """ 

1084 Gets packed array traits. 

1085 

1086 :returns: PackedArrayTraits instance. 

1087 """ 

1088 

1089 return PackedArrayTraits(self) 

1090 

1091 @staticmethod 

1092 def bitsizeof(value: int) -> int: 

1093 """ 

1094 Returns length of Zserio varuint64 type stored in the bit stream in bits. 

1095 

1096 :param value: Zserio varuint64 type value. 

1097 :returns: Length of given Zserio varuint64 type in bits. 

1098 """ 

1099 

1100 return bitsizeof_varuint64(value) 

1101 

1102 @staticmethod 

1103 def initialize_offsets(bitposition: int, value: int) -> int: 

1104 """ 

1105 Initializes indexed offsets for Zserio varuint64 type. 

1106 

1107 :param value: Zserio varuint64 type value. 

1108 :returns: Updated bit stream position which points to the first bit after Zserio varuint64 type. 

1109 """ 

1110 

1111 return bitposition + VarUInt64ArrayTraits.bitsizeof(value) 

1112 

1113 @staticmethod 

1114 def read(reader: BitStreamReader) -> int: 

1115 """ 

1116 Reads Zserio varuint64 type from the bit stream. 

1117 

1118 :param reader: Bit stream from which to read. 

1119 :returns: Read varuint64 value. 

1120 """ 

1121 

1122 return reader.read_varuint64() 

1123 

1124 @staticmethod 

1125 def write(writer: BitStreamWriter, value: int) -> None: 

1126 """ 

1127 Writes Zserio varuint64 type to the bit stream. 

1128 

1129 :param writer: Bit stream where to write. 

1130 :param value: Zserio varuint64 type to write. 

1131 """ 

1132 

1133 writer.write_varuint64(value) 

1134 

1135 

1136class VarUIntArrayTraits: 

1137 """ 

1138 Array traits for Zserio varuint type. 

1139 """ 

1140 

1141 HAS_BITSIZEOF_CONSTANT = False 

1142 NEEDS_BITSIZEOF_POSITION = False 

1143 NEEDS_READ_INDEX = False 

1144 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1145 

1146 @property 

1147 def packed_traits(self) -> PackedArrayTraits: 

1148 """ 

1149 Gets packed array traits. 

1150 

1151 :returns: PackedArrayTraits instance. 

1152 """ 

1153 

1154 return PackedArrayTraits(self) 

1155 

1156 @staticmethod 

1157 def bitsizeof(value: int) -> int: 

1158 """ 

1159 Returns length of Zserio varuint type stored in the bit stream in bits. 

1160 

1161 :param value: Zserio varuint type value. 

1162 :returns: Length of given Zserio varuint type in bits. 

1163 """ 

1164 

1165 return bitsizeof_varuint(value) 

1166 

1167 @staticmethod 

1168 def initialize_offsets(bitposition: int, value: int) -> int: 

1169 """ 

1170 Initializes indexed offsets for Zserio varuint type. 

1171 

1172 :param bitposition: Current bit stream position. 

1173 :param value: Zserio varuint type value. 

1174 :returns: Updated bit stream position which points to the first bit after Zserio varuint type. 

1175 """ 

1176 

1177 return bitposition + VarUIntArrayTraits.bitsizeof(value) 

1178 

1179 @staticmethod 

1180 def read(reader: BitStreamReader) -> int: 

1181 """ 

1182 Reads Zserio varuint type from the bit stream. 

1183 

1184 :param reader: Bit stream from which to read. 

1185 :returns: Read varuint value. 

1186 """ 

1187 

1188 return reader.read_varuint() 

1189 

1190 @staticmethod 

1191 def write(writer: BitStreamWriter, value: int) -> None: 

1192 """ 

1193 Writes Zserio varuint type to the bit stream. 

1194 

1195 :param writer: Bit stream where to write. 

1196 :param value: Zserio varuint type to write. 

1197 """ 

1198 

1199 writer.write_varuint(value) 

1200 

1201 

1202class VarSizeArrayTraits: 

1203 """ 

1204 Array traits for Zserio varsize type. 

1205 """ 

1206 

1207 HAS_BITSIZEOF_CONSTANT = False 

1208 NEEDS_BITSIZEOF_POSITION = False 

1209 NEEDS_READ_INDEX = False 

1210 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1211 

1212 @property 

1213 def packed_traits(self) -> PackedArrayTraits: 

1214 """ 

1215 Gets packed array traits. 

1216 

1217 :returns: PackedArrayTraits instance. 

1218 """ 

1219 

1220 return PackedArrayTraits(self) 

1221 

1222 @staticmethod 

1223 def bitsizeof(value: int) -> int: 

1224 """ 

1225 Returns length of Zserio varsize type stored in the bit stream in bits. 

1226 

1227 :param value: Zserio varsize type value. 

1228 :returns: Length of given Zserio varsize type in bits. 

1229 """ 

1230 

1231 return bitsizeof_varsize(value) 

1232 

1233 @staticmethod 

1234 def initialize_offsets(bitposition: int, value: int) -> int: 

1235 """ 

1236 Initializes indexed offsets for Zserio varsize type. 

1237 

1238 :param bitposition: Current bit stream position. 

1239 :param value: Zserio varsize type value. 

1240 :returns: Updated bit stream position which points to the first bit after Zserio varsize type. 

1241 """ 

1242 

1243 return bitposition + VarSizeArrayTraits.bitsizeof(value) 

1244 

1245 @staticmethod 

1246 def read(reader: BitStreamReader) -> int: 

1247 """ 

1248 Reads Zserio varsize type from the bit stream. 

1249 

1250 :param reader: Bit stream from which to read. 

1251 :returns: Read varsize value. 

1252 """ 

1253 

1254 return reader.read_varsize() 

1255 

1256 @staticmethod 

1257 def write(writer: BitStreamWriter, value: int) -> None: 

1258 """ 

1259 Writes Zserio varsize type to the bit stream. 

1260 

1261 :param writer: Bit stream where to write. 

1262 :param value: Zserio varsize type to write. 

1263 """ 

1264 

1265 writer.write_varsize(value) 

1266 

1267 

1268class VarInt16ArrayTraits: 

1269 """ 

1270 Array traits for Zserio varint16 type. 

1271 """ 

1272 

1273 HAS_BITSIZEOF_CONSTANT = False 

1274 NEEDS_BITSIZEOF_POSITION = False 

1275 NEEDS_READ_INDEX = False 

1276 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1277 

1278 @property 

1279 def packed_traits(self) -> PackedArrayTraits: 

1280 """ 

1281 Gets packed array traits. 

1282 

1283 :returns: PackedArrayTraits instance. 

1284 """ 

1285 

1286 return PackedArrayTraits(self) 

1287 

1288 @staticmethod 

1289 def bitsizeof(value: int) -> int: 

1290 """ 

1291 Returns length of Zserio varint16 type stored in the bit stream in bits. 

1292 

1293 :param value: Zserio varint16 type value. 

1294 :returns: Length of given Zserio varint16 type in bits. 

1295 """ 

1296 

1297 return bitsizeof_varint16(value) 

1298 

1299 @staticmethod 

1300 def initialize_offsets(bitposition: int, value: int) -> int: 

1301 """ 

1302 Initializes indexed offsets for Zserio varint16 type. 

1303 

1304 :param bitposition: Current bit stream position. 

1305 :param value: Zserio varint16 type value. 

1306 :returns: Updated bit stream position which points to the first bit after Zserio varint16 type. 

1307 """ 

1308 

1309 return bitposition + VarInt16ArrayTraits.bitsizeof(value) 

1310 

1311 @staticmethod 

1312 def read(reader: BitStreamReader) -> int: 

1313 """ 

1314 Reads Zserio varint16 type from the bit stream. 

1315 

1316 :param reader: Bit stream from which to read. 

1317 :returns: Read varint16 value. 

1318 """ 

1319 

1320 return reader.read_varint16() 

1321 

1322 @staticmethod 

1323 def write(writer: BitStreamWriter, value: int) -> None: 

1324 """ 

1325 Writes Zserio varint16 type to the bit stream. 

1326 

1327 :param writer: Bit stream where to write. 

1328 :param value: Zserio varint16 type to write. 

1329 """ 

1330 

1331 writer.write_varint16(value) 

1332 

1333 

1334class VarInt32ArrayTraits: 

1335 """ 

1336 Array traits for Zserio varint32 type. 

1337 """ 

1338 

1339 HAS_BITSIZEOF_CONSTANT = False 

1340 NEEDS_BITSIZEOF_POSITION = False 

1341 NEEDS_READ_INDEX = False 

1342 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1343 

1344 @property 

1345 def packed_traits(self) -> PackedArrayTraits: 

1346 """ 

1347 Gets packed array traits. 

1348 

1349 :returns: PackedArrayTraits instance. 

1350 """ 

1351 

1352 return PackedArrayTraits(self) 

1353 

1354 @staticmethod 

1355 def bitsizeof(value: int) -> int: 

1356 """ 

1357 Returns length of Zserio varint32 type stored in the bit stream in bits. 

1358 

1359 :param value: Zserio varint32 type value. 

1360 :returns: Length of given Zserio varint32 type in bits. 

1361 """ 

1362 

1363 return bitsizeof_varint32(value) 

1364 

1365 @staticmethod 

1366 def initialize_offsets(bitposition: int, value: int) -> int: 

1367 """ 

1368 Initializes indexed offsets for Zserio varint32 type. 

1369 

1370 :param bitposition: Current bit stream position. 

1371 :param value: Zserio varint32 type value. 

1372 :returns: Updated bit stream position which points to the first bit after Zserio varint32 type. 

1373 """ 

1374 

1375 return bitposition + VarInt32ArrayTraits.bitsizeof(value) 

1376 

1377 @staticmethod 

1378 def read(reader: BitStreamReader) -> int: 

1379 """ 

1380 Reads Zserio varint32 type from the bit stream. 

1381 

1382 :param reader: Bit stream from which to read. 

1383 :returns: Read varint32 value. 

1384 """ 

1385 

1386 return reader.read_varint32() 

1387 

1388 @staticmethod 

1389 def write(writer: BitStreamWriter, value: int) -> None: 

1390 """ 

1391 Writes Zserio varint32 type to the bit stream. 

1392 

1393 :param writer: Bit stream where to write. 

1394 :param value: Zserio varint32 type to write. 

1395 """ 

1396 

1397 writer.write_varint32(value) 

1398 

1399 

1400class VarInt64ArrayTraits: 

1401 """ 

1402 Array traits for Zserio varint64 type. 

1403 """ 

1404 

1405 HAS_BITSIZEOF_CONSTANT = False 

1406 NEEDS_BITSIZEOF_POSITION = False 

1407 NEEDS_READ_INDEX = False 

1408 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1409 

1410 @property 

1411 def packed_traits(self) -> PackedArrayTraits: 

1412 """ 

1413 Gets packed array traits. 

1414 

1415 :returns: PackedArrayTraits instance. 

1416 """ 

1417 

1418 return PackedArrayTraits(self) 

1419 

1420 @staticmethod 

1421 def bitsizeof(value: int) -> int: 

1422 """ 

1423 Returns length of Zserio varint64 type stored in the bit stream in bits. 

1424 

1425 :param value: Zserio varint64 type value. 

1426 :returns: Length of given Zserio varint64 type in bits. 

1427 """ 

1428 

1429 return bitsizeof_varint64(value) 

1430 

1431 @staticmethod 

1432 def initialize_offsets(bitposition: int, value: int) -> int: 

1433 """ 

1434 Initializes indexed offsets for Zserio varint64 type. 

1435 

1436 :param bitposition: Current bit stream position. 

1437 :param value: Zserio varint64 type value. 

1438 :returns: Updated bit stream position which points to the first bit after Zserio varint64 type. 

1439 """ 

1440 

1441 return bitposition + VarInt64ArrayTraits.bitsizeof(value) 

1442 

1443 @staticmethod 

1444 def read(reader: BitStreamReader) -> int: 

1445 """ 

1446 Reads Zserio varint64 type from the bit stream. 

1447 

1448 :param reader: Bit stream from which to read. 

1449 :returns: Read varint64 value. 

1450 """ 

1451 

1452 return reader.read_varint64() 

1453 

1454 @staticmethod 

1455 def write(writer: BitStreamWriter, value: int) -> None: 

1456 """ 

1457 Writes Zserio varint64 type to the bit stream. 

1458 

1459 :param writer: Bit stream where to write. 

1460 :param value: Zserio varint64 type to write. 

1461 """ 

1462 

1463 writer.write_varint64(value) 

1464 

1465 

1466class VarIntArrayTraits: 

1467 """ 

1468 Array traits for Zserio varint type. 

1469 """ 

1470 

1471 HAS_BITSIZEOF_CONSTANT = False 

1472 NEEDS_BITSIZEOF_POSITION = False 

1473 NEEDS_READ_INDEX = False 

1474 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1475 

1476 @property 

1477 def packed_traits(self) -> PackedArrayTraits: 

1478 """ 

1479 Gets packed array traits. 

1480 

1481 :returns: PackedArrayTraits instance. 

1482 """ 

1483 

1484 return PackedArrayTraits(self) 

1485 

1486 @staticmethod 

1487 def bitsizeof(value: int) -> int: 

1488 """ 

1489 Returns length of Zserio varint type stored in the bit stream in bits. 

1490 

1491 :param value: Zserio varint type value. 

1492 :returns: Length of given Zserio varint type in bits. 

1493 """ 

1494 

1495 return bitsizeof_varint(value) 

1496 

1497 @staticmethod 

1498 def initialize_offsets(bitposition: int, value: int) -> int: 

1499 """ 

1500 Initializes indexed offsets for Zserio varint type. 

1501 

1502 :param bitposition: Current bit stream position. 

1503 :param value: Zserio varint type value. 

1504 :returns: Updated bit stream position which points to the first bit after Zserio varint type. 

1505 """ 

1506 

1507 return bitposition + VarIntArrayTraits.bitsizeof(value) 

1508 

1509 @staticmethod 

1510 def read(reader: BitStreamReader) -> int: 

1511 """ 

1512 Reads Zserio varint type from the bit stream. 

1513 

1514 :param reader: Bit stream from which to read. 

1515 :returns: Read varint value. 

1516 """ 

1517 

1518 return reader.read_varint() 

1519 

1520 @staticmethod 

1521 def write(writer: BitStreamWriter, value: int) -> None: 

1522 """ 

1523 Writes Zserio varint type to the bit stream. 

1524 

1525 :param writer: Bit stream where to write. 

1526 :param value: Zserio varint type to write. 

1527 """ 

1528 

1529 writer.write_varint(value) 

1530 

1531 

1532class Float16ArrayTraits: 

1533 """ 

1534 Array traits for Zserio float16 type. 

1535 """ 

1536 

1537 HAS_BITSIZEOF_CONSTANT = True 

1538 NEEDS_BITSIZEOF_POSITION = False 

1539 NEEDS_READ_INDEX = False 

1540 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float32_array) 

1541 

1542 @property 

1543 def packed_traits(self) -> None: 

1544 """ 

1545 Returns None since float type is not packable. 

1546 

1547 :returns: None. 

1548 """ 

1549 

1550 return None 

1551 

1552 @staticmethod 

1553 def bitsizeof() -> int: 

1554 """ 

1555 Returns length of Zserio float16 type stored in the bit stream in bits. 

1556 

1557 :returns: Length of Zserio float16 type in bits. 

1558 """ 

1559 

1560 return 16 

1561 

1562 @staticmethod 

1563 def initialize_offsets(bitposition: int, _value: float) -> int: 

1564 """ 

1565 Initializes indexed offsets for Zserio float16 type. 

1566 

1567 :param bitposition: Current bit stream position. 

1568 :param _value: Not used. 

1569 :returns: Updated bit stream position which points to the first bit after Zserio float16 type. 

1570 """ 

1571 

1572 return bitposition + Float16ArrayTraits.bitsizeof() 

1573 

1574 @staticmethod 

1575 def read(reader: BitStreamReader) -> float: 

1576 """ 

1577 Reads Zserio float16 type from the bit stream. 

1578 

1579 :param reader: Bit stream from which to read. 

1580 """ 

1581 

1582 return reader.read_float16() 

1583 

1584 @staticmethod 

1585 def write(writer: BitStreamWriter, value: float) -> None: 

1586 """ 

1587 Writes Zserio float16 type to the bit stream. 

1588 

1589 :param writer: Bit stream where to write. 

1590 :param value: Zserio float16 type to write. 

1591 :returns: Read float16 value. 

1592 """ 

1593 

1594 writer.write_float16(value) 

1595 

1596 

1597class Float32ArrayTraits: 

1598 """ 

1599 Array traits for Zserio float32 type. 

1600 """ 

1601 

1602 HAS_BITSIZEOF_CONSTANT = True 

1603 NEEDS_BITSIZEOF_POSITION = False 

1604 NEEDS_READ_INDEX = False 

1605 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float32_array) 

1606 

1607 @property 

1608 def packed_traits(self) -> None: 

1609 """ 

1610 Returns None since float type is not packable. 

1611 

1612 :returns: None. 

1613 """ 

1614 

1615 return None 

1616 

1617 @staticmethod 

1618 def bitsizeof() -> int: 

1619 """ 

1620 Returns length of Zserio float32 type stored in the bit stream in bits. 

1621 

1622 :returns: Length of Zserio float32 type in bits. 

1623 """ 

1624 

1625 return 32 

1626 

1627 @staticmethod 

1628 def initialize_offsets(bitposition: int, _value: float) -> int: 

1629 """ 

1630 Initializes indexed offsets for Zserio float32 type. 

1631 

1632 :param bitposition: Current bit stream position. 

1633 :param _value: Not used. 

1634 :returns: Updated bit stream position which points to the first bit after Zserio float32 type. 

1635 """ 

1636 

1637 return bitposition + Float32ArrayTraits.bitsizeof() 

1638 

1639 @staticmethod 

1640 def read(reader: BitStreamReader) -> float: 

1641 """ 

1642 Reads Zserio float32 type from the bit stream. 

1643 

1644 :param reader: Bit stream from which to read. 

1645 :returns: Read float32 value. 

1646 """ 

1647 

1648 return reader.read_float32() 

1649 

1650 @staticmethod 

1651 def write(writer: BitStreamWriter, value: float) -> None: 

1652 """ 

1653 Writes Zserio float32 type to the bit stream. 

1654 

1655 :param writer: Bit stream where to write. 

1656 :param value: Zserio float32 type to write. 

1657 """ 

1658 

1659 writer.write_float32(value) 

1660 

1661 

1662class Float64ArrayTraits: 

1663 """ 

1664 Array traits for Zserio float64 type. 

1665 """ 

1666 

1667 HAS_BITSIZEOF_CONSTANT = True 

1668 NEEDS_BITSIZEOF_POSITION = False 

1669 NEEDS_READ_INDEX = False 

1670 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float64_array) 

1671 

1672 @property 

1673 def packed_traits(self) -> None: 

1674 """ 

1675 Returns None since float type is not packable. 

1676 

1677 :returns: None. 

1678 """ 

1679 

1680 return None 

1681 

1682 @staticmethod 

1683 def bitsizeof() -> int: 

1684 """ 

1685 Returns length of Zserio float64 type stored in the bit stream in bits. 

1686 

1687 :returns: Length of Zserio float64 type in bits. 

1688 """ 

1689 

1690 return 64 

1691 

1692 @staticmethod 

1693 def initialize_offsets(bitposition: int, _value: float) -> int: 

1694 """ 

1695 Initializes indexed offsets for Zserio float64 type. 

1696 

1697 :param bitposition: Current bit stream position. 

1698 :param _value: Not used. 

1699 :returns: Updated bit stream position which points to the first bit after Zserio float64 type. 

1700 """ 

1701 

1702 return bitposition + Float64ArrayTraits.bitsizeof() 

1703 

1704 @staticmethod 

1705 def read(reader: BitStreamReader) -> float: 

1706 """ 

1707 Reads Zserio float64 type from the bit stream. 

1708 

1709 :param reader: Bit stream from which to read. 

1710 :returns: Read float64 value. 

1711 """ 

1712 

1713 return reader.read_float64() 

1714 

1715 @staticmethod 

1716 def write(writer: BitStreamWriter, value: float) -> None: 

1717 """ 

1718 Writes Zserio float64 type to the bit stream. 

1719 

1720 :param writer: Bit stream where to write. 

1721 :param value: Zserio float64 type to write. 

1722 """ 

1723 

1724 writer.write_float64(value) 

1725 

1726 

1727class BytesArrayTraits: 

1728 """ 

1729 Array traits for Zserio bytes type. 

1730 """ 

1731 

1732 HAS_BITSIZEOF_CONSTANT = False 

1733 NEEDS_BITSIZEOF_POSITION = False 

1734 NEEDS_READ_INDEX = False 

1735 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_bytes_array) 

1736 

1737 @property 

1738 def packed_traits(self) -> None: 

1739 """ 

1740 Returns None since Bytes type is not packable. 

1741 

1742 :returns: None. 

1743 """ 

1744 

1745 return None 

1746 

1747 @staticmethod 

1748 def bitsizeof(value: bytearray) -> int: 

1749 """ 

1750 Returns length of Zserio bytes type stored in the bit stream in bits. 

1751 

1752 :param value: Zserio bytes type value. 

1753 :returns: Length of given Zserio bytes type in bits. 

1754 """ 

1755 

1756 return bitsizeof_bytes(value) 

1757 

1758 @staticmethod 

1759 def initialize_offsets(bitposition: int, value: bytearray) -> int: 

1760 """ 

1761 Initializes indexed offsets for Zserio bytes type. 

1762 

1763 :param bitposition: Current bit stream position. 

1764 :param value: Zserio bytes type value. 

1765 :returns: Updated bit stream position which points to the first bit after Zserio bytes type. 

1766 """ 

1767 

1768 return bitposition + BytesArrayTraits.bitsizeof(value) 

1769 

1770 @staticmethod 

1771 def read(reader: BitStreamReader) -> bytearray: 

1772 """ 

1773 Reads Zserio bytes type from the bit stream. 

1774 

1775 :param reader: Bit stream from which to read. 

1776 :returns: Read bytes value. 

1777 """ 

1778 

1779 return reader.read_bytes() 

1780 

1781 @staticmethod 

1782 def write(writer: BitStreamWriter, value: bytearray) -> None: 

1783 """ 

1784 Writes Zserio bytes type to the bit stream. 

1785 

1786 :param writer: Bit stream where to write. 

1787 :param value: Zserio bytes type to write. 

1788 """ 

1789 

1790 writer.write_bytes(value) 

1791 

1792 

1793class StringArrayTraits: 

1794 """ 

1795 Array traits for Zserio string type. 

1796 """ 

1797 

1798 HAS_BITSIZEOF_CONSTANT = False 

1799 NEEDS_BITSIZEOF_POSITION = False 

1800 NEEDS_READ_INDEX = False 

1801 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_string_array) 

1802 

1803 @property 

1804 def packed_traits(self) -> None: 

1805 """ 

1806 Returns None since String type is not packable. 

1807 

1808 :returns: None. 

1809 """ 

1810 

1811 return None 

1812 

1813 @staticmethod 

1814 def bitsizeof(value: str) -> int: 

1815 """ 

1816 Returns length of Zserio string type stored in the bit stream in bits. 

1817 

1818 :param value: Zserio string type value. 

1819 :returns: Length of given Zserio string type in bits. 

1820 """ 

1821 

1822 return bitsizeof_string(value) 

1823 

1824 @staticmethod 

1825 def initialize_offsets(bitposition: int, value: str) -> int: 

1826 """ 

1827 Initializes indexed offsets for Zserio string type. 

1828 

1829 :param bitposition: Current bit stream position. 

1830 :param value: Zserio string type value. 

1831 :returns: Updated bit stream position which points to the first bit after Zserio string type. 

1832 """ 

1833 

1834 return bitposition + StringArrayTraits.bitsizeof(value) 

1835 

1836 @staticmethod 

1837 def read(reader: BitStreamReader) -> str: 

1838 """ 

1839 Reads Zserio string type from the bit stream. 

1840 

1841 :param reader: Bit stream from which to read. 

1842 :returns: Read string value. 

1843 """ 

1844 

1845 return reader.read_string() 

1846 

1847 @staticmethod 

1848 def write(writer: BitStreamWriter, value: str) -> None: 

1849 """ 

1850 Writes Zserio string type to the bit stream. 

1851 

1852 :param writer: Bit stream where to write. 

1853 :param value: Zserio string type to write. 

1854 """ 

1855 

1856 writer.write_string(value) 

1857 

1858 

1859class BoolArrayTraits: 

1860 """ 

1861 Array traits for Zserio bool type. 

1862 """ 

1863 

1864 HAS_BITSIZEOF_CONSTANT = True 

1865 NEEDS_BITSIZEOF_POSITION = False 

1866 NEEDS_READ_INDEX = False 

1867 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_bool_array) 

1868 

1869 @property 

1870 def packed_traits(self) -> None: 

1871 """ 

1872 Returns None since Bool type is not packable. 

1873 

1874 :returns: None. 

1875 """ 

1876 

1877 return None 

1878 

1879 @staticmethod 

1880 def bitsizeof() -> int: 

1881 """ 

1882 Returns length of Zserio bool type stored in the bit stream in bits. 

1883 

1884 :returns: Length of Zserio bool type in bits. 

1885 """ 

1886 

1887 return 1 

1888 

1889 @staticmethod 

1890 def initialize_offsets(bitposition: int, _value: bool) -> int: 

1891 """ 

1892 Initializes indexed offsets for Zserio bool type. 

1893 

1894 :param bitposition: Current bit stream position. 

1895 :param _value: Not used. 

1896 :returns: Updated bit stream position which points to the first bit after Zserio bool type. 

1897 """ 

1898 

1899 return bitposition + BoolArrayTraits.bitsizeof() 

1900 

1901 @staticmethod 

1902 def read(reader: BitStreamReader) -> bool: 

1903 """ 

1904 Reads Zserio bool type from the bit stream. 

1905 

1906 :param reader: Bit stream from which to read. 

1907 :returns: Read bool value. 

1908 """ 

1909 

1910 return reader.read_bool() 

1911 

1912 @staticmethod 

1913 def write(writer: BitStreamWriter, value: bool) -> None: 

1914 """ 

1915 Writes Zserio bool type to the bit stream. 

1916 

1917 :param writer: Bit stream where to write. 

1918 :param value: Zserio bool type to write. 

1919 """ 

1920 

1921 writer.write_bool(value) 

1922 

1923 

1924class BitBufferArrayTraits: 

1925 """ 

1926 Array traits for Zserio extern bit buffer type. 

1927 """ 

1928 

1929 HAS_BITSIZEOF_CONSTANT = False 

1930 NEEDS_BITSIZEOF_POSITION = False 

1931 NEEDS_READ_INDEX = False 

1932 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_object_array) 

1933 

1934 @property 

1935 def packed_traits(self) -> None: 

1936 """ 

1937 Returns None since BitBuffer type is not packable. 

1938 

1939 :returns: None. 

1940 """ 

1941 

1942 return None 

1943 

1944 @staticmethod 

1945 def bitsizeof(value: BitBuffer) -> int: 

1946 """ 

1947 Returns length of Zserio extern bit buffer type stored in the bit stream in bits. 

1948 

1949 :param value: Zserio extern bit buffer type value. 

1950 :returns: Length of given Zserio string type in bits. 

1951 """ 

1952 

1953 return bitsizeof_bitbuffer(value) 

1954 

1955 @staticmethod 

1956 def initialize_offsets(bitposition: int, value: BitBuffer) -> int: 

1957 """ 

1958 Initializes indexed offsets for Zserio extern bit buffer type. 

1959 

1960 :param bitposition: Current bit stream position. 

1961 :param value: Zserio extern bit buffer type value. 

1962 :returns: Updated bit stream position which points to the first bit after Zserio extern bit buffer type. 

1963 """ 

1964 

1965 return bitposition + BitBufferArrayTraits.bitsizeof(value) 

1966 

1967 @staticmethod 

1968 def read(reader: BitStreamReader) -> BitBuffer: 

1969 """ 

1970 Reads Zserio extern bit buffer type from the bit stream. 

1971 

1972 :param reader: Bit stream from which to read. 

1973 :returns: Read bit buffer value. 

1974 """ 

1975 

1976 return reader.read_bitbuffer() 

1977 

1978 @staticmethod 

1979 def write(writer: BitStreamWriter, value: BitBuffer) -> None: 

1980 """ 

1981 Writes Zserio extern bit buffer type to the bit stream. 

1982 

1983 :param writer: Bit stream where to write. 

1984 :param value: Zserio extern bit buffer type to write. 

1985 """ 

1986 

1987 writer.write_bitbuffer(value) 

1988 

1989 

1990class ObjectArrayTraits: 

1991 """ 

1992 Array traits for Zserio structure, choice, union and enum types. 

1993 """ 

1994 

1995 HAS_BITSIZEOF_CONSTANT = False 

1996 NEEDS_BITSIZEOF_POSITION = True 

1997 NEEDS_READ_INDEX = True 

1998 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_object_array) 

1999 

2000 def __init__(self, element_factory: typing.Any) -> None: 

2001 """ 

2002 Constructor. 

2003 

2004 :param element_factory: Element factory which creates object from the element index. 

2005 """ 

2006 

2007 self._element_factory = element_factory 

2008 self._packed_traits = ( 

2009 ObjectPackedArrayTraits(element_factory) if element_factory.IS_OBJECT_PACKABLE else None 

2010 ) 

2011 

2012 @property 

2013 def packed_traits(self) -> typing.Optional[ObjectPackedArrayTraits]: 

2014 """ 

2015 Gets packed array traits. 

2016 

2017 :returns: ObjectPackedArrayTraits instance. 

2018 """ 

2019 

2020 return self._packed_traits 

2021 

2022 @staticmethod 

2023 def bitsizeof(bitposition: int, value: typing.Any) -> int: 

2024 """ 

2025 Returns length of Zserio object type stored in the bit stream in bits. 

2026 

2027 :param bitposition: Current bit position in bit stream. 

2028 :param value: Zserio object type value. 

2029 :returns: Length of given Zserio object type in bits. 

2030 """ 

2031 

2032 return value.bitsizeof(bitposition) 

2033 

2034 @staticmethod 

2035 def initialize_offsets(bitposition: int, value: typing.Any) -> int: 

2036 """ 

2037 Initializes indexed offsets for the Zserio object type. 

2038 

2039 :param bitposition: Current bit stream position. 

2040 :param value: Zserio object type value. 

2041 :returns: Updated bit stream position which points to the first bit after the Zserio object type. 

2042 """ 

2043 

2044 return value.initialize_offsets(bitposition) 

2045 

2046 def read(self, reader: BitStreamReader, index: int) -> typing.Any: 

2047 """ 

2048 Reads Zserio object type from the bit stream. 

2049 

2050 :param reader: Bit stream from which to read. 

2051 :param index: Element index in the array. 

2052 :returns: Read object. 

2053 """ 

2054 

2055 return self._element_factory.create(reader, index) 

2056 

2057 @staticmethod 

2058 def write(writer: BitStreamWriter, value: typing.Any) -> None: 

2059 """ 

2060 Writes Zserio object type to the bit stream. 

2061 

2062 :param writer: Bit stream where to write. 

2063 :param value: Zserio object type to write. 

2064 """ 

2065 

2066 value.write(writer)