Skip to content

nonmutating swapAt implementations for UnsafeMutable(Raw)BufferPointer #13071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions stdlib/public/core/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,29 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
}
% end
}
% if mutable:

/// Exchanges the values at the specified indices of the buffer.
///
/// Both parameters must be valid indices of the buffer, and not
/// equal to `endIndex`. Passing the same index as both `i` and `j` has no
/// effect.
///
/// - Parameters:
/// - i: The index of the first value to swap.
/// - j: The index of the second value to swap.
@_inlineable
public func swapAt(_ i: Int, _ j: Int) {
guard i != j else { return }
_debugPrecondition(i >= 0 && j >= 0)
_debugPrecondition(i < endIndex && j < endIndex)
let pi = (_position! + i)
let pj = (_position! + j)
let tmp = pi.move()
pi.moveInitialize(from: pj, count: 1)
pj.initialize(to: tmp, count: 1)
}
% end # mutable
}

extension Unsafe${Mutable}BufferPointer {
Expand Down
24 changes: 24 additions & 0 deletions stdlib/public/core/UnsafeRawBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,30 @@ extension Unsafe${Mutable}RawBufferPointer: ${Mutable}Collection {
% end # mutable
}

% if mutable:
/// Exchanges the byte values at the specified indices
/// in this buffer's memory.
///
/// Both parameters must be valid indices of the buffer, and not
/// equal to `endIndex`. Passing the same index as both `i` and `j` has no
/// effect.
///
/// - Parameters:
/// - i: The index of the first byte to swap.
/// - j: The index of the second byte to swap.
@_inlineable
public func swapAt(_ i: Int, _ j: Int) {
guard i != j else { return }
_debugPrecondition(i >= 0 && j >= 0)
_debugPrecondition(i < endIndex && j < endIndex)
let pi = (_position! + i)
let pj = (_position! + j)
let tmp = pi.load(fromByteOffset: 0, as: UInt8.self)
pi.copyMemory(from: pj, byteCount: MemoryLayout<UInt8>.size)
pj.storeBytes(of: tmp, toByteOffset: 0, as: UInt8.self)
}

% end # mutable
/// The number of bytes in the buffer.
///
/// If the `baseAddress` of this buffer is `nil`, the count is zero. However,
Expand Down
43 changes: 43 additions & 0 deletions validation-test/stdlib/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,49 @@ UnsafeMutable${'Raw' if IsRaw else ''}BufferPointerTestSuite.test("subscript/set
expectEqual(1, buffer[3])
}

UnsafeMutable${'Raw' if IsRaw else ''}BufferPointerTestSuite.test("nonmutating-swapAt") {
% if IsRaw:
let allocated = UnsafeMutableRawPointer.allocate(bytes: 4, alignedTo: 8)
let buffer = UnsafeMutableRawBufferPointer(start: allocated, count: 3)
allocated.storeBytes(of: UInt8.max, toByteOffset: 3, as: UInt8.self)
% else:
let allocated = UnsafeMutablePointer<Int>.allocate(capacity: 4)
let buffer = UnsafeMutableBufferPointer(start: allocated, count: 3)
allocated[3] = Int.max
% end
defer { allocated.deallocate() }

buffer[0] = 0
buffer[1] = 1
buffer[2] = 2

buffer.swapAt(0, 0)
expectEqual(Array(buffer), [0, 1, 2])

buffer.swapAt(0, 2)
expectEqual(Array(buffer), [2, 1, 0])

if _isDebugAssertConfiguration() {
expectCrashLater()
}
buffer.swapAt(2, 3)
}

% if not IsRaw:
UnsafeMutableBufferPointerTestSuite.test("nonmutating-swapAt-withARC") {
let buffer = UnsafeMutableBufferPointer<String>.allocate(capacity: 3)
defer { buffer.deallocate() }

_ = buffer.initialize(from: (0..<3).map(String.init(describing:)))

buffer.swapAt(0, 0)
expectEqual(Array(buffer), ["0", "1", "2"])

buffer.swapAt(0, 2)
expectEqual(Array(buffer), ["2", "1", "0"])
}
% end

% end # SelfName

runAllTests()