在 C# 中,IEnumerable<T>
是一个泛型接口,用于表示可迭代的集合。以下是可以用作 IEnumerable<Point[]>
(或其他 IEnumerable<T>
)参数的所有集合类型及其使用场景和示例:
1. 标准泛型集合类型
以下集合直接实现了 IEnumerable<T>
,可以无缝传递:
集合类型 | 特点 | 示例 |
---|---|---|
数组 T[] | 静态大小、内存连续,访问最快 | Point[][] contourArray = new Point[2][]; |
List<T> | 动态数组,支持增删、索引 | List<Point[]> contourList = new List<Point[]>(); |
LinkedList<T> | 双向链表,高效插入/删除 | LinkedList<Point[]> linkedContours = new LinkedList<Point[]>(); |
HashSet<T> | 无序、不重复元素 | HashSet<Point[]> uniqueContours = new HashSet<Point[]>(); |
Queue<T> | 先进先出(FIFO)队列 | Queue<Point[]> contourQueue = new Queue<Point[]>(); |
Stack<T> | 后进先出(LIFO)栈 | Stack<Point[]> contourStack = new Stack<Point[]>(); |
SortedSet<T> | 有序集合,自动排序 | SortedSet<Point[]> sortedContours = new SortedSet<Point[]>(); |
ObservableCollection<T> | 支持数据绑定和通知(WPF) | var observableContours = new ObservableCollection<Point[]>(); |
2. LINQ查询结果
所有 LINQ 操作返回 IEnumerable<T>
,可直接传递:
// 示例:通过Where筛选有效轮廓
IEnumerable<Point[]> filteredContours = contours.Where(c => c.Length >= 3);
TestRegionContours(filteredContours, points);
// 示例:使用Select转换数据
IEnumerable<Point[]> scaledContours = contours.Select(c => ScaleContour(c, 2.0));
3. 非泛型集合
通过 Cast<T>()
或 OfType<T>()
可以将非泛型集合转为 IEnumerable<T>
:
// ArrayList(需引入System.Collections)
ArrayList legacyContours = new ArrayList();
legacyContours.Add(new Point[] { new Point(0,0), new Point(10,0), new Point(5,10) });
// 转换为IEnumerable<Point[]>
var converted = legacyContours.Cast<Point[]>();
TestRegionContours(converted, points);
4. 自定义可迭代类型
只要实现 IEnumerable<T>
接口的自定义集合均可传递:
// 自定义轮廓集合类
public class ContourCollection : IEnumerable<Point[]>
{
private readonly List<Point[]> _contours = new List<Point[]>();
public void Add(Point[] contour) => _contours.Add(contour);
// 实现迭代器
public IEnumerator<Point[]> GetEnumerator() => _contours.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
// 使用自定义集合
var customContours = new ContourCollection();
customContours.Add(new Point[] { new Point(0,0), new Point(10,0), new Point(5,10) });
TestRegionContours(customContours, points);
5. 其他特殊场景
类型 | 说明 | 示例 |
---|---|---|
ImmutableArray<T> | 不可变数组(线程安全) | var immutableArr = ImmutableArray.Create(new Point[] { ... }); |
ConcurrentQueue<T> | 线程安全队列 | ConcurrentQueue<Point[]> threadSafeQueue = new ConcurrentQueue<Point[]>(); |
Span<T> /Memory<T> | 内存片段视图(需要高版本 C#,性能敏感场景) | Span<Point[]> contourSpan = array.AsSpan(); |
yield return 生成器 | 动态生成数据 | IEnumerable<Point[]> GenerateContours() { yield return contour1; ... } |
6. 集合类型选用策略(针对图像处理场景)
(注:根据实际数据类型的访问模式选择最优集合)
场景 | 推荐集合类型 | 原因 |
---|---|---|
高性能计算(100万+点) | Point[][] (多维数组) | 内存连续,CPU缓存友好,并行计算优化 |
动态增删轮廓(交互编辑) | List<Point[]> | 支持高效增删操作 |
检测重复轮廓 | HashSet<Point[]> | 快速判重(需注意比较逻辑可能需要自定义 IEqualityComparer ) |
多线程生产消费(如实时处理) | ConcurrentQueue<Point[]> | 线程安全性高 |
GPU加速计算 | Memory<Point[]> | 便于与GPU内存交互 |
代码示例:多种集合的典型用法
// 场景1:从OpenCV接收数组
Point[][] opencvContours = Cv2.FindContours(...);
TestRegionContours(opencvContours, points);
// 场景2:动态构建复杂轮廓集
var dynamicContours = new List<Point[]>();
dynamicContours.AddRange(LoadCadContours());
dynamicContours.Add(GenerateCircleContour(100, 100, 50));
TestRegionContours(dynamicContours, points);
// 场景3:LINQ混合操作
var optimizedContours = originalContours
.Where(c => c.Length > 5) // 过滤简单轮廓
.Select(SimplifyContour) // 简化多边形
.OrderByDescending(c => c.Length); // 按点数量排序
TestRegionContours(optimizedContours, points);
// 场景4:多线程填充
ConcurrentBag<Point[]> parallelContours = new ConcurrentBag<Point[]>();
Parallel.ForEach(inputImages, img =>
{
Point[] contour = ExtractContour(img);
parallelContours.Add(contour);
});
TestRegionContours(parallelContours, points);
高级主题:内存布局优化
对于超大规模轮廓处理(如50万+轮廓),可通过以下方式优化:
// 1. 使用结构体替代类(减少堆内存开销)
public readonly struct ContourPoint { public int X; public int Y; }
ContourPoint[][] memoryOptimizedContours = ...;
// 2. 统一分配连续内存(提升缓存命中率)
ContourPoint[] buffer = new ContourPoint[totalPoints];
List<ContourPoint[]> overlappedContours = SplitBufferToContours(buffer);
// 3. SIMD矢量化检测(需要System.Numerics)
Vector4[] vectorizedRects = PrecomputeBoundingRects(contours);
通过选择合适的集合类型,可以显著提升轮廓处理应用的性能,尤其是在工业级图像处理的实时场景中。