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

724 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2026-10-19 14:18 +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.compare import compare 

26from zserio.hashcode import ( 

27 HASH_SEED, 

28 calc_hashcode_bool_array, 

29 calc_hashcode_int_array, 

30 calc_hashcode_float32_array, 

31 calc_hashcode_float64_array, 

32 calc_hashcode_bytes_array, 

33 calc_hashcode_string_array, 

34 calc_hashcode_object_array, 

35) 

36from zserio.exception import PythonRuntimeException 

37 

38 

39class Array: 

40 """ 

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

42 """ 

43 

44 def __init__( 

45 self, 

46 array_traits: typing.Any, 

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

48 *, 

49 is_auto: bool = False, 

50 is_implicit: bool = False, 

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

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

53 ) -> None: 

54 """ 

55 Constructor. 

56 

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

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

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

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

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

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

63 """ 

64 

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

66 self._array_traits: typing.Any = array_traits 

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

68 self._is_auto: bool = is_auto 

69 self._is_implicit: bool = is_implicit 

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

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

72 

73 @classmethod 

74 def from_reader( 

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

76 array_traits: typing.Any, 

77 reader: BitStreamReader, 

78 size: int = 0, 

79 *, 

80 is_auto: bool = False, 

81 is_implicit: bool = False, 

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

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

84 ) -> "Array": 

85 """ 

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

87 

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

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

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

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

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

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

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

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

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

97 """ 

98 

99 instance = cls( 

100 array_traits, 

101 is_auto=is_auto, 

102 is_implicit=is_implicit, 

103 set_offset_method=set_offset_method, 

104 check_offset_method=check_offset_method, 

105 ) 

106 instance.read(reader, size) 

107 

108 return instance 

109 

110 @classmethod 

111 def from_reader_packed( 

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

113 array_traits: typing.Any, 

114 reader: BitStreamReader, 

115 size: int = 0, 

116 *, 

117 is_auto: bool = False, 

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

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

120 ) -> "Array": 

121 """ 

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

123 

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

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

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

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

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

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

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

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

132 """ 

133 

134 instance = cls( 

135 array_traits, 

136 is_auto=is_auto, 

137 is_implicit=False, 

138 set_offset_method=set_offset_method, 

139 check_offset_method=check_offset_method, 

140 ) 

141 instance.read_packed(reader, size) 

142 

143 return instance 

144 

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

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

147 if isinstance(other, Array): 

148 return self._raw_array == other._raw_array 

149 

150 return False 

151 

152 def __lt__(self, other: object) -> bool: 

153 if isinstance(other, Array): 

154 return self._zserio_cmp(other) < 0 

155 

156 return NotImplemented 

157 

158 def _zserio_cmp(self, other: "Array") -> int: 

159 # compares only raw_array lexicographically, the same as __eq__ does 

160 for element, other_element in zip(self._raw_array, other._raw_array): 

161 result = compare(element, other_element) 

162 if result != 0: 

163 return result 

164 

165 return compare(len(self._raw_array), len(other._raw_array)) 

166 

167 def __hash__(self) -> int: 

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

169 

170 def __len__(self) -> int: 

171 return len(self._raw_array) 

172 

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

174 return self._raw_array[key] 

175 

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

177 self._raw_array[key] = value 

178 

179 @property 

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

181 """ 

182 Gets raw array. 

183 

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

185 """ 

186 

187 return self._raw_array 

188 

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

190 """ 

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

192 

193 :param bitposition: Current bit stream position. 

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

195 """ 

196 

197 end_bitposition = bitposition 

198 size = len(self._raw_array) 

199 if self._is_auto: 

200 end_bitposition += bitsizeof_varsize(size) 

201 

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

203 element_size = self._array_traits.bitsizeof() 

204 if self._set_offset_method is None: 

205 end_bitposition += size * element_size 

206 else: 

207 end_bitposition = alignto(8, end_bitposition) 

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

209 else: 

210 array_traits_bitsizeof = self._array_traits.bitsizeof 

211 if self._set_offset_method is not None: 

212 if self._array_traits.NEEDS_BITSIZEOF_POSITION: 

213 for element in self._raw_array: 

214 end_bitposition = alignto(8, end_bitposition) 

215 end_bitposition += array_traits_bitsizeof(end_bitposition, element) 

216 else: 

217 for element in self._raw_array: 

218 end_bitposition = alignto(8, end_bitposition) 

219 end_bitposition += array_traits_bitsizeof(element) 

220 else: 

221 if self._array_traits.NEEDS_BITSIZEOF_POSITION: 

222 for element in self._raw_array: 

223 end_bitposition += array_traits_bitsizeof(end_bitposition, element) 

224 else: 

225 for element in self._raw_array: 

226 end_bitposition += array_traits_bitsizeof(element) 

227 

228 return end_bitposition - bitposition 

229 

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

231 """ 

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

233 

234 :param bitposition: Current bit stream position. 

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

236 """ 

237 

238 end_bitposition = bitposition 

239 size = len(self._raw_array) 

240 if self._is_auto: 

241 end_bitposition += bitsizeof_varsize(size) 

242 

243 if size > 0: 

244 context = self._packed_array_traits.create_context() 

245 packed_array_traits_init_context = self._packed_array_traits.init_context 

246 packed_array_traits_bitsizeof = self._packed_array_traits.bitsizeof 

247 

248 for element in self._raw_array: 

249 packed_array_traits_init_context(context, element) 

250 

251 if self._set_offset_method is not None: 

252 for element in self._raw_array: 

253 end_bitposition = alignto(8, end_bitposition) 

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

255 else: 

256 for element in self._raw_array: 

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

258 

259 return end_bitposition - bitposition 

260 

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

262 """ 

263 Initializes indexed offsets for the array. 

264 

265 :param bitposition: Current bit stream position. 

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

267 """ 

268 

269 end_bitposition = bitposition 

270 size = len(self._raw_array) 

271 if self._is_auto: 

272 end_bitposition += bitsizeof_varsize(size) 

273 

274 array_traits_initialize_offsets = self._array_traits.initialize_offsets 

275 if self._set_offset_method is not None: 

276 set_offset = self._set_offset_method 

277 for index in range(size): 

278 end_bitposition = alignto(8, end_bitposition) 

279 set_offset(index, end_bitposition) 

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

281 else: 

282 for element in self._raw_array: 

