Ejemplo n.º 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);
        }
Ejemplo n.º 2
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));
        }