C# 使用 Index 和 Range 简化集合操作
C# 使用 Index 和 Range 简化集合操作
Intro
有的语言数组的索引值是支持负数的,表示从后向前索引,比如:arr[-1]
从 C# 8 开始,C# 支持了数组的反向 Index,和 Range 操作,反向 Index 类似于其他语言中的负索引值,但其实是由编译器帮我们做了一个转换,Range 使得我们对数组截取某一部分的操作会非常简单,下面来看一下如何使用吧
Sample
使用 ^ 可以从集合的最后开始索引元素,如果从数组的最后开始索引元素,最后一个元素应该是 1 而不是0如: arr[^1]
使用 .. 可以基于某个数组截取集合中的某一段创建一个新的数组,比如 var newArray = array[1..^1],再来看一下下面的示例吧
int[] someArray = new int[5] { 1, 2, 3, 4, 5 };
int lastElement = someArray[^1]; // lastElement = 5
lastElement.Dump();
someArray[3..5].Dump();
someArray[1..^1].Dump();
someArray[1..].Dump();
someArray[..^1].Dump();
someArray[..2].Dump();
输出结果如下:

Index
那么它是如何实现的呢,索引值引入了一个新的数据结构 System.Index,当你使用 ^ 运算符的时候,实际转换成了 Index。
Index:
public readonly struct Index : IEquatable<Index>
{
public Index(int value, bool fromEnd = false);
/// <summary>Create an Index pointing at first element.</summary>
public static Index Start => new Index(0);
/// <summary>Create an Index pointing at beyond last element.</summary>
public static Index End => new Index(~0);
//
// Summary:
// Gets a value that indicates whether the index is from the start or the end.
//
// Returns:
// true if the Index is from the end; otherwise, false.
public bool IsFromEnd { get; }
//
// Summary:
// Gets the index value.
//
// Returns:
// The index value.
public int Value { get; }
//
// Summary:
// Creates an System.Index from the end of a collection at a specified index position.
//
// Parameters:
// value:
// The index value from the end of a collection.
//
// Returns:
// The Index value.
public static Index FromEnd(int value);
//
// Summary:
// Create an System.Index from the specified index at the start of a collection.
//
// Parameters:
// value:
// The index position from the start of a collection.
//
// Returns:
// The Index value.
public static Index FromStart(int value);
//
// Summary:
// Returns a value that indicates whether the current object is equal to another
// System.Index object.
//
// Parameters:
// other:
// The object to compare with this instance.
//
// Returns:
// true if the current Index object is equal to other; false otherwise.
public bool Equals(Index other);
//
// Summary:
// Calculates the offset from the start of the collection using the given collection length.
//
// Parameters:
// length:
// The length of the collection that the Index will be used with. Must be a positive value.
//
// Returns:
// The offset.
public int GetOffset(int length);
//
// Summary:
// Converts integer number to an Index.
//
// Parameters:
// value:
// The integer to convert.
//
// Returns:
// An Index representing the integer.
public static implicit operator Index(int value);
}
如果想要自己自定义的集合支持 Index 这种从数组最后索引的特性,只需要加一个类型是 Index 的索引器就可以了,正向索引也是支持的,int 会自动隐式转换为 Index,除了显示的增加 Index 索引器之外,还可以隐式支持,实现一个 int Count {get;} 的属性(属性名叫 Length 也可以),在实现一个 int 类型的索引器就可以了
写一个简单的小示例:
private class TestCollection
{
public IList<int> Data { get; init; }
public int Count => Data.Count;
public int this[int index] => Data[index];
//public int this[Index index] => Data[index.GetOffset(Data.Count)];
}
var array = new TestCollection()
{
Data = new[] { 1, 2, 3 }
};
Console.WriteLine(array[^1]);
Console.WriteLine(array[1]);
Range
Range 是在 Index 的基础上实现的,Range 需要两个 Index 来指定开始和结束
public readonly struct Range : IEquatable<Range>
{
/// <summary>Represent the inclusive start index of the Range.</summary>
public Index Start { get; }
/// <summary>Represent the exclusive end index of the Range.</summary>
public Index End { get; }
/// <summary>Construct a Range object using the start and end indexes.</summary>
/// <param name="start">Represent the inclusive start index of the range.</param>
/// <param name="end">Represent the exclusive end index of the range.</param>
public Range(Index start, Index end)
{
Start = start;
End = end;
}
/// <summary>Create a Range object starting from start index to the end of the collection.</summary>
public static Range StartAt(Index start) => new Range(start, Index.End);
/// <summary>Create a Range object starting from first element in the collection to the end Index.</summary>
public static Range EndAt(Index end) => new Range(Index.Start, end);
/// <summary>Create a Range object starting from first element to the end.</summary>
public static Range All => new Range(Index.Start, Index.End);
/// <summary>Calculate the start offset and length of range object using a collection length.</summary>
/// <param name="length">The length of the collection that the range will be used with. length has to be a positive value.</param>
/// <remarks>
/// For performance reason, we don't validate the input length parameter against negative values.
/// It is expected Range will be used with collections which always have non negative length/count.
/// We validate the range is inside the length scope though.
/// </remarks>
public (int Offset, int Length) GetOffsetAndLength(int length);
}
如何在自己的类中支持 Range 呢?
一种方式是自己直接实现一个类型是 Range 的索引器
另外一种方式是隐式实现,在自定义类中添加一个 Count 属性,然后实现一个 Slice 方法,Slice 方法有两个 int 类型的参数,第一个参数表示 offset,第二个参数表示 length
来看下面这个示例吧,还是刚才那个类,我们支持一下 Range:
private class TestCollection
{
public IList<int> Data { get; init; }
//public int[] this[Range range]
//{
// get
// {
// var rangeInfo = range.GetOffsetAndLength(Data.Count);
// return Data.Skip(rangeInfo.Offset).Take(rangeInfo.Length).ToArray();
// }
//}
public int Count => Data.Count;
public int[] Slice(int start, int length)
{
var array = new int[length];
for (var i = start; i < length && i < Data.Count; i++)
{
array[i] = Data[i];
}
return array;
}
}
More
新的操作符 (^ and ..) 都只是语法糖,本质上是调用 Index、Range
Index 并不是支持负数索引,从最后向前索引只是编译器帮我们做了一个转换,转换成从前到后的索引值,借助于它,我们很多取集合最后一个元素的写法就可以大大的简化了,就可以从原来的 array[array.Length-1] => array[^1]
Range 在创建某个数组的子序列的时候就非常的方便, newArray = array[1..3],需要注意的是,Range 是"左闭右开"的,包含左边界的值,不包含右边界的值
还没使用过 Index/Range 的快去体验一下吧,用它们优化数组的操作吧~~
References
- https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/ranges-indexes
- https://docs.microsoft.com/en-us/dotnet/api/system.index?view=net-5.0
- https://docs.microsoft.com/en-us/dotnet/api/system.range?view=net-5.0
- https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges
- https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Index.cs
- https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Range.cs
- https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs
C# 使用 Index 和 Range 简化集合操作的更多相关文章
- 使用jdk8 stream简化集合操作
使用stream的前提是对lambda表达式和函数式接口有一定的了解,同时对方法引用和普通传参的区别有一定的认识. stream的三大特性:1.不存储数据2.不改变源数据3.延时执行. stream优 ...
- 函数式Android编程(II):Kotlin语言的集合操作
原文标题:Functional Android (II): Collection operations in Kotlin 原文链接:http://antonioleiva.com/collectio ...
- Linq/List/Array/IEnumerable等集合操作
来源:http://www.cnblogs.com/liushanshan/archive/2011/01/05/1926263.html 目录 1 LINQ查询结果集 1 2 Sy ...
- lodash用法系列(1),数组集合操作
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- Groovy系列(4)- Groovy集合操作
Groovy集合操作 Lists List 字面值 您可以按如下所示创建列表. 请注意,[]是空列表表达式 def list = [5, 6, 7, 8] assert list.get(2) == ...
- Django数据库性能优化之 - 使用Python集合操作
前言 最近有个新需求: 人员基础信息(记作人员A),10w 某种类型的人员信息(记作人员B),1000 要求在后台上(Django Admin)分别展示:已录入A的人员B列表.未录入的人员B列表 团队 ...
- JAVASE02-Unit04: 集合框架 、 集合操作 —— 线性表
Unit04: 集合框架 . 集合操作 -- 线性表 操作集合元素相关方法 package day04; import java.util.ArrayList; import java.util.Co ...
- Scala集合操作
大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: 1.数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储技术现在比较经典方案是使用Hadoop,不过也很多方案采用Kafka. ...
- Python 集合操作
1.集合操作 集合是一个无序的,不重复的数据组合, 他的主要作业如下. 1.去重,把一个列表变成集合,就自动去重了 2.关系测试,测试两组数据之前的交集.差集.并集等关系 list_1 = [1,4, ...
随机推荐
- kafka的演进历史
首先如果我开始做一个消息队列,最开始的时候可能就是一台单机上的一个单一的log日志,不断地向这个日志中追加消息即可. 后来,可能由于一个log日志支撑不了太多的读写请求,于是就对这个log日志进行了拆 ...
- PTA甲级—链表
1032 Sharing (25分) 回顾了下链表的基本使用,这题就是判断两个链表是否有交叉点. 我最开始的做法就是用cnt[]记录每个节点的入度,发现入度为2的节点即为答案.后来发现这里忽略了两个链 ...
- P4755 Beautiful Pair (分治 + 主席树)
题意:1e5的数组 计算有多少对 ai * aj <= max(ai ai+1...aj-1 aj) 题解:在处理这种涉及到区间极值的题时 好像是个套路分治 从级值中间分成两个区间 从区间短的那 ...
- 2019ICPC南昌邀请赛 Sequence
题意:给出n个点的权值,m次操作,操作为1时为询问,每次询问给出 l 和 r ,求 f(l,r).操作为0时为修改权值.f(l,r)=f(l,l)⊕f(l,l+1)⊕⋯⊕f(l,r)⊕f(l+1,l+ ...
- 关于贪心算法的经典问题(算法效率 or 动态规划)
如题,贪心算法隶属于提高算法效率的方法,也常与动态规划的思路相挂钩或一同出现.下面介绍几个经典贪心问题.(参考自刘汝佳著<算法竞赛入门经典>).P.S.下文皆是我一个字一个字敲出来的,绝对 ...
- P1435 回文字串(DP)
题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 "Ab3bd"插 ...
- Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! C. Johnny and Another Rating Drop (规律,二进制)
题意:有一个正整数\(n\),要求写出所有\(1\)~\(n\)的二进制数,统计相邻的两个二进制同位置上不同数的个数. 题解:打表找规律,不难发现: \(00000\) \(00001\) ...
- [Golang]-1 Slice与数组的区别
目录 数组 1.创建数组: 2.数组是值拷贝传递: 切片(slice) 1.首先看看slice的源码结构: 2.slice的创建: 3.slice使用make创建 4.切片作为参数传递 5.Golan ...
- kubernetes实战-配置中心(一)configmap资源
在我们的环境中测试使用configmap资源,需要先对我们的环境进行一些准备,首先将dubbo服务调整为0个pod ,然后把zookeeper进行拆分: 拆分zk环境,模拟测试环境跟生产环境: 停止z ...
- kubernetes实战-交付dubbo服务到k8s集群(五)交付dubbo-monitor监控服务到k8s
首先下载 dubbo-monitor源码包7-200 dubbo-monitor是监控zookeeper状态的一个服务,另外还有dubbo-admin,效果一样,感兴趣的可以自己研究一下. # cd ...