Skip to content

Feat/array extensions 2d helper methods #150

Merged
GeWuYou merged 3 commits into
mainfrom
feat/array-extensions-2d-helper-methods-
Mar 30, 2026
Merged

Feat/array extensions 2d helper methods #150
GeWuYou merged 3 commits into
mainfrom
feat/array-extensions-2d-helper-methods-

Conversation

@GeWuYou
Copy link
Copy Markdown
Owner

@GeWuYou GeWuYou commented Mar 29, 2026

Summary by Sourcery

为二维数组添加用于边界检查、安全访问、邻居发现、枚举和尺寸查询的辅助扩展方法。

新特性:

  • 引入通用的 IsInBounds 扩展方法,用于验证二维数组中的坐标是否合法。
  • 为二维数组添加安全元素访问器,包括基于默认值和回退值的获取方式以及 TryGet 模式。
  • 提供辅助方法,以获取二维数组中 4 向和 8 向邻居的坐标。
  • 添加枚举辅助方法,用于迭代二维数组元素及其对应坐标。
  • 暴露用于获取二维数组宽度和高度的便捷扩展属性(WidthHeight)。
Original summary in English

Summary by Sourcery

Add 2D array helper extensions for bounds checking, safe access, neighbor discovery, enumeration, and size queries.

New Features:

  • Introduce generic IsInBounds extension for validating coordinates within 2D arrays.
  • Add safe element accessors for 2D arrays, including default and fallback-based retrieval and TryGet pattern.
  • Provide helpers to retrieve 4-directional and 8-directional neighbor coordinates in 2D arrays.
  • Add enumeration helper to iterate 2D array elements with their coordinates.
  • Expose Width and Height convenience extensions for 2D arrays.

GeWuYou added 2 commits March 29, 2026 22:04
- 实现 IsInBounds 扩展方法用于检查二维数组坐标边界
- 提供泛型支持以适配不同类型的二维数组
- 包含完整的 XML 文档注释说明方法用途和参数
- 遵循 Apache 2.0 开源协议规范添加版权头文件
- 添加 GetOrDefault 方法用于获取越界时返回默认值的元素
- 添加 GetOr 方法用于获取越界时返回指定回退值的元素
- 添加 TryGet 方法用于尝试获取指定位置的元素
- 添加 GetNeighbors4 方法用于获取四个方向的邻居坐标
- 添加 GetNeighbors8 方法用于获取八个方向的邻居坐标
- 添加 Enumerate 方法用于枚举数组中所有元素及坐标
- 添加 Width 和 Height 方法分别获取数组宽高维度
- 在文件头注释中修复版权年份格式问题
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Mar 29, 2026

审阅者指南

新增 ArrayExtensions 实用工具类,为二维数组 T[,] 提供常用辅助方法,包括边界检查、安全元素访问、邻居枚举、完整枚举以及维度访问器。

新增 ArrayExtensions 二维辅助方法的类图

classDiagram
class ArrayExtensions {
  <<static>>
  +bool IsInBounds_T_(T[,] array, int x, int y)
  +T GetOrDefault_T_(T[,] array, int x, int y)
  +T GetOr_T_(T[,] array, int x, int y, T fallback)
  +bool TryGet_T_(T[,] array, int x, int y, out T value)
  +IEnumerable_ValueTuple_int_int_ GetNeighbors4_T_(T[,] array, int x, int y)
  +IEnumerable_ValueTuple_int_int_ GetNeighbors8_T_(T[,] array, int x, int y)
  +IEnumerable_ValueTuple_int_int_T_ Enumerate_T_(T[,] array)
  +int Width_T_(T[,] array)
  +int Height_T_(T[,] array)
}

class T
class GFramework_Core_Extensions

ArrayExtensions <.. T
Loading

文件级更改

Change Details Files
引入一个静态扩展类,为二维数组添加边界检查、安全访问、邻居遍历以及维度辅助方法。
  • 添加 IsInBounds<T>,用于根据数组维度验证二维数组索引是否在边界内
  • 添加 GetOrDefault<T>GetOr<T>,在越界时使用默认值或调用方提供的回退值,安全读取元素
  • 添加 TryGet<T>,在索引位于边界内时返回 bool 并输出元素值,在不在边界时使用 default!
  • 添加 GetNeighbors4<T>GetNeighbors8<T>,使用值元组惰性枚举指定单元格周围在边界内的邻居坐标
  • 添加 Enumerate<T>,通过嵌套循环枚举二维数组的所有坐标和值
  • 添加 Width<T>Height<T> 便捷访问器,用于获取二维数组第一和第二维的大小
GFramework.Core/Extensions/ArrayExtensions.cs

技巧与命令

与 Sourcery 交互

  • 触发新的审阅: 在 Pull Request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub Issue: 在审阅评论下回复,要求 Sourcery 基于该评论创建一个 Issue。你也可以在审阅评论下回复 @sourcery-ai issue 来从该评论创建 Issue。
  • 生成 Pull Request 标题: 在 Pull Request 标题的任意位置写上 @sourcery-ai,即可随时生成标题。也可以在 Pull Request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 Pull Request 摘要: 在 Pull Request 的正文任意位置写上 @sourcery-ai summary,即可在对应位置生成 PR 摘要。也可以在 Pull Request 中评论 @sourcery-ai summary 来在任意时间(重新)生成摘要。
  • 生成审阅者指南: 在 Pull Request 中评论 @sourcery-ai guide,即可在任意时间(重新)生成审阅者指南。
  • 解决所有 Sourcery 评论: 在 Pull Request 中评论 @sourcery-ai resolve,即可将所有 Sourcery 评论标记为已解决。如果你已经处理完所有评论且不想再看到它们,这会很有用。
  • 撤销所有 Sourcery 审阅: 在 Pull Request 中评论 @sourcery-ai dismiss,即可撤销所有现有的 Sourcery 审阅。特别适用于你想从头开始新的审阅时——别忘了再评论 @sourcery-ai review 来触发新的审阅!

