示例#1
0
        public static object PrecInteger(
            CallSiteStorage<Func<CallSite, RubyContext, object, RubyClass, object>>/*!*/ precStorage,
            RubyContext/*!*/ context, object self) {

            var prec = precStorage.GetCallSite("prec", 1);
            return prec.Target(prec, context, self, context.GetClass(typeof(Integer)));
        }
示例#2
0
 private static object Each(CallSiteStorage<EachSite>/*!*/ each, RubyContext/*!*/ context, object self, Proc/*!*/ block) {
     var enumerator = self as Enumerator;
     if (enumerator != null) {
         return enumerator.Each(context, block);
     } else {
         var site = each.GetCallSite("each", RubyCallSignature.WithBlock(0));
         return site.Target(site, context, self, block);
     }
 }
示例#3
0
 public static int Count(CallSiteStorage<EachSite>/*!*/ each, object self)
 {
     int result = 0;
     Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
         result++;
         return null;
     }));
     return result;
 }
示例#4
0
 public static RubyArray Map(CallSiteStorage<EachSite>/*!*/ each, BlockParam collector, object self) {
     RubyArray result = new RubyArray();
     Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
         if (collector != null) {
             if (collector.Yield(item, out item)) {
                 return item;
             }
         }
         result.Add(item);
         return null;
     }));
     return result;
 }
示例#5
0
        public static object Contains(CallSiteStorage<EachSite>/*!*/ each, BinaryOpStorage/*!*/ equals, object self, object value)
        {
            object result = ScriptingRuntimeHelpers.BooleanToObject(false);

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (Protocols.IsEqual(equals, item, value)) {
                    result = ScriptingRuntimeHelpers.BooleanToObject(true);
                    return selfBlock.Break(result);
                }
                return null;
            }));

            return result;
        }
示例#6
0
        public static int Count(CallSiteStorage<EachSite>/*!*/ each, BinaryOpStorage/*!*/ equals, BlockParam comparer, object self, object value)
        {
            if (comparer != null) {
                each.Context.ReportWarning("given block not used");
            }

            int result = 0;
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (Protocols.IsEqual(equals, item, value)) {
                    result++;
                }
                return null;
            }));
            return result;
        }
示例#7
0
        public static RubyModule/*!*/ Include(
            CallSiteStorage<Func<CallSite, RubyModule, RubyModule, object>>/*!*/ appendFeaturesStorage,
            CallSiteStorage<Func<CallSite, RubyModule, RubyModule, object>>/*!*/ includedStorage,
            RubyModule/*!*/ self, [NotNullItems]params RubyModule/*!*/[]/*!*/ modules) {

            RubyUtils.RequireMixins(self, modules);

            var appendFeatures = appendFeaturesStorage.GetCallSite("append_features", 1);
            var included = includedStorage.GetCallSite("included", 1);

            // Kernel#append_features inserts the module at the beginning of ancestors list;
            // ancestors after include: [modules[0], modules[1], ..., modules[N-1], self, ...]
            for (int i = modules.Length - 1; i >= 0; i--) {
                appendFeatures.Target(appendFeatures, modules[i], self);
                included.Target(included, modules[i], self);
            }

            return self;
        }
示例#8
0
        private static object TrueForItems(CallSiteStorage<EachSite>/*!*/ each, BlockParam predicate, object self, bool expected) {
            bool result = expected;
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (predicate != null) {
                    if (predicate.Yield(item, out item)) {
                        return item;
                    }
                }

                bool isTrue = Protocols.IsTrue(item);
                if (isTrue != result) {
                    result = isTrue;
                    return selfBlock.Break(ScriptingRuntimeHelpers.BooleanToObject(isTrue));
                }

                return null;
            }));

            return ScriptingRuntimeHelpers.BooleanToObject(result);
        }
示例#9
0
        private static object TrueForItems(CallSiteStorage<EachSite>/*!*/ each, BlockParam predicate, object self, bool stop, bool positiveResult) {
            object result = ScriptingRuntimeHelpers.BooleanToObject(!positiveResult);
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (predicate != null) {
                    object blockResult;
                    if (predicate.Yield(item, out blockResult)) {
                        result = blockResult;
                        return selfBlock.PropagateFlow(predicate, blockResult);
                    }
                    item = blockResult;
                }

                bool isTrue = Protocols.IsTrue(item);
                if (isTrue == stop) {
                    result = ScriptingRuntimeHelpers.BooleanToObject(positiveResult); 
                    return selfBlock.Break(result);
                }

                return null;
            }));

            return result;
        }
示例#10
0
        public static RubyArray/*!*/ Partition(CallSiteStorage<EachSite>/*!*/ each, BlockParam predicate, object self) {
            RubyArray trueSet = new RubyArray();
            RubyArray falseSet = new RubyArray();

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (predicate == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                object blockResult;
                if (predicate.Yield(item, out blockResult)) {
                    return blockResult;
                }

                if (Protocols.IsTrue(blockResult)) {
                    trueSet.Add(item);
                } else {
                    falseSet.Add(item);
                }

                return null;
            }));

            RubyArray pair = new RubyArray(2);
            pair.Add(trueSet);
            pair.Add(falseSet);
            return pair;
        }
示例#11
0
        public static object Inject(CallSiteStorage<EachSite>/*!*/ each, BlockParam operation, object self, [Optional]object initial) {

            object result = initial;
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (result == Missing.Value) {
                    result = item;
                    return null;
                }

                if (operation == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (operation.Yield(result, item, out result)) {
                    return result;
                }

                return null;
            }));

            return result != Missing.Value ? result : null;
        }
示例#12
0
        private static object GetExtreme(
            CallSiteStorage<EachSite>/*!*/ each, 
            BinaryOpStorage/*!*/ compareStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            BlockParam comparer, object self, int comparisonValue) {

            bool firstItem = true;
            object result = null;

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                // Check for first element
                if (firstItem) {
                    result = item;
                    firstItem = false;
                    return null;
                }

                int compareResult;
                if (comparer != null) {
                    object blockResult;
                    if (comparer.Yield(result, item, out blockResult)) {
                        return blockResult;
                    }

                    if (blockResult == null) {
                        throw RubyExceptions.MakeComparisonError(selfBlock.RubyContext, result, item);
                    }

                    compareResult = Protocols.ConvertCompareResult(lessThanStorage, greaterThanStorage, blockResult);
                } else {
                    compareResult = Protocols.Compare(compareStorage, lessThanStorage, greaterThanStorage, result, item);
                }

                // Check if we have found the new minimum or maximum (-1 to select max, 1 to select min)
                if (compareResult == comparisonValue) {
                    result = item;
                }

                return null;
            }));
            return result;
        }
示例#13
0
        internal static object Each(CallSiteStorage <EachSiteN> /*!*/ each, object self, IList /*!*/ args, Proc /*!*/ block)
        {
            var site = each.GetCallSite("each", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock | RubyCallFlags.HasSplattedArgument));

            return(site.Target(site, self, block, args));
        }
示例#14
0
        public static RubyArray Grep(CallSiteStorage<EachSite>/*!*/ each, BinaryOpStorage/*!*/ caseEquals, 
            BlockParam action, object self, object pattern) {

            RubyArray result = new RubyArray();
            var site = caseEquals.GetCallSite("===");

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (RubyOps.IsTrue(site.Target(site, pattern, item))) {
                    if (action != null) {
                        if (action.Yield(item, out item)) {
                            return item;
                        }
                    }
                    result.Add(item);
                }
                return null;
            }));

            return result;
        }
示例#15
0
 public static Enumerator /*!*/ GetEachWithIndexEnumerator(CallSiteStorage <EachSite> /*!*/ each, BlockParam block, object self)
 {
     return(new Enumerator((_, innerBlock) => EachWithIndex(each, innerBlock, self)));
 }
