Пример #1
0
        public ProtectedPointer(SymbolicExpression sexp)
        {
            this.sexp   = sexp.DangerousGetHandle();
            this.engine = sexp.Engine;

            this.GetFunction <Rf_protect>()(this.sexp);
        }
Пример #2
0
 /// <summary>
 /// Gets the expression as a symbol.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <returns>The symbol.</returns>
 public static Symbol AsSymbol(this SymbolicExpression expression)
 {
     if (!expression.IsSymbol())
     {
         return(null);
     }
     return(new Symbol(expression.Engine, expression.DangerousGetHandle()));
 }
Пример #3
0
 /// <summary>
 /// Gets the expression as an <see cref="REnvironment"/>.
 /// </summary>
 /// <param name="expression">The environment.</param>
 /// <returns>The environment.</returns>
 public static REnvironment AsEnvironment(this SymbolicExpression expression)
 {
     if (!expression.IsEnvironment())
     {
         return(null);
     }
     return(new REnvironment(expression.Engine, expression.DangerousGetHandle()));
 }
Пример #4
0
        /// <summary>
        /// Gets the expression as a function.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The function.</returns>
        public static Function AsFunction(this SymbolicExpression expression)
        {
            switch (expression.Type)
            {
            case SymbolicExpressionType.Closure:
                return(new Closure(expression.Engine, expression.DangerousGetHandle()));

            case SymbolicExpressionType.BuiltinFunction:
                return(new BuiltinFunction(expression.Engine, expression.DangerousGetHandle()));

            case SymbolicExpressionType.SpecialFunction:
                return(new SpecialFunction(expression.Engine, expression.DangerousGetHandle()));

            default:
                throw new ArgumentException();
            }
        }
Пример #5
0
 /// <summary>
 /// Gets the expression as a factor.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <returns>The factor.</returns>
 public static Factor AsFactor(this SymbolicExpression expression)
 {
     if (!IsFactor(expression))
     {
         throw new ArgumentException("Not a factor.", "expression");
     }
     return(new Factor(expression.Engine, expression.DangerousGetHandle()));
 }
Пример #6
0
 /// <summary>
 /// Converts the specified expression to a DataFrame.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <returns>The DataFrame. Returns <c>null</c> if the specified expression is not vector.</returns>
 public static DataFrame AsDataFrame(this SymbolicExpression expression)
 {
     if (!expression.IsDataFrame())
     {
         return(null);
     }
     return(new DataFrame(expression.Engine, expression.DangerousGetHandle()));
 }
Пример #7
0
 /// <summary>
 /// Gets the expression as a language.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <returns>The language.</returns>
 public static Language AsLanguage(this SymbolicExpression expression)
 {
     if (!expression.IsLanguage())
     {
         return(null);
     }
     return(new Language(expression.Engine, expression.DangerousGetHandle()));
 }
Пример #8
0
 /// <summary>
 /// Gets whether the specified expression is an S4 object.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <returns><c>True</c> if the specified expression is an S4 object.</returns>
 public static bool IsS4(this SymbolicExpression expression)
 {
     if (expression == null)
     {
         throw new ArgumentNullException();
     }
     return(expression.GetFunction <Rf_isS4>()(expression.DangerousGetHandle()));
 }
Пример #9
0
        /// <summary>
        /// Converts the specified expression to a CharacterVector.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The LogicalVector. Returns <c>null</c> if the specified expression is not vector.</returns>
        public static CharacterVector AsCharacter(this SymbolicExpression expression)
        {
            if (!expression.IsVector())
            {
                return(null);
            }
            IntPtr coerced = IntPtr.Zero;

            if (expression.IsFactor())
            {
                coerced = expression.GetFunction <Rf_asCharacterFactor>()(expression.DangerousGetHandle());
            }
            else
            {
                coerced = expression.GetFunction <Rf_coerceVector>()(expression.DangerousGetHandle(), SymbolicExpressionType.CharacterVector);
            }
            return(new CharacterVector(expression.Engine, coerced));
        }
Пример #10
0
        /// <summary>
        /// Converts the specified expression to a RawVector.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The LogicalVector. Returns <c>null</c> if the specified expression is not vector.</returns>
        public static RawVector AsRaw(this SymbolicExpression expression)
        {
            if (!expression.IsVector())
            {
                return(null);
            }
            IntPtr coerced = expression.GetFunction <Rf_coerceVector>()(expression.DangerousGetHandle(), SymbolicExpressionType.RawVector);

            return(new RawVector(expression.Engine, coerced));
        }
