コード例 #1
0
ファイル: Precision.cs プロジェクト: joshholmes/ironruby
        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
ファイル: Enumerable.cs プロジェクト: joshholmes/ironruby
 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
ファイル: Enumerable.cs プロジェクト: TerabyteX/main
 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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
 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
ファイル: Enumerable.cs プロジェクト: TerabyteX/main
        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
ファイル: Enumerable.cs プロジェクト: TerabyteX/main
        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
ファイル: ModuleOps.cs プロジェクト: yarrow2/ironruby
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: rafacv/iron_languages
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Integer.cs プロジェクト: parhelia512/ironruby
 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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerator.cs プロジェクト: tnachen/ironruby
        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
ファイル: ClrStringOps.cs プロジェクト: atczyc/ironruby
 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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
 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
ファイル: Enumerator.cs プロジェクト: tnachen/ironruby
 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
ファイル: Enumerator.cs プロジェクト: tnachen/ironruby
 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
ファイル: IoOps.cs プロジェクト: ghouston/ironlanguages
        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
ファイル: Enumerable.cs プロジェクト: ltwlf/IronSP
 public static object Reject(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, object self)
 {
     return(Filter(each, predicate, self, false));
 }
コード例 #40
0
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: ltwlf/IronSP
 public static object TrueForAny(CallSiteStorage <EachSite> /*!*/ each, BlockParam predicate, object self)
 {
     return(TrueForItems(each, predicate, self, false));
 }
コード例 #42
0
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
 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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
 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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
 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
ファイル: Enumerable.cs プロジェクト: Hank923/ironruby
        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);
        }