예제 #1
0
        internal BwtModel Encode(List <byte> bytes)
        {
            _bytes = bytes;

            var initialValue = 0;
            var indexes      = Enumerable.Range(0, _bytes.Count).Select(i => initialValue++).ToList();

            indexes.Sort(Comparer);

            var originalIndex = indexes.IndexOf(0);

            var encodedBytes = new List <byte>(_bytes.Count);

            for (int i = 0; i < _bytes.Count; i++)
            {
                encodedBytes.Add(_bytes[(indexes[i] + _bytes.Count - 1) % _bytes.Count]);
            }

            var model = new BwtModel(originalIndex, encodedBytes);

            return(model);
        }
예제 #2
0
        internal List <byte> Decode(BwtModel model)
        {
            var byteLength = 256;

            var counts = Enumerable.Range(0, byteLength).Select(i => 0).ToList();

            foreach (var element in model.Bytes)
            {
                counts[element]++;
            }

            var sum = 0;

            for (int i = 0; i < byteLength; i++)
            {
                sum      += counts[i];
                counts[i] = sum - counts[i];
            }

            var invertVector = Enumerable.Range(0, model.Bytes.Count).Select(i => 0).ToList();

            for (int i = 0; i < model.Bytes.Count; i++)
            {
                invertVector[counts[model.Bytes[i]]] = i;
                counts[model.Bytes[i]]++;
            }

            var currentIndex = invertVector[model.OriginalIndex];
            var decodedBytes = new List <byte>(model.Bytes.Count);

            for (int i = 0; i < model.Bytes.Count; i++)
            {
                decodedBytes.Add(model.Bytes[currentIndex]);
                currentIndex = invertVector[currentIndex];
            }

            return(decodedBytes);
        }