-
-
Save rintaro/861f983f759b90097aa6433ed9dbf402 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Buffer<T> : Collection { | |
private var _buf: ManagedBuffer<Int, T> | |
public init(capacity: Int) { | |
precondition(!(T.self is AnyObject.Type)) | |
_buf = ManagedBuffer.create(minimumCapacity: capacity) { _ in capacity } | |
} | |
public var count: Int { | |
return _buf.withUnsafeMutablePointerToHeader { $0.pointee } | |
} | |
public var startIndex: Int { return 0 } | |
public var endIndex: Int { return count } | |
public func index(after index: Int) -> Int { | |
return index + 1 | |
} | |
public subscript(index: Int) -> T { | |
get { return _buf.withUnsafeMutablePointerToElements { $0[index] } } | |
set { _buf.withUnsafeMutablePointerToElements { $0[index] = newValue } } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment