Skip to content

Instantly share code, notes, and snippets.

@rintaro
Created May 12, 2017 11:10
Show Gist options
  • Save rintaro/861f983f759b90097aa6433ed9dbf402 to your computer and use it in GitHub Desktop.
Save rintaro/861f983f759b90097aa6433ed9dbf402 to your computer and use it in GitHub Desktop.
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