




"""simple docstring"""





import sys
from collections.abc import Mapping

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter

class  __lowercase    (     TensorFormatter[Mapping, np.ndarray, Mapping]		):


				'''simple docstring'''


				def __init__(				self				:       Optional[int] ,SCREAMING_SNAKE_CASE_				:       str=None ,**SCREAMING_SNAKE_CASE_				:       List[str] ):
										super().__init__(features=SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Optional[Any]							     =   np_array_kwargs

				def  _lowerCamelCase   (				self				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       List[Any] ):
										if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ):
																if column and all(
																    isinstance(SCREAMING_SNAKE_CASE_ ,np.ndarray ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column ):
																						return np.stack(SCREAMING_SNAKE_CASE_ )
																else:
																						# don't use np.array(column, dtype=object)
																						# since it fails in certain cases
																						# see https://stackoverflow.com/q/51005699
																						snake_case__    :   Union[str, Any]							     =   np.empty(len(SCREAMING_SNAKE_CASE_ ) ,dtype=SCREAMING_SNAKE_CASE_ )
																						snake_case__    :   str							     =   column
																						return out
										return column

				def  _lowerCamelCase   (				self				:       Optional[int] ,SCREAMING_SNAKE_CASE_				:       List[Any] ):
										if isinstance(SCREAMING_SNAKE_CASE_ ,(str, bytes, type(SCREAMING_SNAKE_CASE_ )) ):
																return value
										elif isinstance(SCREAMING_SNAKE_CASE_ ,(np.character, np.ndarray) ) and np.issubdtype(value.dtype ,np.character ):
																return value
										elif isinstance(SCREAMING_SNAKE_CASE_ ,np.number ):
																return value

										snake_case__    :   List[Any]							     =   {}

										if isinstance(SCREAMING_SNAKE_CASE_ ,np.ndarray ) and np.issubdtype(value.dtype ,np.integer ):
																snake_case__    :   List[Any]							     =   {'''dtype''': np.intaa}
										elif isinstance(SCREAMING_SNAKE_CASE_ ,np.ndarray ) and np.issubdtype(value.dtype ,np.floating ):
																snake_case__    :   List[str]							     =   {'''dtype''': np.floataa}
										elif config.PIL_AVAILABLE and "PIL" in sys.modules:
																import PIL.Image

																if isinstance(SCREAMING_SNAKE_CASE_ ,PIL.Image.Image ):
																						return np.asarray(SCREAMING_SNAKE_CASE_ ,**self.np_array_kwargs )

										return np.asarray(SCREAMING_SNAKE_CASE_ ,**{**default_dtype, **self.np_array_kwargs} )

				def  _lowerCamelCase   (				self				:       Optional[int] ,SCREAMING_SNAKE_CASE_				:       Optional[Any] ):
										# support for torch, tf, jax etc.
										if config.TORCH_AVAILABLE and "torch" in sys.modules:
																import torch

																if isinstance(SCREAMING_SNAKE_CASE_ ,torch.Tensor ):
																						return self._tensorize(data_struct.detach().cpu().numpy()[()] )
										if hasattr(SCREAMING_SNAKE_CASE_ ,'''__array__''' ) and not isinstance(SCREAMING_SNAKE_CASE_ ,(np.ndarray, np.character, np.number) ):
																snake_case__    :   List[str]							     =   data_struct.__array__()
										# support for nested types like struct of list of struct
										if isinstance(SCREAMING_SNAKE_CASE_ ,np.ndarray ):
																if data_struct.dtype == object:
																						return self._consolidate([self.recursive_tensorize(SCREAMING_SNAKE_CASE_ ) for substruct in data_struct] )
										if isinstance(SCREAMING_SNAKE_CASE_ ,(list, tuple) ):
																return self._consolidate([self.recursive_tensorize(SCREAMING_SNAKE_CASE_ ) for substruct in data_struct] )
										return self._tensorize(SCREAMING_SNAKE_CASE_ )

				def  _lowerCamelCase   (				self				:       Tuple ,SCREAMING_SNAKE_CASE_				:       dict ):
										return map_nested(self._recursive_tensorize ,SCREAMING_SNAKE_CASE_ ,map_list=SCREAMING_SNAKE_CASE_ )

				def  _lowerCamelCase   (				self				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       pa.Table ):
										snake_case__    :   Tuple							     =   self.numpy_arrow_extractor().extract_row(SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Optional[int]							     =   self.python_features_decoder.decode_row(SCREAMING_SNAKE_CASE_ )
										return self.recursive_tensorize(SCREAMING_SNAKE_CASE_ )

				def  _lowerCamelCase   (				self				:       List[str] ,SCREAMING_SNAKE_CASE_				:       pa.Table ):
										snake_case__    :   Union[str, Any]							     =   self.numpy_arrow_extractor().extract_column(SCREAMING_SNAKE_CASE_ )
										snake_case__    :   int							     =   self.python_features_decoder.decode_column(SCREAMING_SNAKE_CASE_ ,pa_table.column_names[0] )
										snake_case__    :   Optional[Any]							     =   self.recursive_tensorize(SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Tuple							     =   self._consolidate(SCREAMING_SNAKE_CASE_ )
										return column





				def  _lowerCamelCase   (				self				:       Optional[Any] ,SCREAMING_SNAKE_CASE_				:       pa.Table ):
										snake_case__    :   Optional[int]							     =   self.numpy_arrow_extractor().extract_batch(SCREAMING_SNAKE_CASE_ )
										snake_case__    :   List[str]							     =   self.python_features_decoder.decode_batch(SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Tuple							     =   self.recursive_tensorize(SCREAMING_SNAKE_CASE_ )
										for column_name in batch:
																snake_case__    :   Optional[int]							     =   self._consolidate(batch[column_name] )
										return batch








"""simple docstring"""





class  __lowercase    :


				'''simple docstring'''


				class  __lowercase    :


								'''simple docstring'''


								_lowerCAmelCase		     ="_prev", "_data", "_next"

								def __init__(				self				:       List[Any] ,SCREAMING_SNAKE_CASE_				:       int ,SCREAMING_SNAKE_CASE_				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       Any ):
														snake_case__    :   Any							     =   link_p
														snake_case__    :   List[str]							     =   element
														snake_case__    :   str							     =   link_n





								def  _lowerCamelCase   (				self				:       Optional[Any] ):
														return (
														    F''' Prev -> {self._prev is not None}, Next -> {self._next is not None}'''
														)







				def __init__(				self				:       Any ):
										snake_case__    :   int							     =   self._Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Optional[int]							     =   self._Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Union[str, Any]							     =   self._trailer
										snake_case__    :   Union[str, Any]							     =   self._header
										snake_case__    :   Any							     =   0

				def __len__(				self				:       str ):
										return self._size

				def  _lowerCamelCase   (				self				:       Union[str, Any] ):
										return self.__len__() == 0

				def  _lowerCamelCase   (				self				:       Any ,SCREAMING_SNAKE_CASE_				:       List[Any] ,SCREAMING_SNAKE_CASE_				:       List[str] ,SCREAMING_SNAKE_CASE_				:       Dict ):
										# Create new_node by setting it's prev.link -> header
										# setting it's next.link -> trailer
										snake_case__    :   Dict							     =   self._Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Any							     =   new_node
										snake_case__    :   str							     =   new_node
										self._size += 1
										return self





				def  _lowerCamelCase   (				self				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       Dict ):
										snake_case__    :   Union[str, Any]							     =   node._prev
										snake_case__    :   Union[str, Any]							     =   node._next

										snake_case__    :   List[Any]							     =   successor
										snake_case__    :   Dict							     =   predecessor
										self._size -= 1
										snake_case__    :   Dict							     =   node._data
										snake_case__    :   List[str]							     =   None
										del node
										return temp







class  __lowercase    (     _DoublyLinkedBase		):


				'''simple docstring'''


				def  _lowerCamelCase   (				self				:       Optional[int] ):
										if self.is_empty():
																raise Exception('''List is empty''' )
										return self._header._next._data

				def  _lowerCamelCase   (				self				:       Union[str, Any] ):
										if self.is_empty():
																raise Exception('''List is empty''' )
										return self._trailer._prev._data

				def  _lowerCamelCase   (				self				:       Any ,SCREAMING_SNAKE_CASE_				:       Dict ):
										return self._insert(self._header ,SCREAMING_SNAKE_CASE_ ,self._header._next )

				def  _lowerCamelCase   (				self				:       Dict ,SCREAMING_SNAKE_CASE_				:       Union[str, Any] ):
										return self._insert(self._trailer._prev ,SCREAMING_SNAKE_CASE_ ,self._trailer )

				def  _lowerCamelCase   (				self				:       Optional[int] ):
										if self.is_empty():
																raise IndexError('''remove_first from empty list''' )
										return self._delete(self._header._next )





				def  _lowerCamelCase   (				self				:       Optional[Any] ):
										if self.is_empty():
																raise IndexError('''remove_first from empty list''' )
										return self._delete(self._trailer._prev )


