|
using System;
using System.Collections.Generic;
using System.Text;
using QiHe.CodeLib;
namespace QiHe.CodeLib.Compress
{
public class RunLengthEncoding
{
public static List<Pair<T, int>> Analyze<T>(IEnumerable<T> list) where T : IComparable<T>
{
List<Pair<T, int>> items = new List<Pair<T, int>>();
T previousItem = default(T);
int count = 0;
foreach (T item in list)
{
if (count == 0)
{
previousItem = item;
count = 1;
}
else
{
if (item.Equals(previousItem))
{
count++;
}
else
{
items.Add(new Pair<T, int>(previousItem, count));
previousItem = item;
count = 1;
}
}
}
if (count > 0)
{
items.Add(new Pair<T, int>(previousItem, count));
}
return items;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.