示例#16
0
        internal static object Each(CallSiteStorage <EachSite> /*!*/ each, object self, Proc /*!*/ block)
        {
            var site = each.GetCallSite("each", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock));

            return(site.Target(site, self, block));
        }
示例#17
0
        public static RubyArray/*!*/ SortBy(
            CallSiteStorage<EachSite>/*!*/ each, 
            BinaryOpStorage/*!*/ comparisonStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            BlockParam keySelector, object self) {

            // collect key, value pairs
            List<KeyValuePair<object, object>> keyValuePairs = new List<KeyValuePair<object, object>>();

            // Collect the key, value pairs
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (keySelector == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                object key;
                if (keySelector.Yield(item, out key)) {
                    return key;
                }

                keyValuePairs.Add(new KeyValuePair<object, object>(key, item));
                return null;
            }));

            // sort by keys
            keyValuePairs.Sort(delegate(KeyValuePair<object, object> x, KeyValuePair<object, object> y) {
                return Protocols.Compare(comparisonStorage, lessThanStorage, greaterThanStorage, x.Key, y.Key);
            });

            // return values
            RubyArray result = new RubyArray(keyValuePairs.Count);
            foreach (KeyValuePair<object, object> pair in keyValuePairs) {
                result.Add(pair.Value);
            }

            return result;
        }
示例#18
0
 public static Enumerator /*!*/ GetGroupByEnumerator(CallSiteStorage <EachSite> /*!*/ each, BlockParam /*!*/ predicate, object self)
 {
     return(new Enumerator((_, block) => GroupBy(each, block, self)));
 }
示例#19
0
        public static object PrecFloat(CallSiteStorage <Func <CallSite, object, RubyClass, object> > /*!*/ precStorage, object self)
        {
            var prec = precStorage.GetCallSite("prec", 1);

            return(prec.Target(prec, self, precStorage.Context.GetClass(typeof(double))));
        }
示例#20
0
 public static object ToRational(CallSiteStorage <Func <CallSite, object, object, object, object> > /*!*/ toRational, RubyScope /*!*/ scope, object /*!*/ self)
 {
     // TODO: reimplement Rational
     return(KernelOps.ToRational(toRational, scope, self, self, ClrInteger.One));
 }
示例#21
0
 public static object Sort(CallSiteStorage <EachSite> /*!*/ each, ComparisonStorage /*!*/ comparisonStorage, BlockParam keySelector, object self)
 {
     return(ArrayOps.SortInPlace(comparisonStorage, keySelector, ToArray(each, self)));
 }
