Skip to content

Commit

Permalink
Merge pull request nvzqz#48 from Maaimusic/swift-4.1
Browse files Browse the repository at this point in the history
Closes nvzqz#46 and nvzqz#47
  • Loading branch information
nvzqz authored Apr 5, 2018
2 parents e4e0889 + ba55703 commit 0fc0523
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 52 deletions.
14 changes: 13 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ matrix:
sudo: required
dist: trusty
env:
- SWIFT_VERSION="4.0-DEVELOPMENT-SNAPSHOT-2017-08-04-a"
- SWIFT_VERSION="4.0.3"
- os: linux
language: generic
sudo: required
dist: trusty
env:
- SWIFT_VERSION="4.1"
- os: linux
language: generic
sudo: required
dist: trusty
env:
- SWIFT_VERSION="4.0.3"
env:
global:
- LC_CTYPE=en_US.UTF-8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ extension CGFloat: Random, RandomToValue, RandomThroughValue, RandomInRange, Ran
return CGFloat(NativeType.random(using: &randomGenerator))
}

}

extension CGFloat {

/// Generates a random `CGFloat` in the closed range using `randomGenerator`.
///
/// - parameter closedRange: The range in which the number will be generated.
Expand Down
87 changes: 57 additions & 30 deletions Sources/RandomKit/Extensions/Swift/Collection+RandomKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ extension RandomRetrievableInRange where Self: Collection, Self.Index: RandomInR

}

#if swift(>=4.1)

extension Collection where Self: RandomRetrievableInRange {

/// Returns a random element of `self`, or `nil` if `self` is empty.
public func uncheckedRandom<R: RandomGenerator>(in range: Range<Index>, using randomGenerator: inout R) -> Iterator.Element {
let upper = range.upperBound
let lower = range.lowerBound
let elementIndex = Int.random(to: distance(from: lower, to: upper), using: &randomGenerator)
return self[index(lower, offsetBy: elementIndex)]
}

}

#else

extension RandomRetrievableInRange where Self: Collection, Self.Index: RandomInRange, Self.IndexDistance: RandomToValue {

/// Returns a random element in `range` without checking whether `self` or `range` is empty.
Expand All @@ -99,6 +115,9 @@ extension Collection where Self: RandomRetrievableInRange, IndexDistance: Random

}

#endif


