Exemple #1
0
        public void EvalFlip(RCRunner runner, RCClosure closure, RCBlock right)
        {
            RCBlock prototype = (RCBlock)right.Get(0);

            string[]       names   = new string[prototype.Count];
            RCVectorBase[] columns = new RCVectorBase[prototype.Count];
            for (int i = 0; i < prototype.Count; ++i)
            {
                RCBlock      name   = prototype.GetName(i);
                RCVectorBase scalar = (RCVectorBase)name.Value;
                columns[i] = RCVectorBase.FromScalar(scalar.Child(0));
                names[i]   = name.Name;
            }
            for (int i = 1; i < right.Count; ++i)
            {
                RCBlock row = (RCBlock)right.Get(i);
                for (int j = 0; j < row.Count; ++j)
                {
                    RCBlock name = (RCBlock)row.GetName(j);
                    int     n    = Array.IndexOf(names, name.Name);
                    if (n >= 0)
                    {
                        RCVectorBase scalar = (RCVectorBase)name.Value;
                        columns[n].Write(scalar.Child(0));
                    }
                }
            }
            RCBlock result = RCBlock.Empty;

            for (int i = 0; i < names.Length; ++i)
            {
                result = new RCBlock(result, names[i], ":", columns[i]);
            }
            runner.Yield(closure, result);
        }
Exemple #2
0
 public static RCVector <L> DoAt <L> (RCClosure closure, RCVector <L> left, RCVector <decimal> right)
 {
     L[] result = new L[right.Count];
     for (int i = 0; i < right.Count; ++i)
     {
         if (right[i] < 0)
         {
             int j = (int)(left.Count + right[i]);
             if (j < 0 || j >= left.Count)
             {
                 throw RCException.Range(closure, (long)right[i], left.Count);
             }
             result[i] = left[j];
         }
         else
         {
             int j = (int)right[i];
             if (j < 0 || j >= left.Count)
             {
                 throw RCException.Range(closure, (long)right[i], left.Count);
             }
             result[i] = left[j];
         }
     }
     return((RCVector <L>)RCVectorBase.FromArray(new RCArray <L> (result)));
 }
Exemple #3
0
        public void EvalUnflip(RCRunner runner, RCClosure closure, RCBlock right)
        {
            // Take a block of arrays and turn them into a block of rows.
            RCBlock[] blocks = new RCBlock[right.Get(0).Count];
            for (int i = 0; i < blocks.Length; ++i)
            {
                blocks[i] = RCBlock.Empty;
            }
            for (int i = 0; i < right.Count; ++i)
            {
                RCBlock      name   = right.GetName(i);
                RCVectorBase vector = (RCVectorBase)name.Value;
                for (int j = 0; j < vector.Count; ++j)
                {
                    RCValue box = RCVectorBase.FromScalar(vector.Child(j));
                    blocks[j] = new RCBlock(blocks[j], name.Name, ":", box);
                }
            }
            RCBlock result = RCBlock.Empty;

            for (int i = 0; i < blocks.Length; ++i)
            {
                result = new RCBlock(result, "", ":", blocks[i]);
            }
            runner.Yield(closure, result);
        }
Exemple #4
0
        protected virtual void DoEach <T> (RCRunner runner,
                                           RCClosure closure,
                                           RCBlock left,
                                           RCVector <T> right)
        {
            if (right.Count == 0)
            {
                runner.Yield(closure, RCBlock.Empty);
                return;
            }
            int i = closure.Index - 2;

            if (i < right.Count)
            {
                RCBlock result = new RCBlock("I", ":", new RCLong(i));
                result = new RCBlock(result,
                                     "R",
                                     ":",
                                     RCVectorBase.FromArray(new RCArray <T> (right[i])));
                left.Eval(runner,
                          new RCClosure(closure, closure.Bot, left, closure.Left, result, 0));
            }
            else
            {
                runner.Yield(closure, closure.Parent.Result);
            }
        }
Exemple #5
0
 protected RCValue ReorderColumn <T> (RCLong rank, RCVector <T> column) where T : IComparable <T>
 {
     return(RCVectorBase.FromArray(
                VectorMath.MonadicOp <long, T> (rank.Data,
                                                delegate(long r)
     {
         return column[(int)r];
     })));
 }