示例#22
0
        public static object GetExtremes(CallSiteStorage <EachSite> /*!*/ each, ComparisonStorage /*!*/ comparisonStorage, BlockParam comparer, object self)
        {
            bool   hasOddItem = false, hasMinMax = false, blockJumped = false;
            object oddItem     = null;
            object blockResult = null;

            object min = null, max = null;

            Func <IronRuby.Runtime.BlockParam, object, object, object> blockProc = delegate(BlockParam /*!*/ selfBlock, object _, object item) {
                if (hasOddItem)
                {
                    hasOddItem = false;

                    int?compareResult = CompareItems(comparisonStorage, oddItem, item, comparer, out blockResult);
                    if (compareResult == null)
                    {
                        goto BlockJumped;
                    }
                    if (compareResult > 0)
                    {
                        // oddItem > item
                        object obj = item;
                        item    = oddItem;
                        oddItem = obj;
                    }

                    // oddItem <= item
                    if (hasMinMax)
                    {
                        compareResult = CompareItems(comparisonStorage, oddItem, min, comparer, out blockResult);
                        if (compareResult == null)
                        {
                            goto BlockJumped;
                        }
                        if (compareResult < 0)
                        {
                            // oddItem < min
                            min = oddItem;
                        }

                        compareResult = CompareItems(comparisonStorage, item, max, comparer, out blockResult);
                        if (compareResult == null)
                        {
                            goto BlockJumped;
                        }
                        if (compareResult > 0)
                        {
                            // item > max
                            max = item;
                        }
                    }
                    else
                    {
                        min       = oddItem;
                        max       = item;
                        hasMinMax = true;
                    }
                }
                else
                {
                    hasOddItem = true;
                    oddItem    = item;
                }

                return(null);

BlockJumped:
                blockJumped = true;
                return(selfBlock.PropagateFlow(comparer, blockResult));
            };

            Each(each, self, Proc.Create(each.Context, blockProc));

            if (blockJumped)
            {
                return(blockResult);
            }

            if (!hasMinMax)
            {
                return(hasOddItem ? new RubyArray(2)
                {
                    oddItem, oddItem
                } : new RubyArray(2)
                {
                    null, null
                });
            }

            if (hasOddItem)
            {
                int?compareResult = CompareItems(comparisonStorage, oddItem, min, comparer, out blockResult);
                if (compareResult == null)
                {
                    return(blockResult);
                }
                if (compareResult < 0)
                {
                    min = oddItem;
                }

                compareResult = CompareItems(comparisonStorage, oddItem, max, comparer, out blockResult);
                if (compareResult == null)
                {
                    return(blockResult);
                }
                if (compareResult > 0)
                {
                    max = oddItem;
                }
            }

            return(new RubyArray(2)
            {
                min, max
            });
        }
示例#23
0
 public static object GetMinimum(CallSiteStorage <EachSite> /*!*/ each, ComparisonStorage /*!*/ comparisonStorage, BlockParam comparer, object self)
 {
     return(GetExtreme(each, comparisonStorage, comparer, self, -1));
 }
示例#24
0
 public static object Inject(CallSiteStorage <EachSite> /*!*/ each, RubyScope /*!*/ scope, object self, [Optional] object initial,
                             [DefaultProtocol, NotNull] string /*!*/ operatorName)
 {
     return(Inject(each, null, scope, operatorName, self, initial));
 }
示例#25
0
 public static object Inject(CallSiteStorage <EachSite> /*!*/ each, [NotNull] BlockParam /*!*/ operation, object self, [Optional] object initial)
 {
     return(Inject(each, operation, null, null, self, initial));
 }
示例#26
0
        public static RubyArray/*!*/ Zip(CallSiteStorage<EachSite>/*!*/ each, ConversionStorage<IList>/*!*/ tryToA, BlockParam block,
            object self, [NotNull]params object[] args) {
            RubyArray results = (block == null) ? new RubyArray() : null;

            // Call to_a on each argument
            IList[] otherArrays = new IList[args.Length];
            for (int i = 0; i < args.Length; i++) {
                otherArrays[i] = Protocols.TryConvertToArray(tryToA, args[i]);
            }

            int index = 0;
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                // Collect items
                RubyArray array = new RubyArray(otherArrays.Length + 1);
                array.Add(item);
                foreach (IList otherArray in otherArrays) {
                    if (index < otherArray.Count) {
                        array.Add(otherArray[index]);
                    } else {
                        array.Add(null);
                    }
                }

                index += 1;

                if (block != null) {
                    object blockResult;
                    if (block.Yield(array, out blockResult)) {
                        return blockResult;
                    }
                } else {
                    results.Add(array);
                }
                return null;
            }));

            return results;
        }
