# automatically generated by the FlatBuffers compiler, do not modify

# namespace: flatbuf

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

# Compressed Sparse Fiber (CSF) sparse tensor index.
class SparseTensorIndexCSF(object):
    __slots__ = ['_tab']

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

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

    # CSF is a generalization of compressed sparse row (CSR) index.
    # See [smith2017knl](http://shaden.io/pub-files/smith2017knl.pdf)
    #
    # CSF index recursively compresses each dimension of a tensor into a set
    # of prefix trees. Each path from a root to leaf forms one tensor
    # non-zero index. CSF is implemented with two arrays of buffers and one
    # arrays of integers.
    #
    # For example, let X be a 2x3x4x5 tensor and let it have the following
    # 8 non-zero values:
    # ```text
    #   X[0, 0, 0, 1] := 1
    #   X[0, 0, 0, 2] := 2
    #   X[0, 1, 0, 0] := 3
    #   X[0, 1, 0, 2] := 4
    #   X[0, 1, 1, 0] := 5
    #   X[1, 1, 1, 0] := 6
    #   X[1, 1, 1, 1] := 7
    #   X[1, 1, 1, 2] := 8
    # ```
    # As a prefix tree this would be represented as:
    # ```text
    #         0          1
    #        / \         |
    #       0   1        1
    #      /   / \       |
    #     0   0   1      1
    #    /|  /|   |    /| |
    #   1 2 0 2   0   0 1 2
    # ```
    # The type of values in indptrBuffers
    # SparseTensorIndexCSF
    def IndptrType(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
        if o != 0:
            x = self._tab.Indirect(o + self._tab.Pos)
            from vastdb.vast_flatbuf.org.apache.arrow.flatbuf.Int import Int
            obj = Int()
            obj.Init(self._tab.Bytes, x)
            return obj
        return None

    # indptrBuffers stores the sparsity structure.
    # Each two consecutive dimensions in a tensor correspond to a buffer in
    # indptrBuffers. A pair of consecutive values at `indptrBuffers[dim][i]`
    # and `indptrBuffers[dim][i + 1]` signify a range of nodes in
    # `indicesBuffers[dim + 1]` who are children of `indicesBuffers[dim][i]` node.
    #
    # For example, the indptrBuffers for the above X is:
    # ```text
    #   indptrBuffer(X) = [
    #                       [0, 2, 3],
    #                       [0, 1, 3, 4],
    #                       [0, 2, 4, 5, 8]
    #                     ].
    # ```
    # SparseTensorIndexCSF
    def IndptrBuffers(self, j):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
        if o != 0:
            x = self._tab.Vector(o)
            x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * 16
            from vastdb.vast_flatbuf.org.apache.arrow.flatbuf.Buffer import Buffer
            obj = Buffer()
            obj.Init(self._tab.Bytes, x)
            return obj
        return None

    # SparseTensorIndexCSF
    def IndptrBuffersLength(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
        if o != 0:
            return self._tab.VectorLen(o)
        return 0

    # SparseTensorIndexCSF
    def IndptrBuffersIsNone(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6))
        return o == 0

    # The type of values in indicesBuffers
    # SparseTensorIndexCSF
    def IndicesType(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(8))
        if o != 0:
            x = self._tab.Indirect(o + self._tab.Pos)
            from vastdb.vast_flatbuf.org.apache.arrow.flatbuf.Int import Int
            obj = Int()
            obj.Init(self._tab.Bytes, x)
            return obj
        return None

    # indicesBuffers stores values of nodes.
    # Each tensor dimension corresponds to a buffer in indicesBuffers.
    # For example, the indicesBuffers for the above X is:
    # ```text
    #   indicesBuffer(X) = [
    #                        [0, 1],
    #                        [0, 1, 1],
    #                        [0, 0, 1, 1],
    #                        [1, 2, 0, 2, 0, 0, 1, 2]
    #                      ].
    # ```
    # SparseTensorIndexCSF
    def IndicesBuffers(self, j):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(10))
        if o != 0:
            x = self._tab.Vector(o)
            x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * 16
            from vastdb.vast_flatbuf.org.apache.arrow.flatbuf.Buffer import Buffer
            obj = Buffer()
            obj.Init(self._tab.Bytes, x)
            return obj
        return None

    # SparseTensorIndexCSF
    def IndicesBuffersLength(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(10))
        if o != 0:
            return self._tab.VectorLen(o)
        return 0

    # SparseTensorIndexCSF
    def IndicesBuffersIsNone(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(10))
        return o == 0

    # axisOrder stores the sequence in which dimensions were traversed to
    # produce the prefix tree.
    # For example, the axisOrder for the above X is:
    # ```text
    #   axisOrder(X) = [0, 1, 2, 3].
    # ```
    # SparseTensorIndexCSF
    def AxisOrder(self, j):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12))
        if o != 0:
            a = self._tab.Vector(o)
            return self._tab.Get(flatbuffers.number_types.Int32Flags, a + flatbuffers.number_types.UOffsetTFlags.py_type(j * 4))
        return 0

    # SparseTensorIndexCSF
    def AxisOrderAsNumpy(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12))
        if o != 0:
            return self._tab.GetVectorAsNumpy(flatbuffers.number_types.Int32Flags, o)
        return 0

    # SparseTensorIndexCSF
    def AxisOrderLength(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12))
        if o != 0:
            return self._tab.VectorLen(o)
        return 0

    # SparseTensorIndexCSF
    def AxisOrderIsNone(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(12))
        return o == 0

def Start(builder): builder.StartObject(5)
def SparseTensorIndexCSFStart(builder):
    """This method is deprecated. Please switch to Start."""
    return Start(builder)
def AddIndptrType(builder, indptrType): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(indptrType), 0)
def SparseTensorIndexCSFAddIndptrType(builder, indptrType):
    """This method is deprecated. Please switch to AddIndptrType."""
    return AddIndptrType(builder, indptrType)
def AddIndptrBuffers(builder, indptrBuffers): builder.PrependUOffsetTRelativeSlot(1, flatbuffers.number_types.UOffsetTFlags.py_type(indptrBuffers), 0)
def SparseTensorIndexCSFAddIndptrBuffers(builder, indptrBuffers):
    """This method is deprecated. Please switch to AddIndptrBuffers."""
    return AddIndptrBuffers(builder, indptrBuffers)
def StartIndptrBuffersVector(builder, numElems): return builder.StartVector(16, numElems, 8)
def SparseTensorIndexCSFStartIndptrBuffersVector(builder, numElems):
    """This method is deprecated. Please switch to Start."""
    return StartIndptrBuffersVector(builder, numElems)
def AddIndicesType(builder, indicesType): builder.PrependUOffsetTRelativeSlot(2, flatbuffers.number_types.UOffsetTFlags.py_type(indicesType), 0)
def SparseTensorIndexCSFAddIndicesType(builder, indicesType):
    """This method is deprecated. Please switch to AddIndicesType."""
    return AddIndicesType(builder, indicesType)
def AddIndicesBuffers(builder, indicesBuffers): builder.PrependUOffsetTRelativeSlot(3, flatbuffers.number_types.UOffsetTFlags.py_type(indicesBuffers), 0)
def SparseTensorIndexCSFAddIndicesBuffers(builder, indicesBuffers):
    """This method is deprecated. Please switch to AddIndicesBuffers."""
    return AddIndicesBuffers(builder, indicesBuffers)
def StartIndicesBuffersVector(builder, numElems): return builder.StartVector(16, numElems, 8)
def SparseTensorIndexCSFStartIndicesBuffersVector(builder, numElems):
    """This method is deprecated. Please switch to Start."""
    return StartIndicesBuffersVector(builder, numElems)
def AddAxisOrder(builder, axisOrder): builder.PrependUOffsetTRelativeSlot(4, flatbuffers.number_types.UOffsetTFlags.py_type(axisOrder), 0)
def SparseTensorIndexCSFAddAxisOrder(builder, axisOrder):
    """This method is deprecated. Please switch to AddAxisOrder."""
    return AddAxisOrder(builder, axisOrder)
def StartAxisOrderVector(builder, numElems): return builder.StartVector(4, numElems, 4)
def SparseTensorIndexCSFStartAxisOrderVector(builder, numElems):
    """This method is deprecated. Please switch to Start."""
    return StartAxisOrderVector(builder, numElems)
def End(builder): return builder.EndObject()
def SparseTensorIndexCSFEnd(builder):
    """This method is deprecated. Please switch to End."""
    return End(builder)