如何根据同一 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;        
    } 

我想根据 indexeswordCount 进行排序,方法是将 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 方法indexeswordCounts[indexes[i]] 进行排序,而无需编写我自己的排序代码

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)