Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/src/zserio/array.py: 100%
709 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-12-05 10:43 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-12-05 10:43 +0000
1"""
2The module implements abstraction for arrays used by Zserio python extension.
3"""
5import typing
7from zserio.bitposition import alignto
8from zserio.bitsizeof import (
9 bitsizeof_varuint16,
10 bitsizeof_varuint32,
11 bitsizeof_varuint64,
12 bitsizeof_varuint,
13 bitsizeof_varint16,
14 bitsizeof_varint32,
15 bitsizeof_varint64,
16 bitsizeof_varint,
17 bitsizeof_varsize,
18 bitsizeof_bytes,
19 bitsizeof_string,
20 bitsizeof_bitbuffer,
21)
22from zserio.bitreader import BitStreamReader
23from zserio.bitwriter import BitStreamWriter
24from zserio.bitbuffer import BitBuffer
25from zserio.hashcode import (
26 HASH_SEED,
27 calc_hashcode_bool_array,
28 calc_hashcode_int_array,
29 calc_hashcode_float32_array,
30 calc_hashcode_float64_array,
31 calc_hashcode_bytes_array,
32 calc_hashcode_string_array,
33 calc_hashcode_object_array,
34)
35from zserio.exception import PythonRuntimeException
38class Array:
39 """
40 Abstraction for arrays to which Zserio arrays are mapped in python.
41 """
43 def __init__(
44 self,
45 array_traits: typing.Any,
46 raw_array: typing.Optional[typing.List] = None,
47 *,
48 is_auto: bool = False,
49 is_implicit: bool = False,
50 set_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None,
51 check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None
52 ) -> None:
53 """
54 Constructor.
56 :param array_traits: Array traits which specify the array type.
57 :param raw_array: Native python list which will be hold by this abstraction.
58 :param is_auto: True if mapped Zserio array is auto array.
59 :param is_implicit: True if mapped Zserio array is implicit array.
60 :param set_offset_method: Set offset method if mapped Zserio array is indexed offset array.
61 :param check_offset_method: Check offset method if mapped Zserio array is indexed offset array.
62 """
64 self._raw_array: typing.List = [] if raw_array is None else raw_array
65 self._array_traits: typing.Any = array_traits
66 self._packed_array_traits: typing.Any = array_traits.packed_traits
67 self._is_auto: bool = is_auto
68 self._is_implicit: bool = is_implicit
69 self._set_offset_method: typing.Optional[typing.Callable[[int, int], None]] = set_offset_method
70 self._check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = check_offset_method
72 @classmethod
73 def from_reader(
74 cls: typing.Type["Array"],
75 array_traits: typing.Any,
76 reader: BitStreamReader,
77 size: int = 0,
78 *,
79 is_auto: bool = False,
80 is_implicit: bool = False,
81 set_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None,
82 check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None
83 ) -> "Array":
84 """
85 Constructs array and reads elements from the given bit stream reader.
87 :param array_traits: Array traits which specify the array type.
88 :param reader: Bit stream from which to read.
89 :param size: Number of elements to read or None in case of implicit or auto arrays.
90 :param raw_array: Native python list which will be hold by this abstraction.
91 :param is_auto: True if mapped Zserio array is auto array.
92 :param is_implicit: True if mapped Zserio array is implicit array.
93 :param set_offset_method: Set offset method if mapped Zserio array is indexed offset array.
94 :param check_offset_method: Check offset method if mapped Zserio array is indexed offset array.
95 :returns: Array instance filled using given bit stream reader.
96 """
98 instance = cls(
99 array_traits,
100 is_auto=is_auto,
101 is_implicit=is_implicit,
102 set_offset_method=set_offset_method,
103 check_offset_method=check_offset_method,
104 )
105 instance.read(reader, size)
107 return instance
109 @classmethod
110 def from_reader_packed(
111 cls: typing.Type["Array"],
112 array_traits: typing.Any,
113 reader: BitStreamReader,
114 size: int = 0,
115 *,
116 is_auto: bool = False,
117 set_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None,
118 check_offset_method: typing.Optional[typing.Callable[[int, int], None]] = None
119 ) -> "Array":
120 """
121 Constructs packed array and reads elements from the given bit stream reader.
123 :param array_traits: Array traits which specify the array type.
124 :param reader: Bit stream from which to read.
125 :param size: Number of elements to read or None in case of implicit or auto arrays.
126 :param raw_array: Native python list which will be hold by this abstraction.
127 :param is_auto: True if mapped Zserio array is auto array.
128 :param set_offset_method: Set offset method if mapped Zserio array is indexed offset array.
129 :param check_offset_method: Check offset method if mapped Zserio array is indexed offset array.
130 :returns: Array instance filled using given bit stream reader.
131 """
133 instance = cls(
134 array_traits,
135 is_auto=is_auto,
136 is_implicit=False,
137 set_offset_method=set_offset_method,
138 check_offset_method=check_offset_method,
139 )
140 instance.read_packed(reader, size)
142 return instance
144 def __eq__(self, other: object) -> bool:
145 # it's enough to check only raw_array because compound types which call this are always the same type
146 if isinstance(other, Array):
147 return self._raw_array == other._raw_array
149 return False
151 def __hash__(self) -> int:
152 return self._array_traits.CALC_HASHCODE_FUNC(HASH_SEED, self._raw_array)
154 def __len__(self) -> int:
155 return len(self._raw_array)
157 def __getitem__(self, key: int) -> typing.Any:
158 return self._raw_array[key]
160 def __setitem__(self, key: int, value: typing.Any) -> None:
161 self._raw_array[key] = value
163 @property
164 def raw_array(self) -> typing.List:
165 """
166 Gets raw array.
168 :returns: Native python list which is hold by the array.
169 """
171 return self._raw_array
173 def bitsizeof(self, bitposition: int) -> int:
174 """
175 Returns length of array stored in the bit stream in bits.
177 :param bitposition: Current bit stream position.
178 :returns: Length of the array stored in the bit stream in bits.
179 """
181 end_bitposition = bitposition
182 size = len(self._raw_array)
183 if self._is_auto:
184 end_bitposition += bitsizeof_varsize(size)
186 if self._array_traits.HAS_BITSIZEOF_CONSTANT and size > 0:
187 element_size = self._array_traits.bitsizeof()
188 if self._set_offset_method is None:
189 end_bitposition += size * element_size
190 else:
191 end_bitposition = alignto(8, end_bitposition)
192 end_bitposition += element_size + (size - 1) * alignto(8, element_size)
193 else:
194 array_traits_bitsizeof = self._array_traits.bitsizeof
195 if self._set_offset_method is not None:
196 if self._array_traits.NEEDS_BITSIZEOF_POSITION:
197 for element in self._raw_array:
198 end_bitposition = alignto(8, end_bitposition)
199 end_bitposition += array_traits_bitsizeof(end_bitposition, element)
200 else:
201 for element in self._raw_array:
202 end_bitposition = alignto(8, end_bitposition)
203 end_bitposition += array_traits_bitsizeof(element)
204 else:
205 if self._array_traits.NEEDS_BITSIZEOF_POSITION:
206 for element in self._raw_array:
207 end_bitposition += array_traits_bitsizeof(end_bitposition, element)
208 else:
209 for element in self._raw_array:
210 end_bitposition += array_traits_bitsizeof(element)
212 return end_bitposition - bitposition
214 def bitsizeof_packed(self, bitposition: int) -> int:
215 """
216 Returns length of the packed array stored in the bit stream in bits.
218 :param bitposition: Current bit stream position.
219 :returns: Length of the array stored in the bit stream in bits.
220 """
222 end_bitposition = bitposition
223 size = len(self._raw_array)
224 if self._is_auto:
225 end_bitposition += bitsizeof_varsize(size)
227 if size > 0:
228 context = self._packed_array_traits.create_context()
229 packed_array_traits_init_context = self._packed_array_traits.init_context
230 packed_array_traits_bitsizeof = self._packed_array_traits.bitsizeof
232 for element in self._raw_array:
233 packed_array_traits_init_context(context, element)
235 if self._set_offset_method is not None:
236 for element in self._raw_array:
237 end_bitposition = alignto(8, end_bitposition)
238 end_bitposition += packed_array_traits_bitsizeof(context, end_bitposition, element)
239 else:
240 for element in self._raw_array:
241 end_bitposition += packed_array_traits_bitsizeof(context, end_bitposition, element)
243 return end_bitposition - bitposition
245 def initialize_offsets(self, bitposition: int) -> int:
246 """
247 Initializes indexed offsets for the array.
249 :param bitposition: Current bit stream position.
250 :returns: Updated bit stream position which points to the first bit after the array.
251 """
253 end_bitposition = bitposition
254 size = len(self._raw_array)
255 if self._is_auto:
256 end_bitposition += bitsizeof_varsize(size)
258 array_traits_initialize_offsets = self._array_traits.initialize_offsets
259 if self._set_offset_method is not None:
260 set_offset = self._set_offset_method
261 for index in range(size):
262 end_bitposition = alignto(8, end_bitposition)
263 set_offset(index, end_bitposition)
264 end_bitposition = array_traits_initialize_offsets(end_bitposition, self._raw_array[index])
265 else:
266 for element in self._raw_array:
267 end_bitposition = array_traits_initialize_offsets(end_bitposition, element)
269 return end_bitposition
271 def initialize_offsets_packed(self, bitposition: int) -> int:
272 """
273 Initializes indexed offsets for the packed array.
275 :param bitposition: Current bit stream position.
276 :returns: Updated bit stream position which points to the first bit after the array.
277 """
279 end_bitposition = bitposition
280 size = len(self._raw_array)
281 if self._is_auto:
282 end_bitposition += bitsizeof_varsize(size)
284 if size > 0:
285 context = self._packed_array_traits.create_context()
286 packed_array_traits_init_context = self._packed_array_traits.init_context
287 packed_array_traits_initialize_offsets = self._packed_array_traits.initialize_offsets
289 for element in self._raw_array:
290 packed_array_traits_init_context(context, element)
292 if self._set_offset_method is not None:
293 set_offset = self._set_offset_method
294 for index in range(size):
295 end_bitposition = alignto(8, end_bitposition)
296 set_offset(index, end_bitposition)
297 end_bitposition = packed_array_traits_initialize_offsets(
298 context, end_bitposition, self._raw_array[index]
299 )
300 else:
301 for element in self._raw_array:
302 end_bitposition = packed_array_traits_initialize_offsets(context, end_bitposition, element)
304 return end_bitposition
306 def read(self, reader: BitStreamReader, size: int = 0) -> None:
307 """
308 Reads array from the bit stream.
310 :param reader: Bit stream from which to read.
311 :param size: Number of elements to read or None in case of implicit or auto arrays.
313 :raises PythonRuntimeException: If the implicit array does not have elements with constant bit size.
314 """
316 self._raw_array.clear()
318 append = self._raw_array.append
319 array_traits_read = self._array_traits.read
320 if self._is_implicit:
321 if not self._array_traits.HAS_BITSIZEOF_CONSTANT:
322 raise PythonRuntimeException("Array: Implicit array elements must have constant bit size!")
324 element_size = self._array_traits.bitsizeof()
325 remaining_bits = reader.buffer_bitsize - reader.bitposition
326 read_size = remaining_bits // element_size
327 for _ in range(read_size):
328 # we know that no traits NEEDS_READ_INDEX here
329 append(array_traits_read(reader))
330 else:
331 if self._is_auto:
332 read_size = reader.read_varsize()
333 else:
334 read_size = size
336 if self._check_offset_method is not None:
337 check_offset = self._check_offset_method
338 reader_alignto = reader.alignto
339 if self._array_traits.NEEDS_READ_INDEX:
340 for index in range(read_size):
341 reader_alignto(8)
342 check_offset(index, reader.bitposition)
343 append(array_traits_read(reader, index))
344 else:
345 for index in range(read_size):
346 reader_alignto(8)
347 check_offset(index, reader.bitposition)
348 append(array_traits_read(reader))
349 else:
350 if self._array_traits.NEEDS_READ_INDEX:
351 for index in range(read_size):
352 append(array_traits_read(reader, index))
353 else:
354 for _ in range(read_size):
355 append(array_traits_read(reader))
357 def read_packed(self, reader: BitStreamReader, size: int = 0) -> None:
358 """
359 Reads packed array from the bit stream.
361 :param reader: Bit stream from which to read.
362 :param size: Number of elements to read or 0 in case of auto arrays.
363 """
365 self._raw_array.clear()
367 if self._is_implicit:
368 raise PythonRuntimeException("Array: Implicit array cannot be packed!")
370 if self._is_auto:
371 read_size = reader.read_varsize()
372 else:
373 read_size = size
375 if read_size > 0:
376 context = self._packed_array_traits.create_context()
378 append = self._raw_array.append
379 packed_array_traits_read = self._packed_array_traits.read
381 if self._check_offset_method is not None:
382 check_offset = self._check_offset_method
383 reader_alignto = reader.alignto
384 for index in range(read_size):
385 reader_alignto(8)
386 check_offset(index, reader.bitposition)
387 append(packed_array_traits_read(context, reader, index))
388 else:
389 for index in range(read_size):
390 append(packed_array_traits_read(context, reader, index))
392 def write(self, writer: BitStreamWriter) -> None:
393 """
394 Writes array to the bit stream.
396 :param writer: Bit stream where to write.
397 """
399 size = len(self._raw_array)
400 if self._is_auto:
401 writer.write_varsize(size)
403 array_traits_write = self._array_traits.write
404 if self._check_offset_method is not None:
405 check_offset = self._check_offset_method
406 writer_alignto = writer.alignto
407 for index in range(size):
408 writer_alignto(8)
409 check_offset(index, writer.bitposition)
410 array_traits_write(writer, self._raw_array[index])
411 else:
412 for element in self._raw_array:
413 array_traits_write(writer, element)
415 def write_packed(self, writer: BitStreamWriter) -> None:
416 """
417 Writes packed array to the bit stream.
419 :param writer: Bit stream where to write.
420 """
422 size = len(self._raw_array)
424 if self._is_auto:
425 writer.write_varsize(size)
427 if size > 0:
428 context = self._packed_array_traits.create_context()
429 packed_array_traits_init_context = self._packed_array_traits.init_context
430 packed_array_traits_write = self._packed_array_traits.write
432 for element in self._raw_array:
433 packed_array_traits_init_context(context, element)
435 if self._check_offset_method is not None:
436 check_offset = self._check_offset_method
437 writer_alignto = writer.alignto
438 for index in range(size):
439 writer_alignto(8)
440 check_offset(index, writer.bitposition)
441 packed_array_traits_write(context, writer, self._raw_array[index])
442 else:
443 for element in self._raw_array:
444 packed_array_traits_write(context, writer, element)
447class DeltaContext:
448 """
449 Context for delta packing created for each packable field.
451 Contexts are always newly created for each array operation (bitsizeof, initialize_offsets, read, write).
452 They must be initialized at first via calling the init method for each packable element present in the
453 array. After the full initialization, only a single method (bitsizeof, read, write) can be repeatedly
454 called for exactly the same sequence of packable elements.
456 Example::
458 context = DeltaContext(array_traits)
459 for element in array:
460 context.init(element) # initialization step, needed for max_bit_number calculation
461 context.write_descriptor(writer) # finishes the initialization
462 for element in array:
463 context.write(writer, element)
464 """
466 def __init__(self) -> None:
467 """Constructor."""
469 self._is_packed = False
470 self._max_bit_number = 0
471 self._previous_element: typing.Optional[int] = None
472 self._processing_started = False
473 self._unpacked_bitsize = 0
474 self._first_element_bitsize = 0
475 self._num_elements = 0
477 def init(self, array_traits: typing.Any, element: int) -> None:
478 """
479 Makes initialization step for the provided array element.
481 :param array_traits: Standard array traits.
482 :param element: Current element of the array.
483 """
485 self._num_elements += 1
486 self._unpacked_bitsize += DeltaContext._bitsizeof_unpacked(array_traits, element)
488 if self._previous_element is None:
489 self._previous_element = element
490 self._first_element_bitsize = self._unpacked_bitsize
491 else:
492 if self._max_bit_number <= self._MAX_BIT_NUMBER_LIMIT:
493 self._is_packed = True
495 delta = element - self._previous_element
496 max_bit_number = delta.bit_length()
497 # if delta is negative, we need one bit more because of sign
498 # if delta is positive, we need one bit more because delta are treated as signed number
499 # if delta is zero, we need one bit more because bit_length() returned zero
500 if max_bit_number > self._max_bit_number:
501 self._max_bit_number = max_bit_number
502 if max_bit_number > self._MAX_BIT_NUMBER_LIMIT:
503 self._is_packed = False
505 self._previous_element = element
507 def bitsizeof(self, array_traits: typing.Any, element: int) -> int:
508 """
509 Returns length of the element representation stored in the bit stream in bits.
511 :param array_traits: Standard integral array traits.
512 :param element: Current element.
514 :returns: Length of the element representation stored in the bit stream in bits.
515 """
517 if not self._processing_started:
518 self._processing_started = True
519 self._finish_init()
521 return self._bitsizeof_descriptor() + DeltaContext._bitsizeof_unpacked(array_traits, element)
522 elif not self._is_packed:
523 return DeltaContext._bitsizeof_unpacked(array_traits, element)
524 else:
525 return self._max_bit_number + 1 if self._max_bit_number > 0 else 0
527 def read(self, array_traits: typing.Any, reader: BitStreamReader) -> int:
528 """
529 Reads the packed element from the bit stream.
531 :param array_traits: Standard array traits.
532 :param reader: Bit stream reader.
533 """
535 if not self._processing_started:
536 self._processing_started = True
537 self._read_descriptor(reader)
539 return self._read_unpacked(array_traits, reader)
540 elif not self._is_packed:
541 return self._read_unpacked(array_traits, reader)
542 else:
543 assert self._previous_element is not None
544 if self._max_bit_number > 0:
545 delta = reader.read_signed_bits_unchecked(self._max_bit_number + 1)
546 self._previous_element += delta
547 return self._previous_element
549 def write(self, array_traits: typing.Any, writer: BitStreamWriter, element: int) -> None:
550 """
551 Writes the packed element representation to the bit stream.
553 :param array_traits: Standard array traits.
554 :param writer: Bit stream writer.
555 :param element: Element to write.
556 """
558 if not self._processing_started:
559 self._processing_started = True
560 self._finish_init()
561 self._write_descriptor(writer)
563 self._write_unpacked(array_traits, writer, element)
564 elif not self._is_packed:
565 self._write_unpacked(array_traits, writer, element)
566 else: # packed and not first
567 assert self._previous_element is not None
568 if self._max_bit_number > 0:
569 delta = element - self._previous_element
570 writer.write_signed_bits_unchecked(delta, self._max_bit_number + 1)
571 self._previous_element = element
573 def _finish_init(self) -> None:
574 if self._is_packed:
575 delta_bitsize = self._max_bit_number + 1 if self._max_bit_number > 0 else 0
576 packed_bitsize_with_descriptor = (
577 1
578 + self._MAX_BIT_NUMBER_BITS # descriptor
579 + self._first_element_bitsize
580 + (self._num_elements - 1) * delta_bitsize
581 )
582 unpacked_bitsize_with_descriptor = 1 + self._unpacked_bitsize
583 if packed_bitsize_with_descriptor >= unpacked_bitsize_with_descriptor:
584 self._is_packed = False
586 def _bitsizeof_descriptor(self) -> int:
587 if self._is_packed:
588 return 1 + self._MAX_BIT_NUMBER_BITS
589 else:
590 return 1
592 @staticmethod
593 def _bitsizeof_unpacked(array_traits: typing.Any, element: int) -> int:
594 if array_traits.HAS_BITSIZEOF_CONSTANT:
595 return array_traits.bitsizeof()
596 else: # we know that NEEDS_BITSIZEOF_POSITION is False here
597 return array_traits.bitsizeof(element)
599 def _read_descriptor(self, reader: BitStreamReader) -> None:
600 self._is_packed = reader.read_bool()
601 if self._is_packed:
602 self._max_bit_number = reader.read_bits_unchecked(self._MAX_BIT_NUMBER_BITS)
604 def _read_unpacked(self, array_traits: typing.Any, reader: BitStreamReader) -> int:
605 element = array_traits.read(reader)
606 self._previous_element = element
607 return element
609 def _write_descriptor(self, writer: BitStreamWriter) -> None:
610 writer.write_bool(self._is_packed)
611 if self._is_packed:
612 writer.write_bits_unchecked(self._max_bit_number, self._MAX_BIT_NUMBER_BITS)
614 def _write_unpacked(self, array_traits: typing.Any, writer: BitStreamWriter, element: int) -> None:
615 self._previous_element = element
616 array_traits.write(writer, element)
618 _MAX_BIT_NUMBER_BITS = 6
619 _MAX_BIT_NUMBER_LIMIT = 62
622class PackedArrayTraits:
623 """
624 Packed array traits.
626 Packed array traits are used for all built-in types.
627 """
629 def __init__(self, array_traits: typing.Any) -> None:
630 """
631 Constructor.
633 :param array_traits: Standard array traits.
634 """
636 self._array_traits = array_traits
638 @staticmethod
639 def create_context() -> DeltaContext:
640 """
641 Creates new packing context - DeltaContext is used for non-object arrays.
643 :returns: New packing context.
644 """
646 return DeltaContext()
648 def init_context(self, delta_context: DeltaContext, element: int) -> None:
649 """
650 Calls context initialization step for the current element.
652 :param delta_context: Delta context.
653 :param element: Current element.
654 """
656 delta_context.init(self._array_traits, element)
658 def bitsizeof(self, delta_context: DeltaContext, _bitposition: int, element: int) -> int:
659 """
660 Returns length of the array element stored in the bit stream in bits.
662 :param delta_context: Delta context.
663 :param _bitposition: Current bit stream position (not used).
664 :param elemnet: Current element.
665 :returns: Length of the array element stored in the bit stream in bits.
666 """
668 return delta_context.bitsizeof(self._array_traits, element)
670 def initialize_offsets(self, delta_context: DeltaContext, bitposition: int, element: int) -> int:
671 """
672 Calls indexed offsets initialization for the current element.
674 :param delta_context: Delta context.
675 :param _bitposition: Current bit stream position.
676 :param element: Current element.
677 :returns: Updated bit stream position which points to the first bit after this element.
678 """
680 return bitposition + delta_context.bitsizeof(self._array_traits, element)
682 def write(self, delta_context: DeltaContext, writer: BitStreamWriter, element: int) -> None:
683 """
684 Writes the element to the bit stream.
686 :param delta_context: Delta context.
687 :param writer: Bit stream writer.
688 :param element: Element to write.
689 """
691 delta_context.write(self._array_traits, writer, element)
693 def read(self, delta_context: DeltaContext, reader: BitStreamReader, _index: int) -> int:
694 """
695 Read an element from the bit stream.
697 :param delta_context: Delta context.
698 :param reader: Bit stream reader.
699 :param _index: Not used.
700 :returns: Read element value.
701 """
703 return delta_context.read(self._array_traits, reader)
706class ObjectPackedArrayTraits:
707 """
708 Packed array traits for Zserio objects.
710 This traits are used for Zserio objects which must implement special *_packed* methods to allow itself
711 to be used in a packed array.
712 """
714 def __init__(self, element_factory: typing.Any):
715 """
716 Constructor.
718 :param element_factory: Element factory which creates packed object from the element index.
719 """
721 self._element_factory = element_factory
723 def create_context(self) -> typing.Any:
724 """
725 Creates new packing context - generated ZserioPackingContext is used for object arrays.
727 :returns: New packing context.
728 """
730 return self._element_factory.create_packing_context()
732 @staticmethod
733 def init_context(context: typing.Any, element: typing.Any) -> None:
734 """
735 Calls context initialization step for the current element.
737 :param context: Packing context.
738 :param element: Current element.
739 """
741 element.init_packing_context(context)
743 @staticmethod
744 def bitsizeof(context: typing.Any, bitposition: int, element: typing.Any) -> int:
745 """
746 Returns length of the array element stored in the bit stream in bits.
748 :param context: Packing context.
749 :param bitposition: Current bit stream position.
750 :param elemnet: Current element.
751 :returns: Length of the array element stored in the bit stream in bits.
752 """
754 return element.bitsizeof_packed(context, bitposition)
756 @staticmethod
757 def initialize_offsets(context: typing.Any, bitposition: int, element: typing.Any) -> int:
758 """
759 Calls indexed offsets initialization for the current element.
761 :param context: Packing context.
762 :param bitposition: Current bit stream position.
763 :param element: Current element.
764 :returns: Updated bit stream position which points to the first bit after this element.
765 """
767 return element.initialize_offsets_packed(context, bitposition)
769 @staticmethod
770 def write(context: typing.Any, writer: BitStreamWriter, element: typing.Any) -> None:
771 """
772 Writes the element to the bit stream.
774 :param context: Packing context.
775 :param writer: Bit stream writer.
776 :param element: Element to write.
777 """
779 element.write_packed(context, writer)
781 def read(self, context: typing.Any, reader: BitStreamReader, index: int) -> typing.Any:
782 """
783 Read an element from the bit stream.
785 :param context: Packing context.
786 :param reader: Bit stream reader.
787 :param index: Index of the current element.
788 :returns: Read element value.
789 """
791 return self._element_factory.create_packed(context, reader, index)
794class BitFieldArrayTraits:
795 """
796 Array traits for unsigned fixed integer Zserio types (uint16, uint32, uint64, bit:5, etc...).
797 """
799 HAS_BITSIZEOF_CONSTANT = True
800 NEEDS_BITSIZEOF_POSITION = False
801 NEEDS_READ_INDEX = False
802 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
804 def __init__(self, numbits: int) -> None:
805 """
806 Constructor.
808 :param numbits: Number of bits for unsigned fixed integer Zserio type.
809 """
811 self._numbits = numbits
813 @property
814 def packed_traits(self) -> PackedArrayTraits:
815 """
816 Gets packed array traits.
818 :returns: PackedArrayTraits instance.
819 """
821 return PackedArrayTraits(self)
823 def bitsizeof(self) -> int:
824 """
825 Returns length of unsigned fixed integer Zserio type stored in the bit stream in bits.
827 :returns: Length of unsigned fixed integer Zserio type in bits.
828 """
830 return self._numbits
832 def initialize_offsets(self, bitposition: int, _value: int) -> int:
833 """
834 Initializes indexed offsets for unsigned fixed integer Zserio type.
836 :param bitposition: Current bit stream position.
837 :param _value: Not used.
838 :returns: Updated bit stream position which points to the first bit after unsigned fixed integer type.
839 """
841 return bitposition + self.bitsizeof()
843 def read(self, reader: BitStreamReader) -> int:
844 """
845 Reads unsigned fixed integer Zserio type from the bit stream.
847 :param reader: Bit stream from which to read.
848 :returns: Read unsigned int value.
849 """
851 return reader.read_bits(self._numbits)
853 def write(self, writer: BitStreamWriter, value: int) -> None:
854 """
855 Writes unsigned fixed integer Zserio type to the bit stream.
857 :param writer: Bit stream where to write.
858 :param value: Unsigned fixed integer Zserio type to write.
859 """
861 writer.write_bits(value, self._numbits)
864class SignedBitFieldArrayTraits:
865 """
866 Array traits for signed fixed integer Zserio types (int16, int32, int64, int:5, etc...).
867 """
869 HAS_BITSIZEOF_CONSTANT = True
870 NEEDS_BITSIZEOF_POSITION = False
871 NEEDS_READ_INDEX = False
872 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
874 def __init__(self, numbits: int) -> None:
875 """
876 Constructor.
878 :param numbits: Number of bits for signed fixed integer Zserio type.
879 """
881 self._numbits = numbits
883 @property
884 def packed_traits(self) -> PackedArrayTraits:
885 """
886 Gets packed array traits.
888 :returns: PackedArrayTraits instance.
889 """
891 return PackedArrayTraits(self)
893 def bitsizeof(self) -> int:
894 """
895 Returns length of signed fixed integer Zserio type stored in the bit stream in bits.
897 :returns: Length of signed fixed integer Zserio type in bits.
898 """
900 return self._numbits
902 def initialize_offsets(self, bitposition: int, _value: int) -> int:
903 """
904 Initializes indexed offsets for signed fixed integer Zserio type.
906 :param bitposition: Current bit stream position.
907 :param _value: Not used.
908 :returns: Updated bit stream position which points to the first bit after signed fixed integer type.
909 """
911 return bitposition + self.bitsizeof()
913 def read(self, reader: BitStreamReader) -> int:
914 """
915 Reads signed fixed integer Zserio type from the bit stream.
917 :param reader: Bit stream from which to read.
918 :returns: Read signed int value.
919 """
921 return reader.read_signed_bits(self._numbits)
923 def write(self, writer: BitStreamWriter, value: int) -> None:
924 """
925 Writes signed fixed integer Zserio type to the bit stream.
927 :param writer: Bit stream where to write.
928 :param value: Signed fixed integer Zserio type to write.
929 """
931 writer.write_signed_bits(value, self._numbits)
934class VarUInt16ArrayTraits:
935 """
936 Array traits for Zserio varuint16 type.
937 """
939 HAS_BITSIZEOF_CONSTANT = False
940 NEEDS_BITSIZEOF_POSITION = False
941 NEEDS_READ_INDEX = False
942 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
944 @property
945 def packed_traits(self) -> PackedArrayTraits:
946 """
947 Gets packed array traits.
949 :returns: PackedArrayTraits instance.
950 """
952 return PackedArrayTraits(self)
954 @staticmethod
955 def bitsizeof(value: int) -> int:
956 """
957 Returns length of Zserio varuint16 type stored in the bit stream in bits.
959 :param value: Zserio varuint16 type value.
960 :returns: Length of given Zserio varuint16 type in bits.
961 """
963 return bitsizeof_varuint16(value)
965 @staticmethod
966 def initialize_offsets(bitposition: int, value: int) -> int:
967 """
968 Initializes indexed offsets for Zserio varuint16 type.
970 :param bitposition: Current bit stream position.
971 :param value: Zserio varuint16 type value.
972 :returns: Updated bit stream position which points to the first bit after Zserio varuint16 type.
973 """
975 return bitposition + VarUInt16ArrayTraits.bitsizeof(value)
977 @staticmethod
978 def read(reader: BitStreamReader) -> int:
979 """
980 Reads Zserio varuint16 type from the bit stream.
982 :param reader: Bit stream from which to read.
983 """
985 return reader.read_varuint16()
987 @staticmethod
988 def write(writer: BitStreamWriter, value: int) -> None:
989 """
990 Writes Zserio varuint16 type to the bit stream.
992 :param writer: Bit stream where to write.
993 :param value: Zserio varuint16 type to write.
994 :returns: Read varuint16 value.
995 """
997 writer.write_varuint16(value)
1000class VarUInt32ArrayTraits:
1001 """
1002 Array traits for Zserio varuint32 type.
1003 """
1005 HAS_BITSIZEOF_CONSTANT = False
1006 NEEDS_BITSIZEOF_POSITION = False
1007 NEEDS_READ_INDEX = False
1008 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
1010 @property
1011 def packed_traits(self) -> PackedArrayTraits:
1012 """
1013 Gets packed array traits.
1015 :returns: PackedArrayTraits instance.
1016 """
1018 return PackedArrayTraits(self)
1020 @staticmethod
1021 def bitsizeof(value: int) -> int:
1022 """
1023 Returns length of Zserio varuint32 type stored in the bit stream in bits.
1025 :param value: Zserio varuint32 type value.
1026 :returns: Length of given Zserio varuint32 type in bits.
1027 """
1029 return bitsizeof_varuint32(value)
1031 @staticmethod
1032 def initialize_offsets(bitposition: int, value: int) -> int:
1033 """
1034 Initializes indexed offsets for Zserio varuint32 type.
1036 :param bitposition: Current bit stream position.
1037 :param value: Zserio varuint32 type value.
1038 :returns: Updated bit stream position which points to the first bit after Zserio varuint32 type.
1039 """
1041 return bitposition + VarUInt32ArrayTraits.bitsizeof(value)
1043 @staticmethod
1044 def read(reader: BitStreamReader) -> int:
1045 """
1046 Reads Zserio varuint32 type from the bit stream.
1048 :param reader: Bit stream from which to read.
1049 :returns: Read varuint32 value.
1050 """
1052 return reader.read_varuint32()
1054 @staticmethod
1055 def write(writer: BitStreamWriter, value: int) -> None:
1056 """
1057 Writes Zserio varuint32 type to the bit stream.
1059 :param writer: Bit stream where to write.
1060 :param value: Zserio varuint32 type to write.
1061 """
1063 writer.write_varuint32(value)
1066class VarUInt64ArrayTraits:
1067 """
1068 Array traits for Zserio varuint64 type.
1069 """
1071 HAS_BITSIZEOF_CONSTANT = False
1072 NEEDS_BITSIZEOF_POSITION = False
1073 NEEDS_READ_INDEX = False
1074 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
1076 @property
1077 def packed_traits(self) -> PackedArrayTraits:
1078 """
1079 Gets packed array traits.
1081 :returns: PackedArrayTraits instance.
1082 """
1084 return PackedArrayTraits(self)
1086 @staticmethod
1087 def bitsizeof(value: int) -> int:
1088 """
1089 Returns length of Zserio varuint64 type stored in the bit stream in bits.
1091 :param value: Zserio varuint64 type value.
1092 :returns: Length of given Zserio varuint64 type in bits.
1093 """
1095 return bitsizeof_varuint64(value)
1097 @staticmethod
1098 def initialize_offsets(bitposition: int, value: int) -> int:
1099 """
1100 Initializes indexed offsets for Zserio varuint64 type.
1102 :param value: Zserio varuint64 type value.
1103 :returns: Updated bit stream position which points to the first bit after Zserio varuint64 type.
1104 """
1106 return bitposition + VarUInt64ArrayTraits.bitsizeof(value)
1108 @staticmethod
1109 def read(reader: BitStreamReader) -> int:
1110 """
1111 Reads Zserio varuint64 type from the bit stream.
1113 :param reader: Bit stream from which to read.
1114 :returns: Read varuint64 value.
1115 """
1117 return reader.read_varuint64()
1119 @staticmethod
1120 def write(writer: BitStreamWriter, value: int) -> None:
1121 """
1122 Writes Zserio varuint64 type to the bit stream.
1124 :param writer: Bit stream where to write.
1125 :param value: Zserio varuint64 type to write.
1126 """
1128 writer.write_varuint64(value)
1131class VarUIntArrayTraits:
1132 """
1133 Array traits for Zserio varuint type.
1134 """
1136 HAS_BITSIZEOF_CONSTANT = False
1137 NEEDS_BITSIZEOF_POSITION = False
1138 NEEDS_READ_INDEX = False
1139 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
1141 @property
1142 def packed_traits(self) -> PackedArrayTraits:
1143 """
1144 Gets packed array traits.
1146 :returns: PackedArrayTraits instance.
1147 """
1149 return PackedArrayTraits(self)
1151 @staticmethod
1152 def bitsizeof(value: int) -> int:
1153 """
1154 Returns length of Zserio varuint type stored in the bit stream in bits.
1156 :param value: Zserio varuint type value.
1157 :returns: Length of given Zserio varuint type in bits.
1158 """
1160 return bitsizeof_varuint(value)
1162 @staticmethod
1163 def initialize_offsets(bitposition: int, value: int) -> int:
1164 """
1165 Initializes indexed offsets for Zserio varuint type.
1167 :param bitposition: Current bit stream position.
1168 :param value: Zserio varuint type value.
1169 :returns: Updated bit stream position which points to the first bit after Zserio varuint type.
1170 """
1172 return bitposition + VarUIntArrayTraits.bitsizeof(value)
1174 @staticmethod
1175 def read(reader: BitStreamReader) -> int:
1176 """
1177 Reads Zserio varuint type from the bit stream.
1179 :param reader: Bit stream from which to read.
1180 :returns: Read varuint value.
1181 """
1183 return reader.read_varuint()
1185 @staticmethod
1186 def write(writer: BitStreamWriter, value: int) -> None:
1187 """
1188 Writes Zserio varuint type to the bit stream.
1190 :param writer: Bit stream where to write.
1191 :param value: Zserio varuint type to write.
1192 """
1194 writer.write_varuint(value)
1197class VarSizeArrayTraits:
1198 """
1199 Array traits for Zserio varsize type.
1200 """
1202 HAS_BITSIZEOF_CONSTANT = False
1203 NEEDS_BITSIZEOF_POSITION = False
1204 NEEDS_READ_INDEX = False
1205 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
1207 @property
1208 def packed_traits(self) -> PackedArrayTraits:
1209 """
1210 Gets packed array traits.
1212 :returns: PackedArrayTraits instance.
1213 """
1215 return PackedArrayTraits(self)
1217 @staticmethod
1218 def bitsizeof(value: int) -> int:
1219 """
1220 Returns length of Zserio varsize type stored in the bit stream in bits.
1222 :param value: Zserio varsize type value.
1223 :returns: Length of given Zserio varsize type in bits.
1224 """
1226 return bitsizeof_varsize(value)
1228 @staticmethod
1229 def initialize_offsets(bitposition: int, value: int) -> int:
1230 """
1231 Initializes indexed offsets for Zserio varsize type.
1233 :param bitposition: Current bit stream position.
1234 :param value: Zserio varsize type value.
1235 :returns: Updated bit stream position which points to the first bit after Zserio varsize type.
1236 """
1238 return bitposition + VarSizeArrayTraits.bitsizeof(value)
1240 @staticmethod
1241 def read(reader: BitStreamReader) -> int:
1242 """
1243 Reads Zserio varsize type from the bit stream.
1245 :param reader: Bit stream from which to read.
1246 :returns: Read varsize value.
1247 """
1249 return reader.read_varsize()
1251 @staticmethod
1252 def write(writer: BitStreamWriter, value: int) -> None:
1253 """
1254 Writes Zserio varsize type to the bit stream.
1256 :param writer: Bit stream where to write.
1257 :param value: Zserio varsize type to write.
1258 """
1260 writer.write_varsize(value)
1263class VarInt16ArrayTraits:
1264 """
1265 Array traits for Zserio varint16 type.
1266 """
1268 HAS_BITSIZEOF_CONSTANT = False
1269 NEEDS_BITSIZEOF_POSITION = False
1270 NEEDS_READ_INDEX = False
1271 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
1273 @property
1274 def packed_traits(self) -> PackedArrayTraits:
1275 """
1276 Gets packed array traits.
1278 :returns: PackedArrayTraits instance.
1279 """
1281 return PackedArrayTraits(self)
1283 @staticmethod
1284 def bitsizeof(value: int) -> int:
1285 """
1286 Returns length of Zserio varint16 type stored in the bit stream in bits.
1288 :param value: Zserio varint16 type value.
1289 :returns: Length of given Zserio varint16 type in bits.
1290 """
1292 return bitsizeof_varint16(value)
1294 @staticmethod
1295 def initialize_offsets(bitposition: int, value: int) -> int:
1296 """
1297 Initializes indexed offsets for Zserio varint16 type.
1299 :param bitposition: Current bit stream position.
1300 :param value: Zserio varint16 type value.
1301 :returns: Updated bit stream position which points to the first bit after Zserio varint16 type.
1302 """
1304 return bitposition + VarInt16ArrayTraits.bitsizeof(value)
1306 @staticmethod
1307 def read(reader: BitStreamReader) -> int:
1308 """
1309 Reads Zserio varint16 type from the bit stream.
1311 :param reader: Bit stream from which to read.
1312 :returns: Read varint16 value.
1313 """
1315 return reader.read_varint16()
1317 @staticmethod
1318 def write(writer: BitStreamWriter, value: int) -> None:
1319 """
1320 Writes Zserio varint16 type to the bit stream.
1322 :param writer: Bit stream where to write.
1323 :param value: Zserio varint16 type to write.
1324 """
1326 writer.write_varint16(value)
1329class VarInt32ArrayTraits:
1330 """
1331 Array traits for Zserio varint32 type.
1332 """
1334 HAS_BITSIZEOF_CONSTANT = False
1335 NEEDS_BITSIZEOF_POSITION = False
1336 NEEDS_READ_INDEX = False
1337 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
1339 @property
1340 def packed_traits(self) -> PackedArrayTraits:
1341 """
1342 Gets packed array traits.
1344 :returns: PackedArrayTraits instance.
1345 """
1347 return PackedArrayTraits(self)
1349 @staticmethod
1350 def bitsizeof(value: int) -> int:
1351 """
1352 Returns length of Zserio varint32 type stored in the bit stream in bits.
1354 :param value: Zserio varint32 type value.
1355 :returns: Length of given Zserio varint32 type in bits.
1356 """
1358 return bitsizeof_varint32(value)
1360 @staticmethod
1361 def initialize_offsets(bitposition: int, value: int) -> int:
1362 """
1363 Initializes indexed offsets for Zserio varint32 type.
1365 :param bitposition: Current bit stream position.
1366 :param value: Zserio varint32 type value.
1367 :returns: Updated bit stream position which points to the first bit after Zserio varint32 type.
1368 """
1370 return bitposition + VarInt32ArrayTraits.bitsizeof(value)
1372 @staticmethod
1373 def read(reader: BitStreamReader) -> int:
1374 """
1375 Reads Zserio varint32 type from the bit stream.
1377 :param reader: Bit stream from which to read.
1378 :returns: Read varint32 value.
1379 """
1381 return reader.read_varint32()
1383 @staticmethod
1384 def write(writer: BitStreamWriter, value: int) -> None:
1385 """
1386 Writes Zserio varint32 type to the bit stream.
1388 :param writer: Bit stream where to write.
1389 :param value: Zserio varint32 type to write.
1390 """
1392 writer.write_varint32(value)
1395class VarInt64ArrayTraits:
1396 """
1397 Array traits for Zserio varint64 type.
1398 """
1400 HAS_BITSIZEOF_CONSTANT = False
1401 NEEDS_BITSIZEOF_POSITION = False
1402 NEEDS_READ_INDEX = False
1403 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
1405 @property
1406 def packed_traits(self) -> PackedArrayTraits:
1407 """
1408 Gets packed array traits.
1410 :returns: PackedArrayTraits instance.
1411 """
1413 return PackedArrayTraits(self)
1415 @staticmethod
1416 def bitsizeof(value: int) -> int:
1417 """
1418 Returns length of Zserio varint64 type stored in the bit stream in bits.
1420 :param value: Zserio varint64 type value.
1421 :returns: Length of given Zserio varint64 type in bits.
1422 """
1424 return bitsizeof_varint64(value)
1426 @staticmethod
1427 def initialize_offsets(bitposition: int, value: int) -> int:
1428 """
1429 Initializes indexed offsets for Zserio varint64 type.
1431 :param bitposition: Current bit stream position.
1432 :param value: Zserio varint64 type value.
1433 :returns: Updated bit stream position which points to the first bit after Zserio varint64 type.
1434 """
1436 return bitposition + VarInt64ArrayTraits.bitsizeof(value)
1438 @staticmethod
1439 def read(reader: BitStreamReader) -> int:
1440 """
1441 Reads Zserio varint64 type from the bit stream.
1443 :param reader: Bit stream from which to read.
1444 :returns: Read varint64 value.
1445 """
1447 return reader.read_varint64()
1449 @staticmethod
1450 def write(writer: BitStreamWriter, value: int) -> None:
1451 """
1452 Writes Zserio varint64 type to the bit stream.
1454 :param writer: Bit stream where to write.
1455 :param value: Zserio varint64 type to write.
1456 """
1458 writer.write_varint64(value)
1461class VarIntArrayTraits:
1462 """
1463 Array traits for Zserio varint type.
1464 """
1466 HAS_BITSIZEOF_CONSTANT = False
1467 NEEDS_BITSIZEOF_POSITION = False
1468 NEEDS_READ_INDEX = False
1469 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_int_array)
1471 @property
1472 def packed_traits(self) -> PackedArrayTraits:
1473 """
1474 Gets packed array traits.
1476 :returns: PackedArrayTraits instance.
1477 """
1479 return PackedArrayTraits(self)
1481 @staticmethod
1482 def bitsizeof(value: int) -> int:
1483 """
1484 Returns length of Zserio varint type stored in the bit stream in bits.
1486 :param value: Zserio varint type value.
1487 :returns: Length of given Zserio varint type in bits.
1488 """
1490 return bitsizeof_varint(value)
1492 @staticmethod
1493 def initialize_offsets(bitposition: int, value: int) -> int:
1494 """
1495 Initializes indexed offsets for Zserio varint type.
1497 :param bitposition: Current bit stream position.
1498 :param value: Zserio varint type value.
1499 :returns: Updated bit stream position which points to the first bit after Zserio varint type.
1500 """
1502 return bitposition + VarIntArrayTraits.bitsizeof(value)
1504 @staticmethod
1505 def read(reader: BitStreamReader) -> int:
1506 """
1507 Reads Zserio varint type from the bit stream.
1509 :param reader: Bit stream from which to read.
1510 :returns: Read varint value.
1511 """
1513 return reader.read_varint()
1515 @staticmethod
1516 def write(writer: BitStreamWriter, value: int) -> None:
1517 """
1518 Writes Zserio varint type to the bit stream.
1520 :param writer: Bit stream where to write.
1521 :param value: Zserio varint type to write.
1522 """
1524 writer.write_varint(value)
1527class Float16ArrayTraits:
1528 """
1529 Array traits for Zserio float16 type.
1530 """
1532 HAS_BITSIZEOF_CONSTANT = True
1533 NEEDS_BITSIZEOF_POSITION = False
1534 NEEDS_READ_INDEX = False
1535 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float32_array)
1537 @property
1538 def packed_traits(self) -> None:
1539 """
1540 Returns None since float type is not packable.
1542 :returns: None.
1543 """
1545 return None
1547 @staticmethod
1548 def bitsizeof() -> int:
1549 """
1550 Returns length of Zserio float16 type stored in the bit stream in bits.
1552 :returns: Length of Zserio float16 type in bits.
1553 """
1555 return 16
1557 @staticmethod
1558 def initialize_offsets(bitposition: int, _value: float) -> int:
1559 """
1560 Initializes indexed offsets for Zserio float16 type.
1562 :param bitposition: Current bit stream position.
1563 :param _value: Not used.
1564 :returns: Updated bit stream position which points to the first bit after Zserio float16 type.
1565 """
1567 return bitposition + Float16ArrayTraits.bitsizeof()
1569 @staticmethod
1570 def read(reader: BitStreamReader) -> float:
1571 """
1572 Reads Zserio float16 type from the bit stream.
1574 :param reader: Bit stream from which to read.
1575 """
1577 return reader.read_float16()
1579 @staticmethod
1580 def write(writer: BitStreamWriter, value: float) -> None:
1581 """
1582 Writes Zserio float16 type to the bit stream.
1584 :param writer: Bit stream where to write.
1585 :param value: Zserio float16 type to write.
1586 :returns: Read float16 value.
1587 """
1589 writer.write_float16(value)
1592class Float32ArrayTraits:
1593 """
1594 Array traits for Zserio float32 type.
1595 """
1597 HAS_BITSIZEOF_CONSTANT = True
1598 NEEDS_BITSIZEOF_POSITION = False
1599 NEEDS_READ_INDEX = False
1600 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float32_array)
1602 @property
1603 def packed_traits(self) -> None:
1604 """
1605 Returns None since float type is not packable.
1607 :returns: None.
1608 """
1610 return None
1612 @staticmethod
1613 def bitsizeof() -> int:
1614 """
1615 Returns length of Zserio float32 type stored in the bit stream in bits.
1617 :returns: Length of Zserio float32 type in bits.
1618 """
1620 return 32
1622 @staticmethod
1623 def initialize_offsets(bitposition: int, _value: float) -> int:
1624 """
1625 Initializes indexed offsets for Zserio float32 type.
1627 :param bitposition: Current bit stream position.
1628 :param _value: Not used.
1629 :returns: Updated bit stream position which points to the first bit after Zserio float32 type.
1630 """
1632 return bitposition + Float32ArrayTraits.bitsizeof()
1634 @staticmethod
1635 def read(reader: BitStreamReader) -> float:
1636 """
1637 Reads Zserio float32 type from the bit stream.
1639 :param reader: Bit stream from which to read.
1640 :returns: Read float32 value.
1641 """
1643 return reader.read_float32()
1645 @staticmethod
1646 def write(writer: BitStreamWriter, value: float) -> None:
1647 """
1648 Writes Zserio float32 type to the bit stream.
1650 :param writer: Bit stream where to write.
1651 :param value: Zserio float32 type to write.
1652 """
1654 writer.write_float32(value)
1657class Float64ArrayTraits:
1658 """
1659 Array traits for Zserio float64 type.
1660 """
1662 HAS_BITSIZEOF_CONSTANT = True
1663 NEEDS_BITSIZEOF_POSITION = False
1664 NEEDS_READ_INDEX = False
1665 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_float64_array)
1667 @property
1668 def packed_traits(self) -> None:
1669 """
1670 Returns None since float type is not packable.
1672 :returns: None.
1673 """
1675 return None
1677 @staticmethod
1678 def bitsizeof() -> int:
1679 """
1680 Returns length of Zserio float64 type stored in the bit stream in bits.
1682 :returns: Length of Zserio float64 type in bits.
1683 """
1685 return 64
1687 @staticmethod
1688 def initialize_offsets(bitposition: int, _value: float) -> int:
1689 """
1690 Initializes indexed offsets for Zserio float64 type.
1692 :param bitposition: Current bit stream position.
1693 :param _value: Not used.
1694 :returns: Updated bit stream position which points to the first bit after Zserio float64 type.
1695 """
1697 return bitposition + Float64ArrayTraits.bitsizeof()
1699 @staticmethod
1700 def read(reader: BitStreamReader) -> float:
1701 """
1702 Reads Zserio float64 type from the bit stream.
1704 :param reader: Bit stream from which to read.
1705 :returns: Read float64 value.
1706 """
1708 return reader.read_float64()
1710 @staticmethod
1711 def write(writer: BitStreamWriter, value: float) -> None:
1712 """
1713 Writes Zserio float64 type to the bit stream.
1715 :param writer: Bit stream where to write.
1716 :param value: Zserio float64 type to write.
1717 """
1719 writer.write_float64(value)
1722class BytesArrayTraits:
1723 """
1724 Array traits for Zserio bytes type.
1725 """
1727 HAS_BITSIZEOF_CONSTANT = False
1728 NEEDS_BITSIZEOF_POSITION = False
1729 NEEDS_READ_INDEX = False
1730 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_bytes_array)
1732 @property
1733 def packed_traits(self) -> None:
1734 """
1735 Returns None since Bytes type is not packable.
1737 :returns: None.
1738 """
1740 return None
1742 @staticmethod
1743 def bitsizeof(value: bytearray) -> int:
1744 """
1745 Returns length of Zserio bytes type stored in the bit stream in bits.
1747 :param value: Zserio bytes type value.
1748 :returns: Length of given Zserio bytes type in bits.
1749 """
1751 return bitsizeof_bytes(value)
1753 @staticmethod
1754 def initialize_offsets(bitposition: int, value: bytearray) -> int:
1755 """
1756 Initializes indexed offsets for Zserio bytes type.
1758 :param bitposition: Current bit stream position.
1759 :param value: Zserio bytes type value.
1760 :returns: Updated bit stream position which points to the first bit after Zserio bytes type.
1761 """
1763 return bitposition + BytesArrayTraits.bitsizeof(value)
1765 @staticmethod
1766 def read(reader: BitStreamReader) -> bytearray:
1767 """
1768 Reads Zserio bytes type from the bit stream.
1770 :param reader: Bit stream from which to read.
1771 :returns: Read bytes value.
1772 """
1774 return reader.read_bytes()
1776 @staticmethod
1777 def write(writer: BitStreamWriter, value: bytearray) -> None:
1778 """
1779 Writes Zserio bytes type to the bit stream.
1781 :param writer: Bit stream where to write.
1782 :param value: Zserio bytes type to write.
1783 """
1785 writer.write_bytes(value)
1788class StringArrayTraits:
1789 """
1790 Array traits for Zserio string type.
1791 """
1793 HAS_BITSIZEOF_CONSTANT = False
1794 NEEDS_BITSIZEOF_POSITION = False
1795 NEEDS_READ_INDEX = False
1796 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_string_array)
1798 @property
1799 def packed_traits(self) -> None:
1800 """
1801 Returns None since String type is not packable.
1803 :returns: None.
1804 """
1806 return None
1808 @staticmethod
1809 def bitsizeof(value: str) -> int:
1810 """
1811 Returns length of Zserio string type stored in the bit stream in bits.
1813 :param value: Zserio string type value.
1814 :returns: Length of given Zserio string type in bits.
1815 """
1817 return bitsizeof_string(value)
1819 @staticmethod
1820 def initialize_offsets(bitposition: int, value: str) -> int:
1821 """
1822 Initializes indexed offsets for Zserio string type.
1824 :param bitposition: Current bit stream position.
1825 :param value: Zserio string type value.
1826 :returns: Updated bit stream position which points to the first bit after Zserio string type.
1827 """
1829 return bitposition + StringArrayTraits.bitsizeof(value)
1831 @staticmethod
1832 def read(reader: BitStreamReader) -> str:
1833 """
1834 Reads Zserio string type from the bit stream.
1836 :param reader: Bit stream from which to read.
1837 :returns: Read string value.
1838 """
1840 return reader.read_string()
1842 @staticmethod
1843 def write(writer: BitStreamWriter, value: str) -> None:
1844 """
1845 Writes Zserio string type to the bit stream.
1847 :param writer: Bit stream where to write.
1848 :param value: Zserio string type to write.
1849 """
1851 writer.write_string(value)
1854class BoolArrayTraits:
1855 """
1856 Array traits for Zserio bool type.
1857 """
1859 HAS_BITSIZEOF_CONSTANT = True
1860 NEEDS_BITSIZEOF_POSITION = False
1861 NEEDS_READ_INDEX = False
1862 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_bool_array)
1864 @property
1865 def packed_traits(self) -> None:
1866 """
1867 Returns None since Bool type is not packable.
1869 :returns: None.
1870 """
1872 return None
1874 @staticmethod
1875 def bitsizeof() -> int:
1876 """
1877 Returns length of Zserio bool type stored in the bit stream in bits.
1879 :returns: Length of Zserio bool type in bits.
1880 """
1882 return 1
1884 @staticmethod
1885 def initialize_offsets(bitposition: int, _value: bool) -> int:
1886 """
1887 Initializes indexed offsets for Zserio bool type.
1889 :param bitposition: Current bit stream position.
1890 :param _value: Not used.
1891 :returns: Updated bit stream position which points to the first bit after Zserio bool type.
1892 """
1894 return bitposition + BoolArrayTraits.bitsizeof()
1896 @staticmethod
1897 def read(reader: BitStreamReader) -> bool:
1898 """
1899 Reads Zserio bool type from the bit stream.
1901 :param reader: Bit stream from which to read.
1902 :returns: Read bool value.
1903 """
1905 return reader.read_bool()
1907 @staticmethod
1908 def write(writer: BitStreamWriter, value: bool) -> None:
1909 """
1910 Writes Zserio bool type to the bit stream.
1912 :param writer: Bit stream where to write.
1913 :param value: Zserio bool type to write.
1914 """
1916 writer.write_bool(value)
1919class BitBufferArrayTraits:
1920 """
1921 Array traits for Zserio extern bit buffer type.
1922 """
1924 HAS_BITSIZEOF_CONSTANT = False
1925 NEEDS_BITSIZEOF_POSITION = False
1926 NEEDS_READ_INDEX = False
1927 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_object_array)
1929 @property
1930 def packed_traits(self) -> None:
1931 """
1932 Returns None since BitBuffer type is not packable.
1934 :returns: None.
1935 """
1937 return None
1939 @staticmethod
1940 def bitsizeof(value: BitBuffer) -> int:
1941 """
1942 Returns length of Zserio extern bit buffer type stored in the bit stream in bits.
1944 :param value: Zserio extern bit buffer type value.
1945 :returns: Length of given Zserio string type in bits.
1946 """
1948 return bitsizeof_bitbuffer(value)
1950 @staticmethod
1951 def initialize_offsets(bitposition: int, value: BitBuffer) -> int:
1952 """
1953 Initializes indexed offsets for Zserio extern bit buffer type.
1955 :param bitposition: Current bit stream position.
1956 :param value: Zserio extern bit buffer type value.
1957 :returns: Updated bit stream position which points to the first bit after Zserio extern bit buffer type.
1958 """
1960 return bitposition + BitBufferArrayTraits.bitsizeof(value)
1962 @staticmethod
1963 def read(reader: BitStreamReader) -> BitBuffer:
1964 """
1965 Reads Zserio extern bit buffer type from the bit stream.
1967 :param reader: Bit stream from which to read.
1968 :returns: Read bit buffer value.
1969 """
1971 return reader.read_bitbuffer()
1973 @staticmethod
1974 def write(writer: BitStreamWriter, value: BitBuffer) -> None:
1975 """
1976 Writes Zserio extern bit buffer type to the bit stream.
1978 :param writer: Bit stream where to write.
1979 :param value: Zserio extern bit buffer type to write.
1980 """
1982 writer.write_bitbuffer(value)
1985class ObjectArrayTraits:
1986 """
1987 Array traits for Zserio structure, choice, union and enum types.
1988 """
1990 HAS_BITSIZEOF_CONSTANT = False
1991 NEEDS_BITSIZEOF_POSITION = True
1992 NEEDS_READ_INDEX = True
1993 CALC_HASHCODE_FUNC = staticmethod(calc_hashcode_object_array)
1995 def __init__(self, element_factory: typing.Any) -> None:
1996 """
1997 Constructor.
1999 :param element_factory: Element factory which creates object from the element index.
2000 """
2002 self._element_factory = element_factory
2003 self._packed_traits = (
2004 ObjectPackedArrayTraits(element_factory) if element_factory.IS_OBJECT_PACKABLE else None
2005 )
2007 @property
2008 def packed_traits(self) -> typing.Optional[ObjectPackedArrayTraits]:
2009 """
2010 Gets packed array traits.
2012 :returns: ObjectPackedArrayTraits instance.
2013 """
2015 return self._packed_traits
2017 @staticmethod
2018 def bitsizeof(bitposition: int, value: typing.Any) -> int:
2019 """
2020 Returns length of Zserio object type stored in the bit stream in bits.
2022 :param bitposition: Current bit position in bit stream.
2023 :param value: Zserio object type value.
2024 :returns: Length of given Zserio object type in bits.
2025 """
2027 return value.bitsizeof(bitposition)
2029 @staticmethod
2030 def initialize_offsets(bitposition: int, value: typing.Any) -> int:
2031 """
2032 Initializes indexed offsets for the Zserio object type.
2034 :param bitposition: Current bit stream position.
2035 :param value: Zserio object type value.
2036 :returns: Updated bit stream position which points to the first bit after the Zserio object type.
2037 """
2039 return value.initialize_offsets(bitposition)
2041 def read(self, reader: BitStreamReader, index: int) -> typing.Any:
2042 """
2043 Reads Zserio object type from the bit stream.
2045 :param reader: Bit stream from which to read.
2046 :param index: Element index in the array.
2047 :returns: Read object.
2048 """
2050 return self._element_factory.create(reader, index)
2052 @staticmethod
2053 def write(writer: BitStreamWriter, value: typing.Any) -> None:
2054 """
2055 Writes Zserio object type to the bit stream.
2057 :param writer: Bit stream where to write.
2058 :param value: Zserio object type to write.
2059 """
2061 value.write(writer)