I am working on a Big Integer package with emphasis on performance. My current design follows what most others have done, i.e. the Big Integer is a UInt64 array with functions contained in a Uint64 array extension. All was well until I needed to implement various multiplication algorithms. In particular, Karatsuba and Toom Cook multiplication. Both of these algorithms are divide and conquer based with Karatsuba breaking down the array into two slices and Toom Cook 3 or more slices. In Swift, the obvious choice for slices is to use array slice. But .... my design is based on an array extension with array arguments, so I am forced to convert slices to arrays which eats up memory and nullifies the performance that can be gained by recursive divide and conquer. What I now need is a way to process both arrays and array slices in the same function. Functions will have two array arguments where one or both can be arrays or slices. Below is how I am considering constructing the functions:
func test<T,V>(_ x: T,_ xPtr: UnsafeMutablePointer<UInt>, _ y: V,_ yPtr: UnsafeMutablePointer<UInt>)
where T: RandomAccessCollection, T.Element == UInt, T.Index == Int,
V: RandomAccessCollection, V.Element == UInt, V.Index == Int {
print("x count", x.count)
print("x", x[x.startIndex...])
print("x elements")
for indx in (0..<x.count) {print(xPtr[indx])}
print(" ")
print("y count", y.count)
print("y", y[y.startIndex...])
print("y elements")
for indx in (0..<y.count) {print(yPtr[indx])}
}
var u1: [UInt] = [1,2,3,4]
var u2 = u1[1...]
test(u1, &u1[u1.startIndex], u2, &u2[u2.startIndex])
With the function construct above I can pass arrays or slices for either argument. I also pass a pointer to each so I can have a zero based pointers for the slices. My question is this. Is there better ways to merge arrays and array slices into functions? What do you think of my construct
(based on advice gained on stack overflow).