Swift: How to declare a 2d array (grid or matrix) in Swift to allow random insert

喜你入骨 提交于 2019-11-28 04:03:32

问题


I need to be able to store information about cells in a 2d matrix or grid. Data is not contiguous so I may need to store data at 5,5 when there is no data at lower rows and columns.

My first thought was an array of arrays dynamically sized. But Swift arrays have bounds that do not grow automatically. If I attempt to place something at index 5 and thats beyond its current size it fails with out of bounds exception.

Is there a collection class in Swift or Cocoa which supports random access to a grid. NSArray doesn't support it either.

Another thought was to store the elements in a dictionary and use a tuple of row, column as the key. However, tuples are not hashable and can't be used as the key to a dictionary.

My current approach is to preinitialize the array with a set size filled with nulls. Is there a better way?


回答1:


Here is a very basic implementation, using Dictionary as backend storage:

struct Matrix2D<KeyElem:Hashable, Value> {

    var _storage:[KeyElem:[KeyElem:Value]] = [:]

    subscript(x:KeyElem, y:KeyElem) -> Value? {
        get {
            return _storage[x]?[y]
        }
        set(val) {
            if _storage[x] == nil {
                _storage[x] = [:]
            }
            _storage[x]![y] = val
        }
    }
}

var matrix = Matrix2D<Int, String>()

matrix[1,2] = "foo"

as DictionaryLiteralConvertible:

extension Matrix2D:DictionaryLiteralConvertible {

    typealias Key = (x:KeyElem, y:KeyElem)

    init(dictionaryLiteral elements: (Key, Value)...) {
        for (key, val) in elements {
            self[key.x, key.y] = val
        }
    }
}

var matrix:Matrix2D = [(1,2):"foo", (2,3):"bar"]

Array backend version

struct Matrix2D<T> {

    var _storage:[[T?]] = []

    subscript(x:Int, y:Int) -> T? {
       get {
            if _storage.count <= x {
                return nil
            }
            if _storage[x].count <= y {
                return nil
            }
            return _storage[x][y]
        }
        set(val) {
            if _storage.count <= x {
                let cols = [[T?]](count: x - _storage.count + 1, repeatedValue: [])
                _storage.extend(cols)
            }
            if _storage[x].count <= y {
                let rows = [T?](count: y - _storage[x].count + 1, repeatedValue: nil)
                _storage[x].extend(rows)
            }
            _storage[x][y] = val
        }
    }
}


来源:https://stackoverflow.com/questions/26563821/swift-how-to-declare-a-2d-array-grid-or-matrix-in-swift-to-allow-random-inser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!