# automatically generated by the FlatBuffers compiler, do not modify

# namespace: Dl

import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()

class TensorShape(object):
    __slots__ = ['_tab']

    @classmethod
    def GetRootAs(cls, buf, offset=0):
        n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
        x = TensorShape()
        x.Init(buf, n + offset)
        return x

    @classmethod
    def GetRootAsTensorShape(cls, buf, offset=0):
        """This method is deprecated. Please switch to GetRootAs."""
        return cls.GetRootAs(buf, offset)
    # TensorShape
    def Init(self, buf, pos):
        self._tab = flatbuffers.table.Table(buf, pos)

    # TensorShape
    def Dim(self, j):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
        if o != 0:
            x = self._tab.Vector(o)
            x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * 4
            x = self._tab.Indirect(x)
            from FlatBuffers.Dl.Dimension import Dimension
            obj = Dimension()
            obj.Init(self._tab.Bytes, x)
            return obj
        return None

    # TensorShape
    def DimLength(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
        if o != 0:
            return self._tab.VectorLen(o)
        return 0

    # TensorShape
    def DimIsNone(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
        return o == 0

def TensorShapeStart(builder):
    builder.StartObject(1)

def Start(builder):
    TensorShapeStart(builder)

def TensorShapeAddDim(builder, dim):
    builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(dim), 0)

def AddDim(builder, dim):
    TensorShapeAddDim(builder, dim)

def TensorShapeStartDimVector(builder, numElems):
    return builder.StartVector(4, numElems, 4)

def StartDimVector(builder, numElems):
    return TensorShapeStartDimVector(builder, numElems)

def TensorShapeEnd(builder):
    return builder.EndObject()

def End(builder):
    return TensorShapeEnd(builder)

import FlatBuffers.Dl.Dimension
try:
    from typing import List
except:
    pass

class TensorShapeT(object):

    # TensorShapeT
    def __init__(self):
        self.dim = None  # type: List[FlatBuffers.Dl.Dimension.DimensionT]

    @classmethod
    def InitFromBuf(cls, buf, pos):
        tensorShape = TensorShape()
        tensorShape.Init(buf, pos)
        return cls.InitFromObj(tensorShape)

    @classmethod
    def InitFromPackedBuf(cls, buf, pos=0):
        n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, pos)
        return cls.InitFromBuf(buf, pos+n)

    @classmethod
    def InitFromObj(cls, tensorShape):
        x = TensorShapeT()
        x._UnPack(tensorShape)
        return x

    # TensorShapeT
    def _UnPack(self, tensorShape):
        if tensorShape is None:
            return
        if not tensorShape.DimIsNone():
            self.dim = []
            for i in range(tensorShape.DimLength()):
                if tensorShape.Dim(i) is None:
                    self.dim.append(None)
                else:
                    dimension_ = FlatBuffers.Dl.Dimension.DimensionT.InitFromObj(tensorShape.Dim(i))
                    self.dim.append(dimension_)

    # TensorShapeT
    def Pack(self, builder):
        if self.dim is not None:
            dimlist = []
            for i in range(len(self.dim)):
                dimlist.append(self.dim[i].Pack(builder))
            TensorShapeStartDimVector(builder, len(self.dim))
            for i in reversed(range(len(self.dim))):
                builder.PrependUOffsetTRelative(dimlist[i])
            dim = builder.EndVector()
        TensorShapeStart(builder)
        if self.dim is not None:
            TensorShapeAddDim(builder, dim)
        tensorShape = TensorShapeEnd(builder)
        return tensorShape
