コード例 #1
0
            static int Decoding(FrequencyNode root, string encodedValue, int index)
            {
                if (root.IsLeaf)
                {
                    Console.Write(root.Char);
                    return(index);
                }

                if (encodedValue[index] == '0')
                {
                    return(Decoding(root.Left, encodedValue, index + 1));
                }
                else
                {
                    return(Decoding(root.Right, encodedValue, index + 1));
                }
            }
コード例 #2
0
            static void AssignCode(FrequencyNode root, string prefix)
            {
                if (root.IsLeaf)
                {
                    root.StringCode = prefix;
                    return;
                }

                if (root.Left != null)
                {
                    AssignCode(root.Left, prefix + "0");
                }

                if (root.Right != null)
                {
                    AssignCode(root.Right, prefix + "1");
                }
            }
コード例 #3
0
        public long IncrementBy(int value)
        {
            if (value == 0)
                return Value;

            if (value < 1)
                throw new ArgumentOutOfRangeException("value");

            int timeValue = GetTimeValue();

            FrequencyNode currentHead = this.head;

            while (currentHead == null || currentHead.TimeValue < timeValue)
            {
                // The head node is out of date or we're empty so we need to
                // create a new node to take it's place. We'll populate the sample count
                // up-front since we're the only one who's got access to the node
                var newHeadNode = new FrequencyNode { TimeValue = timeValue };

                RuntimeHelpers.PrepareConstrainedRegions();
                try { }
                finally
                {
                    // Try to update the head reference to our newly created node.
                    // If we fail we'll just try again since most likely someone else
                    // managed to update the node before us and we can use that instead.
                    if (Interlocked.CompareExchange(ref this.head, newHeadNode, currentHead) == currentHead)
                    {
                        // Succeeded in setting the head reference.

                        // If we weren't empty before we need to update the previous
                        // head node so that it references the new head.
                        if (currentHead != null)
                            currentHead.Next = newHeadNode;

                        // If the tail node reference is null we'll update it to point to
                        // our new head node.
                        Interlocked.CompareExchange(ref this.tail, newHeadNode, null);

                        currentHead = newHeadNode;
                    }
                    else
                    {
                        // Prepare for a retry
                        currentHead = this.head;
                    }
                }
            }

            RuntimeHelpers.PrepareConstrainedRegions();
            try { }
            finally
            {
                Interlocked.Add(ref currentHead.Samples, value);
                Interlocked.Add(ref this.counterValue, value);
            }

            Prune(currentHead.TimeValue);
            return Interlocked.Read(ref this.counterValue);
        }
コード例 #4
0
        public long IncrementBy(int value)
        {
            if (value == 0)
            {
                return(Value);
            }

            if (value < 1)
            {
                throw new ArgumentOutOfRangeException("value");
            }

            int timeValue = GetTimeValue();

            FrequencyNode currentHead = this.head;

            while (currentHead == null || currentHead.TimeValue < timeValue)
            {
                // The head node is out of date or we're empty so we need to
                // create a new node to take it's place. We'll populate the sample count
                // up-front since we're the only one who's got access to the node
                var newHeadNode = new FrequencyNode {
                    TimeValue = timeValue
                };

                RuntimeHelpers.PrepareConstrainedRegions();
                try { }
                finally
                {
                    // Try to update the head reference to our newly created node.
                    // If we fail we'll just try again since most likely someone else
                    // managed to update the node before us and we can use that instead.
                    if (Interlocked.CompareExchange(ref this.head, newHeadNode, currentHead) == currentHead)
                    {
                        // Succeeded in setting the head reference.

                        // If we weren't empty before we need to update the previous
                        // head node so that it references the new head.
                        if (currentHead != null)
                        {
                            currentHead.Next = newHeadNode;
                        }

                        // If the tail node reference is null we'll update it to point to
                        // our new head node.
                        Interlocked.CompareExchange(ref this.tail, newHeadNode, null);

                        currentHead = newHeadNode;
                    }
                    else
                    {
                        // Prepare for a retry
                        currentHead = this.head;
                    }
                }
            }

            RuntimeHelpers.PrepareConstrainedRegions();
            try { }
            finally
            {
                Interlocked.Add(ref currentHead.Samples, value);
                Interlocked.Add(ref this.counterValue, value);
            }

            Prune(currentHead.TimeValue);
            return(Interlocked.Read(ref this.counterValue));
        }
コード例 #5
0
        public static void Test()
        {
            string text = "My names is Marco Rossignoli and I live in Italy";

            Console.WriteLine($"To encode {text} initial len {text.Length}");
            Dictionary <char, int> frequencyTable = new Dictionary <char, int>();

            for (int i = 0; i < text.Length; i++)
            {
                if (!frequencyTable.ContainsKey(text[i]))
                {
                    frequencyTable.Add(text[i], 0);
                }
                frequencyTable[text[i]]++;
            }

            FrequencyNode[] frequencyTableList = frequencyTable.OrderBy(v => v.Value).Select(k => new FrequencyNode()
            {
                FrequencyValue = k.Value, Char = k.Key
            }).ToArray();
            FrequencyNode[] ft = frequencyTableList.ToArray();

            foreach (var v in ft)
            {
                Console.WriteLine($"{v.Char} {v.FrequencyValue}");
            }

            while (ft.Length > 1)
            {
                FrequencyNode node = new FrequencyNode()
                {
                    FrequencyValue = ft[0].FrequencyValue + ft[1].FrequencyValue
                };

                if (ft[0].FrequencyValue < ft[1].FrequencyValue)
                {
                    node.Left  = ft[0];
                    node.Right = ft[1];
                }
                else
                {
                    node.Left  = ft[1];
                    node.Right = ft[0];
                }

                var tmp = new List <FrequencyNode>(ft.Skip(2).ToArray());
                tmp.Add(node);

                ft = tmp.OrderBy(n => n.FrequencyValue).ToArray();
            }

            Console.WriteLine("-------");

            AssignCode(ft[0], "");

            Dictionary <char, string> encodes = new Dictionary <char, string>();

            foreach (FrequencyNode val in frequencyTableList.OrderByDescending(f => f.FrequencyValue))
            {
                Console.WriteLine($"{val.Char} {val.FrequencyValue} {val.StringCode}");
                encodes.Add(val.Char.Value, val.StringCode);
            }

            StringBuilder encoded = new StringBuilder();

            for (int i = 0; i < text.Length; i++)
            {
                encoded.Append(encodes[text[i]]);
            }

            string encodedVal = encoded.ToString();

            Console.WriteLine($"Encoded {encoded} len {encodedVal.Length / 8}");

            int encodedLen   = encodedVal.Length;
            int currentIndex = 0;

            while (currentIndex < encodedLen)
            {
                currentIndex = Decoding(ft[0], encodedVal, currentIndex);
            }

            return;