拆分ICollection<;T>;具有分隔符序列
本文关键字:分隔符 ICollection lt 拆分 gt | 更新日期: 2023-09-27 17:59:35
这是针对C#3.5 的
我有一个ICollection,我正试图将其拆分为单独的ICollections,其中分隔符是一个序列。
例如
ICollection<byte> input = new byte[] { 234, 12, 12, 23, 11, 32, 23, 11 123, 32 };
ICollection<byte> delimiter = new byte[] {23, 11};
List<IICollection<byte>> result = input.splitBy(delimiter);
会导致
result.item(0) = {234, 12, 12};
result.item(1) = {32};
result.item(2) = {123, 32};
private static IEnumerable<IEnumerable<T>> Split<T>
(IEnumerable<T> source, ICollection<T> delimiter)
{
// window represents the last [delimeter length] elements in the sequence,
// buffer is the elements waiting to be output when delimiter is hit
var window = new Queue<T>();
var buffer = new List<T>();
foreach (T element in source)
{
buffer.Add(element);
window.Enqueue(element);
if (window.Count > delimiter.Count)
window.Dequeue();
if (window.SequenceEqual(delimiter))
{
// number of non-delimiter elements in the buffer
int nElements = buffer.Count - window.Count;
if (nElements > 0)
yield return buffer.Take(nElements).ToArray();
window.Clear();
buffer.Clear();
}
}
if (buffer.Any())
yield return buffer;
}
最佳解决方案不会使用SequenceEqual()
来检查每个子范围,否则您可能会迭代序列中每个项目的分隔符长度,这可能会影响性能,尤其是对于大型分隔符序列。可以在枚举源序列时对其进行检查。
这是我想写的,但总有改进的余地。我的目标是具有与String.Split()
类似的语义。
public enum SequenceSplitOptions { None, RemoveEmptyEntries }
public static IEnumerable<IList<T>> SequenceSplit<T>(
this IEnumerable<T> source,
IEnumerable<T> separator)
{
return SequenceSplit(source, separator, SequenceSplitOptions.None);
}
public static IEnumerable<IList<T>> SequenceSplit<T>(
this IEnumerable<T> source,
IEnumerable<T> separator,
SequenceSplitOptions options)
{
if (source == null)
throw new ArgumentNullException("source");
if (options != SequenceSplitOptions.None
&& options != SequenceSplitOptions.RemoveEmptyEntries)
throw new ArgumentException("Illegal option: " + (int)option);
if (separator == null)
{
yield return source.ToList();
yield break;
}
var sep = separator as IList<T> ?? separator.ToList();
if (sep.Count == 0)
{
yield return source.ToList();
yield break;
}
var buffer = new List<T>();
var candidate = new List<T>(sep.Count);
var sindex = 0;
foreach (var item in source)
{
candidate.Add(item);
if (!item.Equals(sep[sindex]))
{ // item is not part of the delimiter
buffer.AddRange(candidate);
candidate.Clear();
sindex = 0;
}
else if (++sindex >= sep.Count)
{ // candidate is the delimiter
if (options == SequenceSplitOptions.None || buffer.Count > 0)
yield return buffer.ToList();
buffer.Clear();
candidate.Clear();
sindex = 0;
}
}
if (candidate.Count > 0)
buffer.AddRange(candidate);
if (options == SequenceSplitOptions.None || buffer.Count > 0)
yield return buffer;
}
public IEnumerable<IEnumerable<T>> SplitByCollection<T>(IEnumerable<T> source,
IEnumerable<T> delimiter)
{
var sourceArray = source.ToArray();
var delimiterCount = delimiter.Count();
int lastIndex = 0;
for (int i = 0; i < sourceArray.Length; i++)
{
if (delimiter.SequenceEqual(sourceArray.Skip(i).Take(delimiterCount)))
{
yield return sourceArray.Skip(lastIndex).Take(i - lastIndex);
i += delimiterCount;
lastIndex = i;
}
}
if (lastIndex < sourceArray.Length)
yield return sourceArray.Skip(lastIndex);
}
称之为。。。
var result = SplitByCollection(input, delimiter);
foreach (var element in result)
{
Console.WriteLine (string.Join(", ", element));
}
返回
234、12、1232123、32
以下是我对它的看法:
public static IEnumerable<IList<byte>> Split(IEnumerable<byte> input, IEnumerable<byte> delimiter)
{
var l = new List<byte>();
var set = new HashSet<byte>(delimiter);
foreach (var item in input)
{
if(!set.Contains(item))
l.Add(item);
else if(l.Count > 0)
{
yield return l;
l = new List<byte>();
}
}
if(l.Count > 0)
yield return l;
}
可能有更好的方法,但这里是我以前使用过的方法:它适用于相对较小的集合:
byte startDelimit = 23;
byte endDelimit = 11;
List<ICollection<byte>> result = new List<ICollection<byte>>();
int lastMatchingPosition = 0;
var inputAsList = input.ToList();
for(int i = 0; i <= inputAsList.Count; i++)
{
if(inputAsList[i] == startDelimit && inputAsList[i + 1] == endDelimit)
{
ICollection<byte> temp = new ICollection<byte>();
for(int j = lastInputPosition; j <= i ; j++)
{
temp.Add(inputAsList[j]);
}
result.Add(temp);
lastMatchingPosition = i + 2;
}
}
我现在没有打开我的IDE,所以我不能按原样编译,或者可能有一些需要插入的漏洞。但当我遇到这个问题时,这就是我的起点。同样,正如我之前所说,如果这是针对大型收藏,它将是缓慢的——因此可能还存在更好的解决方案。