283 end_bitposition = array_traits_initialize_offsets(end_bitposition, element) 

284 

285 return end_bitposition 

286 

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

288 """ 

289 Initializes indexed offsets for the packed array. 

290 

291 :param bitposition: Current bit stream position. 

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

293 """ 

294 

295 end_bitposition = bitposition 

296 size = len(self._raw_array) 

297 if self._is_auto: 

298 end_bitposition += bitsizeof_varsize(size) 

299 

300 if size > 0: 

301 context = self._packed_array_traits.create_context() 

302 packed_array_traits_init_context = self._packed_array_traits.init_context 

303 packed_array_traits_initialize_offsets = self._packed_array_traits.initialize_offsets 

304 

305 for element in self._raw_array: 

306 packed_array_traits_init_context(context, element) 

307 

308 if self._set_offset_method is not None: 

309 set_offset = self._set_offset_method 

310 for index in range(size): 

311 end_bitposition = alignto(8, end_bitposition) 

312 set_offset(index, end_bitposition) 

313 end_bitposition = packed_array_traits_initialize_offsets( 

314 context, end_bitposition, self._raw_array[index] 

315 ) 

316 else: 

317 for element in self._raw_array: 

318 end_bitposition = packed_array_traits_initialize_offsets(context, end_bitposition, element) 

319 

320 return end_bitposition 

321 

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

323 """ 

324 Reads array from the bit stream. 

325 

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

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

328 

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

330 """ 

331 

332 self._raw_array.clear() 

333 

334 append = self._raw_array.append 

335 array_traits_read = self._array_traits.read 

336 if self._is_implicit: 

337 if not self._array_traits.HAS_BITSIZEOF_CONSTANT: 

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

339 

340 element_size = self._array_traits.bitsizeof() 

341 remaining_bits = reader.buffer_bitsize - reader.bitposition 

342 read_size = remaining_bits // element_size 

343 for _ in range(read_size): 

344 # we know that no traits NEEDS_READ_INDEX here 

345 append(array_traits_read(reader)) 

346 else: 

347 if self._is_auto: 

348 read_size = reader.read_varsize() 

349 else: 

350 read_size = size 

351 

352 if self._array_traits.HAS_BITSIZEOF_CONSTANT: 

353 element_size = self._array_traits.bitsizeof() 

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

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

356 

357 if self._check_offset_method is not None: 

358 check_offset = self._check_offset_method 

359 reader_alignto = reader.alignto 

360 if self._array_traits.NEEDS_READ_INDEX: 

361 for index in range(read_size): 

362 reader_alignto(8) 

363 check_offset(index, reader.bitposition) 

364 append(array_traits_read(reader, index)) 

365 else: 

366 for index in range(read_size): 

367 reader_alignto(8) 

368 check_offset(index, reader.bitposition) 

369 append(array_traits_read(reader)) 

370 else: 

371 if self._array_traits.NEEDS_READ_INDEX: 

372 for index in range(read_size): 

373 append(array_traits_read(reader, index)) 

374 else: 

375 for _ in range(read_size): 

376 append(array_traits_read(reader)) 

377 

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

379 """ 

380 Reads packed array from the bit stream. 

381 

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

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

384 """ 

385 

386 self._raw_array.clear() 

387 

388 if self._is_implicit: 

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

390 

391 if self._is_auto: 

392 read_size = reader.read_varsize() 

393 else: 

394 read_size = size 

395 

396 if read_size > 0: 

397 context = self._packed_array_traits.create_context() 

398 

399 append = self._raw_array.append 

400 packed_array_traits_read = self._packed_array_traits.read 

401 

402 if self._check_offset_method is not None: 

403 check_offset = self._check_offset_method 

404 reader_alignto = reader.alignto 

405 for index in range(read_size): 

406 reader_alignto(8) 

407 check_offset(index, reader.bitposition) 

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

409 else: 

410 for index in range(read_size): 

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

412 

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

414 """ 

415 Writes array to the bit stream. 

416 

417 :param writer: Bit stream where to write. 

418 """ 

419 

420 size = len(self._raw_array) 

421 if self._is_auto: 

422 writer.write_varsize(size) 

423 

424 array_traits_write = self._array_traits.write 

425 if self._check_offset_method is not None: 

426 check_offset = self._check_offset_method 

427 writer_alignto = writer.alignto 

428 for index in range(size): 

429 writer_alignto(8) 

430 check_offset(index, writer.bitposition) 

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

432 else: 

433 for element in self._raw_array: 

434 array_traits_write(writer, element) 

435 

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

437 """ 

438 Writes packed array to the bit stream. 

439 

440 :param writer: Bit stream where to write. 

441 """ 

442 

443 size = len(self._raw_array) 

444 

445 if self._is_auto: 

446 writer.write_varsize(size) 

447 

448 if size > 0: 

449 context = self._packed_array_traits.create_context() 

450 packed_array_traits_init_context = self._packed_array_traits.init_context 

451 packed_array_traits_write = self._packed_array_traits.write 

452 

453 for element in self._raw_array: 

454 packed_array_traits_init_context(context, element) 

455 

456 if self._check_offset_method is not None: 

457 check_offset = self._check_offset_method 

458 writer_alignto = writer.alignto 

459 for index in range(size): 

460 writer_alignto(8) 

461 check_offset(index, writer.bitposition) 

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

463 else: 

464 for element in self._raw_array: 

465 packed_array_traits_write(context, writer, element) 

466 

467 

468class DeltaContext: 

469 """ 

470 Context for delta packing created for each packable field. 

471 

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

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

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

475 called for exactly the same sequence of packable elements. 

476 

477 Example:: 

478 

479 context = DeltaContext(array_traits) 

480 for element in array: 

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

482 context.write_descriptor(writer) # finishes the initialization 

483 for element in array: 

484 context.write(writer, element) 

485 """ 

486 

487 def __init__(self) -> None: 

488 """Constructor.""" 

489 

490 self._is_packed = False 

491 self._max_bit_number = 0 

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

493 self._processing_started = False 

494 self._unpacked_bitsize = 0 

495 self._first_element_bitsize = 0 

496 self._num_elements = 0 

497 

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

499 """ 

500 Makes initialization step for the provided array element. 

501 

502 :param array_traits: Standard array traits. 

503 :param element: Current element of the array. 

504 """ 

505 

506 self._num_elements += 1 

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

508 

509 if self._previous_element is None: 

510 self._previous_element = element 

511 self._first_element_bitsize = self._unpacked_bitsize 

512 else: 

513 if self._max_bit_number <= self._MAX_BIT_NUMBER_LIMIT: 

514 self._is_packed = True 

515 

516 delta = element - self._previous_element 

517 max_bit_number = delta.bit_length() 

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

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

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

521 if max_bit_number > self._max_bit_number: 

522 self._max_bit_number = max_bit_number 

523 if max_bit_number > self._MAX_BIT_NUMBER_LIMIT: 

524 self._is_packed = False 

525 

526 self._previous_element = element 

527 

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

529 """ 

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

531 

532 :param array_traits: Standard integral array traits. 

533 :param element: Current element. 

534 

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

536 """ 

537 

538 if not self._processing_started: 

539 self._processing_started = True 

540 self._finish_init() 

541 

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

543 elif not self._is_packed: 

544 return DeltaContext._bitsizeof_unpacked(array_traits, element) 

545 else: 

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

547 

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

549 """ 

550 Reads the packed element from the bit stream. 

551 

552 :param array_traits: Standard array traits. 

553 :param reader: Bit stream reader. 

554 """ 

555 

556 if not self._processing_started: 

557 self._processing_started = True 

558 self._read_descriptor(reader) 

559 

560 return self._read_unpacked(array_traits, reader) 

561 elif not self._is_packed: 

562 return self._read_unpacked(array_traits, reader) 

563 else: 

564 assert self._previous_element is not None 

565 if self._max_bit_number > 0: 

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

567 self._previous_element += delta 

568 return self._previous_element 

569 

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

571 """ 

572 Writes the packed element representation to the bit stream. 

573 

574 :param array_traits: Standard array traits. 

575 :param writer: Bit stream writer. 

576 :param element: Element to write. 

577 """ 

578 

579 if not self._processing_started: 

580 self._processing_started = True 

581 self._finish_init() 

582 self._write_descriptor(writer) 

583 

584 self._write_unpacked(array_traits, writer, element) 

585 elif not self._is_packed: 

586 self._write_unpacked(array_traits, writer, element) 

587 else: # packed and not first 

588 assert self._previous_element is not None 

589 if self._max_bit_number > 0: 

590 delta = element - self._previous_element 

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

592 self._previous_element = element 

593 

594 def _finish_init(self) -> None: 

595 if self._is_packed: 

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

597 packed_bitsize_with_descriptor = ( 

598 1 

599 + self._MAX_BIT_NUMBER_BITS # descriptor 

600 + self._first_element_bitsize 

601 + (self._num_elements - 1) * delta_bitsize 

602 ) 

603 unpacked_bitsize_with_descriptor = 1 + self._unpacked_bitsize 

604 if packed_bitsize_with_descriptor >= unpacked_bitsize_with_descriptor: 

605 self._is_packed = False 

606 

607 def _bitsizeof_descriptor(self) -> int: 

608 if self._is_packed: 

609 return 1 + self._MAX_BIT_NUMBER_BITS 

610 else: 

611 return 1 

612 

613 @staticmethod 

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

615 if array_traits.HAS_BITSIZEOF_CONSTANT: 

616 return array_traits.bitsizeof() 

617 else: # we know that NEEDS_BITSIZEOF_POSITION is False here 

618 return array_traits.bitsizeof(element) 

619 

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

621 self._is_packed = reader.read_bool() 

622 if self._is_packed: 

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

624 

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

626 element = array_traits.read(reader) 

627 self._previous_element = element 

628 return element 

629 

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

631 writer.write_bool(self._is_packed) 

632 if self._is_packed: 

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

634 

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

636 self._previous_element = element 

637 array_traits.write(writer, element) 

638 

639 _MAX_BIT_NUMBER_BITS = 6 

640 _MAX_BIT_NUMBER_LIMIT = 62 

641 

642 

643class PackedArrayTraits: 

644 """ 

645 Packed array traits. 

646 

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

648 """ 

649 

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

651 """ 

652 Constructor. 

653 

654 :param array_traits: Standard array traits. 

655 """ 

656 

657 self._array_traits = array_traits 

658 

659 @staticmethod 

660 def create_context() -> DeltaContext: 

661 """ 

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

663 

664 :returns: New packing context. 

665 """ 

666 

667 return DeltaContext() 

668 

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

670 """ 

671 Calls context initialization step for the current element. 

672 

673 :param delta_context: Delta context. 

674 :param element: Current element. 

675 """ 

676 

677 delta_context.init(self._array_traits, element) 

678 

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

680 """ 

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

682 

683 :param delta_context: Delta context. 

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

685 :param elemnet: Current element. 

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

687 """ 

688 

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

690 

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

692 """ 

693 Calls indexed offsets initialization for the current element. 

694 

695 :param delta_context: Delta context. 

696 :param _bitposition: Current bit stream position. 

697 :param element: Current element. 

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

699 """ 

700 

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

702 

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

704 """ 

705 Writes the element to the bit stream. 

706 

707 :param delta_context: Delta context. 

708 :param writer: Bit stream writer. 

709 :param element: Element to write. 

710 """ 

711 

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

713 

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

715 """ 

716 Read an element from the bit stream. 

717 

718 :param delta_context: Delta context. 

719 :param reader: Bit stream reader. 

720 :param _index: Not used. 

721 :returns: Read element value. 

722 """ 

723 

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

725 

726 

727class ObjectPackedArrayTraits: 

728 """ 

729 Packed array traits for Zserio objects. 

730 

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

732 to be used in a packed array. 

733 """ 

734 

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

736 """ 

737 Constructor. 

738 

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

740 """ 

741 

742 self._element_factory = element_factory 

743 

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

745 """ 

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

747 

748 :returns: New packing context. 

749 """ 

750 

751 return self._element_factory.create_packing_context() 

752 

753 @staticmethod 

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

755 """ 

756 Calls context initialization step for the current element. 

757 

758 :param context: Packing context. 

759 :param element: Current element. 

760 """ 

761 

762 element.init_packing_context(context) 

763 

764 @staticmethod 

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

766 """ 

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

768 

769 :param context: Packing context. 

770 :param bitposition: Current bit stream position. 

771 :param elemnet: Current element. 

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

773 """ 

774 

775 return element.bitsizeof_packed(context, bitposition) 

776 

