Ejemplo n.º 1
0
        public static void DemonstrateUseOfExtensionMethodsToSimplify()
        {
            const string methodName = nameof(DemonstrateUseOfExtensionMethodsToSimplify);

            Console.WriteLine();
            Console.WriteLine($"Performing {methodName}...");
            MutableResourceVault <StringBuilder> vault = CreateExampleVault();

            using var lck = vault.SpinLock();
            Console.WriteLine("Contents: {0}", lck.GetContents());
            Console.WriteLine("First char: {0}", lck.GetCharAt(0));
            //make second char uppercase E
            lck.SetCharAt(1, 'E');
            Console.WriteLine("Changed to uppercase 'E': {0}", lck.GetContents());
            Console.WriteLine();
        }
Ejemplo n.º 2
0
        public static void DemonstrateActions()
        {
            const string methodName = nameof(DemonstrateActions);

            Console.WriteLine();
            Console.WriteLine($"Performing {methodName}...");
            MutableResourceVault <StringBuilder> vault = CreateExampleVault();

            using var lck = vault.SpinLock();
            lck.ExecuteAction((ref StringBuilder res) =>
            {
                for (int i = 0; i < res.Length; ++i)
                {
                    char current = res[i];
                    if (char.IsLetter(current))
                    {
                        res[i] = char.IsLower(current) ? char.ToUpper(current) : char.ToLower(current);
                    }
                }
            });
            string contents = lck.ExecuteQuery((in StringBuilder sb)
                                               => sb.ToString());

            Console.WriteLine("Reversed Upper/Lower res: " +
                              $"{contents}");
            //now let's make every char at idx divisible by three change to q
            int divisibleBy = 3;

            lck.ExecuteAction((ref StringBuilder res, in int d) =>
            {
                for (int i = 0; i < res.Length; ++i)
                {
                    if (i % d == 0)
                    {
                        res[i] = 'q';
                    }
                }
            }, divisibleBy);
            contents = lck.ExecuteQuery((in StringBuilder sb)
                                        => sb.ToString());
            Console.WriteLine($"Made chars at idx divisble by 3 q: " +
                              $"[{contents}]");
            Console.WriteLine();
        }
Ejemplo n.º 3
0
        public static void DemonstrateMixedOperations()
        {
            const string methodName = nameof(DemonstrateMixedOperations);

            Console.WriteLine();
            Console.WriteLine($"Performing {methodName}...");
            MutableResourceVault <StringBuilder> vault = CreateExampleVault();

            using var lck =
                      vault.SpinLock();

            //Find the index of the first char equal to 'o' and insert
            //" it's magic oooh oooh! " after it.
            //return the index and the new contents.
            string      insertMe    = " it's magic oooh oooh! ";
            char        queryLetter = 'o';
            IndexAndVal res         = lck.ExecuteMixedOperation(
                (ref StringBuilder sb, in char ql) =>
            {
                int idx = -1;
                for (int i = 0; i < sb.Length; ++i)
                {
                    if (sb[i] == ql)
                    {
                        idx = i;
                        break;
                    }
                }

                if (idx != -1)
                {
                    sb.Insert(idx + 1, insertMe);
                }

                return(new IndexAndVal(sb.ToString(), idx));
            }, queryLetter);

            Console.WriteLine($"New value: [{res.NewValue}], " +
                              $"Index: [{res.Idx}].");
            Console.WriteLine();
        }
Ejemplo n.º 4
0
        public static void ShowWrapperUsage()
        {
            Console.WriteLine("Begin showing wrapper usage.");
            MutableResourceVault <List <int> > vault =
                MutableResourceVault <List <int> > .CreateAtomicMutableResourceVault(() =>
                                                                                     new List <int> {
                1, 2, 3, 4
            },
                                                                                     TimeSpan.FromMilliseconds(250));

            ImmutableArray <int> finalContents;

            {
                using var lck =
                          vault.SpinLock();
                List <int> addUs = new List <int> {
                    5, 6, 7, 8
                };
                //ERROR DotNetVault_VsDelegateCapture cannot capute non-vault
                //safe param addUs, of type List, not vault-safe
                //lck.ExecuteAction((ref List<int> res) => res.AddRange(addUs));

                //Ok reference is to thin readonly wrapper around list of vault-safe type.
                //state cannot be commingled in the delegate.
                VsListWrapper <int> wrapper = VsListWrapper <int> .FromList(addUs);

                lck.ExecuteAction((ref List <int> res) => res.AddRange(wrapper));
                finalContents = lck.ExecuteQuery((in List <int> res) =>
                                                 res.ToImmutableArray());
            }
            Console.WriteLine("Printing final contents: ");
            StringBuilder sb = new StringBuilder("{");

            foreach (var i in finalContents)
            {
                sb.Append($"{i}, ");
            }

            if (sb[^ 1] == ' ' && sb[^ 2] == ',')
Ejemplo n.º 5
0
        public static void DemonstrateQueries()
        {
            const string methodName = nameof(DemonstrateQueries);

            Console.WriteLine();
            Console.WriteLine($"Performing {methodName}...");
            MutableResourceVault <StringBuilder> vault = CreateExampleVault();

            using var lck = vault.SpinLock();
            string contents = lck.ExecuteQuery((in StringBuilder res) => res.ToString());

            Console.WriteLine($"Contents: {contents}.");
            int ancillaryValue           = 7;
            int lengthPlusAncillaryValue =
                lck.ExecuteQuery((in StringBuilder sb, in int anc) => sb.Length + anc,
                                 ancillaryValue);

            Console.WriteLine("Length of contents (content length: " +
                              $"[{lck.ExecuteQuery( (in StringBuilder sb) => sb.Length)}]) " +
                              $"plus [{nameof(ancillaryValue)}] of [{ancillaryValue}]: " +
                              $"{lengthPlusAncillaryValue.ToString()}");
            int  idx    = 1;
            char offSet = (char)32;
            char valOfCharSpecifiedIndexMadeUppercase =
                lck.ExecuteQuery(
                    (in StringBuilder sb, in char offset) =>
                    (char)(sb[idx] - offSet), offSet);

            Console.WriteLine($"Char at idx [{idx.ToString()}] " +
                              $"(current val: [{contents[1]}]) made upper " +
                              $"case: [{valOfCharSpecifiedIndexMadeUppercase}].");
            Console.WriteLine();

            Console.WriteLine("Bug50Demo3 START");
            Bug50Demo3();
            Console.WriteLine("Bug50Demo3 END");
        }