Coverage for /home/jenkins/workspace/NDS/Zserio/NDS_ZSERIO-linux-build/compiler/extensions/python/runtime/src/zserio/array.py: 100%

709 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-12-13 15:12 +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 (bitsizeof_varuint16, bitsizeof_varuint32, bitsizeof_varuint64, bitsizeof_varuint, 

9 bitsizeof_varint16, bitsizeof_varint32, bitsizeof_varint64, bitsizeof_varint, 

10 bitsizeof_varsize, bitsizeof_bytes, bitsizeof_string, bitsizeof_bitbuffer) 

11from zserio.bitreader import BitStreamReader 

12from zserio.bitwriter import BitStreamWriter 

13from zserio.bitbuffer import BitBuffer 

14from zserio.hashcode import (HASH_SEED, calc_hashcode_bool_array, calc_hashcode_int_array, 

15 calc_hashcode_float32_array, calc_hashcode_float64_array, 

16 calc_hashcode_bytes_array, calc_hashcode_string_array, calc_hashcode_object_array) 

17from zserio.exception import PythonRuntimeException 

18 

19class Array: 

20 """ 

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

22 """ 

23 

24 def __init__(self, 

25 array_traits: typing.Any, 

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

27 *, 

28 is_auto: bool = False, 

29 is_implicit: bool = False, 

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

31 check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None) -> None: 

32 """ 

33 Constructor. 

34 

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

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

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

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

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

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

41 """ 

42 

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

44 self._array_traits: typing.Any = array_traits 

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

46 self._is_auto: bool = is_auto 

47 self._is_implicit: bool = is_implicit 

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

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

50 

51 @classmethod 

52 def from_reader(cls: typing.Type['Array'], 

53 array_traits: typing.Any, 

54 reader: BitStreamReader, 

55 size: int = 0, 

56 *, 

57 is_auto: bool = False, 

58 is_implicit: bool = False, 

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

60 check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None) -> 'Array': 

61 """ 

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

63 

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

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

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

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

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

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

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

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

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

73 """ 

74 

75 instance = cls(array_traits, is_auto=is_auto, is_implicit=is_implicit, 

76 set_offset_method=set_offset_method, check_offset_method=check_offset_method) 

77 instance.read(reader, size) 

78 

79 return instance 

80 

81 @classmethod 

82 def from_reader_packed( 

83 cls: typing.Type['Array'], 

84 array_traits: typing.Any, 

85 reader: BitStreamReader, 

86 size: int = 0, 

87 *, 

88 is_auto: bool = False, 

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

90 check_offset_method: 

91 typing.Optional[typing.Callable[[int, int], None]] = None) -> 'Array': 

92 """ 

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

94 

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

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

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

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

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

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

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

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

103 """ 

104 

105 instance = cls(array_traits, is_auto=is_auto, is_implicit=False, set_offset_method=set_offset_method, 

106 check_offset_method=check_offset_method) 

107 instance.read_packed(reader, size) 

108 

109 return instance 

110 

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

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

113 if isinstance(other, Array): 

114 return self._raw_array == other._raw_array 

115 

116 return False 

117 

118 def __hash__(self) -> int: 

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

120 

121 def __len__(self) -> int: 

122 return len(self._raw_array) 

123 

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

125 return self._raw_array[key] 

126 

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

128 self._raw_array[key] = value 

129 

130 @property 

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

132 """ 

133 Gets raw array. 

134 

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

136 """ 

137 

138 return self._raw_array 

139 

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

141 """ 

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

143 

144 :param bitposition: Current bit stream position. 

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

146 """ 

147 

148 end_bitposition = bitposition 

149 size = len(self._raw_array) 

150 if self._is_auto: 

151 end_bitposition += bitsizeof_varsize(size) 

152 

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

154 element_size = self._array_traits.bitsizeof() 

155 if self._set_offset_method is None: 

156 end_bitposition += size * element_size 

157 else: 

158 end_bitposition = alignto(8, end_bitposition) 

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

160 else: 

161 array_traits_bitsizeof = self._array_traits.bitsizeof 

162 if self._set_offset_method is not None: 

163 if self._array_traits.NEEDS_BITSIZEOF_POSITION: 

164 for element in self._raw_array: 

165 end_bitposition = alignto(8, end_bitposition) 

166 end_bitposition += array_traits_bitsizeof(end_bitposition, element) 

167 else: 

168 for element in self._raw_array: 

169 end_bitposition = alignto(8, end_bitposition) 

170 end_bitposition += array_traits_bitsizeof(element) 

171 else: 

172 if self._array_traits.NEEDS_BITSIZEOF_POSITION: 

173 for element in self._raw_array: 

174 end_bitposition += array_traits_bitsizeof(end_bitposition, element) 

175 else: 

176 for element in self._raw_array: 

177 end_bitposition += array_traits_bitsizeof(element) 

178 

179 return end_bitposition - bitposition 

180 

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

182 """ 

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

184 

185 :param bitposition: Current bit stream position. 

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

187 """ 

188 

189 end_bitposition = bitposition 

190 size = len(self._raw_array) 

191 if self._is_auto: 

192 end_bitposition += bitsizeof_varsize(size) 

193 

194 if size > 0: 

195 context = self._packed_array_traits.create_context() 

196 packed_array_traits_init_context = self._packed_array_traits.init_context 

197 packed_array_traits_bitsizeof = self._packed_array_traits.bitsizeof 

198 

199 for element in self._raw_array: 

200 packed_array_traits_init_context(context, element) 

201 

202 if self._set_offset_method is not None: 

203 for element in self._raw_array: 

204 end_bitposition = alignto(8, end_bitposition) 

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

206 else: 

207 for element in self._raw_array: 

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

209 

210 

211 return end_bitposition - bitposition 

212 

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

214 """ 

215 Initializes indexed offsets for the array. 

216 

217 :param bitposition: Current bit stream position. 

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

219 """ 

220 

221 end_bitposition = bitposition 

222 size = len(self._raw_array) 

223 if self._is_auto: 

224 end_bitposition += bitsizeof_varsize(size) 

225 

226 array_traits_initialize_offsets = self._array_traits.initialize_offsets 

227 if self._set_offset_method is not None: 

228 set_offset = self._set_offset_method 

229 for index in range(size): 

230 end_bitposition = alignto(8, end_bitposition) 

231 set_offset(index, end_bitposition) 

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

233 else: 

234 for element in self._raw_array: 

235 end_bitposition = array_traits_initialize_offsets(end_bitposition, element) 

236 

237 return end_bitposition 

238 

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

240 """ 

241 Initializes indexed offsets for the packed array. 

242 

243 :param bitposition: Current bit stream position. 

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

245 """ 

246 

247 end_bitposition = bitposition 

248 size = len(self._raw_array) 

249 if self._is_auto: 

250 end_bitposition += bitsizeof_varsize(size) 

251 

252 if size > 0: 

253 context = self._packed_array_traits.create_context() 

254 packed_array_traits_init_context = self._packed_array_traits.init_context 

255 packed_array_traits_initialize_offsets = self._packed_array_traits.initialize_offsets 

256 

257 for element in self._raw_array: 

258 packed_array_traits_init_context(context, element) 

259 

260 if self._set_offset_method is not None: 

261 set_offset = self._set_offset_method 

262 for index in range(size): 

263 end_bitposition = alignto(8, end_bitposition) 

264 set_offset(index, end_bitposition) 

265 end_bitposition = packed_array_traits_initialize_offsets(context, end_bitposition, 

266 self._raw_array[index]) 

267 else: 

268 for element in self._raw_array: 

269 end_bitposition = packed_array_traits_initialize_offsets(context, end_bitposition, 

270 element) 

271 

272 return end_bitposition 

273 

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

275 """ 

276 Reads array from the bit stream. 

277 

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

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

280 

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

282 """ 

283 

284 self._raw_array.clear() 

285 

286 append = self._raw_array.append 

287 array_traits_read = self._array_traits.read 

288 if self._is_implicit: 

289 if not self._array_traits.HAS_BITSIZEOF_CONSTANT: 

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

291 

292 element_size = self._array_traits.bitsizeof() 

293 remaining_bits = reader.buffer_bitsize - reader.bitposition 

294 read_size = remaining_bits // element_size 

295 for _ in range(read_size): 

296 # we know that no traits NEEDS_READ_INDEX here 

297 append(array_traits_read(reader)) 

298 else: 

299 if self._is_auto: 

300 read_size = reader.read_varsize() 

301 else: 

302 read_size = size 

303 

304 if self._check_offset_method is not None: 

305 check_offset = self._check_offset_method 

306 reader_alignto = reader.alignto 

307 if self._array_traits.NEEDS_READ_INDEX: 

308 for index in range(read_size): 

309 reader_alignto(8) 

310 check_offset(index, reader.bitposition) 

311 append(array_traits_read(reader, index)) 

312 else: 

313 for index in range(read_size): 

314 reader_alignto(8) 

315 check_offset(index, reader.bitposition) 

316 append(array_traits_read(reader)) 

317 else: 

318 if self._array_traits.NEEDS_READ_INDEX: 

319 for index in range(read_size): 

320 append(array_traits_read(reader, index)) 

321 else: 

322 for _ in range(read_size): 

323 append(array_traits_read(reader)) 

324 

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

326 """ 

327 Reads packed array from the bit stream. 

328 

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

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

331 """ 

332 

333 self._raw_array.clear() 

334 

335 if self._is_implicit: 

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

337 

338 if self._is_auto: 

339 read_size = reader.read_varsize() 

340 else: 

341 read_size = size 

342 

343 if read_size > 0: 

344 context = self._packed_array_traits.create_context() 

345 

346 append = self._raw_array.append 

347 packed_array_traits_read = self._packed_array_traits.read 

348 

349 if self._check_offset_method is not None: 

350 check_offset = self._check_offset_method 

351 reader_alignto = reader.alignto 

352 for index in range(read_size): 

353 reader_alignto(8) 

354 check_offset(index, reader.bitposition) 

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

356 else: 

357 for index in range(read_size): 

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

359 

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

361 """ 

362 Writes array to the bit stream. 

363 

364 :param writer: Bit stream where to write. 

365 """ 

366 

367 size = len(self._raw_array) 

368 if self._is_auto: 

369 writer.write_varsize(size) 

370 

371 array_traits_write = self._array_traits.write 

372 if self._check_offset_method is not None: 

373 check_offset = self._check_offset_method 

374 writer_alignto = writer.alignto 

375 for index in range(size): 

376 writer_alignto(8) 

377 check_offset(index, writer.bitposition) 

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

379 else: 

380 for element in self._raw_array: 

381 array_traits_write(writer, element) 

382 

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

384 """ 

385 Writes packed array to the bit stream. 

386 

387 :param writer: Bit stream where to write. 

388 """ 

389 

390 size = len(self._raw_array) 

391 

392 if self._is_auto: 

393 writer.write_varsize(size) 

394 

395 if size > 0: 

396 context = self._packed_array_traits.create_context() 

397 packed_array_traits_init_context = self._packed_array_traits.init_context 

398 packed_array_traits_write = self._packed_array_traits.write 

399 

400 for element in self._raw_array: 

401 packed_array_traits_init_context(context, element) 

402 

403 if self._check_offset_method is not None: 

404 check_offset = self._check_offset_method 

405 writer_alignto = writer.alignto 

406 for index in range(size): 

407 writer_alignto(8) 

408 check_offset(index, writer.bitposition) 

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

410 else: 

411 for element in self._raw_array: 

412 packed_array_traits_write(context, writer, element) 

413 

414class DeltaContext: 

415 """ 

416 Context for delta packing created for each packable field. 

417 

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

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

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

421 called for exactly the same sequence of packable elements. 

422 

423 Example:: 

424 

425 context = DeltaContext(array_traits) 

426 for element in array: 

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

428 context.write_descriptor(writer) # finishes the initialization 

429 for element in array: 

430 context.write(writer, element) 

431 """ 

432 

433 def __init__(self) -> None: 

434 """ Constructor. """ 

435 

436 self._is_packed = False 

437 self._max_bit_number = 0 

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

439 self._processing_started = False 

440 self._unpacked_bitsize = 0 

441 self._first_element_bitsize = 0 

442 self._num_elements = 0 

443 

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

445 """ 

446 Makes initialization step for the provided array element. 

447 

448 :param array_traits: Standard array traits. 

449 :param element: Current element of the array. 

450 """ 