Пример #11
0
        /// <summary>
        /// Converts the specified expression to a DynamicVector.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The DynamicVector. Returns <c>null</c> if the specified expression is not vector.</returns>
        public static DynamicVector AsVector(this SymbolicExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException();
            }
            IntPtr coerced = expression.GetFunction <Rf_coerceVector>()(expression.DangerousGetHandle(), expression.Type);

            return(new DynamicVector(expression.Engine, coerced));
        }
Пример #12
0
        private void SetValue(int rowIndex, int columnIndex, string value)
        {
            int offset           = GetOffset(rowIndex, columnIndex);
            SymbolicExpression s = value == null ? Engine.NilValue : new InternalString(Engine, value);

            using (new ProtectedPointer(s))
            {
                Marshal.WriteIntPtr(DataPointer, offset, s.DangerousGetHandle());
            }
        }
Пример #13
0
        /// <summary>
        /// Gets whether the specified expression is factor.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns><c>True</c> if the specified expression is factor.</returns>
        public static bool IsFactor(this SymbolicExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException();
            }
            var handle = expression.DangerousGetHandle();

            return(expression.GetFunction <Rf_isFactor>()(handle));
        }
Пример #14
0
        /// <summary>
        /// Converts the specified expression to a RawMatrix.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>The RawMatrix. Returns <c>null</c> if the specified expression is not vector.</returns>
        public static RawMatrix AsRawMatrix(this SymbolicExpression expression)
        {
            if (!expression.IsVector())
            {
                return(null);
            }

            int rowCount    = 0;
            int columnCount = 0;

            if (expression.IsMatrix())
            {
                if (expression.Type == SymbolicExpressionType.RawVector)
                {
                    return(new RawMatrix(expression.Engine, expression.DangerousGetHandle()));
                }
                else
                {
                    rowCount    = expression.GetFunction <Rf_nrows>()(expression.DangerousGetHandle());
                    columnCount = expression.GetFunction <Rf_ncols>()(expression.DangerousGetHandle());
                }
            }

            if (columnCount == 0)
            {
                rowCount    = expression.GetFunction <Rf_length>()(expression.DangerousGetHandle());
                columnCount = 1;
            }

            IntPtr             coerced   = expression.GetFunction <Rf_coerceVector>()(expression.DangerousGetHandle(), SymbolicExpressionType.RawVector);
            var                dim       = new IntegerVector(expression.Engine, new[] { rowCount, columnCount });
            SymbolicExpression dimSymbol = expression.Engine.GetPredefinedSymbol("R_DimSymbol");
            var                matrix    = new RawMatrix(expression.Engine, coerced);

            matrix.SetAttribute(dimSymbol, dim);
            return(matrix);
        }
Пример #15
0
        /// <summary>
        /// Defines a symbol in this environment.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="expression">The symbol.</param>
        public void SetSymbol(string name, SymbolicExpression expression)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name", "'name' cannot be null");
            }
            if (name == string.Empty)
            {
                throw new ArgumentException("'name' cannot be an empty string");
            }
            if (expression == null)
            {
                expression = Engine.NilValue;
            }
            if (expression.Engine != this.Engine)
            {
                throw new ArgumentException();
            }
            IntPtr installedName = this.GetFunction <Rf_install>()(name);

            this.GetFunction <Rf_defineVar>()(installedName, expression.DangerousGetHandle(), handle);
        }
Пример #16
0
 /// <summary>
 /// Gets whether the specified expression is list.
 /// </summary>
 /// <param name="expression">The expression.</param>
 /// <returns><c>True</c> if the specified expression is list.</returns>
 public static bool IsList(this SymbolicExpression expression)
 {
     if (expression == null)
     {
         throw new ArgumentNullException();
     }
     // See issue 81. Rf_isList in the R API is NOT the correct thing to use (yes, hard to be more conter-intuitive)
     return(expression.Type == SymbolicExpressionType.List ||
            // ?is.list ==> "is.list returns TRUE if and only if its argument is a list or a pairlist of length > 0"
            (expression.Type == SymbolicExpressionType.Pairlist && expression.GetFunction <Rf_length>()(expression.DangerousGetHandle()) > 0));
 }
Пример #17
0
 private void WriteSymbolicExpression(SymbolicExpression sexp, IntPtr pointer, int offset)
 {
     Marshal.WriteIntPtr(pointer, offset, sexp.DangerousGetHandle());
 }