示例#27
0
        private static object EachSlice(CallSiteStorage<EachSite>/*!*/ each, RubyContext/*!*/ context, BlockParam/*!*/ block, object self, int sliceSize, 
            bool includeIncomplete, Func<RubyArray/*!*/, RubyArray>/*!*/ newSliceFactory) {

            if (sliceSize <= 0) {
                throw RubyExceptions.CreateArgumentError("invalid slice size");
            }

            RubyArray slice = null;

            EnumerableModule.Each(each, context, self, Proc.Create(context, delegate(BlockParam/*!*/ selfBlock, object item) {
                if (slice == null) {
                    slice = new RubyArray(sliceSize);
                }
                
                slice.Add(item);
                
                if (slice.Count == sliceSize) {
                    if (block == null) {
                        throw RubyExceptions.NoBlockGiven();
                    }

                    var completeSlice = slice;
                    slice = newSliceFactory(slice);

                    object blockResult;
                    if (block.Yield(completeSlice, out blockResult)) {
                        return blockResult;
                    }
                }

                return null;
            }));

            if (slice != null && includeIncomplete) {
                if (block == null) {
                    throw RubyExceptions.NoBlockGiven();
                } 

                object blockResult;
                if (block.Yield(slice, out blockResult)) {
                    return blockResult;
                }
            }

            return null;
        }
示例#28
0
 public static object Select(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, RubyStruct /*!*/ self)
 {
     return(Enumerable.Select(each, predicate, self));
 }
示例#29
0
 public static object Match(CallSiteStorage<Func<CallSite, RubyScope, object, string, object>>/*!*/ storage,
     RubyScope/*!*/ scope, string/*!*/ self, object obj) {
     var site = storage.GetCallSite("=~", new RubyCallSignature(1, RubyCallFlags.HasScope | RubyCallFlags.HasImplicitSelf));
     return site.Target(site, scope, obj, self);
 }
示例#30
0
 public static object Select(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, object self)
 {
     return((predicate != null) ? FilterImpl(each, predicate, self, true) : FilterEnum(each, predicate, self, true));
 }
示例#31
0
        public static object EachWithIndex(CallSiteStorage<EachSite>/*!*/ each, BlockParam/*!*/ block, object self) {
            // for some reason each_with_index always checks for a block, even if there's nothing to yield
            if (block == null) {
                throw RubyExceptions.NoBlockGiven();
            }

            int index = 0;

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                object blockResult;
                if (block.Yield(item, index, out blockResult)) {
                    return blockResult;
                }
                index += 1;
                return null;
            }));

            return self;
        }
示例#32
0
 public static RubyArray/*!*/ Reject(CallSiteStorage<EachSite>/*!*/ each, BlockParam predicate, object self) {
     return Filter(each, predicate, self, false);
 }
示例#33
0
 private static Enumerator /*!*/ FilterEnum(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, object self, bool acceptingValue)
 {
     return(new Enumerator((_, block) => FilterImpl(each, block, self, acceptingValue)));
 }
示例#34
0
 public static object EachSlice(CallSiteStorage<EachSite>/*!*/ each, RubyContext/*!*/ context, BlockParam/*!*/ block, object self, [DefaultProtocol]int sliceSize) {
     return EachSlice(each, context, block, self, sliceSize, true, (slice) => null);
 }
示例#35
0
 public static object /*!*/ New(CallSiteStorage <Func <CallSite, object, object, object> > /*!*/ storage,
                                TypeGroup /*!*/ self, params object[] /*!*/ args)
 {
     return(New("new", storage, self, args));
 }
示例#36
0
 public static object EachCons(CallSiteStorage<EachSite>/*!*/ each, RubyContext/*!*/ context, BlockParam/*!*/ block, object self, [DefaultProtocol]int sliceSize) {
     return EachSlice(each, context, block, self, sliceSize, false, (slice) => {
         RubyArray newSlice = new RubyArray(slice.Count);
         for (int i = 1; i < slice.Count; i++) {
             newSlice.Add(slice[i]);
         }
         return newSlice;
     });
 }
示例#37
0
 public static object ClrNew(CallSiteStorage <Func <CallSite, object, object, object, object> > /*!*/ storage, BlockParam block,
                             TypeGroup /*!*/ self, params object[] /*!*/ args)
 {
     return(New("clr_new", storage, block, self, args));
 }
示例#38
0
        public static object CopyStream(
            ConversionStorage<MutableString>/*!*/ toPath, ConversionStorage<int>/*!*/ toInt, RespondToStorage/*!*/ respondTo,
            BinaryOpStorage/*!*/ writeStorage, CallSiteStorage<Func<CallSite, object, object, object, object>>/*!*/ readStorage,
            RubyClass/*!*/ self, object src, object dst, [DefaultParameterValue(-1)]int count, [DefaultParameterValue(-1)]int src_offset) {

            if (count < -1) {
                throw RubyExceptions.CreateArgumentError("count should be >= -1");
            }

            if (src_offset < -1) {
                throw RubyExceptions.CreateArgumentError("src_offset should be >= -1");
            }

            RubyIO srcIO = src as RubyIO;
            RubyIO dstIO = dst as RubyIO;
            Stream srcStream = null, dstStream = null;
            var context = toPath.Context;
            CallSite<Func<CallSite, object, object, object>> writeSite = null;
            CallSite<Func<CallSite, object, object, object, object>> readSite = null;

            try {
                if (srcIO == null || dstIO == null) {
                    var toPathSite = toPath.GetSite(TryConvertToPathAction.Make(toPath.Context));
                    var srcPath = toPathSite.Target(toPathSite, src);
                    if (srcPath != null) {
                        srcStream = new FileStream(context.DecodePath(srcPath), FileMode.Open, FileAccess.Read);
                    } else {
                        readSite = readStorage.GetCallSite("read", 2);
                    }

                    var dstPath = toPathSite.Target(toPathSite, dst);
                    if (dstPath != null) {
                        dstStream = new FileStream(context.DecodePath(dstPath), FileMode.Truncate);
                    } else {
                        writeSite = writeStorage.GetCallSite("write", 1);
                    }
                } else {
                    srcStream = srcIO.GetReadableStream();
                    dstStream = dstIO.GetWritableStream();
                }

                if (src_offset != -1) {
                    if (srcStream == null) {
                        throw RubyExceptions.CreateArgumentError("cannot specify src_offset for non-IO");
                    }
                    srcStream.Seek(src_offset, SeekOrigin.Current);
                }

                MutableString userBuffer = null;
                byte[] buffer = null;

                long bytesCopied = 0;
                long remaining = (count < 0) ? Int64.MaxValue : count;
                int minBufferSize = 16 * 1024;
                
                if (srcStream != null) {
                    buffer = new byte[Math.Min(minBufferSize, remaining)];
                }

                while (remaining > 0) {
                    int bytesRead;
                    int chunkSize = (int)Math.Min(minBufferSize, remaining);
                    if (srcStream != null) {
                        userBuffer = null;
                        bytesRead = srcStream.Read(buffer, 0, chunkSize);
                    } else {
                        userBuffer = MutableString.CreateBinary();
                        bytesRead = Protocols.CastToFixnum(toInt, readSite.Target(readSite, src, chunkSize, userBuffer));
                    }
                    
                    if (bytesRead <= 0) {
                        break;
                    }

                    if (dstStream != null) {
                        if (userBuffer != null) {
                            dstStream.Write(userBuffer, 0, bytesRead);
                        } else {
                            dstStream.Write(buffer, 0, bytesRead);
                        }
                    } else {
                        if (userBuffer == null) {
                            userBuffer = MutableString.CreateBinary(bytesRead).Append(buffer, 0, bytesRead);
                        } else {
                            userBuffer.SetByteCount(bytesRead);
                        }
                        writeSite.Target(writeSite, dst, userBuffer);
                    }
                    bytesCopied += bytesRead;
                    remaining -= bytesRead;
                }
                return Protocols.Normalize(bytesCopied);

            } finally {
                if (srcStream != null) {
                    srcStream.Close();
                }
                if (dstStream != null) {
                    dstStream.Close();
                }
            }
        }
