上次编写的C# 查找大集合中包含多少个小集合&删除大集合中的小集合-CSDN博客 中删除大集合中的小集合在数据量比较大(百万条数据)的时候速度过慢。所以对代码进行优化,提高数据处理能力。
/// <summary>
/// 删除大集合和中所有的小集合
/// </summary>
/// <param name="list">大集合</param>
/// <param name="childList">小集合</param>
/// <returns>删除后的值</returns>
public static List<byte> ListRemoveList(this List<byte> list, List<byte> childList)
{
List<int> indexs = new List<int>();
if (list == null || list.Count == 0 || childList == null || childList.Count == 0)
{
return list;
}
for (int i = 0; i < list.Count; i++)
{
if (childList[0] == list[i] && (i + childList.Count <= list.Count))
{
bool success = true;
for (int j = 0; j < childList.Count; j++)
{
if (list[i + j] != childList[j])
{
success = false;
break;
}
}
if (success)
{
list.RemoveRange(i, childList.Count);
i -= childList.Count;
if (i < 0) i = 0;
}
}
}
return list;
}
关于代码的使用情况。
一、查找大集合中包含多少小集合
在串口通讯中会接收设备返回的字节数组,一些设备返回的消息会有一个头标识(如{ 0x3A, 0xA3 }),由于可能返回的数据比较大会拆分成多次返回,或者是多组数据同时返回,所以可以根据此方法对数据进行拼合或拆分,用于找到完整的数据。
二、删除大集合中的小集合
前段时间调用Edge浏览器的大声朗读功能时,使用websocket 通讯,返回音频数据时会有一个相同的数据头,需要把数据头删除后再保存为音频文件才行。所以写了这个方法进行处理。