Files
swift-mirror/stdlib/core/UnsafeArray.swift
Dave Abrahams c33a59c6a7 [build] Complete gyb support
Remove all gyb-generated files and generate them automatically from .gyb
files.  Rename the proof-of-concept UnsafeArray.swift.gyb back to
UnsafeArray.swift.  Never forget to update the .gyb file or regenerate
again!

Swift SVN r14445
2014-02-27 03:01:16 +00:00

57 lines
1.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// \brief Wrapper for a contiguous array of T. UnsafeArray is both a
/// Collectionwhich is multi-pass if you use indices or call
/// generate() on itand a Generator, which can only be assumed to be
/// single-pass. It's not clear how well this combination will work
/// out, or whether all Collections should also be Streams; consider
/// this an experiment.
struct UnsafeArray<T> : Collection, Generator {
func countElements() -> Int {
return endIndex - startIndex
}
var startIndex: Int {
return 0
}
var endIndex: Int {
return _end - _position
}
subscript(i: Int) -> T {
assert(i >= 0)
assert(i < endIndex)
return (_position + i).get()
}
init(start: UnsafePointer<T>, length: Int) {
_position = start
_end = start + length
}
mutating func next() -> T? {
if _position == _end {
return .None
}
return .Some((_position++).get())
}
func generate() -> UnsafeArray {
return self
}
var _position, _end: UnsafePointer<T>
}