有没有比嵌套循环更好的方法?

is there a better way then nested loops?

我有以下代码:

(0...engine.rows-1).forEach {row in
        (0...engine.cols-1).forEach {col in
            //print("(\(row),\(col)) state = \(engine.grid[(row,col)])")
            switch engine.grid[(row,col)] {
            case CellState.empty: emptyCount = emptyCount + 1
            case CellState.alive: aliveCount = aliveCount + 1
            case CellState.died: diedCount = diedCount + 1
            case CellState.born: bornCount = bornCount + 1
            }
        }
    }

看起来过滤器可以更有效地执行此操作,但我不理解复杂对象的语法。如果没有过滤器,是否有更好的方法在 swift 中进行嵌套循环?

谢谢

这看起来像 Conway's Game of Life

您正在循环计算各种细胞状态的网格。嵌套循环是执行此操作的自然方式。我建议使用 for in 而不是 forEach。另外,我建议创建一个字典来保存计数:

// Create dictionary to hold counts
var counts: [CellState : Int] = [.alive: 0, .died: 0, .born: 0, .empty: 0]

for row in 0 ..< engine.rows {
    for col in 0 ..< engine.cols {
        //print("(\(row),\(col)) state = \(engine.grid[(row,col)])")
        counts[engine.grid[(row, col)]] += 1
    }
}

另一种方式:

您没有向我们提供有关您的 Engine classstruct 的信息。根据 grid 的实现,可能有一种方法可以获取所有单元格的数组。

例如,如果您使用 创建 grid,那么您可以使用 grid.data.

将所有单元格作为数组获取
struct Engine {
    let rows: Int
    let cols: Int
    var grid: NDimArray<CellState>

    init(rows: Int, cols: Int) {
        self.rows = rows
        self.cols = cols
        self.grid = NDimArray<CellState>(dimensions: rows, cols, initial: CellState.empty)
    } 
}

设置单元格状态如下所示:

var engine = Engine(rows: 20, cols: 20)
engine.grid[0, 0] = .alive
engine.grid[0, 1] = .alive

那么计算细胞类型的代码就变成了:

var counts: [CellState : Int] = [.alive: 0, .died: 0, .born: 0, .empty: 0]
engine.grid.data.forEach { cell in counts[cell] += 1 }