예제 #1
0
        protected virtual void DoSwitch <T> (RCRunner runner,
                                             RCClosure closure,
                                             RCVector <T> left,
                                             RCBlock right,
                                             Picker <T> picker)
        {
            int i = closure.Index - 2;

            if (i < left.Count)
            {
                bool    eval;
                RCValue code = picker(left[i], out eval);
                if (eval)
                {
                    RCClosure child = new RCClosure(closure,
                                                    closure.Bot,
                                                    code,
                                                    closure.Left,
                                                    RCBlock.Empty,
                                                    0);
                    code.Eval(runner, child);
                }
                else
                {
                    // In this case the "code" can be a value, normally part of a generated program
                    runner.Yield(closure, code);
                }
            }
            else
            {
                runner.Yield(closure, closure.Parent.Result);
            }
        }
예제 #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)));
 }
예제 #3
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);
            }
        }
예제 #4
0
        protected virtual T[] DoSort <T> (SortDirection direction, RCVector <T> vector)
        {
            Comparison <T> comparison = (Comparison <T>)_comparers[vector.TypeCode][direction];

            T[] array = vector.ToArray();
            Array.Sort(array, comparison);
            return(array);
        }
예제 #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];
     })));
 }
예제 #6
0
        protected RCArray <T> DoInter <T> (RCVector <T> left, RCVector <T> right)
        {
            HashSet <T> lhs = new HashSet <T> (left);
            HashSet <T> rsh = new HashSet <T> (right);

            lhs.IntersectWith(rsh);
            T[] array = new T[lhs.Count];
            lhs.CopyTo(array);
            return(new RCArray <T> (array));
        }
예제 #7
0
        protected RCArray <T> DoReverse <T> (RCVector <T> right)
        {
            RCArray <T> results = new RCArray <T> (right.Count);

            for (int i = right.Count - 1; i >= 0; --i)
            {
                results.Write(right[i]);
            }
            return(results);
        }
예제 #8
0
        protected RCArray <T> DoRepeat <T> (RCVector <long> left, RCVector <T> right)
        {
            long        count  = left[0];
            RCArray <T> result = new RCArray <T> ((int)count * right.Count);

            for (int i = 0; i < count; ++i)
            {
                result.Write(right.Data);
            }
            return(result);
        }
예제 #9
0
        protected RCArray <T> DoUnion <T> (RCVector <T> left, RCVector <T> right)
        {
            HashSet <T> results = new HashSet <T> (left);

            for (int i = 0; i < right.Count; ++i)
            {
                results.Add(right[i]);
            }
            T[] array = new T[results.Count];
            results.CopyTo(array);
            return(new RCArray <T> (array));
        }
예제 #10
0
        protected RCArray <T> DoAppend <T> (RCBlock right)
        {
            RCVector <T> current = (RCVector <T>)right.Get(0);
            RCArray <T>  result  = new RCArray <T> (current.Count * 3);

            result.Write(current.Data);
            for (int i = 1; i < right.Count; ++i)
            {
                current = (RCVector <T>)right.Get(i);
                result.Write(current.Data);
            }
            return(result);
        }
예제 #11
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)));
        }
예제 #12
0
        protected virtual RCArray <T> CoerceBlock <T> (RCBlock right)
        {
            RCArray <T> result = new RCArray <T> ();

            for (int i = 0; i < right.Count; ++i)
            {
                RCVector <T> value = (RCVector <T>)right.Get(i);
                for (int j = 0; j < value.Count; ++j)
                {
                    result.Write(value[j]);
                }
            }
            return(result);
        }
예제 #13
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)));
 }
예제 #14
0
        protected RCArray <T> DoAlternate <T> (RCVector <T> left, RCVector <T> right)
        {
            if (left.Count != right.Count)
            {
                throw new Exception("alternate requires equal length vectors");
            }
            RCArray <T> result = new RCArray <T> (left.Count * 2);

            for (int i = 0; i < left.Count; ++i)
            {
                result.Write(left[i]);
                result.Write(right[i]);
            }
            return(result);
        }
예제 #15
0
        protected RCArray <T> DoExcept <T> (RCVector <T> left, RCVector <T> right)
        {
            HashSet <T> results = new HashSet <T> (left);

            for (int i = 0; i < right.Count; ++i)
            {
                if (results.Contains(right[i]))
                {
                    results.Remove(right[i]);
                }
            }
            T[] array = new T[results.Count];
            results.CopyTo(array);
            return(new RCArray <T> (array));
        }
예제 #16
0
        protected RCArray <T> DoUnique <T> (RCVector <T> right)
        {
            RCArray <T> results = new RCArray <T> ();
            HashSet <T> items   = new HashSet <T> ();

            for (int i = 0; i < right.Count; ++i)
            {
                if (!items.Contains(right[i]))
                {
                    items.Add(right[i]);
                    results.Write(right[i]);
                }
            }
            return(results);
        }
예제 #17
0
        protected RCLong DoFind <T> (RCVector <T> things, RCVector <T> within)
            where T : IComparable <T>
        {
            RCArray <long> result = new RCArray <long> ();

            for (int i = 0; i < within.Count; ++i)
            {
                for (int j = 0; j < things.Count; ++j)
                {
                    if (things[j].CompareTo(within[i]) == 0)
                    {
                        result.Write(i);
                    }
                }
            }
            return(new RCLong(result));
        }
예제 #18
0
 protected void DoTree <T> (TreeNode parent,
                            RCVector <T> right,
                            ref double a,
                            ref double g,
                            Area <T> area,
                            Format <T> format)
 {
     for (int i = 0; i < right.Count; ++i)
     {
         TreeNode child = new TreeNode(parent, null, i);
         child.n = area(right[i]);
         child.m = child.n;
         child.g = Math.Abs(child.n);
         child.v = format(right[i]);
         a      += child.n;
         g      += child.g;
     }
 }
예제 #19
0
 protected void DoChart <T> (RCCube result,
                             RCSymbolScalar name,
                             long row,
                             long col,
                             RCVector <T> right)
 {
     for (int i = 0; i < right.Count; ++i)
     {
         string         val = right[i].ToString();
         RCSymbolScalar s   = RCSymbolScalar.From(row, (long)col + i, 0L);
         RCSymbolScalar k   = new RCSymbolScalar(name, (long)i);
         result.WriteCell("r", s, row);
         result.WriteCell("c", s, (long)col + i);
         result.WriteCell("l", s, 0L);
         result.WriteCell("k", s, k);
         result.WriteCell("v", s, val);
         result.Write(s);
     }
 }
예제 #20
0
        protected T[] DoAppend <T> (RCVector <T> left, RCVector <T> right)
        {
            // Why did I do it this way instead of using write on RCArray?
            // I think it came before that, should change it.
            int length = left.Count + right.Count;

            T[] result = new T[length];
            int i      = 0;

            for (; i < left.Count; ++i)
            {
                result[i] = left[i];
            }
            for (; i < length; ++i)
            {
                result[i] = right[i - left.Count];
            }
            return(result);
        }
예제 #21
0
        protected T[] DoShuffle <T> (Random random, RCVector <T> right)
        {
            // wikipedia discusses a variant of this algorithm that allows you to
            // initialize the array and shuffle it in a single operation.
            // It would be cool to implement that.
            T[] result = new T[right.Count];
            for (int i = 0; i < result.Length; ++i)
            {
                result[i] = right[i];
            }

            for (int i = result.Length - 1; i > 0; i--)
            {
                int n    = random.Next(i + 1);
                T   temp = result[i];
                result[i] = result[n];
                result[n] = temp;
            }
            return(result);
        }
예제 #22
0
        protected RCArray <T> DoDups <T> (RCVector <T> right)
        {
            RCArray <T> result = new RCArray <T> ();
            HashSet <T> items  = new HashSet <T> ();

            for (int i = 0; i < right.Count; ++i)
            {
                if (!items.Contains(right[i]))
                {
                    items.Add(right[i]);
                }
                else
                {
                    // Three or more instances of the dup
                    // will be represented two or more times
                    result.Write(right[i]);
                }
            }
            return(result);
        }
예제 #23
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.");
     }
 }
예제 #24
0
        protected RCArray <T> DoSub <T> (RCVector <T> left, RCVector <T> right)
        {
            Dictionary <T, T> map = new Dictionary <T, T> ();

            for (int i = 0; i < left.Count; ++i, ++i)
            {
                map.Add(left[i], left[i + 1]);
            }
            RCArray <T> result = new RCArray <T> (right.Count);

            for (int i = 0; i < right.Count; ++i)
            {
                T val;
                if (map.TryGetValue(right[i], out val))
                {
                    result.Write(val);
                }
                else
                {
                    result.Write(right[i]);
                }
            }
            return(result);
        }