【问题标题】:Is it possible to build a generic conversion for integer arrays in swift 2.2?是否可以在 swift 2.2 中为整数数组构建通用转换?
【发布时间】:2016-09-22 23:34:32
【问题描述】:

我无法超越下图所示的点。

我得到:

Cannot convert value of type 'Element' to expected argument type 'IntegerLiteralType' (aka 'Int')

在我将数据从“元素”移动到“T”的那一行。另外,我还没有弄清楚如何将截断扩展到 UInt。

我们将不胜感激。

protocol Truncating {
    init(truncatingBitPattern: IntegerLiteralType)
}

extension Int {
    init(truncatingBitPattern value: Int) { self.init(truncatingBitPattern: value.toIntMax()) }
}

protocol Bitshiftable {
    func <<(lhs: Self, rhs: Self) -> Self
    func >>(lhs: Self, rhs: Self) -> Self
    func <<=(inout lhs: Self, rhs: Self)
    func >>=(inout lhs: Self, rhs: Self)
}

protocol ArrayConvertable: IntegerType, Bitshiftable, Truncating {}

extension Int    : ArrayConvertable {}
extension Int8   : ArrayConvertable {}
extension Int16  : ArrayConvertable {}
extension Int32  : ArrayConvertable {}
extension Int64  : ArrayConvertable {}
//extension UInt   : ArrayConvertable {}
extension UInt8  : ArrayConvertable {}
extension UInt16 : ArrayConvertable {}
extension UInt32 : ArrayConvertable {}
extension UInt64 : ArrayConvertable {}

extension Array where Element: ArrayConvertable {
    func toInt<T: ArrayConvertable>() -> T?
    {
        let targetSize = sizeof(T)
        let sourceSize = sizeof(Element)
        let sourceByteCount = count * sourceSize
        guard targetSize == sourceByteCount else { return nil }

        var n: T = 0
        for e in self {
            var dataChunk = e
            for i in 0..<sourceSize {
                n = n | T(truncatingBitPattern: dataChunk)
                if i + 1 < sourceSize {
                    n = n << 8
                    dataChunk = dataChunk >> 8
                }
            }
        }

        return n
    }
}

func test() {
    let a8:  [UInt8]  = [0xab, 0xba, 0xda, 0xba, 0xd0, 0xd1, 0xd2, 0xd3, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10]
    let a16: [UInt16] = [0xabba, 0xdaba, 0xd0d1, 0xd2d3, 0xfedc, 0xba98, 0x7654, 0x3210]
    let a32: [UInt32] = [0xabbadaba, 0xd0d1d2d3, 0xfedcba98, 0x76543210]
    let a64: [UInt64] = [0xabbadabad0d1d2d3, 0xfedcba9876543210]

    var n8:  UInt8  = 0
    var n16: UInt16 = 0
    var n32: UInt32 = 0
    var n64: UInt64 = 0

    print("--- 08 bit source")
    n8  = a8.toInt()!
    n16 = a8.toInt()!
    n32 = a8.toInt()!
    n64 = a8.toInt()!
    print(n8, n16, n32, n64)

    print("--- 16 bit source")
    n8  = a16.toInt()!
    n16 = a16.toInt()!
    n32 = a16.toInt()!
    n64 = a16.toInt()!
    print(n8, n16, n32, n64)

    print("--- 32 bit source")
    n8  = a32.toInt()!
    n16 = a32.toInt()!
    n32 = a32.toInt()!
    n64 = a32.toInt()!
    print(n8, n16, n32, n64)

    print("--- 64 bit source")
    n8  = a64.toInt()!
    n16 = a64.toInt()!
    n32 = a64.toInt()!
    n64 = a64.toInt()!
    print(n8, n16, n32, n64)
}

test()

【问题讨论】:

  • 无论实现如何:您的断言 targetSize == sourceByteCount 在示例中总是会失败,因为输入数组对于单个 8/16/32/64 位整数来说太大了。或者结果应该是目标类型的array
  • 如果输入为let a32: [UInt32] = [0x01020304, 0x05060708, 0x090a0b0c, 0x0d0e0f10],目标类型为UInt16,预期的结果是什么?对于目标类型UInt64 ?
  • @MartinR body 有一些错误,但是 targetSize == sourceByteCount 如果,例如,目标是 UInt16,源是 [UInt8].count == 2。这个函数的结果总是整数之一。
  • @MartinR 您的示例的结果最好是 0x0304。
  • 我正在研究的一个可能方向是将 truncatingBitPattern 推广到所有整数。

标签: arrays swift generics


【解决方案1】:

这是一个可能的解决方案,它只是重新解释数据。 不需要协议:

extension Array where Element: IntegerType {
    func toInt<T: IntegerType>() -> T? {
        guard sizeof(T) <= count * sizeof(Element) else { return nil }
        return UnsafePointer<T>(self).memory
    }
}

例子:

let a32: [UInt32] = [0x11223344, 0x55667788, 0x99AABBCC, 0xDDEEFF00]
if let u16: UInt16 = a32.toInt() {
    print(String(format:"%#04x", u16)) // 0x3344
}

【讨论】:

  • 这真的很优雅。一个潜在的陷阱是数组不能保证是连续的,这对于您的示例来说不是问题,但可能是从 [UInt8] 到 UInt64 作为示例。
  • @SeanVikoren:我相当确定将数组传递给一个接受指针的函数(如UnsafePointer&lt;T&gt;(self))会传递元素的地址连续存储。
猜你喜欢
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 2017-08-16
  • 1970-01-01
  • 2012-02-12
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多