Skip to content
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

Migrated to Swift 5 and improved implementation #2

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Strategist"
name: "Strategist",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Strategist",
targets: [
"Strategist",
]
),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Strategist",
dependencies: [
]
),
.testTarget(
name: "StrategistTests",
dependencies: [
"Strategist",
]
),
]
)
87 changes: 0 additions & 87 deletions Sources/GameTree.swift

This file was deleted.

116 changes: 0 additions & 116 deletions Sources/ParallelMonteCarloTreeSearch.swift

This file was deleted.

File renamed without changes.
29 changes: 19 additions & 10 deletions Sources/Extensions.swift → Sources/Strategist/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ extension Collection where Index == Int {
///
/// - complexity: O(1).
/// - returns: Randomly selected element from `self`.
func sample(_ randomSource: RandomSource = Strategist.defaultRandomSource) -> Iterator.Element? {
func sample(
using randomSource: RandomSource = Int.random(in:)
) -> Iterator.Element? {
let count = self.count
guard count > 0 else {
return nil
}
let index = randomSource(UInt32(count))
let index = randomSource(0..<count)
return self[Int(index)]
}
}
Expand All @@ -55,11 +57,13 @@ extension IteratorProtocol {
///
/// - complexity: O(`Array(self).count`).
/// - returns: Randomly selected element from `self`.
mutating func sample(_ randomSource: RandomSource = Strategist.defaultRandomSource) -> Element? {
mutating func sample(
using randomSource: RandomSource = Int.random(in:)
) -> Element? {
var result = self.next()
var count = 2
var count = 1
while let element = self.next() {
if randomSource(UInt32(count)) == 0 {
if randomSource(0..<count) == 0 {
result = element
}
count += 1
Expand All @@ -72,8 +76,10 @@ extension IteratorProtocol where Element : Comparable {
//Returns a random sample from maximum elements in self or nil if the sequence is empty.
//
/// -complexity: O(elements.count).
mutating func sampleMaxElement(randomSource: RandomSource = Strategist.defaultRandomSource) -> Element? {
return self.sampleMaxElement(randomSource: randomSource) { $0 < $1 }
mutating func sampleMaxElement(
using randomSource: RandomSource = Int.random(in:)
) -> Element? {
return self.sampleMaxElement(using: randomSource) { $0 < $1 }
}
}

Expand All @@ -82,17 +88,20 @@ extension IteratorProtocol {
///
/// - complexity: O(elements.count).
/// - requires: `isOrderedBefore` is a strict weak ordering over `self`.
mutating func sampleMaxElement(randomSource: RandomSource = Strategist.defaultRandomSource, isOrderedBefore: (Element, Element) throws -> Bool) rethrows -> Element? {
mutating func sampleMaxElement(
using randomSource: RandomSource = Int.random(in:),
isOrderedBefore: (Element, Element) throws -> Bool
) rethrows -> Element? {
guard var maxElement = self.next() else {
return nil
}
var maxElementCount = 0
var maxElementCount = 1
while let element = self.next() {
if try isOrderedBefore(maxElement, element) {
maxElement = element
maxElementCount = 1
} else if try !isOrderedBefore(element, maxElement) {
if randomSource(UInt32(maxElementCount)) == 0 {
if randomSource(0..<maxElementCount) == 0 {
maxElement = element
}
maxElementCount += 1
Expand Down
Loading