Exemplo n.º 1
0
        public void ExpandPartially(int width, int height,
                                    Func <int, int, T, bool> fn)
        {
            int q = 0;
            RunLengthEncodingEntry <T> curr = null;
            T     val   = default(T);
            ulong total = 0L;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    if (total == 0L)
                    {
                        curr  = this[q];
                        val   = curr.Value;
                        total = curr.Count;
                        q++;
                    }
                    if (!fn(i, j, val))
                    {
                        return;
                    }
                    total--;
                }
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
0
        public static RunLengthEncodingEntry <T> Parse(string input)
        {
            Regex r = new Regex(",");

            string[] s = r.Split(input);
            var      q = new RunLengthEncodingEntry <T>(ulong.Parse(s[0]), default(T));

            q.TemporaryRepresentation = s[1];
            q.PartiallyTranslated     = true;
            return(q);
        }
Exemplo n.º 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);
        }
Exemplo n.º 6
0
        protected RunLengthEncoding(SerializationInfo info, StreamingContext cntxt)
        {
            string body = info.GetString("a");
            Regex  r    = new Regex(" ");

            string[] items = r.Split(body);
            totalCount = ulong.Parse(items[0]);
            for (int i = 1; i < items.Length; i++)
            {
                if (!items[i].Equals(string.Empty))
                {
                    encoding.Add(RunLengthEncodingEntry <T> .Parse(items[i]));
                }
            }
            TranslationFinished = false;
        }
Exemplo n.º 7
0
        public void DestructiveExpand(int width, int height, Action <int, int, T> fn)
        {
            int q = 0;
            RunLengthEncodingEntry <T> curr = null;
            T val = default(T);

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    if (curr == null || curr.Count == 0L)
                    {
                        curr = this[q++];
                        val  = curr.Value;
                    }
                    fn(i, j, val);
                    curr--;
                }
            }
        }
Exemplo n.º 8
0
        public void Expand(int width, int height, Action <int, int, T> fn)
        {
            int q = 0;
            RunLengthEncodingEntry <T> curr = null;
            T     val   = default(T);
            ulong total = 0L;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    if (total == 0L)
                    {
                        curr  = this[q];
                        val   = curr.Value;
                        total = curr.Count;
                        q++;
                    }
                    fn(i, j, val);
                    total--;
                }
            }
        }