// Swift Array to IVector Adaptor import WinSDK extension Array { public func toVector() -> AnyIVector { ArrayVector(self) } } internal class ArrayVector : IVector { typealias Element = T private var storage: Array internal init(_ storage: Array){ self.storage = storage } // MARK: WinRT APIs func getAt(_ index: UInt32) -> T { storage[Int(index)] } var size : UInt32 { UInt32(storage.count) } func indexOf(_ item: T, _ index: inout UInt32) -> Bool { return false } func append(_ item: T) { storage.append(item ) } func setAt(_ index: UInt32, _ item: T) { storage[Int(index)] = item } func insertAt(_ index: UInt32, _ item: T) { storage.insert(item, at: Int(index)) } func removeAt(_ index: UInt32) { storage.remove(at: Int(index) )} func removeAtEnd() { storage.removeLast() } func clear() { storage.removeAll() } func getView() -> AnyIVectorView? { return ArrayVectorView(storage) } func first() -> AnyIIterator? { ArrayIterator(storage) } } extension ArrayVector where T: Equatable { func indexOf(_ item: T, _ index: inout UInt32) throws -> Bool { guard let foundIndex = storage.firstIndex(of: item) else { return false } index = UInt32(foundIndex) return true } } extension ArrayVector { public func queryInterface(_ iid: WindowsFoundation.IID) -> IUnknownRef? { nil } } internal class ArrayVectorView : IVectorView { typealias Element = T private var storage: Array internal init(_ storage: Array){ self.storage = storage } func getAt(_ index: UInt32) -> T { storage[Int(index)] } var size : UInt32 { UInt32(storage.count) } func indexOf(_ item: T, _ index: inout UInt32) -> Bool { return false } func first() -> AnyIIterator? { ArrayIterator(storage) } } extension ArrayVectorView where T: Equatable { func indexOf(_ item: T, _ index: inout UInt32) throws -> Bool { guard let foundIndex = storage.firstIndex(of: item) else { return false } index = UInt32(foundIndex) return true } } extension ArrayVectorView { public func queryInterface(_ iid: WindowsFoundation.IID) -> IUnknownRef? { nil } } class ArrayIterator: IIterator { typealias Element = T private let storage: Array private var index: Int = 0 init(_ storage: Array){ self.storage = storage } func moveNext() -> Bool { index += 1; return index < storage.count } var current: T { storage[index] } var hasCurrent: Bool { index < storage.count } func queryInterface(_ iid: IID) -> IUnknownRef? { nil } }