Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从嵌套稀疏字典生成IEnumerable_C#_Ienumerable - Fatal编程技术网

C#从嵌套稀疏字典生成IEnumerable

C#从嵌套稀疏字典生成IEnumerable,c#,ienumerable,C#,Ienumerable,我有一个稀疏的元素矩阵,表示为 Dictionary<int, Dictionary<int, StructuredCell>> CellValues = new Dictionary<int, Dictionary<int, StructuredCell>>(); 其中,列数和行数的最大值范围内的任何x,y组合返回为null。我似乎找不到这样使用yield的示例 public IEnumerable<StructuredCell>

我有一个稀疏的元素矩阵,表示为

Dictionary<int, Dictionary<int, StructuredCell>> CellValues = new Dictionary<int, Dictionary<int, StructuredCell>>();

其中,列数和行数的最大值范围内的任何x,y组合返回为null。我似乎找不到这样使用
yield
的示例

public IEnumerable<StructuredCell> Cells(){
    for (int i = 0; i < maxColumn; i++)
        {
            Dictionary<int, StructuredCell> row = null;
            CellValues.TryGetValue(i, out row);
            for (int j = 0; j < maxRow; j++)
            {
                if (row == null) yield return null;
                StructuredCell cell = null;
                row.TryGetValue(j, out cell);
                yield return cell;
            }
        }
}
public IEnumerable单元格(){
对于(int i=0;i
类似

public IEnumerable<StructuredCell> Cells(){
    for (int i = 0; i < maxColumn; i++)
        {
            Dictionary<int, StructuredCell> row = null;
            CellValues.TryGetValue(i, out row);
            for (int j = 0; j < maxRow; j++)
            {
                if (row == null) yield return null;
                StructuredCell cell = null;
                row.TryGetValue(j, out cell);
                yield return cell;
            }
        }
}
public IEnumerable单元格(){
对于(int i=0;i
基于密钥非常小这一事实,您可以在此处进行大量优化

public class DataStructure {
    private const int MAX_VALUE = 100000;
    private readonly Dictionary<long, StructuredCell> CellValues;

    private void Add(int keyOne, int keyTwo, StructuredCell cell) {
        long hashKey = keyOne*MAX_VALUE + keyTwo;

        CellValues[hashKey] = cell;
    }

    private void Remove(int keyOne, int keyTwo)
    {
        long hashKey = keyOne * MAX_VALUE + keyTwo;

        CellValues.Remove(hashKey);
    }

    private IEnumerable<StructuredCell> GetCells() {
        return CellValues.Values;
    }
}
公共类数据结构{
私有常量int最大值=100000;
私有只读字典值;
私有void Add(int-keyOne、int-keyTwo、StructuredCell单元格){
长hashKey=keyOne*MAX_值+keytoo;
CellValues[hashKey]=单元格;
}
私有无效删除(int-keyOne,int-keyTwo)
{
长hashKey=keyOne*MAX_值+keytoo;
CellValues.Remove(hashKey);
}
私有IEnumerable GetCells(){
返回单元格值;
}
}
您可以保留一个简单的键->值字典,其中

key=hash(key-one,key-two)


您不需要任何花哨的惰性构造(yield),因为您已经有了可用的值。

基于键非常小的事实,您可以在这里进行许多优化

public class DataStructure {
    private const int MAX_VALUE = 100000;
    private readonly Dictionary<long, StructuredCell> CellValues;

    private void Add(int keyOne, int keyTwo, StructuredCell cell) {
        long hashKey = keyOne*MAX_VALUE + keyTwo;

        CellValues[hashKey] = cell;
    }

    private void Remove(int keyOne, int keyTwo)
    {
        long hashKey = keyOne * MAX_VALUE + keyTwo;

        CellValues.Remove(hashKey);
    }

    private IEnumerable<StructuredCell> GetCells() {
        return CellValues.Values;
    }
}
公共类数据结构{
私有常量int最大值=100000;
私有只读字典值;
私有void Add(int-keyOne、int-keyTwo、StructuredCell单元格){
长hashKey=keyOne*MAX_值+keytoo;
CellValues[hashKey]=单元格;
}
私有无效删除(int-keyOne,int-keyTwo)
{
长hashKey=keyOne*MAX_值+keytoo;
CellValues.Remove(hashKey);
}
私有IEnumerable GetCells(){
返回单元格值;
}
}
您可以保留一个简单的键->值字典,其中

key=hash(key-one,key-two)



你不需要任何奇特的惰性构造(yield),因为你已经有了可用的值。

所以你想写
Cells()
,这样它就可以从嵌套字典中返回所有嵌入的
StructuredCell
值?是的,我想写一个
Cells()
返回所有单元格的可枚举列表,将空的填充为空。我之所以想这样做,是因为整个列表永远不会完全在内存中。我假设
IEnumerable
&
yield
不需要内存中的完整列表,但可以在运行时计算每个列表。“其中,列和行数的最大值范围内的任何x,y组合返回为null。”这意味着什么?如果你坚持使用
yield
试试其他有效的方法(比如填写列表),然后找到更好的方法。你字典中的整数键有多大?我不是坚持使用yield,只是好奇而已。假设字典中唯一的
CellValue
CellValues[10][10]=Object
那么
Cells()
将返回一个IEnumerable,它将为
CellValues[0][0]、CellValues[0][1]、CellValues[0][2]、…CellValues[1][1]、…
返回空值,因此您想编写
Cells()
以便它从嵌套字典中返回所有嵌入的
StructuredCell
值?是的,我想编写
Cells()
,返回所有单元格的可枚举列表,将空单元格填充为空。我之所以想这样做,是因为整个列表永远不会完全在内存中。我假设
IEnumerable
&
yield
不需要内存中的完整列表,但可以在运行时计算每个列表。“其中,列和行数的最大值范围内的任何x,y组合返回为null。”这意味着什么?如果你坚持使用
yield
试试其他有效的方法(比如填写列表),然后找到更好的方法。你字典中的整数键有多大?我不是坚持使用yield,只是好奇而已。假设字典中唯一的
CellValue
CellValues[10][10]=Object
那么
Cells()
将返回一个IEnumerable,它将为
CellValues[0][0]、CellValues[0][1]、CellValues[0][2]、…CellValues[1][1]返回空值,…
这不会将缺少的值返回为
null
,但会吗?这样做的想法是,这个字典可以满10%,并且
IEnumerable
将返回100%的单元格。因此,您需要一个MaxColumn和MaxRow变量。并使用for循环,这不会将缺少的值返回为
null
,但会吗?这样做的想法是,这个字典可以满10%,并且
IEnumerable
将返回100%的单元格。因此,您需要一个MaxColumn和MaxRow变量。并使用循环键映射进行良好的思考。在对yield方法进行性能测试以进行比较之后,我可能会在中使用该方法。您可以使用
string
而不是
long
,以提高可读性<代码>$“{keytone},{keyTwo}”
@Kalten不,因为