1import 'dart:typed_data'; 2 3/// Represents the number of bits a value occupies. 4enum BitWidth { 5 width8, 6 width16, 7 width32, 8 width64 9} 10 11class BitWidthUtil { 12 static int toByteWidth(BitWidth self) { 13 return 1 << self.index; 14 } 15 static BitWidth width(num value) { 16 if (value.toInt() == value) { 17 var v = value.toInt().abs(); 18 if (v >> 7 == 0) return BitWidth.width8; 19 if (v >> 15 == 0) return BitWidth.width16; 20 if (v >> 31 == 0) return BitWidth.width32; 21 return BitWidth.width64; 22 } 23 return value == _toF32(value) ? BitWidth.width32 : BitWidth.width64; 24 } 25 static BitWidth uwidth(num value) { 26 if (value.toInt() == value) { 27 var v = value.toInt().abs(); 28 if (v >> 8 == 0) return BitWidth.width8; 29 if (v >> 16 == 0) return BitWidth.width16; 30 if (v >> 32 == 0) return BitWidth.width32; 31 return BitWidth.width64; 32 } 33 return value == _toF32(value) ? BitWidth.width32 : BitWidth.width64; 34 } 35 static BitWidth fromByteWidth(int value) { 36 if (value == 1) { 37 return BitWidth.width8; 38 } 39 if (value == 2) { 40 return BitWidth.width16; 41 } 42 if (value == 4) { 43 return BitWidth.width32; 44 } 45 if (value == 8) { 46 return BitWidth.width64; 47 } 48 throw Exception('Unexpected value ${value}'); 49 } 50 static int paddingSize(int bufSize, int scalarSize) { 51 return (~bufSize + 1) & (scalarSize - 1); 52 } 53 static double _toF32(double value) { 54 var bdata = ByteData(4); 55 bdata.setFloat32(0, value); 56 return bdata.getFloat32(0); 57 } 58 59 static BitWidth max(BitWidth self, BitWidth other) { 60 if (self.index < other.index) { 61 return other; 62 } 63 return self; 64 } 65} 66 67/// Represents all internal FlexBuffer types. 68enum ValueType { 69 Null, Int, UInt, Float, 70 Key, String, IndirectInt, IndirectUInt, IndirectFloat, 71 Map, Vector, VectorInt, VectorUInt, VectorFloat, VectorKey, 72 @Deprecated('VectorString is deprecated due to a flaw in the binary format (https://github.com/google/flatbuffers/issues/5627)') 73 VectorString, 74 VectorInt2, VectorUInt2, VectorFloat2, 75 VectorInt3, VectorUInt3, VectorFloat3, 76 VectorInt4, VectorUInt4, VectorFloat4, 77 Blob, Bool, VectorBool 78} 79 80class ValueTypeUtils { 81 static int toInt(ValueType self) { 82 if (self == ValueType.VectorBool) return 36; 83 return self.index; 84 } 85 86 static ValueType fromInt(int value) { 87 if (value == 36) return ValueType.VectorBool; 88 return ValueType.values[value]; 89 } 90 91 static bool isInline(ValueType self) { 92 return self == ValueType.Bool 93 || toInt(self) <= toInt(ValueType.Float); 94 } 95 96 static bool isNumber(ValueType self) { 97 return toInt(self) >= toInt(ValueType.Int) 98 && toInt(self) <= toInt(ValueType.Float); 99 } 100 101 static bool isIndirectNumber(ValueType self) { 102 return toInt(self) >= toInt(ValueType.IndirectInt) 103 && toInt(self) <= toInt(ValueType.IndirectFloat); 104 } 105 106 static bool isTypedVectorElement(ValueType self) { 107 return self == ValueType.Bool || 108 ( 109 toInt(self) >= toInt(ValueType.Int) 110 && toInt(self) <= toInt(ValueType.String) 111 ); 112 } 113 114 static bool isTypedVector(ValueType self) { 115 return self == ValueType.VectorBool || 116 ( 117 toInt(self) >= toInt(ValueType.VectorInt) 118 && toInt(self) <= toInt(ValueType.VectorString) 119 ); 120 } 121 122 static bool isFixedTypedVector(ValueType self) { 123 return ( 124 toInt(self) >= toInt(ValueType.VectorInt2) 125 && toInt(self) <= toInt(ValueType.VectorFloat4) 126 ); 127 } 128 129 static bool isAVector(ValueType self) { 130 return ( 131 isTypedVector(self) || isFixedTypedVector(self) || self == ValueType.Vector 132 ); 133 } 134 135 static ValueType toTypedVector(ValueType self, int length) { 136 if (length == 0) { 137 return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt)); 138 } 139 if (length == 2) { 140 return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt2)); 141 } 142 if (length == 3) { 143 return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt3)); 144 } 145 if (length == 4) { 146 return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt4)); 147 } 148 throw Exception('unexpected length ' + length.toString()); 149 } 150 151 static ValueType typedVectorElementType(ValueType self) { 152 return ValueTypeUtils.fromInt(toInt(self) - toInt(ValueType.VectorInt) + toInt(ValueType.Int)); 153 } 154 155 static ValueType fixedTypedVectorElementType(ValueType self) { 156 return ValueTypeUtils.fromInt((toInt(self) - toInt(ValueType.VectorInt2)) % 3 + toInt(ValueType.Int)); 157 } 158 159 static int fixedTypedVectorElementSize(ValueType self) { 160 return (toInt(self) - toInt(ValueType.VectorInt2)) ~/ 3 + 2; 161 } 162 163 static int packedType(ValueType self, BitWidth bitWidth) { 164 return bitWidth.index | (toInt(self) << 2); 165 } 166} 167