簡體   English   中英

將變量分配給 function 中的類型,我怎樣才能讓類型本身成為描述變量 output 類型的輸入? : Swift 5

[英]Assigning variable to a type in a function, how can I have the type itself be an input that will describe the type of a variable output? : Swift 5

我制作了一個包含數據、偏移量和讀取方法的文件讀取結構(不知何故 fread() 讓我感到困惑)。

我已經讓它工作了,現在想在其中實現一個方法,它將采用 nxm 矩陣項和 output 值矩陣。 (類似於 matlab 中的 fread(FileID, [ nm ], "(datatype)"))。

這是我的想法

// should be able to handle all sorts of datatypes and output n by m matrix.
    mutating func matRead( dim : [[Int]], dtype : Int ){
        if dim.count != 2{
            fatalError("Dimensions dont match \"n by m\" in matRead")
        }
        // make sure to preallocate with zeros.
        
        var mat_Out : typecast[dtype][2].self = []
        
    }

我有一個看起來如此的類型轉換字典

// typecast dictionary key : [ array_type, byte, element_type ]
let typecast : [Int16:[Any]] = [ 1: [ [UInt8].self  , 1, "uint8"],
                              2: [ [UInt16].self , 2, "uint16"],  // use .self to reference data type itself.
                              3: [ [UInt32].self , 4, "uint32"],
                              4: [ [Int8].self   , 1, "int8"  ],
                              5: [ [Int16].self  , 2, "int16" ],
                              6: [ [Int32].self  , 4, "int32" ],
                              7: [ [Float32].self, 4, "float32"],
                              8: [ [Float64].self, 8, "float64"],
                              12:[ [Int32].self  , 4, "int32" ]   ]

這是問題: matRead() function 的第 8 行不起作用。 Swift 不明白我正在嘗試使用類型轉換字典將 output 矩陣分配給新的數組類型。 我也試過 "as" var mat_Out: Any = [] as typecast[dtype][0] ,同樣的錯誤(Swift 編譯器認為我的意思是把括號放在外面)。

或者,我可以 go 很長的路要走,然后根據字符串值手動進行類型轉換(已經必須這樣做:參見下面的 fread 結構代碼),但是如果有另一種方法,它將非常節省時間。


下面的 FileReading 結構(檢查計數的冗余使用,但我沒有清理它)

struct fRead {
    var off : Int = 0
    let data : Data
    
    mutating func resetOffsetToZero() {
        off = 0
    }
    
    mutating func setOffset( _ to : Int ) {
        off.self = to
    }
    mutating func moveOffset( _ by : Int) {
        off.self += by
    }
    func getOffset() -> Int {
        return off
    }
    // reading without specified number of elements defaults to 1.
    // these mutating funcs vary only in their output type and byte offset size.
    mutating func int32Read(count : Int = 1) -> [Int32] {
        var int32out : [Int32] = []
        if count > 1 {
            for i in 0...count-1 {
                int32out.append( Int32( data.subdata(in: off + i * 4..<(off + (i + 1) * 4) ).withUnsafeBytes{ $0.load(as: Int32.self )} ))
            }
            off += count * 4
        }
        else if count == 1 {
            int32out =  [ Int32( data.subdata(in: off..<(off+4) ).withUnsafeBytes{ $0.load(as: Int32.self )} )]
            off += 4
        }
        else if count == 0 {
            return []
        } else if count < 0{
            print("Warning, fReadint32Read( count : Int = 1) called with a negative count, returning empty array.")
        }
        return int32out
    }
    
    mutating func int16Read(count : Int = 1) -> [Int16] {
        var int16out : [Int16] = []
        if count > 1 {
            for i in 0...count-1 {
                int16out.append( data.subdata(in: off + i * 2..<(off + (i + 1) * 2) ).withUnsafeBytes{ $0.load(as: Int16.self )} )
            }
            off += count * 2
        }
        else if count == 1 {
            int16out =  [ data.subdata(in: off..<(off+2) ).withUnsafeBytes{ $0.load(as: Int16.self )} ]
            off += 2
        }
        else if count == 0 {
            return []
        } else if count < 0 {
            print("Warning, fRead.int16Read( count : Int = 1) called with a negative count, returning empty array.")
        }
        return int16out
    }
    
