Beispiel #1
0
        private void AddDynamicVector(IEnumerable values)
        {
            var start    = StartVector();
            var typed    = true;
            var prevType = -1;

            foreach (object value in values)
            {
                var currentType = AddDynamic(value);

                if (typed == false || TypesUtil.IsTypedVectorElement(currentType) == false)
                {
                    typed = false;
                    continue;
                }

                if (prevType == -1)
                {
                    prevType = (int)currentType;
                }

                if (typed)
                {
                    typed = prevType == (int)currentType;
                }
            }
            EndVector(start, typed, false);
        }
Beispiel #2
0
        public                  FlxValue this[int index]
        {
            get
            {
                if (index < 0 || index >= _length)
                {
                    throw new Exception($"Bad index {index}, should be 0...{_length}");
                }

                if (TypesUtil.IsTypedVector(_type))
                {
                    var elemOffset = _offset + (index * _byteWidth);
                    return(new FlxValue(_buffer, elemOffset, _byteWidth, 1, TypesUtil.TypedVectorElementType(_type)));
                }

                if (TypesUtil.IsFixedTypedVector(_type))
                {
                    var elemOffset = _offset + (index * _byteWidth);
                    return(new FlxValue(_buffer, elemOffset, _byteWidth, 1, TypesUtil.FixedTypedVectorElementType(_type)));
                }

                if (_type == Type.Vector)
                {
                    var packedType = _buffer[_offset + _length * _byteWidth + index];
                    var elemOffset = _offset + (index * _byteWidth);
                    return(new FlxValue(_buffer, elemOffset, _byteWidth, packedType));
                }
                throw new Exception($"Bad index {index}, should be 0...{_length}");
            }
        }
Beispiel #3
0
        public BitWidth StoredWidth(BitWidth bitWidth = BitWidth.Width8)
        {
            if (TypesUtil.IsInline(ValueType))
            {
                return((BitWidth)Math.Max((int)bitWidth, (int)Width));
            }

            return(Width);
        }
Beispiel #4
0
        public string ToPrettyJson(string left = "", bool childrenOnly = false)
        {
            if (_type == Type.Map)
            {
                return(AsMap.ToPrettyJson(left, childrenOnly));
            }
            if (TypesUtil.IsAVector(_type))
            {
                return(AsVector.ToPrettyJson(left, childrenOnly));
            }

            if (childrenOnly)
            {
                return(ToJson);
            }

            return($"{left}{ToJson}");
        }
Beispiel #5
0
        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");
        }
Beispiel #6
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));
        }
Beispiel #7
0
 public byte StoredPackedType(BitWidth bitWidth = BitWidth.Width8)
 {
     return(TypesUtil.PackedType(ValueType, StoredWidth(bitWidth)));
 }