请拆招:将两个已排序集合分解成两个独立部分的集合和一个共有部分的集合?
请拆招:将两个已排序集合分解成两个独立部分的集合和一个共有部分的集合?
请看下面的代码。
请看下面的代码。
using System;
using System.Collections.Generic;
namespace Juxiantang
{
public static class CollectionOperate
{
/// <summary>
/// 对两个已排序的集合进行分解合并操作
/// a中独有的存放在c.A中
/// a,b都有的存放在c.AB中
/// b中独有的存放在c.B中
/// </summary>
/// <typeparam name="T">必须是可以比较的类型</typeparam>
/// <param name="a">集合a</param>
/// <param name="b">集合b</param>
/// <returns>返回c(A, AB, B)</returns>
public static ThreeCollection<T> BreakUpSortedCollection<T>(IEnumerable<T> a, IEnumerable<T> b)
where T : IComparable<T>
{
IEnumerator<T> ia = a.GetEnumerator();
IEnumerator<T> ib = b.GetEnumerator();
ThreeCollection<T> c = new ThreeCollection<T>();
if (!ia.MoveNext()) //a集合为空
{
c.B.AddRange(b);
return c;
}
if (!ib.MoveNext()) //b集合为空
{
c.A.AddRange(a);
return c;
}
ia.Reset();
ib.Reset();
ia.MoveNext();
ib.MoveNext();
Lookup<T>(ia, ib, c.A, c.AB, c.B);
return c;
}
/// <summary>
/// 针对集合a的当前值a.Current,对比集合b的当前值b.Current
/// 根据对比情况将a.Current或b.Current放入A, AB, B中
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ia"></param>
/// <param name="ib"></param>
/// <param name="A"></param>
/// <param name="AB"></param>
/// <param name="B"></param>
private static void Lookup<T>(IEnumerator<T> ia, IEnumerator<T> ib, List<T> A, List<T> AB, List<T> B)
where T : IComparable<T>
{
int compareResult = (ia.Current.CompareTo(ib.Current));
if (compareResult < 0) //a.Current < b.Current
{
A.Add(ia.Current);
if (ia.MoveNext())
{
Lookup<T>(ia, ib, A, AB, B);
}
else
{
do
{
B.Add(ib.Current);
} while (ib.MoveNext());
}
}
else if (compareResult == 0) //a.Current == b.Current
{
AB.Add(ia.Current);
if (!ia.MoveNext())
{
while (ib.MoveNext())
{
B.Add(ib.Current);
}
}
else if (ib.MoveNext())
{
Lookup<T>(ia, ib, A, AB, B);
}
}
else // a.Current > b.Current
{
B.Add(ib.Current);
if (ib.MoveNext())
{
Lookup<T>(ib, ia, B, AB, A);
}
else
{
do
{
A.Add(ia.Current);
} while (ia.MoveNext());
}
}
}
}
/// <summary>
/// 简单将A, AB, B集合在一起的实体
/// </summary>
/// <typeparam name="T">泛型</typeparam>
public class ThreeCollection<T>
{
List<T> a = new List<T>();
List<T> ab = new List<T>();
List<T> b = new List<T>();
public List<T> B
{
get { return b; }
set { b = value; }
}
public List<T> AB
{
get { return ab; }
set { ab = value; }
}
public List<T> A
{
get { return a; }
set { a = value; }
}
}
public class Program
{
static void Main()
{
int[] b = { 1, 2, 3, 4, 5, 6 };
int[] a = { 8, 10, 11 };
ThreeCollection<int> c = CollectionOperate.BreakUpSortedCollection<int>(a, b);
System.Console.Write("A = ");
Print<int>(c.A);
System.Console.Write("AB = ");
Print<int>(c.AB);
System.Console.Write("B = ");
Print<int>(c.B);
System.Console.ReadLine();
}
static void Print<T>(IEnumerable<T> list) where T : IFormattable
{
foreach (T o in list)
{
System.Console.Write(o.ToString());
System.Console.Write(" ");
}
System.Console.Write("\n");
}
}
}
using System.Collections.Generic;
namespace Juxiantang
{
public static class CollectionOperate
{
/// <summary>
/// 对两个已排序的集合进行分解合并操作
/// a中独有的存放在c.A中
/// a,b都有的存放在c.AB中
/// b中独有的存放在c.B中
/// </summary>
/// <typeparam name="T">必须是可以比较的类型</typeparam>
/// <param name="a">集合a</param>
/// <param name="b">集合b</param>
/// <returns>返回c(A, AB, B)</returns>
public static ThreeCollection<T> BreakUpSortedCollection<T>(IEnumerable<T> a, IEnumerable<T> b)
where T : IComparable<T>
{
IEnumerator<T> ia = a.GetEnumerator();
IEnumerator<T> ib = b.GetEnumerator();
ThreeCollection<T> c = new ThreeCollection<T>();
if (!ia.MoveNext()) //a集合为空
{
c.B.AddRange(b);
return c;
}
if (!ib.MoveNext()) //b集合为空
{
c.A.AddRange(a);
return c;
}
ia.Reset();
ib.Reset();
ia.MoveNext();
ib.MoveNext();
Lookup<T>(ia, ib, c.A, c.AB, c.B);
return c;
}
/// <summary>
/// 针对集合a的当前值a.Current,对比集合b的当前值b.Current
/// 根据对比情况将a.Current或b.Current放入A, AB, B中
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ia"></param>
/// <param name="ib"></param>
/// <param name="A"></param>
/// <param name="AB"></param>
/// <param name="B"></param>
private static void Lookup<T>(IEnumerator<T> ia, IEnumerator<T> ib, List<T> A, List<T> AB, List<T> B)
where T : IComparable<T>
{
int compareResult = (ia.Current.CompareTo(ib.Current));
if (compareResult < 0) //a.Current < b.Current
{
A.Add(ia.Current);
if (ia.MoveNext())
{
Lookup<T>(ia, ib, A, AB, B);
}
else
{
do
{
B.Add(ib.Current);
} while (ib.MoveNext());
}
}
else if (compareResult == 0) //a.Current == b.Current
{
AB.Add(ia.Current);
if (!ia.MoveNext())
{
while (ib.MoveNext())
{
B.Add(ib.Current);
}
}
else if (ib.MoveNext())
{
Lookup<T>(ia, ib, A, AB, B);
}
}
else // a.Current > b.Current
{
B.Add(ib.Current);
if (ib.MoveNext())
{
Lookup<T>(ib, ia, B, AB, A);
}
else
{
do
{
A.Add(ia.Current);
} while (ia.MoveNext());
}
}
}
}
/// <summary>
/// 简单将A, AB, B集合在一起的实体
/// </summary>
/// <typeparam name="T">泛型</typeparam>
public class ThreeCollection<T>
{
List<T> a = new List<T>();
List<T> ab = new List<T>();
List<T> b = new List<T>();
public List<T> B
{
get { return b; }
set { b = value; }
}
public List<T> AB
{
get { return ab; }
set { ab = value; }
}
public List<T> A
{
get { return a; }
set { a = value; }
}
}
public class Program
{
static void Main()
{
int[] b = { 1, 2, 3, 4, 5, 6 };
int[] a = { 8, 10, 11 };
ThreeCollection<int> c = CollectionOperate.BreakUpSortedCollection<int>(a, b);
System.Console.Write("A = ");
Print<int>(c.A);
System.Console.Write("AB = ");
Print<int>(c.AB);
System.Console.Write("B = ");
Print<int>(c.B);
System.Console.ReadLine();
}
static void Print<T>(IEnumerable<T> list) where T : IFormattable
{
foreach (T o in list)
{
System.Console.Write(o.ToString());
System.Console.Write(" ");
}
System.Console.Write("\n");
}
}
}