m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.0 KiB

6 months ago
  1. # Copyright 2014 Google Inc. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from . import encode
  15. from . import number_types as N
  16. class Table(object):
  17. """Table wraps a byte slice and provides read access to its data.
  18. The variable `Pos` indicates the root of the FlatBuffers object therein."""
  19. __slots__ = ("Bytes", "Pos")
  20. def __init__(self, buf, pos):
  21. N.enforce_number(pos, N.UOffsetTFlags)
  22. self.Bytes = buf
  23. self.Pos = pos
  24. def Offset(self, vtableOffset):
  25. """Offset provides access into the Table's vtable.
  26. Deprecated fields are ignored by checking the vtable's length."""
  27. vtable = self.Pos - self.Get(N.SOffsetTFlags, self.Pos)
  28. vtableEnd = self.Get(N.VOffsetTFlags, vtable)
  29. if vtableOffset < vtableEnd:
  30. return self.Get(N.VOffsetTFlags, vtable + vtableOffset)
  31. return 0
  32. def Indirect(self, off):
  33. """Indirect retrieves the relative offset stored at `offset`."""
  34. N.enforce_number(off, N.UOffsetTFlags)
  35. return off + encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
  36. def String(self, off):
  37. """String gets a string from data stored inside the flatbuffer."""
  38. N.enforce_number(off, N.UOffsetTFlags)
  39. off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
  40. start = off + N.UOffsetTFlags.bytewidth
  41. length = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
  42. return bytes(self.Bytes[start:start+length])
  43. def VectorLen(self, off):
  44. """VectorLen retrieves the length of the vector whose offset is stored
  45. at "off" in this object."""
  46. N.enforce_number(off, N.UOffsetTFlags)
  47. off += self.Pos
  48. off += encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
  49. ret = encode.Get(N.UOffsetTFlags.packer_type, self.Bytes, off)
  50. return ret
  51. def Vector(self, off):
  52. """Vector retrieves the start of data of the vector whose offset is
  53. stored at "off" in this object."""
  54. N.enforce_number(off, N.UOffsetTFlags)
  55. off += self.Pos
  56. x = off + self.Get(N.UOffsetTFlags, off)
  57. # data starts after metadata containing the vector length
  58. x += N.UOffsetTFlags.bytewidth
  59. return x
  60. def Union(self, t2, off):
  61. """Union initializes any Table-derived type to point to the union at
  62. the given offset."""
  63. assert type(t2) is Table
  64. N.enforce_number(off, N.UOffsetTFlags)
  65. off += self.Pos
  66. t2.Pos = off + self.Get(N.UOffsetTFlags, off)
  67. t2.Bytes = self.Bytes
  68. def Get(self, flags, off):
  69. """
  70. Get retrieves a value of the type specified by `flags` at the
  71. given offset.
  72. """
  73. N.enforce_number(off, N.UOffsetTFlags)
  74. return flags.py_type(encode.Get(flags.packer_type, self.Bytes, off))
  75. def GetSlot(self, slot, d, validator_flags):
  76. N.enforce_number(slot, N.VOffsetTFlags)
  77. if validator_flags is not None:
  78. N.enforce_number(d, validator_flags)
  79. off = self.Offset(slot)
  80. if off == 0:
  81. return d
  82. return self.Get(validator_flags, self.Pos + off)
  83. def GetVectorAsNumpy(self, flags, off):
  84. """
  85. GetVectorAsNumpy returns the vector that starts at `Vector(off)`
  86. as a numpy array with the type specified by `flags`. The array is
  87. a `view` into Bytes, so modifying the returned array will
  88. modify Bytes in place.
  89. """
  90. offset = self.Vector(off)
  91. length = self.VectorLen(off) # TODO: length accounts for bytewidth, right?
  92. numpy_dtype = N.to_numpy_type(flags)
  93. return encode.GetVectorAsNumpy(numpy_dtype, self.Bytes, length, offset)
  94. def GetArrayAsNumpy(self, flags, off, length):
  95. """
  96. GetArrayAsNumpy returns the array with fixed width that starts at `Vector(offset)`
  97. with length `length` as a numpy array with the type specified by `flags`. The
  98. array is a `view` into Bytes so modifying the returned will modify Bytes in place.
  99. """
  100. numpy_dtype = N.to_numpy_type(flags)
  101. return encode.GetVectorAsNumpy(numpy_dtype, self.Bytes, length, off)
  102. def GetVOffsetTSlot(self, slot, d):
  103. """
  104. GetVOffsetTSlot retrieves the VOffsetT that the given vtable location
  105. points to. If the vtable value is zero, the default value `d`
  106. will be returned.
  107. """
  108. N.enforce_number(slot, N.VOffsetTFlags)
  109. N.enforce_number(d, N.VOffsetTFlags)
  110. off = self.Offset(slot)
  111. if off == 0:
  112. return d
  113. return off