如何解决如何根据同一 ref 结构内另一个 Span<T> 的值对 ref 结构内的 Span<T> 进行排序?
我有以下引用结构:
public ref struct WordCollection
{
public int length;
public int currentIndex;
public Span<byte> buffer;
public Span<int> indexes;
public Span<int> wordSize;
public Span<int> wordLocation;
public Span<int> wordCount;
}
我想根据 indexes
对 wordCount
进行排序,方法是将 wordCount[indexes[i]]
与 wordCount[indexes[j]]
进行比较
第一个想法是使用 Sort<T>(Span<T>,Comparison<T>)
但是这样的事情是行不通的
void DoStuff()
{
WordCollection wordCollection = new WordCollection
{
length = 0,currentIndex = 0,buffer = stackalloc byte[MaxBufferSize],indexes = stackalloc int[MaxItems],wordSize = stackalloc int[MaxCollectionSize],wordLocation = stackalloc int[MaxCollectionSize],wordCount = stackalloc int[MaxCollectionSize]
};
...................
static int Compare(int x,int y)
{
return wordCollection.wordCount[x].Compareto(wordCollection.wordCount[y]);
}
wordCollection.indexes.sort(Compare);
}
错误是:不能在匿名方法、lambda 表达式或查询表达式中使用 ref local 'wordCollection'
还有 public static void Sort<TKey,TValue,TComparer> (this Span<TKey> keys,Span<TValue> items,TComparer comparer) where TComparer : System.Collections.Generic.IComparer<TKey>;
但我也不能使用它,因为 ref 结构不能实现接口。
那么,有没有一种方法可以使用框架提供的 Sort 方法按 indexes
对 wordCounts[indexes[i]]
进行排序,而无需编写我自己的排序代码?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。