<commit_before>﻿// Copyright (c) 2013, 2014 <NAME>
// See the file COPYING for license details.

using System;
using System.Collections;

namespace x2
{
    // x2 Coroutine and YieldInstructions will not work in MultiThreadedFlow!

    public abstract class YieldInstruction : IEnumerator
    {
        // Alias of MoveNext()
        public bool Continue()
        {
            return MoveNext();
        }

        // IEnumerator interface implementation
        public abstract object Current { get; }
        public abstract bool MoveNext();
        public void Reset() { }  // not supported
    }

    public class Coroutine : YieldInstruction
    {
        private IEnumerator routine;
        private bool running;
        private bool started;

        public object Context { get; set; }

        public override object Current
        {
            get { return (running ? routine.Current : null); }
        }

        public Coroutine()
        {
        }

        public IEnumerator Start(IEnumerator routine)
        {
            this.routine = routine;
            running = (routine != null);
            MoveNext();
            return this;
        }

        public override bool MoveNext()
        {
            if (!started)
            {
                started = true;
                return routine.MoveNext();
            }

            if (running)
            {
                if (routine.Current.GetType().IsSubclassOf(typeof(Coroutine)))
                {
                    Coroutine coroutine = (Coroutine)routine.Current;
                    if (coroutine.running)
                    {
                        return true;
                    }

                }
                else if (routine.Current.GetType().IsSubclassOf(typeof(YieldInstruction)))
                {
                    YieldInstruction instruction = (YieldInstruction)routine.Current;
                    if (instruction.Continue())
                    {
                        return true;
                    }
                }
                if (routine.MoveNext())
                {
                    return true;
                }
                running = false;
            }
            return false;
        }
    }
}
<commit_msg>Coroutine: added static Start() shorthands<commit_after>﻿// Copyright (c) 2013, 2014 <NAME>
// See the file COPYING for license details.

using System;
using System.Collections;

namespace x2
{
    // x2 Coroutine and YieldInstructions will not work in MultiThreadedFlow!

    public abstract class YieldInstruction : IEnumerator
    {
        // Alias of MoveNext()
        public bool Continue()
        {
            return MoveNext();
        }

        // IEnumerator interface implementation
        public abstract object Current { get; }
        public abstract bool MoveNext();
        public void Reset() { }  // not supported
    }

    public class Coroutine : YieldInstruction
    {
        private IEnumerator routine;
        private bool running;
        private bool started;

        public object Context { get; set; }

        public override object Current
        {
            get { return (running ? routine.Current : null); }
        }

        public Coroutine()
        {
        }

        public static void Start(Func<Coroutine, IEnumerator> func)
        {
            Coroutine coroutine = new Coroutine();
            coroutine.Start(func(coroutine));
        }

        public static void Start<T>(Func<Coroutine, T, IEnumerator> func, T arg)
        {
            Coroutine coroutine = new Coroutine();
            coroutine.Start(func(coroutine, arg));
        }

        public static void Start<T1, T2>(Func<Coroutine, T1, T2, IEnumerator> func, T1 arg1, T2 arg2)
        {
            Coroutine coroutine = new Coroutine();
            coroutine.Start(func(coroutine, arg1, arg2));
        }

        public static void Start<T1, T2, T3>(Func<Coroutine, T1, T2, T3, IEnumerator> func, T1 arg1, T2 arg2, T3 arg3)
        {
            Coroutine coroutine = new Coroutine();
            coroutine.Start(func(coroutine, arg1, arg2, arg3));
        }

        public IEnumerator Start(IEnumerator routine)
        {
            this.routine = routine;
            running = (routine != null);
            MoveNext();
            return this;
        }

        public override bool MoveNext()
        {
            if (!started)
            {
                started = true;
                return routine.MoveNext();
            }

            if (running)
            {
                if (routine.Current.GetType().IsSubclassOf(typeof(Coroutine)))
                {
                    Coroutine coroutine = (Coroutine)routine.Current;
                    if (coroutine.running)
                    {
                        return true;
                    }

                }
                else if (routine.Current.GetType().IsSubclassOf(typeof(YieldInstruction)))
                {
                    YieldInstruction instruction = (YieldInstruction)routine.Current;
                    if (instruction.Continue())
                    {
                        return true;
                    }
                }
                if (routine.MoveNext())
                {
                    return true;
                }
                running = false;
            }
            return false;
        }
    }
}

{'fraction_non_alphanumeric': 0.08130081300813008, 'fraction_numerical': 0.008314855875831485, 'mean_word_length': 1.3073316283034953, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2526502', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1348, 'n_words': 351}