예제 #1
0
            public enumerator(ContextualStream <T> stream)
            {
                this.stream = stream;

                //.CreateChild(new CSScope.Prototype
                //{
                //    countScope = RWScope.None,
                //    valueScope = new InfiniteIntervalScope(0, null, RWScope.Read)
                //});
            }
 public static async Task FibonacciTest()
 {
     await TaskCollector.With(tasks =>
     {
         Console.WriteLine("Fibonacci:");
         var fiboNums = new ContextualStream <BigInteger>();
         tasks.Add(addFibonacciNumbers(fiboNums));
         tasks.Add(PrintValues(fiboNums));
     });
 }
예제 #3
0
            public IReactive <bool> MoveNextAsync()
            {
                return(Reactive.From <bool>(async resultResolver =>
                {
                    await TaskCollector.With(async tc =>
                    {
                        using (ContextualStream <T> childStream =
                                   stream.CreateChildWithin(new CSScope(valueScope:
                                                                        InfiniteIntervalScope.Create(
                                                                            RWScope.Read, 0, null))))
                            using (IAccessContext <enumeratorScope> childEnumContext
                                       = context.CreateChild(new enumeratorScope(
                                                                 RWScope.ReadWrite, RWScope.ReadWrite)))
                            {
                                long curNextIndex =
                                    await readNextIndex(childEnumContext);
                                CSScope readNextNodeScope = new CSScope(valueScope:
                                                                        InfiniteIntervalScope.Create(RWScope.Read,
                                                                                                     curNextIndex, curNextIndex + 1));
                                childStream.Context.RequiredScope =
                                    readNextNodeScope;

                                node nextNode;

                                if (curNextIndex == 0)
                                {
                                    /* Erste Iteration. */
                                    tc.Add(incrementNextIndex(childEnumContext));
                                    nextNode = await childStream.getFirstNode(
                                        childStream.Context);
                                }
                                else
                                {
                                    node curCurrentNode =
                                        await readCurrentNode(childEnumContext);

                                    if (curCurrentNode == null)
                                    {
                                        throw new InvalidOperationException(
                                            "The stream has already " +
                                            "been iterated through.");
                                    }
                                    tc.Add(incrementNextIndex(childEnumContext));
                                    await childStream.Context.UntilAvailable();
                                    nextNode = curCurrentNode.Successor;
                                }
                                resultResolver.Resolve(nextNode != null);
                                childStream.Dispose();
                                tc.Add(setCurrentNode(nextNode, childEnumContext));
                            }
                    });
                }));
            }
        public static async Task SimpleTest()
        {
            await TaskCollector.With(async tasks =>
            {
                ContextualStream <int> stream = new ContextualStream <int>();

                tasks.Add(stream.Add(3));
                AssertEquals(await stream.Get(0), 3);
                tasks.Add(stream.Add(5));
                tasks.Add(stream.Add(7));
                AssertEquals(stream.Get(2).Result, 7);
                AssertEquals(stream.Get(0).Result, 3);
                await PrintValues(stream);
            });
        }
        private static async Task addFibonacciNumbers(
            ContextualStream <BigInteger> stream, int count = 30)
        {
            await TaskCollector.With(async tc =>
            {
                await stream.InChild(new CSScope(
                                         RWScope.ReadWrite,
                                         InfiniteIntervalScope.Create(
                                             RWScope.ReadWrite, 0, null)),
                                     async childStream =>
                {
                    await Task.Yield();

                    IAppender <BigInteger> appender =
                        childStream.GetAppender();

                    BigInteger a = 1, b = 1;
                    tc.Add(appender.Append(a));

                    /* FIXME: Users of appender generally don’t know
                     * the internals of ContextualStream.
                     * Poss. solution: IAppender.MaxRequiredScope.
                     * Then it would also be useful, if (the) access scopes
                     * could be merged. */
                    childStream.Context.RequiredScope = new CSScope(
                        RWScope.ReadWrite,
                        InfiniteIntervalScope.Create(
                            RWScope.ReadWrite, 1, null));
                    tc.Add(appender.Append(b));
                    childStream.Context.RequiredScope = new CSScope(
                        RWScope.ReadWrite,
                        InfiniteIntervalScope.Create(
                            RWScope.ReadWrite, 2, null));
                    for (int i = 2; i < count; i++)
                    {
                        BigInteger c = a + b;
                        await Task.Delay(250);
                        a = b;
                        b = c;
                        tc.Add(appender.Append(c));
                        childStream.Context.RequiredScope = new CSScope(
                            RWScope.ReadWrite,
                            InfiniteIntervalScope.Create(
                                RWScope.ReadWrite, i + 1, null));
                    }
                });
            });
        }
예제 #6
0
            public IReactive <T> GetCurrentAsync()
            {
                return(Reactive.From <T>(async resultResolver =>
                {
                    using (ContextualStream <T> childStream =
                               stream.CreateChildWithin(new CSScope(valueScope:
                                                                    InfiniteIntervalScope.Create(
                                                                        RWScope.Read, 0, null))))
                        using (IAccessContext <enumeratorScope> childEnumContext =
                                   context.CreateChildWithin(new enumeratorScope(
                                                                 RWScope.Read,
                                                                 RWScope.Read)))
                        {
                            long curNextIndex =
                                await readNextIndex(childEnumContext);
                            childEnumContext.RequiredScope =
                                new enumeratorScope(RWScope.Read);
                            CSScope readValueScope = new CSScope(valueScope:
                                                                 InfiniteIntervalScope.Create(
                                                                     RWScope.Read, curNextIndex - 1, curNextIndex));
                            childStream.Context.RequiredScope = readValueScope;

                            node curNode =
                                await readCurrentNode(childEnumContext);
                            childEnumContext.Dispose();

                            if (curNode == null)
                            {
                                throw new InvalidOperationException(
                                    "The enumerator is already disposed.");
                            }

                            await childStream.Context.UntilAvailable(
                                readValueScope);
                            resultResolver.Resolve(curNode.Value);
                        }
                }));
            }
예제 #7
0
 public appender(ContextualStream <T> stream)
 {
     this.stream        = stream;
     lastKnownNode      = AsyncBox.Create <node>(null);
     lastKnownNodeIndex = AsyncBox.Create(0);
 }
예제 #8
0
 private ContextualStream(ContextualStream <T> parentContextual,
                          IAccessContext <CSScope> context)
     : base(context, factory)
 {
     state = parentContextual.state;
 }