extension MutableCollection where Self: Shuffleable {

/// Shuffles the elements of `self` and returns the result.
Expand Down Expand Up @@ -145,11 +164,23 @@ extension MutableCollection where Self: UniqueShuffleableInRange {

extension UnsafeBufferPointer: RandomRetrievableInRange {}

extension UnsafeMutableBufferPointer: RandomRetrievableInRange, ShuffleableInRange, UniqueShuffleableInRange {
extension UnsafeMutableBufferPointer {

/// Shuffles the elements of `self`.
public func shuffle<R: RandomGenerator>(using randomGenerator: inout R) {
shuffle(in: indices, using: &randomGenerator)
/// Shuffles the elements of `self` in `range`.
public func shuffle<R: RandomGenerator>(in range: CountableRange<Int>, using randomGenerator: inout R) {
shuffle(in: Range(range), using: &randomGenerator)
}

/// Shuffles the elements of `self` in a unique order in `range`.
public func shuffleUnique<R: RandomGenerator>(in range: Range<Int>, using randomGenerator: inout R) {
if range.isEmpty {
return
}
for i in CountableRange(uncheckedBounds: (range.lowerBound, range.upperBound &- 1)) {
let randomRange = Range(uncheckedBounds: (i &+ 1, range.upperBound))
let j = Int.uncheckedRandom(in: randomRange, using: &randomGenerator)
swap(&self[i], &self[j])
}
}

/// Shuffles the elements of `self` in `range`.
Expand All @@ -162,26 +193,34 @@ extension UnsafeMutableBufferPointer: RandomRetrievableInRange, ShuffleableInRan
}
}

/// Shuffles the elements of `self` in `range`.
public func shuffle<R: RandomGenerator>(in range: CountableRange<Int>, using randomGenerator: inout R) {
shuffle(in: Range(range), using: &randomGenerator)
}

/// Shuffles the elements of `self` in a unique order.
public func shuffleUnique<R: RandomGenerator>(using randomGenerator: inout R) {
shuffleUnique(in: indices, using: &randomGenerator)
}

/// Shuffles the elements of `self` in a unique order in `range`.
public func shuffleUnique<R: RandomGenerator>(in range: Range<Int>, using randomGenerator: inout R) {
if range.isEmpty {
return
}
for i in CountableRange(uncheckedBounds: (range.lowerBound, range.upperBound &- 1)) {
let randomRange = Range(uncheckedBounds: (i &+ 1, range.upperBound))
let j = Int.uncheckedRandom(in: randomRange, using: &randomGenerator)
swap(&self[i], &self[j])
}
public func shuffleUnique<R: RandomGenerator>(in range: CountableRange<Int>, using randomGenerator: inout R) {
shuffleUnique(in: Range(range), using: &randomGenerator)
}

}

extension UnsafeMutableBufferPointer: RandomRetrievableInRange, ShuffleableInRange, UniqueShuffleableInRange {

/// Shuffles the elements of `self`.
public func shuffle<R: RandomGenerator>(using randomGenerator: inout R) {
shuffle(in: indices, using: &randomGenerator)
}

}

extension UnsafeRawBufferPointer: RandomRetrievableInRange {}

extension UnsafeMutableRawBufferPointer {

/// Shuffles the elements of `self` in `range`.
public func shuffle<R: RandomGenerator>(in range: CountableRange<Int>, using randomGenerator: inout R) {
shuffle(in: Range(range), using: &randomGenerator)
}

/// Shuffles the elements of `self` in a unique order in `range`.
Expand All @@ -191,8 +230,6 @@ extension UnsafeMutableBufferPointer: RandomRetrievableInRange, ShuffleableInRan

}

extension UnsafeRawBufferPointer: RandomRetrievableInRange {}

extension UnsafeMutableRawBufferPointer: RandomRetrievableInRange, ShuffleableInRange, UniqueShuffleableInRange {

private var _casted: UnsafeMutableBufferPointer<UInt8> {
Expand All @@ -209,11 +246,6 @@ extension UnsafeMutableRawBufferPointer: RandomRetrievableInRange, ShuffleableIn
_casted.shuffle(in: range, using: &randomGenerator)
}

/// Shuffles the elements of `self` in `range`.
public func shuffle<R: RandomGenerator>(in range: CountableRange<Int>, using randomGenerator: inout R) {
shuffle(in: Range(range), using: &randomGenerator)
}

/// Shuffles the elements of `self` in a unique order.
public func shuffleUnique<R: RandomGenerator>(using randomGenerator: inout R) {
_casted.shuffleUnique(using: &randomGenerator)
Expand All @@ -224,11 +256,6 @@ extension UnsafeMutableRawBufferPointer: RandomRetrievableInRange, ShuffleableIn
_casted.shuffleUnique(in: range, using: &randomGenerator)
}

/// Shuffles the elements of `self` in a unique order in `range`.
public func shuffleUnique<R: RandomGenerator>(in range: CountableRange<Int>, using randomGenerator: inout R) {
shuffleUnique(in: Range(range), using: &randomGenerator)
}

}

extension Array: RandomRetrievableInRange, ShuffleableInRange, UniqueShuffleableInRange {
Expand Down
35 changes: 14 additions & 21 deletions Sources/RandomKit/Extensions/Swift/String+RandomKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ extension String: Random {
return String(result)
}

#if swift(>=4.0)
// No character view for you
#else
/// Generates a random `String` with a length of `10` from `characters`.
///
/// - parameter characters: The characters from which the string will be generated.
Expand All @@ -104,13 +107,12 @@ extension String: Random {
using randomGenerator: inout R) -> String? where I.Stride: SignedInteger {
var result = ""
for _ in 0 ..< length {
guard let random = characters.random(using: &randomGenerator) else {
return nil
}
let random = self.random(using: &randomGenerator)
result.append(random)
}
return result
}
#endif

/// Generates a random `String` with a length of `10` from `scalars`.
///
Expand Down Expand Up @@ -156,40 +158,31 @@ extension String: Random {
public static func random<I: ExpressibleByIntegerLiteral & Strideable, R: RandomGenerator>(ofLength length: I,
from string: String,
using randomGenerator: inout R) -> String? where I.Stride: SignedInteger {
return random(ofLength: length, from: string.characters, using: &randomGenerator)
return random(ofLength: length, from: string, using: &randomGenerator)
}

}

extension String.UnicodeScalarView: RandomRetrievableInRange {}
#if swift(>=4.0)
extension String: RandomRetrievableInRange {}
#else
extension String.CharacterView: RandomRetrievableInRange {}
#endif

extension String.UnicodeScalarView: RandomRetrievableInRange {}
extension String.UTF8View: RandomRetrievableInRange {}
extension String.UTF16View: RandomRetrievableInRange {}

extension String: Shuffleable, UniqueShuffleable {

/// Shuffles the elements in `self` and returns the result.
public func shuffled<R: RandomGenerator>(using randomGenerator: inout R) -> String {
return String(Array(characters).shuffled(using: &randomGenerator))
return String(self.shuffled(using: &randomGenerator))
}

/// Shuffles the elements in `self` in a unique order and returns the result.
public func shuffledUnique<R: RandomGenerator>(using randomGenerator: inout R) -> String {
return String(Array(characters).shuffledUnique(using: &randomGenerator))
}

}

extension String.CharacterView: Shuffleable, UniqueShuffleable {

/// Shuffles the elements in `self` and returns the result.
public func shuffled<R: RandomGenerator>(using randomGenerator: inout R) -> String.CharacterView {
return String.CharacterView(Array(self).shuffled(using: &randomGenerator))
}

/// Shuffles the elements in `self` in a unique order and returns the result.
public func shuffledUnique<R: RandomGenerator>(using randomGenerator: inout R) -> String.CharacterView {
return String.CharacterView(Array(self).shuffledUnique(using: &randomGenerator))
return String(self.shuffledUnique(using: &randomGenerator))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ extension UnicodeScalar: Random, RandomInRange, RandomInClosedRange {
}
}

}

extension UnicodeScalar {

/// Returns an optional random value of `Self` inside of the range.
public static func random<R: RandomGenerator>(in range: Range<UInt8>,
using randomGenerator: inout R) -> UnicodeScalar? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ extension RandomBytesGenerator {
if remainder > 0 {
var remaining = randomBytes()
let remainingBuffer = buffer.advanced(by: size &- remainder)
#if swift(>=4.1)
remainingBuffer.copyMemory(from: &remaining, byteCount: remainder)
#else
remainingBuffer.copyBytes(from: &remaining, count: remainder)
#endif
}
}

Expand Down

0 comments on commit 0fc0523

Please sign in to comment.