Exemple #6
0
        public static RCVector <L> DoWhere <L> (RCVector <L> left, RCVector <bool> right)
        {
            RCArray <L> result = new RCArray <L> ();

            for (int i = 0; i < right.Count; ++i)
            {
                if (right[i])
                {
                    result.Write(left[i]);
                }
            }
            return((RCVector <L>)RCVectorBase.FromArray(new RCArray <L> (result)));
        }
Exemple #7
0
        public void EvalDyadic(RCRunner runner, RCClosure closure, object left, object right)
        {
            // Brian! come back here to prevent the native exception
            RCOperator   op          = (RCOperator)closure.Code;
            RCVectorBase leftVector  = left as RCVectorBase;
            RCVectorBase rightVector = right as RCVectorBase;

            if (leftVector == null || rightVector == null)
            {
                throw RCException.Overload(closure, op.Name, left, right);
            }
            runner.Yield(closure, VectorMath.InvokeDyadic(closure, op.Name, leftVector, rightVector));
        }
Exemple #8
0
 public static RCVector <L> DoAt <L> (RCClosure closure, RCVector <L> left, RCVector <byte> right)
 {
     L[] result = new L[right.Count];
     for (int i = 0; i < right.Count; ++i)
     {
         int j = right[i];
         if (j < 0 || j >= left.Count)
         {
             throw RCException.Range(closure, right[i], left.Count);
         }
         result[i] = left[j];
     }
     return((RCVector <L>)RCVectorBase.FromArray(new RCArray <L> (result)));
 }
Exemple #9
0
        public static RCValue InvokeSequential(RCClosure closure, string name, RCVectorBase right)
        {
            RCActivator.OverloadKey key = new RCActivator.OverloadKey(name,
                                                                      typeof(object),
                                                                      null,
                                                                      right.ScalarType);
            Overload overload;

            if (!_overloads.TryGetValue(key, out overload))
            {
                throw RCException.Overload(closure, name, right);
            }
            object array = overload.Invoke(right.Array);

            return(RCVectorBase.FromArray(array));
        }
Exemple #10
0
        public void EvalNetformat(RCRunner runner, RCClosure closure, RCString left, RCBlock right)
        {
            RCVectorBase[] columns = new RCVectorBase[right.Count];
            for (int i = 0; i < right.Count; ++i)
            {
                columns[i] = (RCVectorBase)right.Get(i);
                if (columns[i].Count != columns[0].Count)
                {
                    throw new Exception(string.Format(
                                            "All columns must have the same count. Expected: {0}, Actual: {1}",
                                            columns[0].Count,
                                            columns[i].Count));
                }
            }
            RCArray <object[]> formatParams = new RCArray <object[]> (columns[0].Count);
            RCArray <string>   result       = new RCArray <string> (columns[0].Count);

            for (int i = 0; i < columns[0].Count; ++i)
            {
                formatParams.Write(new object[right.Count]);
                for (int j = 0; j < right.Count; ++j)
                {
                    formatParams[i][j] = columns[j].Child(i);
                    RCTimeScalar?time = formatParams[i][j] as RCTimeScalar ?;
                    if (time.HasValue)
                    {
                        if (time.Value.Type == RCTimeType.Timespan)
                        {
                            throw new Exception(
                                      "netformat does not handle Timespans, please pass a specific date and time.");
                        }
                        formatParams[i][j] = new DateTime(time.Value.Ticks);
                    }
                }
            }
            for (int i = 0; i < formatParams.Count; ++i)
            {
                result.Write(string.Format(left[0], formatParams[i]));
            }
            runner.Yield(closure, new RCString(result));
        }
Exemple #11
0
 public static RCVector <L> DoRange <L> (RCVector <long> left, RCVector <L> right)
 {
     if (left.Count == 0)
     {
         return((RCVector <L>)RCVectorBase.FromArray(new RCArray <L> ()));
     }
     else if (left.Count == 1)
     {
         RCArray <L> result = new RCArray <L> ();
         for (int i = (int)left[0]; i < right.Count; ++i)
         {
             result.Write(right[i]);
         }
         return((RCVector <L>)RCVectorBase.FromArray(new RCArray <L> (result)));
     }
     else if (left.Count % 2 == 0)
     {
         RCArray <L> result = new RCArray <L> ();
         int         pair   = 0;
         while (pair < left.Count / 2)
         {
             int i = (int)left[2 * pair];
             int j = (int)left[2 * pair + 1];
             while (i <= j)
             {
                 result.Write(right[i]);
                 ++i;
             }
             ++pair;
         }
         return((RCVector <L>)RCVectorBase.FromArray(new RCArray <L> (result)));
     }
     else
     {
         throw new Exception("count of left argument must be 1 or an even number.");
     }
 }
