internal Type Add(string value) { var bytes = Encoding.UTF8.GetBytes(value); var length = (ulong)bytes.Length; var bitWidth = BitWidthUtil.Width(length); if (_options.HasFlag(Options.ShareStrings) && _stringCache.ContainsKey(value)) { _stack.Add(StackValue.Value(_stringCache[value], bitWidth, Type.String)); return(Type.String); } var byteWidth = Align(bitWidth); Write(length, byteWidth); var stringOffset = _offset; var newOffset = NewOffset(length + 1); Buffer.BlockCopy(bytes, 0, _bytes, (int)_offset, (int)length); _offset = newOffset; _stack.Add(StackValue.Value(stringOffset, bitWidth, Type.String)); if (_options.HasFlag(Options.ShareStrings)) { _stringCache[value] = stringOffset; } return(Type.String); }
private byte Align(BitWidth width) { var byteWidth = 1UL << (int)width; _offset += BitWidthUtil.PaddingSize(_offset, byteWidth); return((byte)byteWidth); }
public static StackValue Value(ulong value) { return(new StackValue { Width = BitWidthUtil.Width(value), UValue = value, ValueType = Type.Uint }); }
public static StackValue Value(double value) { return(new StackValue { Width = BitWidthUtil.Width(value), DValue = value, ValueType = Type.Float }); }
internal Type AddIndirect(double value) { var type = Type.IndirectFloat; var bitWidth = BitWidthUtil.Width(value); var byteWidth = Align(bitWidth); var valueOffset = _offset; Write(value, byteWidth); _stack.Add(StackValue.Value(valueOffset, bitWidth, type)); return(type); }
internal Type Add(byte[] value) { var bitWidth = BitWidthUtil.Width(value.Length); var byteWidth = Align(bitWidth); Write(value.Length, byteWidth); var newOffset = NewOffset(value.Length); var blobOffset = _offset; Buffer.BlockCopy(value, 0, _bytes, _offset, value.Length); _offset = newOffset; _stack.Add(StackValue.Value(blobOffset, bitWidth, Type.Blob)); return(Type.Blob); }
public BitWidth ElementWidth(ulong size, int index) { if (TypesUtil.IsInline(ValueType)) { return(Width); } for (var i = 0; i < 4; i++) { var width = (ulong)1 << i; var offsetLoc = size + BitWidthUtil.PaddingSize(size, width) + (ulong)index * width; var offset = offsetLoc - UValue; var bitWidth = BitWidthUtil.Width(offset); if ((1UL << (byte)bitWidth) == width) { return(bitWidth); } } throw new Exception($"Element with size: {size} and index: {index} is of unknown width"); }
private StackValue CreateVector(int start, int vecLen, int step, bool typed, bool fix, StackValue?keys = null) { var bitWidth = BitWidthUtil.Width(vecLen); var prefixElems = 1; if (keys != null) { var elemWidth = keys.Value.ElementWidth(_offset, 0); if ((int)elemWidth > (int)bitWidth) { bitWidth = elemWidth; } prefixElems += 2; } var vectorType = Type.Key; for (var i = start; i < _stack.Count; i += step) { var elemWidth = _stack[i].ElementWidth(_offset, i + prefixElems); if ((int)elemWidth > (int)bitWidth) { bitWidth = elemWidth; } if (typed) { if (i == start) { vectorType = _stack[i].TypeOfValue; } else { if (vectorType != _stack[i].TypeOfValue) { throw new Exception($"Your typed vector is of type {vectorType} but the item on index {i} is of type {_stack[i].TypeOfValue}"); } } } } if (TypesUtil.IsTypedVectorElement(vectorType) == false) { throw new Exception("Your fixed types are not one of: Int / UInt / Float / Key"); } var byteWidth = Align(bitWidth); if (keys != null) { Write(keys.Value, byteWidth); Write(1 << (int)keys.Value.InternalWidth, byteWidth); } if (!fix) { Write(vecLen, byteWidth); } var vloc = _offset; for (var i = start; i < _stack.Count; i += step) { Write(_stack[i], byteWidth); } if (!typed) { for (var i = start; i < _stack.Count; i += step) { Write(_stack[i].StoredPackedType()); } } if (keys != null) { return(StackValue.Value(vloc, bitWidth, Type.Map)); } if (typed) { var type = TypesUtil.ToTypedVector(vectorType, (byte)(fix ? vecLen : 0)); return(StackValue.Value(vloc, bitWidth, type)); } return(StackValue.Value(vloc, bitWidth, Type.Vector)); }