C#之IEnumerable详解

在 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);

通过选择合适的集合类型,可以显著提升轮廓处理应用的性能,尤其是在工业级图像处理的实时场景中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X-Vision

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值