Example #1
0
        public static RunLengthEncoding <T> Parse(string encoding, Func <string, T> conv)
        {
            RunLengthEncoding <T> rle = ParsePartially(encoding);

            rle.CompleteTranslation(conv);
            return(rle);
        }
Example #2
0
        public static RunLengthEncoding <T> Encode(T[][] array)
        {
            RunLengthEncoding <T>      enc  = new RunLengthEncoding <T>();
            RunLengthEncodingEntry <T> curr = null;

            enc.totalCount = (ulong)array.Length;
            for (int i = 0; i < array.Length; i++)
            {
                enc.totalCount += (ulong)array[i].Length;
                for (int j = 0; j < array[i].Length; j++)
                {
                    var e = array[i][j];
                    if (curr == null)
                    {
                        curr = new RunLengthEncodingEntry <T>(1, e);
                        continue;
                    }
                    else if (curr.Value.CompareTo(e) != 0)
                    {
                        enc.encoding.Add(curr);
                        curr = new RunLengthEncodingEntry <T>(1, e);
                    }
                    else
                    {
                        curr++;
                    }
                }
            }
            enc.encoding.Add(curr);
            return(enc);
        }
Example #3
0
        public static RunLengthEncoding <T> Encode(IEnumerable <T> enumeration)
        {
            RunLengthEncoding <T>      enc  = new RunLengthEncoding <T>();
            RunLengthEncodingEntry <T> curr = null;

            foreach (var e in enumeration)
            {
                if (curr == null)
                {
                    curr = new RunLengthEncodingEntry <T>(1, e);
                }
                else
                {
                    if (curr.Value.CompareTo(e) == 0)
                    {
                        curr++;
                    }
                    else
                    {
                        enc.encoding.Add(curr);
                        curr = new RunLengthEncodingEntry <T>(1, e);
                    }
                }
            }
            enc.encoding.Add(curr);
            return(enc);
        }
Example #4
0
        public static RunLengthEncoding <T> Encode(IEnumerable <RunLengthEncodingEntry <T> > enc)
        {
            RunLengthEncoding <T> e = new RunLengthEncoding <T>();

            foreach (var v in enc)
            {
                e.encoding.Add(v);
                e.totalCount += v.Count;
            }
            return(e);
        }
Example #5
0
        public static RunLengthEncoding <T> ParsePartially(string encoding)
        {
            RunLengthEncoding <T> enc = new RunLengthEncoding <T>();

            string[] elements = spaces.Split(encoding);
            enc.totalCount = ulong.Parse(elements[0]);
            for (int i = 1; i < elements.Length; i++)
            {
                if (!elements[i].Equals(string.Empty))
                {
                    enc.encoding.Add(RunLengthEncodingEntry <T> .Parse(elements[i]));
                }
            }
            return(enc);
        }