Exemple #12
0
        public void EvalSort(RCRunner runner, RCClosure closure, RCSymbol left, RCBlock right)
        {
            SortDirection direction = ToDir(left);
            string        col       = (string)left[0].Part(1);
            RCVectorBase  column    = (RCVectorBase)right.Get(col);
            // It would be nice if there was an easy way to call one operator from another.
            // I tried to add one but found I would have to create a weird closure for this
            // purpose.
            // So I decided to wait and see and live with the switch statement for now.
            RCLong rank;

            switch (column.TypeCode)
            {
            case 'x':
                rank = new RCLong(RankUtils.DoRank <byte> (direction, (RCByte)column));
                break;

            case 'l':
                rank = new RCLong(RankUtils.DoRank <long> (direction, (RCLong)column));
                break;

            case 'd':
                rank = new RCLong(RankUtils.DoRank <double> (direction, (RCDouble)column));
                break;

            case 'm':
                rank = new RCLong(RankUtils.DoRank <decimal> (direction, (RCDecimal)column));
                break;

            case 's':
                rank = new RCLong(RankUtils.DoRank <string> (direction, (RCString)column));
                break;

            case 'b':
                rank = new RCLong(RankUtils.DoRank <bool> (direction, (RCBoolean)column));
                break;

            case 'y':
                rank = new RCLong(RankUtils.DoRank <RCSymbolScalar> (direction, (RCSymbol)column));
                break;

            case 't':
                rank = new RCLong(RankUtils.DoRank <RCTimeScalar> (direction, (RCTime)column));
                break;

            default:
                throw new Exception("Type:" + column.TypeCode + " is not supported by sort");
            }
            RCBlock result = RCBlock.Empty;

            for (int i = 0; i < right.Count; ++i)
            {
                RCBlock name = right.GetName(i);
                column = (RCVectorBase)name.Value;
                RCValue reordered;
                switch (column.TypeCode)
                {
                case 'x':
                    reordered = ReorderColumn <byte> (rank, (RCVector <byte>)column);
                    break;

                case 'l':
                    reordered = ReorderColumn <long> (rank, (RCVector <long>)column);
                    break;

                case 'd':
                    reordered = ReorderColumn <double> (rank, (RCVector <double>)column);
                    break;

                case 'm':
                    reordered = ReorderColumn <decimal> (rank, (RCVector <decimal>)column);
                    break;

                case 's':
                    reordered = ReorderColumn <string> (rank, (RCVector <string>)column);
                    break;

                case 'b':
                    reordered = ReorderColumn <bool> (rank, (RCVector <bool>)column);
                    break;

                case 'y':
                    reordered = ReorderColumn <RCSymbolScalar> (rank,
                                                                (RCVector <RCSymbolScalar>)column);
                    break;

                case 't':
                    reordered = ReorderColumn <RCTimeScalar> (rank,
                                                              (RCVector <RCTimeScalar>)column);
                    break;

                default:
                    throw new Exception("Type:" + column.TypeCode + " is not supported by sort");
                }
                result = new RCBlock(result, name.Name, ":", reordered);
            }
            runner.Yield(closure, result);
        }
Exemple #13
0
        protected void DoTree(TreeNode parent, RCBlock right, ref double a, ref double g)
        {
            for (int i = 0; i < right.Count; ++i)
            {
                RCBlock current   = right.GetName(i);
                object  shortName = current.Name;
                if (shortName.Equals(""))
                {
                    shortName = (long)i;
                }
                RCSymbolScalar s    = new RCSymbolScalar(parent.s, (long)i);
                RCSymbolScalar n    = new RCSymbolScalar(parent.k, shortName);
                TreeNode       node = new TreeNode(s, n);
                node.v        = current.Name;
                node.children = new RCArray <TreeNode> ();
                RCVectorBase vector = current.Value as RCVectorBase;
                if (vector != null)
                {
                    switch (vector.TypeCode)
                    {
                    case 'l': DoTree <long> (node,
                                             (RCVector <long>)vector,
                                             ref node.n,
                                             ref node.g,
                                             Areal,
                                             Formatl); break;

                    case 'd': DoTree <double> (node,
                                               (RCVector <double>)vector,
                                               ref node.n,
                                               ref node.g,
                                               Aread,
                                               Formatd); break;

                    case 'm': DoTree <decimal> (node,
                                                (RCVector <decimal>)vector,
                                                ref node.n,
                                                ref node.g,
                                                Aream,
                                                Formatm); break;

                    case 's': DoTree <string> (node,
                                               (RCVector <string>)vector,
                                               ref node.n,
                                               ref node.g,
                                               Areas,
                                               Formats); break;

                    case 'x': DoTree <byte> (node,
                                             (RCVector <byte>)vector,
                                             ref node.n,
                                             ref node.g,
                                             Areax,
                                             Formatx); break;

                    case 'y': DoTree <RCSymbolScalar> (node,
                                                       (RCVector <RCSymbolScalar>)vector,
                                                       ref node.n,
                                                       ref
                                                       node.g,
                                                       Areay,
                                                       Formaty); break;

                    case 'b': DoTree <bool> (node,
                                             (RCVector <bool>)vector,
                                             ref node.n,
                                             ref node.g,
                                             Areab,
                                             Formatb); break;

                    default: throw new Exception("Unknown typecode: " + vector.TypeCode);
                    }
                    a += node.n;
                    g += Math.Abs(node.g);
                    parent.children.Write(node);
                    continue;
                }
                RCBlock block = current.Value as RCBlock;
                if (block != null)
                {
                    DoTree(node, block, ref node.n, ref node.g);
                    a += node.n;
                    g += Math.Abs(node.g);
                    parent.children.Write(node);
                    continue;
                }
                RCCube cube = current.Value as RCCube;
                if (cube != null)
                {
                    DoTree(node, cube, ref node.n, ref node.g);
                    a += node.n;
                    g += Math.Abs(node.g);
                    parent.children.Write(node);
                    continue;
                }
                RCOperator oper = current.Value as RCOperator;
                if (oper != null)
                {
                    DoTree(node, oper, ref node.n, ref node.g);
                    a += node.n;
                    g += Math.Abs(node.g);
                    parent.children.Write(node);
                    continue;
                }
                RCReference reference = current.Value as RCReference;
                if (reference != null)
                {
                    DoTree(node, reference, ref node.n, ref node.g);
                    a += node.n;
                    g += Math.Abs(node.g);
                    parent.children.Write(node);
                    continue;
                }
            }
        }
Exemple #14
0
        public void EvalAppend(RCRunner runner, RCClosure closure, RCBlock right)
        {
            if (right.Count == 0)
            {
                runner.Yield(closure, RCBlock.Empty);
            }
            RCValue      first  = right.Get(0);
            RCVectorBase vector = first as RCVectorBase;

            if (vector != null)
            {
                RCVectorBase result;
                switch (vector.TypeCode)
                {
                case 'x': result = new RCByte(DoAppend <byte> (right)); break;

                case 'l': result = new RCLong(DoAppend <long> (right)); break;

                case 'd': result = new RCDouble(DoAppend <double> (right)); break;

                case 'm': result = new RCDecimal(DoAppend <decimal> (right)); break;

                case 's': result = new RCString(DoAppend <string> (right)); break;

                case 'b': result = new RCBoolean(DoAppend <bool> (right)); break;

                case 'y': result = new RCSymbol(DoAppend <RCSymbolScalar> (right)); break;

                case 't': result = new RCTime(DoAppend <RCTimeScalar> (right)); break;

                default: throw new Exception("Type:" + vector.TypeCode + " is not supported by sort");
                }
                runner.Yield(closure, result);
                return;
            }
            RCCube cube = first as RCCube;

            if (cube != null)
            {
                RCCube result = new RCCube(cube);
                for (int i = 1; i < right.Count; ++i)
                {
                    Writer writer = new Writer(result, null, true, true, 0);
                    writer.Write((RCCube)right.Get(i));
                }
                runner.Yield(closure, result);
                return;
            }
            // Individual values are now appended to the result just like the children of
            // blocks.
            {
                RCBlock result = RCBlock.Empty;
                for (int i = 0; i < right.Count; ++i)
                {
                    RCBlock top = right.GetName(i);
                    if (top.Value is RCBlock)
                    {
                        RCBlock list = (RCBlock)right.Get(i);
                        for (int j = 0; j < list.Count; ++j)
                        {
                            RCBlock item = list.GetName(j);
                            result = new RCBlock(result, item.Name, ":", item.Value);
                        }
                    }
                    else
                    {
                        result = new RCBlock(result, top.Name, ":", top.Value);
                    }
                }
                runner.Yield(closure, result);
            }
        }
