public void ConstructorSetsPublicPropertiesAsExpected()
        {
            const int limit = 300;
            const OnFullDictionaryOption onFullDictionaryOption = OnFullDictionaryOption.Empty;

            var lzWDictionary = new LzWDictionary(limit, onFullDictionaryOption);

            Assert.AreEqual(limit, lzWDictionary.Limit);
            Assert.AreEqual(onFullDictionaryOption, lzWDictionary.OnFullDictionaryOption);
        }
Exemplo n.º 2
0
        public void Setup()
        {
            lzWEncoder = new LzWEncoder();

            fileReaderMock = new Mock <IFileReader>();
            fileWriterMock = new Mock <IFileWriter>();

            onFullDictionaryOption = OnFullDictionaryOption.Empty;
            numberOfBitsForIndex   = 12;
        }
Exemplo n.º 3
0
        public LzWDictionary(int limit, OnFullDictionaryOption onFullDictionaryOption)
        {
            if (limit < 256)
            {
                throw new ArgumentException($"{nameof(limit)} must be at least 256");
            }

            Limit = limit;
            OnFullDictionaryOption = onFullDictionaryOption;

            dictionary = new Dictionary <string, uint>();
            for (uint key = 0; key < 256; key++)
            {
                dictionary.Add(((char)key).ToString(), key);
            }

            dictionaryLastIndex = 255;
        }
Exemplo n.º 4
0
 private void WriteHeader(IFileWriter fileWriter, OnFullDictionaryOption onFullDictionaryOption, int numberOfBitsIndex)
 {
     fileWriter.WriteValueOnBits((uint)numberOfBitsIndex, 4);
     fileWriter.WriteValueOnBits((uint)onFullDictionaryOption, 1);
 }
Exemplo n.º 5
0
        public void EncodeFile(IFileReader fileReader, IFileWriter fileWriter, OnFullDictionaryOption onFullDictionaryOption, int numberOfBitsIndex)
        {
            if (fileReader == null)
            {
                throw new ArgumentNullException(nameof(fileReader));
            }

            if (fileWriter == null)
            {
                throw new ArgumentNullException(nameof(fileWriter));
            }

            if (numberOfBitsIndex < 9 || numberOfBitsIndex > 15)
            {
                throw new ArgumentException($"{nameof(numberOfBitsIndex)} must be at least 9, and at most 15");
            }

            WriteHeader(fileWriter, onFullDictionaryOption, numberOfBitsIndex);
            IndexesFromLastRun.Clear();

            LzWDictionary = new LzWDictionary((int)Math.Pow(2, numberOfBitsIndex) - 1, onFullDictionaryOption);

            var lastCharacter = (char)fileReader.ReadBits(8);
            var shouldStop = false;

            while (true)
            {
                var currentString = lastCharacter.ToString();
                uint lastIndex = 0;

                if (shouldStop)
                {
                    break;
                }

                while (true)
                {
                    if (LzWDictionary.ContainsString(currentString))
                    {
                        lastIndex = LzWDictionary.GetIndexByString(currentString);

                        if (shouldStop)
                        {
                            fileWriter.WriteValueOnBits(lastIndex, (byte)numberOfBitsIndex);
                            IndexesFromLastRun.Add(lastIndex);

                            break;
                        }
                    }
                    else
                    {
                        LzWDictionary.Add(currentString);

                        fileWriter.WriteValueOnBits(lastIndex, (byte)numberOfBitsIndex);
                        IndexesFromLastRun.Add(lastIndex);

                        break;
                    }

                    if (!fileReader.ReachedEndOfFile)
                    {
                        var readByte = (byte)fileReader.ReadBits(8);
                        currentString += (char)readByte;
                        lastCharacter = (char)readByte;
                    }
                    else
                    {
                        shouldStop = true;
                    }
                }
            }

            fileWriter.Flush();
        }