[swift-users] Swift Initializer Generics
Tyler Fleming Cloutier
cloutiertyler at aol.com
Mon Jan 18 21:14:43 CST 2016
I will be damned! And that was a fast response too! I absolutely would not ever have thought of extending Float and Double to do this. That is crafty indeed. Is there a reason to convert to the array to an UnsafeBufferPointer before passing it into the Accelerate function?
Are you aware of any Swift 3.0 generics improvements that might make this more obvious?
Thanks for the help!
Tyler
> On Jan 18, 2016, at 6:47 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
>
> On Mon, Jan 18, 2016 at 6:34 PM, Tyler Fleming Cloutier
> <cloutiertyler at aol.com> wrote:
>> I suspect I’m misunderstanding your suggestion since I can’t quite work out
>> how to do this. I’ve been wrangling the type checker here for a while, but I
>> don’t know what the devil to do. With the different syntax for associated
>> types, generic parameters, and constraints it’s a bit difficult to explore
>> different without knowing exactly what to do.
>
> Here's a sketch.
>
> //
> // Mock of the Accelerate library.
> //
>
> // For exposition only, use the Accelerate function instead.
> func fastDotProductOfDoubles(
> lhs: UnsafeBufferPointer<Double>,
> _ rhs: UnsafeBufferPointer<Double>
> ) -> Double {
> fatalError("call vDSP_dotprD")
> }
>
> // For exposition only, use the Accelerate function instead.
> func fastDotProductOfFloats(
> lhs: UnsafeBufferPointer<Float>,
> _ rhs: UnsafeBufferPointer<Float>
> ) -> Float {
> fatalError("call vDSP_dotpr")
> }
>
> // For exposition only, use the Accelerate function instead.
> func fastSumOfDoubles(data: UnsafeBufferPointer<Double>) -> Double {
> fatalError("call Accelerate")
> }
>
> // For exposition only, use the Accelerate function instead.
> func fastSumOfFloats(data: UnsafeBufferPointer<Float>) -> Float {
> fatalError("call Accelerate")
> }
>
> //
> // Swift wrapper for the Accelerate library.
> //
>
> protocol AccelerateFloatingPoint {
> static func _dot(
> lhs: UnsafeBufferPointer<Self>,
> _ rhs: UnsafeBufferPointer<Self>
> ) -> Self
>
> static func _sum(data: UnsafeBufferPointer<Self>) -> Self
> }
>
> extension Float : AccelerateFloatingPoint {
> static func _dot(
> lhs: UnsafeBufferPointer<Float>,
> _ rhs: UnsafeBufferPointer<Float>
> ) -> Float {
> return fastDotProductOfFloats(lhs, rhs)
> }
>
> static func _sum(data: UnsafeBufferPointer<Float>) -> Float {
> return fastSumOfFloats(data)
> }
> }
>
> extension Double : AccelerateFloatingPoint {
> static func _dot(
> lhs: UnsafeBufferPointer<Double>,
> _ rhs: UnsafeBufferPointer<Double>
> ) -> Double {
> return fastDotProductOfDoubles(lhs, rhs)
> }
>
> static func _sum(data: UnsafeBufferPointer<Double>) -> Double {
> return fastSumOfDoubles(data)
> }
> }
>
> func dot<T : AccelerateFloatingPoint>(lhs: [T], _ rhs: [T]) -> T {
> return lhs.withUnsafeBufferPointer {
> (lhs) in
> return rhs.withUnsafeBufferPointer {
> (rhs) in
> return T._dot(lhs, rhs)
> }
> }
> }
>
> func sum<T : AccelerateFloatingPoint>(data: [T]) -> T {
> return data.withUnsafeBufferPointer {
> (data) in
> return T._sum(data)
> }
> }
>
> Dmitri
>
> --
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-users
mailing list