777 @staticmethod 

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

779 """ 

780 Calls indexed offsets initialization for the current element. 

781 

782 :param context: Packing context. 

783 :param bitposition: Current bit stream position. 

784 :param element: Current element. 

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

786 """ 

787 

788 return element.initialize_offsets_packed(context, bitposition) 

789 

790 @staticmethod 

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

792 """ 

793 Writes the element to the bit stream. 

794 

795 :param context: Packing context. 

796 :param writer: Bit stream writer. 

797 :param element: Element to write. 

798 """ 

799 

800 element.write_packed(context, writer) 

801 

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

803 """ 

804 Read an element from the bit stream. 

805 

806 :param context: Packing context. 

807 :param reader: Bit stream reader. 

808 :param index: Index of the current element. 

809 :returns: Read element value. 

810 """ 

811 

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

813 

814 

815class BitFieldArrayTraits: 

816 """ 

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

818 """ 

819 

820 HAS_BITSIZEOF_CONSTANT = True 

821 NEEDS_BITSIZEOF_POSITION = False 

822 NEEDS_READ_INDEX = False 

823 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

824 

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

826 """ 

827 Constructor. 

828 

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

830 """ 

831 

832 self._numbits = numbits 

833 

834 @property 

835 def packed_traits(self) -> PackedArrayTraits: 

836 """ 

837 Gets packed array traits. 

838 

839 :returns: PackedArrayTraits instance. 

840 """ 

841 

842 return PackedArrayTraits(self) 

843 

844 def bitsizeof(self) -> int: 

845 """ 

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

847 

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

849 """ 

850 

851 return self._numbits 

852 

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

854 """ 

855 Initializes indexed offsets for unsigned fixed integer Zserio type. 

856 

857 :param bitposition: Current bit stream position. 

858 :param _value: Not used. 

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

860 """ 

861 

862 return bitposition + self.bitsizeof() 

863 

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

865 """ 

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

867 

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

869 :returns: Read unsigned int value. 

870 """ 

871 

872 return reader.read_bits(self._numbits) 

873 

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

875 """ 

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

877 

878 :param writer: Bit stream where to write. 

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

880 """ 

881 

882 writer.write_bits(value, self._numbits) 

883 

884 

885class SignedBitFieldArrayTraits: 

886 """ 

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

888 """ 

889 

890 HAS_BITSIZEOF_CONSTANT = True 

891 NEEDS_BITSIZEOF_POSITION = False 

892 NEEDS_READ_INDEX = False 

893 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

894 

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

896 """ 

897 Constructor. 

898 

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

900 """ 

901 

902 self._numbits = numbits 

903 

904 @property 

905 def packed_traits(self) -> PackedArrayTraits: 

906 """ 

907 Gets packed array traits. 

908 

909 :returns: PackedArrayTraits instance. 

910 """ 

911 

912 return PackedArrayTraits(self) 

913 

914 def bitsizeof(self) -> int: 

915 """ 

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

917 

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

919 """ 

920 

921 return self._numbits 

922 

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

924 """ 

925 Initializes indexed offsets for signed fixed integer Zserio type. 

926 

927 :param bitposition: Current bit stream position. 

928 :param _value: Not used. 

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

930 """ 

931 

932 return bitposition + self.bitsizeof() 

933 

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

935 """ 

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

937 

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

939 :returns: Read signed int value. 

940 """ 

941 

942 return reader.read_signed_bits(self._numbits) 

943 

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

945 """ 

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

947 

948 :param writer: Bit stream where to write. 

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

950 """ 

951 

952 writer.write_signed_bits(value, self._numbits) 

953 

954 

955class VarUInt16ArrayTraits: 

956 """ 

957 Array traits for Zserio varuint16 type. 

958 """ 

959 

960 HAS_BITSIZEOF_CONSTANT = False 

961 NEEDS_BITSIZEOF_POSITION = False 

962 NEEDS_READ_INDEX = False 

963 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

964 

965 @property 

966 def packed_traits(self) -> PackedArrayTraits: 

967 """ 

968 Gets packed array traits. 

969 

970 :returns: PackedArrayTraits instance. 

971 """ 

972 

973 return PackedArrayTraits(self) 

974 

975 @staticmethod 

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

977 """ 

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

979 

980 :param value: Zserio varuint16 type value. 

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

982 """ 

983 

984 return bitsizeof_varuint16(value) 

985 

986 @staticmethod 

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

988 """ 

989 Initializes indexed offsets for Zserio varuint16 type. 

990 

991 :param bitposition: Current bit stream position. 

992 :param value: Zserio varuint16 type value. 

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

994 """ 

995 

996 return bitposition + VarUInt16ArrayTraits.bitsizeof(value) 

997 

998 @staticmethod 

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

1000 """ 

1001 Reads Zserio varuint16 type from the bit stream. 

1002 

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

1004 """ 

1005 

1006 return reader.read_varuint16() 

1007 

1008 @staticmethod 

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

1010 """ 

1011 Writes Zserio varuint16 type to the bit stream. 

1012 

1013 :param writer: Bit stream where to write. 

1014 :param value: Zserio varuint16 type to write. 

1015 :returns: Read varuint16 value. 

1016 """ 

1017 

1018 writer.write_varuint16(value) 

1019 

1020 

1021class VarUInt32ArrayTraits: 

1022 """ 

1023 Array traits for Zserio varuint32 type. 

1024 """ 

1025 

1026 HAS_BITSIZEOF_CONSTANT = False 

1027 NEEDS_BITSIZEOF_POSITION = False 

1028 NEEDS_READ_INDEX = False 

1029 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1030 

1031 @property 

1032 def packed_traits(self) -> PackedArrayTraits: 

1033 """ 

1034 Gets packed array traits. 

1035 

1036 :returns: PackedArrayTraits instance. 

1037 """ 

1038 

1039 return PackedArrayTraits(self) 

1040 

1041 @staticmethod 

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

1043 """ 

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

1045 

1046 :param value: Zserio varuint32 type value. 

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

1048 """ 

1049 

1050 return bitsizeof_varuint32(value) 

1051 

1052 @staticmethod 

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

1054 """ 

1055 Initializes indexed offsets for Zserio varuint32 type. 

1056 

1057 :param bitposition: Current bit stream position. 

1058 :param value: Zserio varuint32 type value. 

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

1060 """ 

1061 

1062 return bitposition + VarUInt32ArrayTraits.bitsizeof(value) 

1063 

1064 @staticmethod 

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

1066 """ 

1067 Reads Zserio varuint32 type from the bit stream. 

1068 

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

1070 :returns: Read varuint32 value. 

1071 """ 

1072 

1073 return reader.read_varuint32() 

1074 

1075 @staticmethod 

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

1077 """ 

1078 Writes Zserio varuint32 type to the bit stream. 

1079 

1080 :param writer: Bit stream where to write. 

1081 :param value: Zserio varuint32 type to write. 

1082 """ 

1083 

1084 writer.write_varuint32(value) 

1085 

1086 

1087class VarUInt64ArrayTraits: 

1088 """ 

1089 Array traits for Zserio varuint64 type. 

1090 """ 

1091 

1092 HAS_BITSIZEOF_CONSTANT = False 

1093 NEEDS_BITSIZEOF_POSITION = False 

1094 NEEDS_READ_INDEX = False 

1095 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1096 

1097 @property 

1098 def packed_traits(self) -> PackedArrayTraits: 

1099 """ 

1100 Gets packed array traits. 

1101 

1102 :returns: PackedArrayTraits instance. 

1103 """ 

1104 

1105 return PackedArrayTraits(self) 

1106 

1107 @staticmethod 

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

1109 """ 

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

1111 

1112 :param value: Zserio varuint64 type value. 

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

1114 """ 

1115 

1116 return bitsizeof_varuint64(value) 

1117 

1118 @staticmethod 

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

1120 """ 

1121 Initializes indexed offsets for Zserio varuint64 type. 

1122 

1123 :param value: Zserio varuint64 type value. 

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

1125 """ 

1126 

1127 return bitposition + VarUInt64ArrayTraits.bitsizeof(value) 

1128 

1129 @staticmethod 

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

1131 """ 

1132 Reads Zserio varuint64 type from the bit stream. 

1133 

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

1135 :returns: Read varuint64 value. 

1136 """ 

1137 

1138 return reader.read_varuint64() 

1139 

1140 @staticmethod 

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

1142 """ 

1143 Writes Zserio varuint64 type to the bit stream. 

1144 

1145 :param writer: Bit stream where to write. 

1146 :param value: Zserio varuint64 type to write. 

1147 """ 

1148 

1149 writer.write_varuint64(value) 

1150 

1151 

1152class VarUIntArrayTraits: 

1153 """ 

1154 Array traits for Zserio varuint type. 

1155 """ 

1156 

1157 HAS_BITSIZEOF_CONSTANT = False 

1158 NEEDS_BITSIZEOF_POSITION = False 

1159 NEEDS_READ_INDEX = False 

1160 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1161 

1162 @property 

1163 def packed_traits(self) -> PackedArrayTraits: 

1164 """ 

1165 Gets packed array traits. 

1166 

1167 :returns: PackedArrayTraits instance. 

1168 """ 

1169 

1170 return PackedArrayTraits(self) 

1171 

1172 @staticmethod 

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

1174 """ 

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

1176 

1177 :param value: Zserio varuint type value. 

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

1179 """ 

1180 

1181 return bitsizeof_varuint(value) 

1182 

1183 @staticmethod 

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

1185 """ 

1186 Initializes indexed offsets for Zserio varuint type. 

1187 

1188 :param bitposition: Current bit stream position. 

1189 :param value: Zserio varuint type value. 

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

1191 """ 

1192 

1193 return bitposition + VarUIntArrayTraits.bitsizeof(value) 

1194 

1195 @staticmethod 

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

1197 """ 

1198 Reads Zserio varuint type from the bit stream. 

1199 

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

1201 :returns: Read varuint value. 

1202 """ 

1203 

1204 return reader.read_varuint() 

1205 

1206 @staticmethod 

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

1208 """ 

1209 Writes Zserio varuint type to the bit stream. 

1210 

1211 :param writer: Bit stream where to write. 

1212 :param value: Zserio varuint type to write. 

1213 """ 

1214 

1215 writer.write_varuint(value) 

1216 

1217 

1218class VarSizeArrayTraits: 

1219 """ 

1220 Array traits for Zserio varsize type. 

1221 """ 

1222 

1223 HAS_BITSIZEOF_CONSTANT = False 

1224 NEEDS_BITSIZEOF_POSITION = False 

1225 NEEDS_READ_INDEX = False 

1226 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1227 

1228 @property 

1229 def packed_traits(self) -> PackedArrayTraits: 

1230 """ 

1231 Gets packed array traits. 

1232 

1233 :returns: PackedArrayTraits instance. 

1234 """ 

1235 

1236 return PackedArrayTraits(self) 

1237 

1238 @staticmethod 

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

1240 """ 

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

1242 

1243 :param value: Zserio varsize type value. 

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

1245 """ 

1246 

1247 return bitsizeof_varsize(value) 

1248 

1249 @staticmethod 

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

1251 """ 

1252 Initializes indexed offsets for Zserio varsize type. 

1253 

1254 :param bitposition: Current bit stream position. 

1255 :param value: Zserio varsize type value. 

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

1257 """ 

1258 

1259 return bitposition + VarSizeArrayTraits.bitsizeof(value) 

1260 

1261 @staticmethod 

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

1263 """ 

1264 Reads Zserio varsize type from the bit stream. 

1265 

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

1267 :returns: Read varsize value. 

1268 """ 

1269 

1270 return reader.read_varsize() 

1271 

1272 @staticmethod 

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

1274 """ 

1275 Writes Zserio varsize type to the bit stream. 

1276 

1277 :param writer: Bit stream where to write. 

1278 :param value: Zserio varsize type to write. 

1279 """ 

1280 

1281 writer.write_varsize(value) 

1282 

1283 

1284class VarInt16ArrayTraits: 

1285 """ 

1286 Array traits for Zserio varint16 type. 

1287 """ 

1288 

1289 HAS_BITSIZEOF_CONSTANT = False 

1290 NEEDS_BITSIZEOF_POSITION = False 

1291 NEEDS_READ_INDEX = False 

1292 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1293 

1294 @property 

1295 def packed_traits(self) -> PackedArrayTraits: 

1296 """ 

1297 Gets packed array traits. 

1298 

1299 :returns: PackedArrayTraits instance. 

1300 """ 

1301 

1302 return PackedArrayTraits(self) 

1303 

1304 @staticmethod 

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

1306 """ 

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

1308 

1309 :param value: Zserio varint16 type value. 

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

1311 """ 

1312 

1313 return bitsizeof_varint16(value) 

1314 

1315 @staticmethod 

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

1317 """ 

1318 Initializes indexed offsets for Zserio varint16 type. 

1319 

1320 :param bitposition: Current bit stream position. 

1321 :param value: Zserio varint16 type value. 

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

1323 """ 

1324 

1325 return bitposition + VarInt16ArrayTraits.bitsizeof(value) 

1326 

1327 @staticmethod 

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

1329 """ 

1330 Reads Zserio varint16 type from the bit stream. 

1331 

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

1333 :returns: Read varint16 value. 

1334 """ 

1335 

1336 return reader.read_varint16() 

1337 

1338 @staticmethod 

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

1340 """ 

1341 Writes Zserio varint16 type to the bit stream. 

1342 

1343 :param writer: Bit stream where to write. 

1344 :param value: Zserio varint16 type to write. 

1345 """ 

1346 

1347 writer.write_varint16(value) 

1348 

1349 

1350class VarInt32ArrayTraits: 

1351 """ 

1352 Array traits for Zserio varint32 type. 

1353 """ 

1354 

1355 HAS_BITSIZEOF_CONSTANT = False 

1356 NEEDS_BITSIZEOF_POSITION = False 

1357 NEEDS_READ_INDEX = False 

1358 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1359 

1360 @property 

1361 def packed_traits(self) -> PackedArrayTraits: 

1362 """ 

1363 Gets packed array traits. 

1364 

1365 :returns: PackedArrayTraits instance. 

1366 """ 

1367 

1368 return PackedArrayTraits(self) 

1369 

1370 @staticmethod 

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

1372 """ 

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

1374 

1375 :param value: Zserio varint32 type value. 

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

1377 """ 

1378 

1379 return bitsizeof_varint32(value) 

1380 

1381 @staticmethod 

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

1383 """ 

1384 Initializes indexed offsets for Zserio varint32 type. 

1385 

1386 :param bitposition: Current bit stream position. 

1387 :param value: Zserio varint32 type value. 

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

1389 """ 

1390 

1391 return bitposition + VarInt32ArrayTraits.bitsizeof(value) 

1392 

1393 @staticmethod 

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

1395 """ 

1396 Reads Zserio varint32 type from the bit stream. 

1397 

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

1399 :returns: Read varint32 value. 

1400 """ 

1401 

1402 return reader.read_varint32() 

1403 

1404 @staticmethod 

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

1406 """ 

1407 Writes Zserio varint32 type to the bit stream. 

1408 

1409 :param writer: Bit stream where to write. 

1410 :param value: Zserio varint32 type to write. 

1411 """ 

1412 

1413 writer.write_varint32(value) 

1414 

1415 

1416class VarInt64ArrayTraits: 

1417 """ 

1418 Array traits for Zserio varint64 type. 

1419 """ 

1420 

1421 HAS_BITSIZEOF_CONSTANT = False 

1422 NEEDS_BITSIZEOF_POSITION = False 

1423 NEEDS_READ_INDEX = False 

1424 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1425 

1426 @property 

1427 def packed_traits(self) -> PackedArrayTraits: 

1428 """ 

1429 Gets packed array traits. 

1430 

1431 :returns: PackedArrayTraits instance. 

1432 """ 

1433 

1434 return PackedArrayTraits(self) 

1435 

1436 @staticmethod 

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

1438 """ 

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

1440 

1441 :param value: Zserio varint64 type value. 

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

1443 """ 

1444 

1445 return bitsizeof_varint64(value) 

1446 

1447 @staticmethod 

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

1449 """ 

1450 Initializes indexed offsets for Zserio varint64 type. 

1451 

1452 :param bitposition: Current bit stream position. 

1453 :param value: Zserio varint64 type value. 

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

1455 """ 

1456 

1457 return bitposition + VarInt64ArrayTraits.bitsizeof(value) 

1458 

1459 @staticmethod 

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

1461 """ 

1462 Reads Zserio varint64 type from the bit stream. 

1463 

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

1465 :returns: Read varint64 value. 

1466 """ 

1467 

1468 return reader.read_varint64() 

1469 

1470 @staticmethod 

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

1472 """ 

1473 Writes Zserio varint64 type to the bit stream. 

1474 

1475 :param writer: Bit stream where to write. 

1476 :param value: Zserio varint64 type to write. 

1477 """ 

1478 

1479 writer.write_varint64(value) 

1480 

1481 

1482class VarIntArrayTraits: 

1483 """ 

1484 Array traits for Zserio varint type. 

1485 """ 

1486 

1487 HAS_BITSIZEOF_CONSTANT = False 

1488 NEEDS_BITSIZEOF_POSITION = False 

1489 NEEDS_READ_INDEX = False 

1490 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1491 

1492 @property 

1493 def packed_traits(self) -> PackedArrayTraits: 

1494 """ 

1495 Gets packed array traits. 

1496 

1497 :returns: PackedArrayTraits instance. 

1498 """ 

1499 

1500 return PackedArrayTraits(self) 

1501 

1502 @staticmethod 

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

1504 """ 

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

1506 

1507 :param value: Zserio varint type value. 

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

1509 """ 

1510 

1511 return bitsizeof_varint(value) 

1512 

1513 @staticmethod 

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

1515 """ 

1516 Initializes indexed offsets for Zserio varint type. 

1517 

1518 :param bitposition: Current bit stream position. 

1519 :param value: Zserio varint type value. 

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

1521 """ 

1522 

1523 return bitposition + VarIntArrayTraits.bitsizeof(value) 

1524 

1525 @staticmethod 

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

1527 """ 

1528 Reads Zserio varint type from the bit stream. 

1529 

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

1531 :returns: Read varint value. 

1532 """ 

1533 

1534 return reader.read_varint() 

1535 

1536 @staticmethod 

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

1538 """ 

1539 Writes Zserio varint type to the bit stream. 

1540 

1541 :param writer: Bit stream where to write. 

1542 :param value: Zserio varint type to write. 

1543 """ 

1544 

1545 writer.write_varint(value) 

1546 

1547 

1548class Float16ArrayTraits: 

1549 """ 

1550 Array traits for Zserio float16 type. 

1551 """ 

1552 

1553 HAS_BITSIZEOF_CONSTANT = True 

1554 NEEDS_BITSIZEOF_POSITION = False 

1555 NEEDS_READ_INDEX = False 

1556 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float32_array) 

1557 

1558 @property 

1559 def packed_traits(self) -> None: 

1560 """ 

1561 Returns None since float type is not packable. 

1562 

1563 :returns: None. 

1564 """ 

1565 

1566 return None 

1567 

1568 @staticmethod 

1569 def bitsizeof() -> int: 

1570 """ 

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

1572 

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

1574 """ 

1575 

1576 return 16 

1577 

1578 @staticmethod 

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

1580 """ 

1581 Initializes indexed offsets for Zserio float16 type. 

1582 

1583 :param bitposition: Current bit stream position. 

1584 :param _value: Not used. 

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

1586 """ 

1587 

1588 return bitposition + Float16ArrayTraits.bitsizeof() 

1589 

1590 @staticmethod 

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

1592 """ 

1593 Reads Zserio float16 type from the bit stream. 

1594 

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

1596 """ 

1597 

1598 return reader.read_float16() 

1599 

1600 @staticmethod 

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

1602 """ 

1603 Writes Zserio float16 type to the bit stream. 

1604 

1605 :param writer: Bit stream where to write. 

1606 :param value: Zserio float16 type to write. 

1607 :returns: Read float16 value. 

1608 """ 

1609 

1610 writer.write_float16(value) 

1611 

1612 

1613class Float32ArrayTraits: 

1614 """ 

1615 Array traits for Zserio float32 type. 

1616 """ 

1617 

1618 HAS_BITSIZEOF_CONSTANT = True 

1619 NEEDS_BITSIZEOF_POSITION = False 

1620 NEEDS_READ_INDEX = False 

1621 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float32_array) 

1622 

1623 @property 

1624 def packed_traits(self) -> None: 

1625 """ 

1626 Returns None since float type is not packable. 

1627 

1628 :returns: None. 

1629 """ 

1630 

1631 return None 

1632 

1633 @staticmethod 

1634 def bitsizeof() -> int: 

1635 """ 

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

1637 

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

1639 """ 

1640 

1641 return 32 

1642 

1643 @staticmethod 

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

1645 """ 

1646 Initializes indexed offsets for Zserio float32 type. 

1647 

1648 :param bitposition: Current bit stream position. 

1649 :param _value: Not used. 

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

1651 """ 

1652 

1653 return bitposition + Float32ArrayTraits.bitsizeof() 

1654 

1655 @staticmethod 

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

1657 """ 

1658 Reads Zserio float32 type from the bit stream. 

1659 

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

1661 :returns: Read float32 value. 

1662 """ 

1663 

1664 return reader.read_float32() 

1665 

1666 @staticmethod 

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

1668 """ 

1669 Writes Zserio float32 type to the bit stream. 

1670 

1671 :param writer: Bit stream where to write. 

1672 :param value: Zserio float32 type to write. 

1673 """ 

1674 

1675 writer.write_float32(value) 

1676 

1677 

1678class Float64ArrayTraits: 

1679 """ 

1680 Array traits for Zserio float64 type. 

1681 """ 

1682 

1683 HAS_BITSIZEOF_CONSTANT = True 

1684 NEEDS_BITSIZEOF_POSITION = False 

1685 NEEDS_READ_INDEX = False 

1686 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float64_array) 

1687 

1688 @property 

1689 def packed_traits(self) -> None: 

1690 """ 

1691 Returns None since float type is not packable. 

1692 

1693 :returns: None. 

1694 """ 

1695 

1696 return None 

1697 

1698 @staticmethod 

1699 def bitsizeof() -> int: 

1700 """ 

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

1702 

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

1704 """ 

1705 

1706 return 64 

1707 

1708 @staticmethod 

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

1710 """ 

1711 Initializes indexed offsets for Zserio float64 type. 

1712 

1713 :param bitposition: Current bit stream position. 

1714 :param _value: Not used. 

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

1716 """ 

1717 

1718 return bitposition + Float64ArrayTraits.bitsizeof() 

1719 

1720 @staticmethod 

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

1722 """ 

1723 Reads Zserio float64 type from the bit stream. 

1724 

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

1726 :returns: Read float64 value. 

1727 """ 

1728 

1729 return reader.read_float64() 

1730 

1731 @staticmethod 

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

1733 """ 

1734 Writes Zserio float64 type to the bit stream. 

1735 

1736 :param writer: Bit stream where to write. 

1737 :param value: Zserio float64 type to write. 

1738 """ 

1739 

1740 writer.write_float64(value) 

1741 

1742 

1743class BytesArrayTraits: 

1744 """ 

1745 Array traits for Zserio bytes type. 

1746 """ 

1747 

1748 HAS_BITSIZEOF_CONSTANT = False 

1749 NEEDS_BITSIZEOF_POSITION = False 

1750 NEEDS_READ_INDEX = False 

1751 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_bytes_array) 

1752 

1753 @property 

1754 def packed_traits(self) -> None: 

1755 """ 

1756 Returns None since Bytes type is not packable. 

1757 

1758 :returns: None. 

1759 """ 

1760 

1761 return None 

1762 

1763 @staticmethod 

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

1765 """ 

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

1767 

1768 :param value: Zserio bytes type value. 

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

1770 """ 

1771 

1772 return bitsizeof_bytes(value) 

1773 

1774 @staticmethod 

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

1776 """ 

1777 Initializes indexed offsets for Zserio bytes type. 

1778 

1779 :param bitposition: Current bit stream position. 

1780 :param value: Zserio bytes type value. 

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

1782 """ 

1783 

1784 return bitposition + BytesArrayTraits.bitsizeof(value) 

1785 

1786 @staticmethod 

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

1788 """ 

1789 Reads Zserio bytes type from the bit stream. 

1790 

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

1792 :returns: Read bytes value. 

1793 """ 

1794 

1795 return reader.read_bytes() 

1796 

1797 @staticmethod 

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

1799 """ 

1800 Writes Zserio bytes type to the bit stream. 

1801 

1802 :param writer: Bit stream where to write. 

1803 :param value: Zserio bytes type to write. 

1804 """ 

1805 

1806 writer.write_bytes(value) 

1807 

1808 

1809class StringArrayTraits: 

1810 """ 

1811 Array traits for Zserio string type. 

1812 """ 

1813 

1814 HAS_BITSIZEOF_CONSTANT = False 

1815 NEEDS_BITSIZEOF_POSITION = False 

1816 NEEDS_READ_INDEX = False 

1817 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_string_array) 

1818 

1819 @property 

1820 def packed_traits(self) -> None: 

1821 """ 

1822 Returns None since String type is not packable. 

1823 

1824 :returns: None. 

1825 """ 

1826 

1827 return None 

1828 

1829 @staticmethod 

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

1831 """ 

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

1833 

1834 :param value: Zserio string type value. 

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

1836 """ 

1837 

1838 return bitsizeof_string(value) 

1839 

1840 @staticmethod 

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

1842 """ 

1843 Initializes indexed offsets for Zserio string type. 

1844 

1845 :param bitposition: Current bit stream position. 

1846 :param value: Zserio string type value. 

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

1848 """ 

1849 

1850 return bitposition + StringArrayTraits.bitsizeof(value) 

1851 

1852 @staticmethod 

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

1854 """ 

1855 Reads Zserio string type from the bit stream. 

1856 

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

1858 :returns: Read string value. 

1859 """ 

1860 

1861 return reader.read_string() 

1862 

1863 @staticmethod 

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

1865 """ 

1866 Writes Zserio string type to the bit stream. 

1867 

1868 :param writer: Bit stream where to write. 

1869 :param value: Zserio string type to write. 

1870 """ 

1871 

1872 writer.write_string(value) 

1873 

1874 

1875class BoolArrayTraits: 

1876 """ 

1877 Array traits for Zserio bool type. 

1878 """ 

1879 

1880 HAS_BITSIZEOF_CONSTANT = True 

1881 NEEDS_BITSIZEOF_POSITION = False 

1882 NEEDS_READ_INDEX = False 

1883 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_bool_array) 

1884 

1885 @property 

1886 def packed_traits(self) -> None: 

1887 """ 

1888 Returns None since Bool type is not packable. 

1889 

1890 :returns: None. 

1891 """ 

1892 

1893 return None 

1894 

1895 @staticmethod 

1896 def bitsizeof() -> int: 

1897 """ 

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

1899 

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

1901 """ 

1902 

1903 return 1 

1904 

1905 @staticmethod 

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

1907 """ 

1908 Initializes indexed offsets for Zserio bool type. 

1909 

1910 :param bitposition: Current bit stream position. 

1911 :param _value: Not used. 

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

1913 """ 

1914 

1915 return bitposition + BoolArrayTraits.bitsizeof() 

1916 

1917 @staticmethod 

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

1919 """ 

1920 Reads Zserio bool type from the bit stream. 

1921 

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

1923 :returns: Read bool value. 

1924 """ 

1925 

1926 return reader.read_bool() 

1927 

1928 @staticmethod 

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

1930 """ 

1931 Writes Zserio bool type to the bit stream. 

1932 

1933 :param writer: Bit stream where to write. 

1934 :param value: Zserio bool type to write. 

1935 """ 

1936 

1937 writer.write_bool(value) 

1938 

1939 

1940class BitBufferArrayTraits: 

1941 """ 

1942 Array traits for Zserio extern bit buffer type. 

1943 """ 

1944 

1945 HAS_BITSIZEOF_CONSTANT = False 

1946 NEEDS_BITSIZEOF_POSITION = False 

1947 NEEDS_READ_INDEX = False 

1948 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_object_array) 

1949 

1950 @property 

1951 def packed_traits(self) -> None: 

1952 """ 

1953 Returns None since BitBuffer type is not packable. 

1954 

1955 :returns: None. 

1956 """ 

1957 

1958 return None 

1959 

1960 @staticmethod 

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

1962 """ 

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

1964 

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

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

1967 """ 

1968 

1969 return bitsizeof_bitbuffer(value) 

1970 

1971 @staticmethod 

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

1973 """ 

1974 Initializes indexed offsets for Zserio extern bit buffer type. 

1975 

1976 :param bitposition: Current bit stream position. 

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

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

1979 """ 

1980 

1981 return bitposition + BitBufferArrayTraits.bitsizeof(value) 

1982 

1983 @staticmethod 

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

1985 """ 

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

1987 

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

1989 :returns: Read bit buffer value. 

1990 """ 

1991 

1992 return reader.read_bitbuffer() 

1993 

1994 @staticmethod 

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

1996 """ 

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

1998 

1999 :param writer: Bit stream where to write. 

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

2001 """ 

2002 

2003 writer.write_bitbuffer(value) 

2004 

2005 

2006class ObjectArrayTraits: 

2007 """ 

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

2009 """ 

2010 

2011 HAS_BITSIZEOF_CONSTANT = False 

2012 NEEDS_BITSIZEOF_POSITION = True 

2013 NEEDS_READ_INDEX = True 

2014 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_object_array) 

2015 

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

2017 """ 

2018 Constructor. 

2019 

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

2021 """ 

2022 

2023 self._element_factory = element_factory 

2024 self._packed_traits = ( 

2025 ObjectPackedArrayTraits(element_factory) if element_factory.IS_OBJECT_PACKABLE else None 

2026 ) 

2027 

2028 @property 

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

2030 """ 

2031 Gets packed array traits. 

2032 

2033 :returns: ObjectPackedArrayTraits instance. 

2034 """ 

2035 

2036 return self._packed_traits 

2037 

2038 @staticmethod 

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

2040 """ 

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

2042 

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

2044 :param value: Zserio object type value. 

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

2046 """ 

2047 

2048 return value.bitsizeof(bitposition) 

2049 

2050 @staticmethod 

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

2052 """ 

2053 Initializes indexed offsets for the Zserio object type. 

2054 

2055 :param bitposition: Current bit stream position. 

2056 :param value: Zserio object type value. 

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

2058 """ 

2059 

2060 return value.initialize_offsets(bitposition) 

2061 

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

2063 """ 

2064 Reads Zserio object type from the bit stream. 

2065 

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

2067 :param index: Element index in the array. 

2068 :returns: Read object. 

2069 """ 

2070 

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

2072 

2073 @staticmethod 

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

2075 """ 

2076 Writes Zserio object type to the bit stream. 

2077 

2078 :param writer: Bit stream where to write. 

2079 :param value: Zserio object type to write. 

2080 """ 

2081 

2082 value.write(writer)