Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        internal void AddKey(string value)
        {
            if (_options.HasFlag(Options.ShareKeys) && _keyCache.ContainsKey(value))
            {
                _stack.Add(StackValue.Value(_keyCache[value], BitWidth.Width8, Type.Key));
                return;
            }
            var bytes     = Encoding.UTF8.GetBytes(value);
            var length    = (ulong)bytes.Length;
            var keyOffset = _offset;
            var newOffset = NewOffset(length + 1);

            Buffer.BlockCopy(bytes, 0, _bytes, (int)_offset, (int)length);
            _offset = newOffset;
            _stack.Add(StackValue.Value(keyOffset, BitWidth.Width8, Type.Key));
            if (_options.HasFlag(Options.ShareKeys))
            {
                _keyCache[value] = keyOffset;
            }
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
 internal Type Add(bool value)
 {
     _stack.Add(StackValue.Value(value));
     return(Type.Bool);
 }
Ejemplo n.º 7
0
 internal Type Add(double value)
 {
     _stack.Add(StackValue.Value(value));
     return(Type.Float);
 }
Ejemplo n.º 8
0
 internal Type Add(ulong value)
 {
     _stack.Add(StackValue.Value(value));
     return(Type.Uint);
 }