451 

452 self._num_elements += 1 

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

454 

455 if self._previous_element is None: 

456 self._previous_element = element 

457 self._first_element_bitsize = self._unpacked_bitsize 

458 else: 

459 if self._max_bit_number <= self._MAX_BIT_NUMBER_LIMIT: 

460 self._is_packed = True 

461 

462 delta = element - self._previous_element 

463 max_bit_number = delta.bit_length() 

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

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

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

467 if max_bit_number > self._max_bit_number: 

468 self._max_bit_number = max_bit_number 

469 if max_bit_number > self._MAX_BIT_NUMBER_LIMIT: 

470 self._is_packed = False 

471 

472 self._previous_element = element 

473 

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

475 """ 

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

477 

478 :param array_traits: Standard integral array traits. 

479 :param element: Current element. 

480 

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

482 """ 

483 

484 if not self._processing_started: 

485 self._processing_started = True 

486 self._finish_init() 

487 

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

489 elif not self._is_packed: 

490 return DeltaContext._bitsizeof_unpacked(array_traits, element) 

491 else: 

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

493 

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

495 """ 

496 Reads the packed element from the bit stream. 

497 

498 :param array_traits: Standard array traits. 

499 :param reader: Bit stream reader. 

500 """ 

501 

502 if not self._processing_started: 

503 self._processing_started = True 

504 self._read_descriptor(reader) 

505 

506 return self._read_unpacked(array_traits, reader) 

507 elif not self._is_packed: 

508 return self._read_unpacked(array_traits, reader) 

509 else: 

510 assert self._previous_element is not None 

511 if self._max_bit_number > 0: 

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

513 self._previous_element += delta 

514 return self._previous_element 

515 

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

517 """ 

518 Writes the packed element representation to the bit stream. 

519 

520 :param array_traits: Standard array traits. 

521 :param writer: Bit stream writer. 

522 :param element: Element to write. 

523 """ 

524 

525 if not self._processing_started: 

526 self._processing_started = True 

527 self._finish_init() 

528 self._write_descriptor(writer) 

529 

530 self._write_unpacked(array_traits, writer, element) 

531 elif not self._is_packed: 

532 self._write_unpacked(array_traits, writer, element) 

533 else: # packed and not first 

534 assert self._previous_element is not None 

535 if self._max_bit_number > 0: 

536 delta = element - self._previous_element 

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

538 self._previous_element = element 

539 

540 def _finish_init(self) -> None: 

541 if self._is_packed: 

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

543 packed_bitsize_with_descriptor = ( 

544 1 + self._MAX_BIT_NUMBER_BITS + # descriptor 

545 self._first_element_bitsize + (self._num_elements - 1) * delta_bitsize 

546 ) 

547 unpacked_bitsize_with_descriptor = 1 + self._unpacked_bitsize 

548 if packed_bitsize_with_descriptor >= unpacked_bitsize_with_descriptor: 

549 self._is_packed = False 

550 

551 def _bitsizeof_descriptor(self) -> int: 

552 if self._is_packed: 

553 return 1 + self._MAX_BIT_NUMBER_BITS 

554 else: 

555 return 1 

556 

557 @staticmethod 

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

559 if array_traits.HAS_BITSIZEOF_CONSTANT: 

560 return array_traits.bitsizeof() 

561 else: # we know that NEEDS_BITSIZEOF_POSITION is False here 

562 return array_traits.bitsizeof(element) 

563 

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

565 self._is_packed = reader.read_bool() 

566 if self._is_packed: 

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

568 

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

570 element = array_traits.read(reader) 

571 self._previous_element = element 

572 return element 

573 

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

575 writer.write_bool(self._is_packed) 

576 if self._is_packed: 

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

578 

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

580 self._previous_element = element 

581 array_traits.write(writer, element) 

582 

583 _MAX_BIT_NUMBER_BITS = 6 

584 _MAX_BIT_NUMBER_LIMIT = 62 

585 

586class PackedArrayTraits: 

587 """ 

588 Packed array traits. 

589 

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

591 """ 

592 

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

594 """ 

595 Constructor. 

596 

597 :param array_traits: Standard array traits. 

598 """ 

599 

600 self._array_traits = array_traits 

601 

602 @staticmethod 

603 def create_context() -> DeltaContext: 

604 """ 

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

606 

607 :returns: New packing context. 

608 """ 

609 

610 return DeltaContext() 

611 

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

613 """ 

614 Calls context initialization step for the current element. 

615 

616 :param delta_context: Delta context. 

617 :param element: Current element. 

618 """ 

619 

620 delta_context.init(self._array_traits, element) 

621 

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

623 """ 

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

625 

626 :param delta_context: Delta context. 

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

628 :param elemnet: Current element. 

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

630 """ 

631 

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

633 

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

635 """ 

636 Calls indexed offsets initialization for the current element. 

637 

638 :param delta_context: Delta context. 

639 :param _bitposition: Current bit stream position. 

640 :param element: Current element. 

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

642 """ 

643 

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

645 

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

647 """ 

648 Writes the element to the bit stream. 

649 

650 :param delta_context: Delta context. 

651 :param writer: Bit stream writer. 

652 :param element: Element to write. 

653 """ 

654 

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

656 

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

658 """ 

659 Read an element from the bit stream. 

660 

661 :param delta_context: Delta context. 

662 :param reader: Bit stream reader. 

663 :param _index: Not used. 

664 :returns: Read element value. 

665 """ 

666 

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

668 

669class ObjectPackedArrayTraits: 

670 """ 

671 Packed array traits for Zserio objects. 

672 

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

674 to be used in a packed array. 

675 """ 

676 

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

678 """ 

679 Constructor. 

680 

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

682 """ 

683 

684 self._element_factory = element_factory 

685 

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

687 """ 

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

689 

690 :returns: New packing context. 

691 """ 

692 

693 return self._element_factory.create_packing_context() 

694 

695 @staticmethod 

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

697 """ 

698 Calls context initialization step for the current element. 

699 

700 :param context: Packing context. 

701 :param element: Current element. 

702 """ 

703 

704 element.init_packing_context(context) 

705 

706 @staticmethod 

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

708 """ 

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

710 

711 :param context: Packing context. 

712 :param bitposition: Current bit stream position. 

713 :param elemnet: Current element. 

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

715 """ 

716 

717 return element.bitsizeof_packed(context, bitposition) 

718 

719 @staticmethod 

720 def initialize_offsets(context: typing.Any, 

721 bitposition: int, element: typing.Any) -> int: 

722 """ 

723 Calls indexed offsets initialization for the current element. 

724 

725 :param context: Packing context. 

726 :param bitposition: Current bit stream position. 

727 :param element: Current element. 

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

729 """ 

730 

731 return element.initialize_offsets_packed(context, bitposition) 

732 

733 @staticmethod 

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

735 """ 

736 Writes the element to the bit stream. 

737 

738 :param context: Packing context. 

739 :param writer: Bit stream writer. 

740 :param element: Element to write. 

741 """ 

742 

743 element.write_packed(context, writer) 

744 

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

746 """ 

747 Read an element from the bit stream. 

748 

749 :param context: Packing context. 

750 :param reader: Bit stream reader. 

751 :param index: Index of the current element. 

752 :returns: Read element value. 

753 """ 

754 

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

756 

757class BitFieldArrayTraits: 

758 """ 

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

760 """ 

761 

762 HAS_BITSIZEOF_CONSTANT = True 

763 NEEDS_BITSIZEOF_POSITION = False 

764 NEEDS_READ_INDEX = False 

765 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

766 

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

768 """ 

769 Constructor. 

770 

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

772 """ 

773 

774 self._numbits = numbits 

775 

776 @property 

777 def packed_traits(self) -> PackedArrayTraits: 

778 """ 

779 Gets packed array traits. 

780 

781 :returns: PackedArrayTraits instance. 

782 """ 

783 

784 return PackedArrayTraits(self) 

785 

786 def bitsizeof(self) -> int: 

787 """ 

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

789 

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

791 """ 

792 

793 return self._numbits 

794 

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

796 """ 

797 Initializes indexed offsets for unsigned fixed integer Zserio type. 

798 

799 :param bitposition: Current bit stream position. 

800 :param _value: Not used. 

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

802 """ 

803 

804 return bitposition + self.bitsizeof() 

805 

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

807 """ 

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

809 

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

811 :returns: Read unsigned int value. 

812 """ 

813 

814 return reader.read_bits(self._numbits) 

815 

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

817 """ 

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

819 

820 :param writer: Bit stream where to write. 

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

822 """ 

823 

824 writer.write_bits(value, self._numbits) 

825 

826class SignedBitFieldArrayTraits: 

827 """ 

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

829 """ 

830 

831 HAS_BITSIZEOF_CONSTANT = True 

832 NEEDS_BITSIZEOF_POSITION = False 

833 NEEDS_READ_INDEX = False 

834 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

835 

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

837 """ 

838 Constructor. 

839 

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

841 """ 

842 

843 self._numbits = numbits 

844 

845 @property 

846 def packed_traits(self) -> PackedArrayTraits: 

847 """ 

848 Gets packed array traits. 

849 

850 :returns: PackedArrayTraits instance. 

851 """ 

852 

853 return PackedArrayTraits(self) 

854 

855 def bitsizeof(self) -> int: 

856 """ 

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

858 

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

860 """ 

861 

862 return self._numbits 

863 

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

865 """ 

866 Initializes indexed offsets for signed fixed integer Zserio type. 

867 

868 :param bitposition: Current bit stream position. 

869 :param _value: Not used. 

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

871 """ 

872 

873 return bitposition + self.bitsizeof() 

874 

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

876 """ 

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

878 

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

880 :returns: Read signed int value. 

881 """ 

882 

883 return reader.read_signed_bits(self._numbits) 

884 

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

886 """ 

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

888 

889 :param writer: Bit stream where to write. 

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

891 """ 

892 

893 writer.write_signed_bits(value, self._numbits) 

894 

895class VarUInt16ArrayTraits: 

896 """ 

897 Array traits for Zserio varuint16 type. 

898 """ 

899 

900 HAS_BITSIZEOF_CONSTANT = False 

901 NEEDS_BITSIZEOF_POSITION = False 

902 NEEDS_READ_INDEX = False 

903 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

904 

905 @property 

906 def packed_traits(self) -> PackedArrayTraits: 

907 """ 

908 Gets packed array traits. 

909 

910 :returns: PackedArrayTraits instance. 

911 """ 

912 

913 return PackedArrayTraits(self) 

914 

915 @staticmethod 

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

917 """ 

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

919 

920 :param value: Zserio varuint16 type value. 

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

922 """ 

923 

924 return bitsizeof_varuint16(value) 

925 

926 @staticmethod 

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

928 """ 

929 Initializes indexed offsets for Zserio varuint16 type. 

930 

931 :param bitposition: Current bit stream position. 

932 :param value: Zserio varuint16 type value. 

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

934 """ 

935 

936 return bitposition + VarUInt16ArrayTraits.bitsizeof(value) 

937 

938 @staticmethod 

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

940 """ 

941 Reads Zserio varuint16 type from the bit stream. 

942 

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

944 """ 

945 

946 return reader.read_varuint16() 

947 

948 @staticmethod 

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

950 """ 

951 Writes Zserio varuint16 type to the bit stream. 

952 

953 :param writer: Bit stream where to write. 

954 :param value: Zserio varuint16 type to write. 

955 :returns: Read varuint16 value. 

956 """ 

957 

958 writer.write_varuint16(value) 

959 

960class VarUInt32ArrayTraits: 

961 """ 

962 Array traits for Zserio varuint32 type. 

963 """ 

964 

965 HAS_BITSIZEOF_CONSTANT = False 

966 NEEDS_BITSIZEOF_POSITION = False 

967 NEEDS_READ_INDEX = False 

968 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

969 

970 @property 

971 def packed_traits(self) -> PackedArrayTraits: 

972 """ 

973 Gets packed array traits. 

974 

975 :returns: PackedArrayTraits instance. 

976 """ 

977 

978 return PackedArrayTraits(self) 

979 

980 @staticmethod 

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

982 """ 

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

984 

985 :param value: Zserio varuint32 type value. 

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

987 """ 

988 

989 return bitsizeof_varuint32(value) 

990 

991 @staticmethod 

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

993 """ 

994 Initializes indexed offsets for Zserio varuint32 type. 

995 

996 :param bitposition: Current bit stream position. 

997 :param value: Zserio varuint32 type value. 

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

999 """ 

1000 

1001 return bitposition + VarUInt32ArrayTraits.bitsizeof(value) 

1002 

1003 @staticmethod 

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

1005 """ 

1006 Reads Zserio varuint32 type from the bit stream. 

1007 

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

1009 :returns: Read varuint32 value. 

1010 """ 

1011 

1012 return reader.read_varuint32() 

1013 

1014 @staticmethod 

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

1016 """ 

1017 Writes Zserio varuint32 type to the bit stream. 

1018 

1019 :param writer: Bit stream where to write. 

1020 :param value: Zserio varuint32 type to write. 

1021 """ 

1022 

1023 writer.write_varuint32(value) 

1024 

1025class VarUInt64ArrayTraits: 

1026 """ 

1027 Array traits for Zserio varuint64 type. 

1028 """ 

1029 

1030 HAS_BITSIZEOF_CONSTANT = False 

1031 NEEDS_BITSIZEOF_POSITION = False 

1032 NEEDS_READ_INDEX = False 

1033 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1034 

1035 @property 

1036 def packed_traits(self) -> PackedArrayTraits: 

1037 """ 

1038 Gets packed array traits. 

1039 

1040 :returns: PackedArrayTraits instance. 

1041 """ 

1042 

1043 return PackedArrayTraits(self) 

1044 

1045 @staticmethod 

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

1047 """ 

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

1049 

1050 :param value: Zserio varuint64 type value. 

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

1052 """ 

1053 

1054 return bitsizeof_varuint64(value) 

1055 

1056 @staticmethod 

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

1058 """ 

1059 Initializes indexed offsets for Zserio varuint64 type. 

1060 

1061 :param value: Zserio varuint64 type value. 

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

1063 """ 

1064 

1065 return bitposition + VarUInt64ArrayTraits.bitsizeof(value) 

1066 

1067 @staticmethod 

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

1069 """ 

1070 Reads Zserio varuint64 type from the bit stream. 

1071 

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

1073 :returns: Read varuint64 value. 

1074 """ 

1075 

1076 return reader.read_varuint64() 

1077 

1078 @staticmethod 

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

1080 """ 

1081 Writes Zserio varuint64 type to the bit stream. 

1082 

1083 :param writer: Bit stream where to write. 

1084 :param value: Zserio varuint64 type to write. 

1085 """ 

1086 

1087 writer.write_varuint64(value) 

1088 

1089class VarUIntArrayTraits: 

1090 """ 

1091 Array traits for Zserio varuint type. 

1092 """ 

1093 

1094 HAS_BITSIZEOF_CONSTANT = False 

1095 NEEDS_BITSIZEOF_POSITION = False 

1096 NEEDS_READ_INDEX = False 

1097 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1098 

1099 @property 

1100 def packed_traits(self) -> PackedArrayTraits: 

1101 """ 

1102 Gets packed array traits. 

1103 

1104 :returns: PackedArrayTraits instance. 

1105 """ 

1106 

1107 return PackedArrayTraits(self) 

1108 

1109 @staticmethod 

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

1111 """ 

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

1113 

1114 :param value: Zserio varuint type value. 

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

1116 """ 

1117 

1118 return bitsizeof_varuint(value) 

1119 

1120 @staticmethod 

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

1122 """ 

1123 Initializes indexed offsets for Zserio varuint type. 

1124 

1125 :param bitposition: Current bit stream position. 

1126 :param value: Zserio varuint type value. 

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

1128 """ 

1129 

1130 return bitposition + VarUIntArrayTraits.bitsizeof(value) 

1131 

1132 @staticmethod 

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

1134 """ 

1135 Reads Zserio varuint type from the bit stream. 

1136 

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

1138 :returns: Read varuint value. 

1139 """ 

1140 

1141 return reader.read_varuint() 

1142 

1143 @staticmethod 

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

1145 """ 

1146 Writes Zserio varuint type to the bit stream. 

1147 

1148 :param writer: Bit stream where to write. 

1149 :param value: Zserio varuint type to write. 

1150 """ 

1151 

1152 writer.write_varuint(value) 

1153 

1154class VarSizeArrayTraits: 

1155 """ 

1156 Array traits for Zserio varsize type. 

1157 """ 

1158 

1159 HAS_BITSIZEOF_CONSTANT = False 

1160 NEEDS_BITSIZEOF_POSITION = False 

1161 NEEDS_READ_INDEX = False 

1162 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1163 

1164 @property 

1165 def packed_traits(self) -> PackedArrayTraits: 

1166 """ 

1167 Gets packed array traits. 

1168 

1169 :returns: PackedArrayTraits instance. 

1170 """ 

1171 

1172 return PackedArrayTraits(self) 

1173 

1174 @staticmethod 

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

1176 """ 

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

1178 

1179 :param value: Zserio varsize type value. 

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

1181 """ 

1182 

1183 return bitsizeof_varsize(value) 

1184 

1185 @staticmethod 

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

1187 """ 

1188 Initializes indexed offsets for Zserio varsize type. 

1189 

1190 :param bitposition: Current bit stream position. 

1191 :param value: Zserio varsize type value. 

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

1193 """ 

1194 

1195 return bitposition + VarSizeArrayTraits.bitsizeof(value) 

1196 

1197 @staticmethod 

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

1199 """ 

1200 Reads Zserio varsize type from the bit stream. 

1201 

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

1203 :returns: Read varsize value. 

1204 """ 

1205 

1206 return reader.read_varsize() 

1207 

1208 @staticmethod 

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

1210 """ 

1211 Writes Zserio varsize type to the bit stream. 

1212 

1213 :param writer: Bit stream where to write. 

1214 :param value: Zserio varsize type to write. 

1215 """ 

1216 

1217 writer.write_varsize(value) 

1218 

1219class VarInt16ArrayTraits: 

1220 """ 

1221 Array traits for Zserio varint16 type. 

1222 """ 

1223 

1224 HAS_BITSIZEOF_CONSTANT = False 

1225 NEEDS_BITSIZEOF_POSITION = False 

1226 NEEDS_READ_INDEX = False 

1227 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1228 

1229 @property 

1230 def packed_traits(self) -> PackedArrayTraits: 

1231 """ 

1232 Gets packed array traits. 

1233 

1234 :returns: PackedArrayTraits instance. 

1235 """ 

1236 

1237 return PackedArrayTraits(self) 

1238 

1239 @staticmethod 

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

1241 """ 

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

1243 

1244 :param value: Zserio varint16 type value. 

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

1246 """ 

1247 

1248 return bitsizeof_varint16(value) 

1249 

1250 @staticmethod 

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

1252 """ 

1253 Initializes indexed offsets for Zserio varint16 type. 

1254 

1255 :param bitposition: Current bit stream position. 

1256 :param value: Zserio varint16 type value. 

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

1258 """ 

1259 

1260 return bitposition + VarInt16ArrayTraits.bitsizeof(value) 

1261 

1262 @staticmethod 

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

1264 """ 

1265 Reads Zserio varint16 type from the bit stream. 

1266 

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

1268 :returns: Read varint16 value. 

1269 """ 

1270 

1271 return reader.read_varint16() 

1272 

1273 @staticmethod 

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

1275 """ 

1276 Writes Zserio varint16 type to the bit stream. 

1277 

1278 :param writer: Bit stream where to write. 

1279 :param value: Zserio varint16 type to write. 

1280 """ 

1281 

1282 writer.write_varint16(value) 

1283 

1284class VarInt32ArrayTraits: 

1285 """ 

1286 Array traits for Zserio varint32 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 varint32 type stored in the bit stream in bits. 

1308 

1309 :param value: Zserio varint32 type value. 

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

1311 """ 

1312 

1313 return bitsizeof_varint32(value) 

1314 

1315 @staticmethod 

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

1317 """ 

1318 Initializes indexed offsets for Zserio varint32 type. 

1319 

1320 :param bitposition: Current bit stream position. 

1321 :param value: Zserio varint32 type value. 

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

1323 """ 

1324 

1325 return bitposition + VarInt32ArrayTraits.bitsizeof(value) 

1326 

1327 @staticmethod 

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

1329 """ 

1330 Reads Zserio varint32 type from the bit stream. 

1331 

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

1333 :returns: Read varint32 value. 

1334 """ 

1335 

1336 return reader.read_varint32() 

1337 

1338 @staticmethod 

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

1340 """ 

1341 Writes Zserio varint32 type to the bit stream. 

1342 

1343 :param writer: Bit stream where to write. 

1344 :param value: Zserio varint32 type to write. 

1345 """ 

1346 

1347 writer.write_varint32(value) 

1348 

1349class VarInt64ArrayTraits: 

1350 """ 

1351 Array traits for Zserio varint64 type. 

1352 """ 

1353 

1354 HAS_BITSIZEOF_CONSTANT = False 

1355 NEEDS_BITSIZEOF_POSITION = False 

1356 NEEDS_READ_INDEX = False 

1357 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1358 

1359 @property 

1360 def packed_traits(self) -> PackedArrayTraits: 

1361 """ 

1362 Gets packed array traits. 

1363 

1364 :returns: PackedArrayTraits instance. 

1365 """ 

1366 

1367 return PackedArrayTraits(self) 

1368 

1369 @staticmethod 

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

1371 """ 

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

1373 

1374 :param value: Zserio varint64 type value. 

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

1376 """ 

1377 

1378 return bitsizeof_varint64(value) 

1379 

1380 @staticmethod 

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

1382 """ 

1383 Initializes indexed offsets for Zserio varint64 type. 

1384 

1385 :param bitposition: Current bit stream position. 

1386 :param value: Zserio varint64 type value. 

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

1388 """ 

1389 

1390 return bitposition + VarInt64ArrayTraits.bitsizeof(value) 

1391 

1392 @staticmethod 

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

1394 """ 

1395 Reads Zserio varint64 type from the bit stream. 

1396 

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

1398 :returns: Read varint64 value. 

1399 """ 

1400 

1401 return reader.read_varint64() 

1402 

1403 @staticmethod 

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

1405 """ 

1406 Writes Zserio varint64 type to the bit stream. 

1407 

1408 :param writer: Bit stream where to write. 

1409 :param value: Zserio varint64 type to write. 

1410 """ 

1411 

1412 writer.write_varint64(value) 

1413 

1414class VarIntArrayTraits: 

1415 """ 

1416 Array traits for Zserio varint type. 

1417 """ 

1418 

1419 HAS_BITSIZEOF_CONSTANT = False 

1420 NEEDS_BITSIZEOF_POSITION = False 

1421 NEEDS_READ_INDEX = False 

1422 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array) 

1423 

1424 @property 

1425 def packed_traits(self) -> PackedArrayTraits: 

1426 """ 

1427 Gets packed array traits. 

1428 

1429 :returns: PackedArrayTraits instance. 

1430 """ 

1431 

1432 return PackedArrayTraits(self) 

1433 

1434 @staticmethod 

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

1436 """ 

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

1438 

1439 :param value: Zserio varint type value. 

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

1441 """ 

1442 

1443 return bitsizeof_varint(value) 

1444 

1445 @staticmethod 

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

1447 """ 

1448 Initializes indexed offsets for Zserio varint type. 

1449 

1450 :param bitposition: Current bit stream position. 

1451 :param value: Zserio varint type value. 

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

1453 """ 

1454 

1455 return bitposition + VarIntArrayTraits.bitsizeof(value) 

1456 

1457 @staticmethod 

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

1459 """ 

1460 Reads Zserio varint type from the bit stream. 

1461 

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

1463 :returns: Read varint value. 

1464 """ 

1465 

1466 return reader.read_varint() 

1467 

1468 @staticmethod 

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

1470 """ 

1471 Writes Zserio varint type to the bit stream. 

1472 

1473 :param writer: Bit stream where to write. 

1474 :param value: Zserio varint type to write. 

1475 """ 

1476 

1477 writer.write_varint(value) 

1478 

1479class Float16ArrayTraits: 

1480 """ 

1481 Array traits for Zserio float16 type. 

1482 """ 

1483 

1484 HAS_BITSIZEOF_CONSTANT = True 

1485 NEEDS_BITSIZEOF_POSITION = False 

1486 NEEDS_READ_INDEX = False 

1487 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float32_array) 

1488 

1489 @property 

1490 def packed_traits(self) -> None: 

1491 """ 

1492 Returns None since float type is not packable. 

1493 

1494 :returns: None. 

1495 """ 

1496 

1497 return None 

1498 

1499 @staticmethod 

1500 def bitsizeof() -> int: 

1501 """ 

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

1503 

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

1505 """ 

1506 

1507 return 16 

1508 

1509 @staticmethod 

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

1511 """ 

1512 Initializes indexed offsets for Zserio float16 type. 

1513 

1514 :param bitposition: Current bit stream position. 

1515 :param _value: Not used. 

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

1517 """ 

1518 

1519 return bitposition + Float16ArrayTraits.bitsizeof() 

1520 

1521 @staticmethod 

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

1523 """ 

1524 Reads Zserio float16 type from the bit stream. 

1525 

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

1527 """ 

1528 

1529 return reader.read_float16() 

1530 

1531 @staticmethod 

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

1533 """ 

1534 Writes Zserio float16 type to the bit stream. 

1535 

1536 :param writer: Bit stream where to write. 

1537 :param value: Zserio float16 type to write. 

1538 :returns: Read float16 value. 

1539 """ 

1540 

1541 writer.write_float16(value) 

1542 

1543class Float32ArrayTraits: 

1544 """ 

1545 Array traits for Zserio float32 type. 

1546 """ 

1547 

1548 HAS_BITSIZEOF_CONSTANT = True 

1549 NEEDS_BITSIZEOF_POSITION = False 

1550 NEEDS_READ_INDEX = False 

1551 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float32_array) 

1552 

1553 @property 

1554 def packed_traits(self) -> None: 

1555 """ 

1556 Returns None since float type is not packable. 

1557 

1558 :returns: None. 

1559 """ 

1560 

1561 return None 

1562 

1563 @staticmethod 

1564 def bitsizeof() -> int: 

1565 """ 

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

1567 

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

1569 """ 

1570 

1571 return 32 

1572 

1573 @staticmethod 

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

1575 """ 

1576 Initializes indexed offsets for Zserio float32 type. 

1577 

1578 :param bitposition: Current bit stream position. 

1579 :param _value: Not used. 

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

1581 """ 

1582 

1583 return bitposition + Float32ArrayTraits.bitsizeof() 

1584 

1585 @staticmethod 

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

1587 """ 

1588 Reads Zserio float32 type from the bit stream. 

1589 

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

1591 :returns: Read float32 value. 

1592 """ 

1593 

1594 return reader.read_float32() 

1595 

1596 @staticmethod 

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

1598 """ 

1599 Writes Zserio float32 type to the bit stream. 

1600 

1601 :param writer: Bit stream where to write. 

1602 :param value: Zserio float32 type to write. 

1603 """ 

1604 

1605 writer.write_float32(value) 

1606 

1607class Float64ArrayTraits: 

1608 """ 

1609 Array traits for Zserio float64 type. 

1610 """ 

1611 

1612 HAS_BITSIZEOF_CONSTANT = True 

1613 NEEDS_BITSIZEOF_POSITION = False 

1614 NEEDS_READ_INDEX = False 

1615 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float64_array) 

1616 

1617 @property 

1618 def packed_traits(self) -> None: 

1619 """ 

1620 Returns None since float type is not packable. 

1621 

1622 :returns: None. 

1623 """ 

1624 

1625 return None 

1626 

1627 @staticmethod 

1628 def bitsizeof() -> int: 

1629 """ 

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

1631 

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

1633 """ 

1634 

1635 return 64 

1636 

1637 @staticmethod 

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

1639 """ 

1640 Initializes indexed offsets for Zserio float64 type. 

1641 

1642 :param bitposition: Current bit stream position. 

1643 :param _value: Not used. 

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

1645 """ 

1646 

1647 return bitposition + Float64ArrayTraits.bitsizeof() 

1648 

1649 @staticmethod 

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

1651 """ 

1652 Reads Zserio float64 type from the bit stream. 

1653 

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

1655 :returns: Read float64 value. 

1656 """ 

1657 

1658 return reader.read_float64() 

1659 

1660 @staticmethod 

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

1662 """ 

1663 Writes Zserio float64 type to the bit stream. 

1664 

1665 :param writer: Bit stream where to write. 

1666 :param value: Zserio float64 type to write. 

1667 """ 

1668 

1669 writer.write_float64(value) 

1670 

1671class BytesArrayTraits: 

1672 """ 

1673 Array traits for Zserio bytes type. 

1674 """ 

1675 

1676 HAS_BITSIZEOF_CONSTANT = False 

1677 NEEDS_BITSIZEOF_POSITION = False 

1678 NEEDS_READ_INDEX = False 

1679 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_bytes_array) 

1680 

1681 @property 

1682 def packed_traits(self) -> None: 

1683 """ 

1684 Returns None since Bytes type is not packable. 

1685 

1686 :returns: None. 

1687 """ 

1688 

1689 return None 

1690 

1691 @staticmethod 

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

1693 """ 

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

1695 

1696 :param value: Zserio bytes type value. 

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

1698 """ 

1699 

1700 return bitsizeof_bytes(value) 

1701 

1702 @staticmethod 

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

1704 """ 

1705 Initializes indexed offsets for Zserio bytes type. 

1706 

1707 :param bitposition: Current bit stream position. 

1708 :param value: Zserio bytes type value. 

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

1710 """ 

1711 

1712 return bitposition + BytesArrayTraits.bitsizeof(value) 

1713 

1714 @staticmethod 

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

1716 """ 

1717 Reads Zserio bytes type from the bit stream. 

1718 

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

1720 :returns: Read bytes value. 

1721 """ 

1722 

1723 return reader.read_bytes() 

1724 

1725 @staticmethod 

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

1727 """ 

1728 Writes Zserio bytes type to the bit stream. 

1729 

1730 :param writer: Bit stream where to write. 

1731 :param value: Zserio bytes type to write. 

1732 """ 

1733 

1734 writer.write_bytes(value) 

1735 

1736class StringArrayTraits: 

1737 """ 

1738 Array traits for Zserio string type. 

1739 """ 

1740 

1741 HAS_BITSIZEOF_CONSTANT = False 

1742 NEEDS_BITSIZEOF_POSITION = False 

1743 NEEDS_READ_INDEX = False 

1744 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_string_array) 

1745 

1746 @property 

1747 def packed_traits(self) -> None: 

1748 """ 

1749 Returns None since String type is not packable. 

1750 

1751 :returns: None. 

1752 """ 

1753 

1754 return None 

1755 

1756 @staticmethod 

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

1758 """ 

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

1760 

1761 :param value: Zserio string type value. 

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

1763 """ 

1764 

1765 return bitsizeof_string(value) 

1766 

1767 @staticmethod 

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

1769 """ 

1770 Initializes indexed offsets for Zserio string type. 

1771 

1772 :param bitposition: Current bit stream position. 

1773 :param value: Zserio string type value. 

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

1775 """ 

1776 

1777 return bitposition + StringArrayTraits.bitsizeof(value) 

1778 

1779 @staticmethod 

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

1781 """ 

1782 Reads Zserio string type from the bit stream. 

1783 

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

1785 :returns: Read string value. 

1786 """ 

1787 

1788 return reader.read_string() 

1789 

1790 @staticmethod 

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

1792 """ 

1793 Writes Zserio string type to the bit stream. 

1794 

1795 :param writer: Bit stream where to write. 

1796 :param value: Zserio string type to write. 

1797 """ 

1798 

1799 writer.write_string(value) 

1800 

1801class BoolArrayTraits: 

1802 """ 

1803 Array traits for Zserio bool type. 

1804 """ 

1805 

1806 HAS_BITSIZEOF_CONSTANT = True 

1807 NEEDS_BITSIZEOF_POSITION = False 

1808 NEEDS_READ_INDEX = False 

1809 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_bool_array) 

1810 

1811 @property 

1812 def packed_traits(self) -> None: 

1813 """ 

1814 Returns None since Bool type is not packable. 

1815 

1816 :returns: None. 

1817 """ 

1818 

1819 return None 

1820 

1821 @staticmethod 

1822 def bitsizeof() -> int: 

1823 """ 

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

1825 

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

1827 """ 

1828 

1829 return 1 

1830 

1831 @staticmethod 

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

1833 """ 

1834 Initializes indexed offsets for Zserio bool type. 

1835 

1836 :param bitposition: Current bit stream position. 

1837 :param _value: Not used. 

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

1839 """ 

1840 

1841 return bitposition + BoolArrayTraits.bitsizeof() 

1842 

1843 @staticmethod 

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

1845 """ 

1846 Reads Zserio bool type from the bit stream. 

1847 

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

1849 :returns: Read bool value. 

1850 """ 

1851 

1852 return reader.read_bool() 

1853 

1854 @staticmethod 

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

1856 """ 

1857 Writes Zserio bool type to the bit stream. 

1858 

1859 :param writer: Bit stream where to write. 

1860 :param value: Zserio bool type to write. 

1861 """ 

1862 

1863 writer.write_bool(value) 

1864 

1865class BitBufferArrayTraits: 

1866 """ 

1867 Array traits for Zserio extern bit buffer type. 

1868 """ 

1869 

1870 HAS_BITSIZEOF_CONSTANT = False 

1871 NEEDS_BITSIZEOF_POSITION = False 

1872 NEEDS_READ_INDEX = False 

1873 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_object_array) 

1874 

1875 @property 

1876 def packed_traits(self) -> None: 

1877 """ 

1878 Returns None since BitBuffer type is not packable. 

1879 

1880 :returns: None. 

1881 """ 

1882 

1883 return None 

1884 

1885 @staticmethod 

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

1887 """ 

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

1889 

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

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

1892 """ 

1893 

1894 return bitsizeof_bitbuffer(value) 

1895 

1896 @staticmethod 

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

1898 """ 

1899 Initializes indexed offsets for Zserio extern bit buffer type. 

1900 

1901 :param bitposition: Current bit stream position. 

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

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

1904 """ 

1905 

1906 return bitposition + BitBufferArrayTraits.bitsizeof(value) 

1907 

1908 @staticmethod 

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

1910 """ 

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

1912 

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

1914 :returns: Read bit buffer value. 

1915 """ 

1916 

1917 return reader.read_bitbuffer() 

1918 

1919 @staticmethod 

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

1921 """ 

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

1923 

1924 :param writer: Bit stream where to write. 

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

1926 """ 

1927 

1928 writer.write_bitbuffer(value) 

1929 

1930class ObjectArrayTraits: 

1931 """ 

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

1933 """ 

1934 

1935 HAS_BITSIZEOF_CONSTANT = False 

1936 NEEDS_BITSIZEOF_POSITION = True 

1937 NEEDS_READ_INDEX = True 

1938 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_object_array) 

1939 

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

1941 """ 

1942 Constructor. 

1943 

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

1945 """ 

1946 

1947 self._element_factory = element_factory 

1948 self._packed_traits = (ObjectPackedArrayTraits(element_factory) 

1949 if element_factory.IS_OBJECT_PACKABLE else None) 

1950 

1951 @property 

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

1953 """ 

1954 Gets packed array traits. 

1955 

1956 :returns: ObjectPackedArrayTraits instance. 

1957 """ 

1958 

1959 return self._packed_traits 

1960 

1961 @staticmethod 

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

1963 """ 

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

1965 

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

1967 :param value: Zserio object type value. 

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

1969 """ 

1970 

1971 return value.bitsizeof(bitposition) 

1972 

1973 @staticmethod 

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

1975 """ 

1976 Initializes indexed offsets for the Zserio object type. 

1977 

1978 :param bitposition: Current bit stream position. 

1979 :param value: Zserio object type value. 

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

1981 """ 

1982 

1983 return value.initialize_offsets(bitposition) 

1984 

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

1986 """ 

1987 Reads Zserio object type from the bit stream. 

1988 

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

1990 :param index: Element index in the array. 

1991 :returns: Read object. 

1992 """ 

1993 

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

1995 

1996 @staticmethod 

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

1998 """ 

1999 Writes Zserio object type to the bit stream. 

2000 

2001 :param writer: Bit stream where to write. 

2002 :param value: Zserio object type to write. 

2003 """ 

2004 

2005 value.write(writer)