    mutating func float64Read(count : Int = 1) -> [Float64] {
        var float64out : [Float64] = []
        if count > 1 {
            for i in 0...count-1 {
                float64out.append( data.subdata(in: off + i * 8..<(off + (i + 1) * 8) ).withUnsafeBytes{ $0.load(as: Float64.self )} )
            }
            off += count * 8
        }
        else if count == 1 {
            float64out =  [ data.subdata(in: off..<(off+8) ).withUnsafeBytes{ $0.load(as: Float64.self )} ]
            off += 8
        }
        else if count == 0 {
            return []
        } else if count < 0 {
            print("Warning, fRead.int16Read( count : Int = 1) called with a negative count, returning empty array.")
        }
        return float64out
    }
    
    mutating func uint32Read(count : Int = 1) -> [UInt32] {
        var uint32out : [UInt32] = []
        if count > 1 {
            for i in 0...count-1 {
                uint32out.append( data.subdata(in: off + i * 4..<(off + (i + 1) * 4) ).withUnsafeBytes{ $0.load(as: UInt32.self )} )
            }
            off += count * 4
        }
        else if count == 1 {
            uint32out =  [ data.subdata(in: off..<(off+4) ).withUnsafeBytes{ $0.load(as: UInt32.self )} ]
            off += 4
        }
        else if count == 0 {
            return []
        } else if count < 0 {
            print("Warning, fRead.int16Read( count : Int = 1) called with a negative count, returning empty array.")
        }
        return uint32out
    }
    
    mutating func int64Read(count : Int = 1) -> [Int64] {
        var int64out : [Int64] = []
        if count > 1 {
            for i in 0...count-1 {
                int64out.append( Int64( data.subdata(in: off + i * 4..<(off + (i + 1) * 8) ).withUnsafeBytes{ $0.load(as: Int64.self )} ))
            }
            off += count * 8
        }
        else if count == 1 {
            int64out =  [ Int64( data.subdata(in: off..<(off+8) ).withUnsafeBytes{ $0.load(as: Int64.self )} )]
            off += 8
        }
        else if count == 0 {
            return []
        } else if count < 0{
            print("Warning, fReadint64Read( count : Int = 1) called with a negative count, returning empty array.")
        }
        return int64out
    }
    // should be able to handle all sorts of datatypes and output n by m matrix.
    mutating func matRead( dim : [[Int]], dtype : Int ){
        if dim.count != 2{
            fatalError("Dimensions dont match \"n by m\" in matRead")
        }
        // make sure to preallocate with zeros.
        
        var mat_Out : Any = [] as typecast[dtype][0]
        
    }
}

// I only discovered afterwards that 0...0 range works! So all the testing for  ==1 , == 0 wasn't necessary

通用解決方案不是最好的方法嗎? 以下是元素為 integer 的通用版本,因此浮點數( BinaryFloatingPoint )需要類似的 function 。

mutating func matRead<T: BinaryInteger>(count: Int = 1) -> [T] {
    var intOut : [T] = []
    let width = T().bitWidth / 8
    if count > 1 {
        for i in 0...count-1 {
            intOut.append( T( data.subdata(in: off + i * 4..<(off + (i + 1) * width) ).withUnsafeBytes{ $0.load(as: T.self )} ))
        }
        off += count * width
    }
    else if count == 1 {
        intOut =  [ T( data.subdata(in: off..<(off+width) ).withUnsafeBytes{ $0.load(as: T.self )} )]
        off += width
    }
    else if count == 0 {
        return []
    } else if count < 0{
        print("Warning, fReadint64Read( count : Int = 1) called with a negative count, returning empty array.")
    }
    return intOut
}

請注意,上面的代碼中有一個“錯誤”,我不確定在i * 4..<(off + (i + 1)中用什么替換 4,因為您在函數之間使用的數字如此不一致。我想它應該是width / 2但我留給你更新那部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM