处理集合的时候会遇到两个集合的操作。比如使用websocket通讯时,会查找数据起始点。或者整合数据的情况。记录一下处理方式。
/// <summary>
/// 查找大集合和中包含多少个小集合
/// </summary>
/// <param name="list">大集合</param>
/// <param name="childList">小集合</param>
/// <returns>所在位置列表</returns>
public static List<int> IndexOfList(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 indexs;
}
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)
{
indexs.Add(i);
}
}
}
return indexs;
}
/// <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)
{
for (int j = 0; j < childList.Count; j++)
{
indexs.Add(i + j);
}
}
}
}
List<byte> rest = new List<byte>();
for (int i = 0; i < list.Count; i++)
{
if (!indexs.Contains(i))
rest.Add(list[i]);
}
return rest;
}