自定义你的体验

访问你的 控制面板 可以:

  • 启用或禁用审阅功能,例如 Sourcery 生成的 Pull Request 摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅指令。
  • 调整其他审阅设置。

获取帮助

Original review guide in English

Reviewer's Guide

Adds a new ArrayExtensions utility class providing common 2D array helpers, including bounds checking, safe element access, neighbor enumeration, full enumeration, and dimension accessors for T[,] arrays.

Class diagram for new ArrayExtensions 2D helpers

classDiagram
class ArrayExtensions {
  <<static>>
  +bool IsInBounds_T_(T[,] array, int x, int y)
  +T GetOrDefault_T_(T[,] array, int x, int y)
  +T GetOr_T_(T[,] array, int x, int y, T fallback)
  +bool TryGet_T_(T[,] array, int x, int y, out T value)
  +IEnumerable_ValueTuple_int_int_ GetNeighbors4_T_(T[,] array, int x, int y)
  +IEnumerable_ValueTuple_int_int_ GetNeighbors8_T_(T[,] array, int x, int y)
  +IEnumerable_ValueTuple_int_int_T_ Enumerate_T_(T[,] array)
  +int Width_T_(T[,] array)
  +int Height_T_(T[,] array)
}

class T
class GFramework_Core_Extensions

ArrayExtensions <.. T
Loading

File-Level Changes

Change Details Files
Introduce a static extension class that adds bounds checking, safe access, neighbor traversal, and dimension helpers for 2D arrays.
  • Add IsInBounds for validating 2D array indices against array dimensions
  • Add GetOrDefault and GetOr to safely read elements with default or caller-provided fallback values when out of bounds
  • Add TryGet that returns a bool and outputs the element when indices are within bounds, using default! when not
  • Add GetNeighbors4 and GetNeighbors8 to lazily enumerate in-bounds neighbor coordinates around a given cell using value tuples
  • Add Enumerate to yield all coordinates and values of a 2D array via nested loops
  • Add Width and Height convenience accessors for the first and second dimensions of a 2D array
GFramework.Core/Extensions/ArrayExtensions.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepsource-io
Copy link
Copy Markdown

deepsource-io Bot commented Mar 29, 2026

DeepSource Code Review

We reviewed changes in 76a1406...fa82c33 on this pull request. Below is the summary for the review, and you can see the individual issues we found as inline review comments.

See full review on DeepSource ↗

PR Report Card

Overall Grade   Security  

Reliability  

Complexity  

Hygiene  

Code Review Summary

Analyzer Status Updated (UTC) Details
C# Mar 30, 2026 12:18a.m. Review ↗
Secrets Mar 30, 2026 12:18a.m. Review ↗

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我在这里给出一些高层次的反馈:

  • IsInBoundsEnumerate 循环中,你多次调用 GetLength(0/1);建议将这些长度缓存在局部变量中,以避免在紧凑循环中重复进行边界元数据查找。
  • TryGet 在越界时会将 default! 赋值给 value;如果你预期会使用引用类型或可为空的值类型,可能需要使用可空性特性来标注这个 out 参数,或者明确在文档中说明,当失败时 value 会是 default(T),以提升可读性和清晰度。
给 AI Agent 的提示词
Please address the comments from this code review:

## Overall Comments
- In `IsInBounds` and the `Enumerate` loops you repeatedly call `GetLength(0/1)`; consider caching these lengths in local variables to avoid redundant bounds metadata lookups in tight loops.
- `TryGet` assigns `default!` to `value` when out of bounds; if you expect reference types or nullable value types, you might want to annotate the out parameter with nullable attributes or explicitly document that `value` will be `default(T)` on failure for clarity.

Sourcery 对开源项目是免费的——如果你喜欢我们的代码审查,请考虑分享给他人 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈来改进后续的代码审查。
Original comment in English

Hey - I've left some high level feedback:

  • In IsInBounds and the Enumerate loops you repeatedly call GetLength(0/1); consider caching these lengths in local variables to avoid redundant bounds metadata lookups in tight loops.
  • TryGet assigns default! to value when out of bounds; if you expect reference types or nullable value types, you might want to annotate the out parameter with nullable attributes or explicitly document that value will be default(T) on failure for clarity.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `IsInBounds` and the `Enumerate` loops you repeatedly call `GetLength(0/1)`; consider caching these lengths in local variables to avoid redundant bounds metadata lookups in tight loops.
- `TryGet` assigns `default!` to `value` when out of bounds; if you expect reference types or nullable value types, you might want to annotate the out parameter with nullable attributes or explicitly document that `value` will be `default(T)` on failure for clarity.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

- 在 IsInBounds 和 Enumerate 方法中缓存数组维度长度,避免重复读取元数据
- 为 TryGet 方法添加 MaybeNullWhen 特性注解以改善空值检查
- 更新 TryGet 方法文档说明其在失败时返回默认值的行为
- 新增 ArrayExtensionsTests 类,包含 TryGet 和 Enumerate 方法的完整测试用例
- 添加 using System.Diagnostics.CodeAnalysis 引入空值相关特性支持
@GeWuYou GeWuYou merged commit 63cb6b5 into main Mar 30, 2026
9 checks passed
@GeWuYou GeWuYou deleted the feat/array-extensions-2d-helper-methods- branch March 30, 2026 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant