Пример #1
0
        }//end DecompressFile method

        //
        //COMPRESSION METHODS
        //


        //
        //
        //Compression #1
        //This counts the character frequency of the input file and puts that data into an array. It then returns this array of character frequency objects.
        static Array BuildCharArray(string inputFilePath)
        {
            CharacterFrequency[] cfArray  = new CharacterFrequency[256];
            FileStream           fileRead = new FileStream(inputFilePath, FileMode.Open);

            //This fills each element in the CharacterFrequency array with its respective ASCII value. The index of the element is the ASCII value.
            for (int i = 0; i < cfArray.Length; ++i)
            {
                //Converts the array element to its respective ASCII character value.
                cfArray[i] = new CharacterFrequency(Convert.ToChar(i));
            }

            //for loop iterates once per character in the receieved file.
            long fileLength = fileRead.Length;

            for (int i = 0; i < fileLength; ++i)
            {
                //Creates a temporary CharacterFrequency object that stores the next read byte.
                CharacterFrequency tempCF = new CharacterFrequency(Convert.ToChar(fileRead.ReadByte()));

                //Creates a temporary int that stores the ASCII value found in the tempCF CharacterFrequency object.
                int tempASCII = Convert.ToInt32(tempCF.GetCh());

                //Searches the array that holds CharacterFrequency objects. The index for the search is the ASCII value for the character in the temporary CharacterFrequency object.
                if (cfArray[tempASCII].Equals(tempCF) == true)
                {
                    //If the CharacterFrequency object is found in the CharacterFrequency array, the object is incremented.
                    cfArray[tempASCII].Increment();
                }
            }

            fileRead.Close();
            return(cfArray);
        }//end BuildCharArray method
Пример #2
0
        }//end GetHashCode method

        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }
            if (obj == this)
            {
                return(0);
            }
            if (!(obj is CharacterFrequency))
            {
                throw new ArgumentException("comparing obj is not a CharacterFrequency");
            }

            CharacterFrequency cf = obj as CharacterFrequency;

            return(this.GetFrequency().CompareTo(cf.GetFrequency())); //Compares the character frequency.
        }//end CompareTo method
Пример #3
0
        }//end Increment method

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }
            if (this == obj)
            {
                return(true);
            }
            if (!(obj is CharacterFrequency))
            {
                return(false);
            }

            CharacterFrequency cf = obj as CharacterFrequency;

            return(this.GetCh() == cf.GetCh());
        }//end Equals method
Пример #4
0
        }//end BuildCharOLL method

        //
        //
        //Compression #3
        //This creates a binary tree using the recieved OrderdLinkedList<BinaryTree<CharacterFrequency>> object. This binary tree combines the first two objects in the OrderedLinkedList and puts it back into the OLL in its appropriate spot.
        //When combining the first two BT objects, the first object becomes the left child, the second becomes the right child, and a new BT object is created that sums the character frequency of the children. This object becomes the root.
        static BinaryTree <CharacterFrequency> BuildEncTree(OrderedLinkedList <BinaryTree <CharacterFrequency> > oll)
        {
            //While there is more than one object in the OrderedLinkedList
            while (oll.Count() != 1)
            {
                //Creates two temporary BinaryTree<CharacterFrequency> objects
                BinaryTree <CharacterFrequency> bt1 = null;
                BinaryTree <CharacterFrequency> bt2 = null;

                //These two BT objects refererence the first two objects in the OrderedLinkedList and the first two objects in the OLL are removed
                bt1 = oll.First.Value;
                oll.RemoveFirst();
                bt2 = oll.First.Value;
                oll.RemoveFirst();

                //Creates a new CharacterFrequency object based on the two CF objects stored in the two BinaryTree objects taken from the OrderedLinkedList. Sets the character to '\0' and the frequency to the sum of both CF frequencies
                int newFrequency          = bt1.Current.Data.GetFrequency() + bt2.Current.Data.GetFrequency();
                CharacterFrequency rootCF = new CharacterFrequency('\0', newFrequency);

                //Creates a new BinaryTree object whose root value is the newly created CF object with the '\0' character
                //Adds the first BT object as the left child of this new BinaryTree object and adds the second BT object as the right child
                BinaryTree <CharacterFrequency> tree = new BinaryTree <CharacterFrequency>();
                tree.Insert(rootCF, BinaryTree <CharacterFrequency> .Relative.root);
                tree.Insert(bt1.Root, BinaryTree <CharacterFrequency> .Relative.leftChild);
                tree.Insert(bt2.Root, BinaryTree <CharacterFrequency> .Relative.rightChild);
                //Adds this new BT object that combined the first two objects in the OrderedLinkedList back into the OLL
                oll.Add(tree);

                //Resets the values
                rootCF = null;
                tree   = null;
                bt1    = null;
                bt2    = null;
            }
            //Returns the completed BinaryTree object holding all the CharacterFrequency objects.
            return(oll.First.Value); //The tree has the most frequent nodes on the left and the least frequent on the right
        }//end BuildEncTree method