Ejemplo n.º 1
0
        static void SetContainmentTest()
        {
            var set = new ChangeableHashSet <int>(new [] { 1, 2, 3, 4 });


            var greater1 = set.Filter(i => i > 1);


            var reader = greater1.GetReader();

            Report.Begin("set = {1,2,3,4}");
            void print()
            {
                var deltas = reader.GetChanges(AdaptiveToken.Top);

                foreach (var d in deltas)
                {
                    if (d.Count > 0)
                    {
                        Report.Line("add {0}", d.Value);
                    }
                    else
                    {
                        Report.Line("rem {0}", d.Value);
                    }
                }
            }

            Report.End();



            Report.Begin("set = {1,2,3,4,5,6}");
            using (Adaptive.Transact)
            {
                set.Add(5); set.Add(6);
            }
            print();
            Report.End();

            Report.Begin("set = {2,4,6}");
            using (Adaptive.Transact)
            {
                set.Remove(1); set.Remove(3); set.Remove(5);
            }
            print();
            Report.End();

            Report.Begin("set = {4,6}");
            using (Adaptive.Transact)
            {
                set.Remove(2);
            }
            print();
            Report.End();

            Report.Begin("set = {4,5,6}");
            using (Adaptive.Transact)
            {
                set.Add(5);
            }
            print();
            Report.End();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // Create a modref cell. can be changed via side effects
            var input = new ChangeableValue <int>(10);

            var output = input.Map(x => x * 2);

            Console.WriteLine($"output was: {output}");
            // Prints: output was Aardvark.Base.Incremental.ModModule+MapMod`2[System.Int32,System.Int32]
            // not what we expected. Since mods are lazy and tostring does not force them we need
            // to pull the value out of it.

            Console.WriteLine($"output was: {output.GetValue()}"); // F# equivalent: Mod.force : IMod<'a> -> 'a
            // output was: 20

            using (Adaptive.Transact)
            {
                input.Value = 20;
            }

            Console.WriteLine($"output was: {output.GetValue()}");
            // output was: 40

            // semantically, output now dependens on input declaratively.
            // the dependency graph looks like:
            //
            //       (x) => x * 2
            // input ----------------> output
            // mods are nodes, and the edges are annotated with transition functions.


            // the same works for collection types, e.g. an unordered set
            // can be created as such:
            var inputSet = new ChangeableHashSet <int>(new List <int> {
                1, 2, 100
            });

            // similarly to LINQ, there are extensions for incrementally
            // reacting to changes on the input set.
            var less10 = inputSet.Where(x => x < 10);

            Console.Write("less10: ");
            // similarly to GetValue(), ToArray() evaluates the current state
            // of the adaptive set instance.
            less10.ToArray().ForEach(x => Console.Write($"{x},"));
            Console.WriteLine();

            // again, atomic modifications can be submitted using a transaction.
            using (Adaptive.Transact)
            {
                inputSet.AddRange(new List <int>()
                {
                    3, 10000
                });
            }

            // and re-evaluate to investigate the changes.
            Console.Write("less10: ");
            less10.ToArray().ForEach(x => Console.Write($"{x},"));
            Console.WriteLine();
        }
Ejemplo n.º 3
0
        static void AdvancedASetTest()
        {
            Action <IOpReader <CountingHashSet <int>, FSharpHashSetDelta <int> > > print = (r) =>
            {
                var deltas  = r.GetChanges(AdaptiveToken.Top);
                var content = r.State;

                var deltaStr   = deltas.Select(d => d.Count > 0 ? string.Format("Add {0}", d.Value) : string.Format("Rem {0}", d.Value)).Join(", ");
                var contentStr = content.OrderBy(a => a).Select(i => i.ToString()).Join(", ");

                Report.Line("deltas = [{0}]", deltaStr);
                Report.Line("content = [{0}]", contentStr);
            };

            var i0    = new ChangeableHashSet <int>(new [] { 1, 2, 3 });
            var i1    = new ChangeableHashSet <int>(new [] { 4, 5 });
            var i2    = new ChangeableHashSet <int>(new [] { 6, 7 });
            var input = new ChangeableHashSet <IAdaptiveHashSet <int> >(new [] { i0, i1 });

            var flat = input.SelectMany(a => a);

            Report.Begin("initial");
            var reader = flat.GetReader();

            print(reader);
            Report.End();


            Report.Begin("add i2; i1.rem 4");
            Report.Line("all changes (inner and outer) shall be seen consistently");
            using (Adaptive.Transact)
            {
                input.Add(i2);
                i1.Remove(4);
            }
            print(reader);
            Report.End();


            Report.Begin("rem i0; i0.add 10");
            Report.Line("the set shall not see the (Add 10) operation since i0 was removed as a whole");
            using (Adaptive.Transact)
            {
                input.Remove(i0);
                i0.Add(10);
            }
            print(reader);
            Report.End();



            Report.Begin("add i0; i0.rem 10");
            Report.Line("the set shall not see the (Add 10) operation since 10 was removed from i0");
            using (Adaptive.Transact)
            {
                input.Add(i0);
                i0.Remove(10);
            }
            print(reader);
            Report.End();
        }