DseApp.ArrayUtils
1.0.0
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package DseApp.ArrayUtils --version 1.0.0
NuGet\Install-Package DseApp.ArrayUtils -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DseApp.ArrayUtils" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DseApp.ArrayUtils" Version="1.0.0" />
<PackageReference Include="DseApp.ArrayUtils" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DseApp.ArrayUtils --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DseApp.ArrayUtils, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DseApp.ArrayUtils@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DseApp.ArrayUtils&version=1.0.0
#tool nuget:?package=DseApp.ArrayUtils&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
DseApp.ArrayUtils
高性能的多维数组一维读写工具类,支持任意维度和基本类型的数组操作。
特性
- ✅ 支持任意维度数组(1D、2D、3D...N维)
- ✅ 支持所有基本类型(int、double、float、bool、string等)
- ✅ 高性能的批量写入操作
- ✅ 类型安全的泛型设计
- ✅ 完善的错误处理和边界检查
- ✅ 跨平台支持(.NET Standard 2.0+)
安装
dotnet add package DseApp.ArrayUtils
快速开始
using DseApp.ArrayUtils;
// 创建二维数组
int[,] matrix = new int[3, 4];
// 单个值读写
ArrayUtils.Write(matrix, 42, 5); // 在线性索引5位置写入42
int value = ArrayUtils.Read<int>(matrix, 5); // 读取线性索引5的值
// 批量写入
int[] data = new int[] { 100, 200, 300, 400 };
ArrayUtils.Write(matrix, data, 2); // 从索引2开始批量写入
// 获取数组信息
int totalLength = ArrayUtils.GetLength(matrix); // 返回12
string shapeInfo = ArrayUtils.GetShapeInfo(matrix); // "Rank: 2, Dimensions: [3, 4], Length: 12"
API 参考
核心方法
Read<T>(Array src, int index)
从任意维度数组中按线性索引读取值。
Write<T>(Array src, T value, int index)
向任意维度数组中按线性索引写入值。
Write<T>(Array src, T[] values, int startIndex)
批量向任意维度数组中按起始线性索引写入一维数组数据(高性能版本)。
GetLength(Array src)
获取任意维度数组的总元素个数。
GetLinearIndex(Array src, params int[] indices)
从多维索引获取线性索引。
GetShapeInfo(Array src)
获取数组形状信息。
性能优化
批量写入操作采用了优化的索引递进算法,避免了重复的索引计算:
// 传统方法:每次都重新计算索引
for (int i = 0; i < values.Length; i++)
{
int[] indices = GetIndicesFromLinearIndex(src, startIndex + i); // O(rank)
src.SetValue(values[i], indices);
}
// 总复杂度:O(n × rank)
// 优化方法:递进式索引更新
int[] indices = GetIndicesFromLinearIndex(src, startIndex); // 只计算一次
for (int i = 0; i < values.Length; i++)
{
src.SetValue(values[i], indices);
IncrementIndices(indices, dimensions); // O(1) 平均复杂度
}
// 总复杂度:O(n + rank)
支持的框架
- .NET Standard 2.0
- .NET Standard 2.1
- .NET 6.0
- .NET 7.0
- .NET 8.0
示例
二维数组操作
int[,] matrix = new int[3, 4];
// 初始化
for (int i = 0; i < ArrayUtils.GetLength(matrix); i++)
{
ArrayUtils.Write(matrix, i + 1, i);
}
// 读取特定位置
Console.WriteLine(ArrayUtils.Read<int>(matrix, 5)); // 输出: 6
// 批量修改
int[] newData = { 100, 200, 300 };
ArrayUtils.Write(matrix, newData, 3);
三维数组操作
int[,,] cube = new int[2, 3, 4];
int[] data = new int[24];
for (int i = 0; i < data.Length; i++)
{
data[i] = i + 1;
}
ArrayUtils.Write(cube, data, 0);
Console.WriteLine(ArrayUtils.Read<int>(cube, 23)); // 输出: 24
类型转换
double[,] doubleMatrix = new double[2, 2];
ArrayUtils.Write(doubleMatrix, 3.14159, 0);
double pi = ArrayUtils.Read<double>(doubleMatrix, 0);
贡献
欢迎提交 Issue 和 Pull Request!
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.