示例#39
0
 public static object Reject(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, object self)
 {
     return(Filter(each, predicate, self, false));
 }
示例#40
0
        public static RubyArray/*!*/ ToArray(CallSiteStorage<EachSite>/*!*/ each, object self) {
            RubyArray data = new RubyArray();

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                data.Add(item);
                return null;
            }));

            return data;
        }
示例#41
0
 public static object TrueForAny(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, object self)
 {
     return(TrueForItems(each, predicate, self, false));
 }
示例#42
0
        private static RubyArray/*!*/ Filter(CallSiteStorage<EachSite>/*!*/ each, BlockParam predicate, object self, bool acceptingValue) {
            RubyArray result = new RubyArray();

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (predicate == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                object blockResult;
                if (predicate.Yield(item, out blockResult)) {
                    return blockResult;
                }

                // Check if the result is what we expect (use true to select, false to reject)
                if (Protocols.IsTrue(blockResult) == acceptingValue) {
                    result.Add(item);
                }
                return null;
            }));

            return result;
        }
示例#43
0
 public static Enumerator /*!*/ GetMapEnumerator(CallSiteStorage <EachSite> /*!*/ each, BlockParam collector, object self)
 {
     return(new Enumerator((_, block) => Map(each, block, self)));
 }
示例#44
0
        public static bool Contains(CallSiteStorage<EachSite>/*!*/ each, BinaryOpStorage/*!*/ equals, object self, object value) {
            bool result = false;

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (Protocols.IsEqual(equals, item, value)) {
                    result = true;
                    return selfBlock.Break(result);
                }
                return null;
            }));

            return result;
        }
示例#45
0
 public static Enumerator /*!*/ GetDropWhileEnumerator(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, object self)
 {
     return(new Enumerator((_, block) => DropWhile(each, block, self)));
 }
示例#46
0
 public static object GetMinimum(
     CallSiteStorage<EachSite>/*!*/ each, 
     BinaryOpStorage/*!*/ compareStorage,
     BinaryOpStorage/*!*/ lessThanStorage,
     BinaryOpStorage/*!*/ greaterThanStorage,
     BlockParam comparer, object self) {
     return GetExtreme(each, compareStorage, lessThanStorage, greaterThanStorage, comparer, self, 1/*look for min*/);
 }
示例#47
0
 public static Enumerator /*!*/ GetCycleEnumerator(CallSiteStorage <EachSite> /*!*/ each, BlockParam block, object self,
                                                   [DefaultProtocol, DefaultParameterValue(Int32.MaxValue)] int iterations)
 {
     return(new Enumerator((_, innerBlock) => Cycle(each, innerBlock, self, iterations)));
 }
示例#48
0
 internal static object Each(CallSiteStorage<EachSite>/*!*/ each, object self, Proc/*!*/ block) {
     var site = each.GetCallSite("each", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock));
     return site.Target(site, self, block);
 }
示例#49
0
 public static object Cycle(CallSiteStorage <EachSite> /*!*/ each, BlockParam block, object self, DynamicNull iterations)
 {
     return((block != null) ? Cycle(each, block, self, Int32.MaxValue) : GetCycleEnumerator(each, block, self, Int32.MaxValue));
 }
示例#50
0
        public static object Sort(
            CallSiteStorage<EachSite>/*!*/ each, 
            BinaryOpStorage/*!*/ comparisonStorage,
            BinaryOpStorage/*!*/ lessThanStorage,
            BinaryOpStorage/*!*/ greaterThanStorage,
            BlockParam keySelector, object self) {

            return ArrayOps.SortInPlace(comparisonStorage, lessThanStorage, greaterThanStorage, keySelector, ToArray(each, self));
        }
示例#51
0
 public static Enumerator /*!*/ GetEachSliceEnumerator(CallSiteStorage <EachSite> /*!*/ each, BlockParam block, object self, [DefaultProtocol] int sliceSize)
 {
     return(new Enumerator((_, innerBlock) => EachSlice(each, innerBlock, self, sliceSize)));
 }
示例#52
0
 public static object TrueForAny(CallSiteStorage<EachSite>/*!*/ each, BlockParam predicate, object self) {
     return TrueForItems(each, predicate, self, false);
 }
示例#53
0
 public static object EachSlice(CallSiteStorage <EachSite> /*!*/ each, [NotNull] BlockParam /*!*/ block, object self, [DefaultProtocol] int sliceSize)
 {
     return(EachSlice(each, block, self, sliceSize, true, (slice) => null));
 }
示例#54
0
 public static Enumerator /*!*/ GetFindEnumerator(CallSiteStorage <EachSite> /*!*/ each, CallSiteStorage <Func <CallSite, object, object> > /*!*/ callStorage,
                                                  BlockParam predicate, object self, [Optional] object ifNone)
 {
     return(new Enumerator((_, block) => Find(each, callStorage, block, self, ifNone)));
 }
示例#55
0
 public static Enumerator /*!*/ GetFindIndexEnumerator(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, object self)
 {
     return(new Enumerator((_, block) => FindIndex(each, block, self)));
 }
示例#56
0
        public static object Find(CallSiteStorage<EachSite>/*!*/ each, CallSiteStorage<Func<CallSite, object, object>>/*!*/ callStorage,
            BlockParam predicate, object self, [Optional]object ifNone) {
            object result = Missing.Value;

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (predicate == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                object blockResult;
                if (predicate.Yield(item, out blockResult)) {
                    result = blockResult;
                    return selfBlock.Break(blockResult);
                }

                if (Protocols.IsTrue(blockResult)) {
                    result = item;
                    return selfBlock.Break(item);
                }

                return null;
            }));

            if (result == Missing.Value) {
                if (ifNone == Missing.Value || ifNone == null) {
                    return null;
                }

                var site = callStorage.GetCallSite("call", 0);
                result = site.Target(site, ifNone);
            }
            return result;
        }
示例#57
0
        public static object Zip(CallSiteStorage <EachSite> /*!*/ each, ConversionStorage <IList> /*!*/ tryToAry, CallSiteStorage <EachSite> /*!*/ otherEach, BlockParam block, object self, [DefaultProtocol, NotNullItems] params object /*!*/[] /*!*/ args)
        {
            RubyArray results = (block == null) ? new RubyArray() : null;
            object    result  = results;

            // coerce the args into arrays
            var coercedArgs = new List <IList>(args.Length);

            foreach (var otherArrayObject in args)
            {
                IList otherArray = Protocols.TryCastToArray(tryToAry, otherArrayObject);
                if (otherArray != null)
                {
                    coercedArgs.Add(otherArray);
                }
                else     // added in MRI 1.9.2 - if to_ary fails, try call .each to extract values
                {
                    otherArray = new List <object>();
                    Each(otherEach, otherArrayObject, Proc.Create(otherEach.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) {
                        otherArray.Add(item);
                        return(null);
                    }));
                    coercedArgs.Add(otherArray);
                }
            }


            int index = 0;

            Each(each, self, Proc.Create(each.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) {
                // Collect items
                RubyArray array = new RubyArray(args.Length + 1);
                array.Add(item);
                foreach (var otherArray in coercedArgs)
                {
                    if (index < otherArray.Count)
                    {
                        array.Add(otherArray[index]);
                    }
                    else
                    {
                        array.Add(null);
                    }
                }

                index += 1;

                if (block != null)
                {
                    object blockResult;
                    if (block.Yield(array, out blockResult))
                    {
                        result = blockResult;
                        return(selfBlock.PropagateFlow(block, blockResult));
                    }
                }
                else
                {
                    results.Add(array);
                }
                return(null);
            }));

            return(result);
        }