Exemple #15
0
        protected void DoChart(RCCube result,
                               RCSymbolScalar parent,
                               ref long row,
                               long col,
                               RCBlock
                               right)
        {
            for (int i = 0; i < right.Count; ++i)
            {
                RCBlock current   = right.GetName(i);
                object  shortName = current.Name;
                if (shortName.Equals(""))
                {
                    shortName = (long)i;
                }
                RCSymbolScalar name = new RCSymbolScalar(parent, shortName);
                RCSymbolScalar cell = RCSymbolScalar.From(row, col, 0L);
                result.WriteCell("r", cell, row);
                result.WriteCell("c", cell, col);
                result.WriteCell("l", cell, 0L);
                result.WriteCell("k", cell, name);
                result.WriteCell("v", cell, shortName is long?shortName.ToString() : shortName);
                result.Axis.Write(cell);
                RCVectorBase vector = current.Value as RCVectorBase;
                if (vector != null)
                {
                    ++col;
                    switch (vector.TypeCode)
                    {
                    case 'l': DoChart <long> (result, name, row, col, (RCVector <long>)vector); break;

                    case 'd': DoChart <double> (result, name, row, col, (RCVector <double>)vector); break;

                    case 'm': DoChart <decimal> (result, name, row, col, (RCVector <decimal>)vector); break;

                    case 's': DoChart <string> (result, name, row, col, (RCVector <string>)vector); break;

                    case 'x': DoChart <byte> (result, name, row, col, (RCVector <byte>)vector); break;

                    case 'y': DoChart <RCSymbolScalar> (result,
                                                        name,
                                                        row,
                                                        col,
                                                        (RCVector <RCSymbolScalar>)vector); break;

                    case 'b': DoChart <bool> (result, name, row, col, (RCVector <bool>)vector); break;

                    default: throw new Exception("Unknown typecode: " + vector.TypeCode);
                    }
                    --col;
                    ++row;
                    continue;
                }
                RCBlock block = current.Value as RCBlock;
                if (block != null)
                {
                    ++col;
                    ++row;
                    DoChart(result, name, ref row, col, block);
                    --col;
                    continue;
                }
                RCOperator oper = current.Value as RCOperator;
                if (oper != null)
                {
                    ++col;
                    string val = oper.ToString();
                    cell = RCSymbolScalar.From(row, col, 0L);
                    result.WriteCell("r", cell, (long)row);
                    result.WriteCell("c", cell, (long)col);
                    result.WriteCell("l", cell, 0L);
                    result.WriteCell("k", cell, new RCSymbolScalar(name, 0L));
                    result.WriteCell("v", cell, val);
                    result.Write(cell);
                    ++row;
                    --col;
                    continue;
                }
                RCReference reference = current.Value as RCReference;
                if (reference != null)
                {
                    ++col;
                    string val = reference.ToString();
                    cell = RCSymbolScalar.From(row, col, 0L);
                    result.WriteCell("r", cell, (long)row);
                    result.WriteCell("c", cell, (long)col);
                    result.WriteCell("l", cell, 0L);
                    result.WriteCell("k", cell, new RCSymbolScalar(name, 0L));
                    result.WriteCell("v", cell, val);
                    result.Write(cell);
                    ++row;
                    --col;
                    continue;
                }
                RCCube cube = current.Value as RCCube;
                if (cube != null)
                {
                    ++col;
                    ++row;
                    DoChart(result, name, ref row, col, cube);
                    ++row;
                    --col;
                    continue;
                }
            }
        }