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

Concurrency lockdown #42

Merged
merged 7 commits into from
Apr 13, 2024
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
4 changes: 4 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ jobs:
steps:
- uses: actions/checkout@v4

# default Xcode for macOS 14 image is v15.0.1
- name: Select Xcode 15.3
run: sudo xcode-select -s /Applications/Xcode_15.3.app

- name: Show Build SDK
run: xcodebuild -showsdks

Expand Down
14 changes: 8 additions & 6 deletions Sources/Lindenmayer/Examples/Examples3D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ public enum Examples3D: Sendable {
/// ![A screenshot showing four rendered trees that correspond to the parameters as described in The Algorithmic Beauty of Plants for figure 2.6](rendered_2_6_trees.png)
///
/// The example source for this L-system is [available on GitHub](https://github.com/heckj/Lindenmayer/blob/main/Sources/Lindenmayer/Examples3D.swift).
public static let monopodialTree = LSystem.create(
///

public static let monopodialTree: ParameterizedRandomContextualLSystem<MonopodialDefn, Xoshiro> = LSystem.create(
[Trunk(growthDistance: defines.trunklength, diameter: defines.trunkdiameter)],
with: Xoshiro(seed: 42),
using: defines
Expand Down Expand Up @@ -349,7 +351,7 @@ public enum Examples3D: Sendable {
/// ![A screenshot showing four rendered trees that correspond to the parameters as described in The Algorithmic Beauty of Plants for figure 2.7](rendered_2_7_trees.png)
///
/// The example source for this L-system is [available on GitHub](https://github.com/heckj/Lindenmayer/blob/main/Sources/Lindenmayer/Examples3D.swift).
public static let sympodialTree = LSystem.create(
public static let sympodialTree: ParameterizedRandomContextualLSystem<SympodialDefn, Xoshiro> = LSystem.create(
MainBranch(growthDistance: 10, diameter: 1),
with: Xoshiro(seed: 0),
using: figure2_7A
Expand Down Expand Up @@ -415,19 +417,19 @@ public enum Examples3D: Sendable {
}

public static let randomBush = LSystem.create(Stem2(length: 1), with: Xoshiro(seed: 42))
.rewriteWithRNG(directContext: Stem2.self) { stem, rng -> [Module] in
.rewriteWithRNG(directContext: Stem2.self) { stem, rng async -> [Module] in

let upper: Float = 45.0
let lower: Float = 15.0

if rng.p(0.5) {
return [
if await rng.p(0.5) {
return await [
StaticStem2(length: 2),
Modules.PitchDown(angle: Angle(degrees: Double(rng.randomFloat(in: lower ... upper)))),
Stem2(length: stem.length),
]
} else {
return [
return await [
StaticStem2(length: 2),
Modules.PitchUp(angle: Angle(degrees: Double(rng.randomFloat(in: lower ... upper)))),
Stem2(length: stem.length),
Expand Down
16 changes: 8 additions & 8 deletions Sources/Lindenmayer/LSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public enum LSystem: Sendable {
/// - Parameters:
/// - axiom: An initial module that represents the initial state of the Lindenmayer system..
/// - prng: An optional psuedo-random number generator to use for randomness in rule productions.
public static func create<RNGType>(_ axiom: Module, with prng: RNGType?) -> RandomContextualLSystem<RNGType> {
public static func create<RNGType: Sendable>(_ axiom: Module, with prng: RNGType?) -> RandomContextualLSystem<RNGType> {
if let prng {
return RandomContextualLSystem(axiom: [axiom], state: nil, newStateIndicators: nil, prng: RNGWrapper(prng))
}
Expand All @@ -66,7 +66,7 @@ public enum LSystem: Sendable {
/// - Parameters:
/// - axiom: A sequence of modules that represents the initial state of the Lindenmayer system..
/// - prng: An optional psuedo-random number generator to use for for randomness in rule productions.
public static func create<RNGType>(_ axiom: [Module], with prng: RNGType?) -> RandomContextualLSystem<RNGType> {
public static func create<RNGType: Sendable>(_ axiom: [Module], with prng: RNGType?) -> RandomContextualLSystem<RNGType> {
if let prng {
return RandomContextualLSystem(axiom: axiom, state: nil, newStateIndicators: nil, prng: RNGWrapper(prng))
}
Expand All @@ -78,22 +78,22 @@ public enum LSystem: Sendable {
/// - axiom: An initial module that represents the initial state of the Lindenmayer system.
/// - prng: An optional psuedo-random number generator to use for for randomness in rule productions.
/// - parameters: An instance of type you provide that the L-system provides to the rules you create for use as parameters.
public static func create<PType, RNGType>(_ axiom: Module, with prng: RNGType?, using parameters: PType) -> ParameterizedRandomContextualLSystem<PType, RNGType> {
public static func create<PType: Sendable, RNGType: Sendable>(_ axiom: Module, with prng: RNGType?, using parameters: PType) -> ParameterizedRandomContextualLSystem<PType, RNGType> {
if let prng {
return ParameterizedRandomContextualLSystem(axiom: [axiom], state: nil, newStateIndicators: nil, parameters: ParametersWrapper(parameters), prng: RNGWrapper(prng), rules: [])
return ParameterizedRandomContextualLSystem(axiom: [axiom], state: nil, newStateIndicators: nil, parameters: parameters, prng: RNGWrapper(prng), rules: [])
}
return ParameterizedRandomContextualLSystem(axiom: [axiom], state: nil, newStateIndicators: nil, parameters: ParametersWrapper(parameters), prng: RNGWrapper(Xoshiro(seed: 42) as! RNGType), rules: [])
return ParameterizedRandomContextualLSystem(axiom: [axiom], state: nil, newStateIndicators: nil, parameters: parameters, prng: RNGWrapper(Xoshiro(seed: 42) as! RNGType), rules: [])
}

/// Creates a new Lindenmayer system from an initial state, using the random number generator and parameter instance that you provide.
/// - Parameters:
/// - axiom: A sequence of modules that represents the initial state of the Lindenmayer system..
/// - prng: An optional psuedo-random number generator to use for for randomness in rule productions.
/// - parameters: An instance of type you provide that the L-system provides to the rules you create for use as parameters.
public static func create<PType, RNGType>(_ axiom: [Module], with prng: RNGType?, using parameters: PType) -> ParameterizedRandomContextualLSystem<PType, RNGType> {
public static func create<PType: Sendable, RNGType: Sendable>(_ axiom: [Module], with prng: RNGType?, using parameters: PType) -> ParameterizedRandomContextualLSystem<PType, RNGType> {
if let prng {
return ParameterizedRandomContextualLSystem(axiom: axiom, state: nil, newStateIndicators: nil, parameters: ParametersWrapper(parameters), prng: RNGWrapper(prng), rules: [])
return ParameterizedRandomContextualLSystem(axiom: axiom, state: nil, newStateIndicators: nil, parameters: parameters, prng: RNGWrapper(prng), rules: [])
}
return ParameterizedRandomContextualLSystem(axiom: axiom, state: nil, newStateIndicators: nil, parameters: ParametersWrapper(parameters), prng: RNGWrapper(Xoshiro(seed: 42) as! RNGType), rules: [])
return ParameterizedRandomContextualLSystem(axiom: axiom, state: nil, newStateIndicators: nil, parameters: parameters, prng: RNGWrapper(Xoshiro(seed: 42) as! RNGType), rules: [])
}
}
24 changes: 12 additions & 12 deletions Sources/Lindenmayer/LSystemTypes/ContextualLSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public extension ContextualLSystem {
/// - produces: A closure that you provide that returns a list of modules to replace the matching module.
/// - Returns: A new L-System with the additional rule added.
func rewrite<DC>(_ direct: DC.Type,
where evalClosure: @escaping (DC) -> Bool,
produces produceClosure: @escaping (DC) -> [Module]) -> Self where DC: Module
where evalClosure: @Sendable @escaping (DC) -> Bool,
produces produceClosure: @Sendable @escaping (DC) -> [Module]) -> Self where DC: Module
{
let newRule = RewriteRuleDirect(direct: direct, where: evalClosure, produce: produceClosure)
var newRuleSet: [Rule] = rules
Expand All @@ -147,7 +147,7 @@ public extension ContextualLSystem {
/// - produce: A closure that you provide that returns a list of modules to replace the matching module.
/// - Returns: A new L-System with the additional rule added.
func rewrite<DC>(_ direct: DC.Type,
produces produceClosure: @escaping (DC) -> [Module]) -> Self where DC: Module
produces produceClosure: @Sendable @escaping (DC) -> [Module]) -> Self where DC: Module
{
let newRule = RewriteRuleDirect(direct: direct, where: nil, produce: produceClosure)
var newRuleSet: [Rule] = rules
Expand All @@ -162,8 +162,8 @@ public extension ContextualLSystem {
/// - produces: A closure that you provide that returns a list of modules to replace the matching module.
/// - Returns: A new L-System with the additional rule added.
func rewrite<LC, DC>(leftContext: LC.Type, directContext: DC.Type,
where evalClosure: @escaping (LC, DC) -> Bool,
produces produceClosure: @escaping (LC, DC) -> [Module]) -> Self where LC: Module, DC: Module
where evalClosure: @Sendable @escaping (LC, DC) -> Bool,
produces produceClosure: @Sendable @escaping (LC, DC) -> [Module]) -> Self where LC: Module, DC: Module
{
let newRule = RewriteRuleLeftDirect(leftType: leftContext, directType: directContext, where: evalClosure, produces: produceClosure)
var newRuleSet: [Rule] = rules
Expand All @@ -177,7 +177,7 @@ public extension ContextualLSystem {
/// - produce: A closure that you provide that returns a list of modules to replace the matching module.
/// - Returns: A new L-System with the additional rule added.
func rewrite<LC, DC>(leftContext: LC.Type, directContext: DC.Type,
produces produceClosure: @escaping (LC, DC) -> [Module]) -> Self where LC: Module, DC: Module
produces produceClosure: @Sendable @escaping (LC, DC) -> [Module]) -> Self where LC: Module, DC: Module
{
let newRule = RewriteRuleLeftDirect(leftType: leftContext, directType: directContext, where: nil, produces: produceClosure)
var newRuleSet: [Rule] = rules
Expand All @@ -192,8 +192,8 @@ public extension ContextualLSystem {
/// - produces: A closure that you provide that returns a list of modules to replace the matching module.
/// - Returns: A new L-System with the additional rule added.
func rewrite<DC, RC>(directContext: DC.Type, rightContext: RC.Type,
where evalClosure: @escaping (DC, RC) -> Bool,
produces produceClosure: @escaping (DC, RC) -> [Module]) -> Self where DC: Module, RC: Module
where evalClosure: @Sendable @escaping (DC, RC) -> Bool,
produces produceClosure: @Sendable @escaping (DC, RC) -> [Module]) -> Self where DC: Module, RC: Module
{
let newRule = RewriteRuleDirectRight(directType: directContext, rightType: rightContext, where: evalClosure, produces: produceClosure)
var newRuleSet: [Rule] = rules
Expand All @@ -207,7 +207,7 @@ public extension ContextualLSystem {
/// - produce: A closure that you provide that returns a list of modules to replace the matching module.
/// - Returns: A new L-System with the additional rule added.
func rewrite<DC, RC>(directContext: DC.Type, rightContext: RC.Type,
produces produceClosure: @escaping (DC, RC) -> [Module]) -> Self where DC: Module, RC: Module
produces produceClosure: @Sendable @escaping (DC, RC) -> [Module]) -> Self where DC: Module, RC: Module
{
let newRule = RewriteRuleDirectRight(directType: directContext, rightType: rightContext, where: nil, produces: produceClosure)
var newRuleSet: [Rule] = rules
Expand All @@ -222,8 +222,8 @@ public extension ContextualLSystem {
/// - produces: A closure that you provide that returns a list of modules to replace the matching module.
/// - Returns: A new L-System with the additional rule added.
func rewrite<LC, DC, RC>(leftContext: LC.Type, directContext: DC.Type, rightContext: RC.Type,
where evalClosure: @escaping (LC, DC, RC) -> Bool,
produces produceClosure: @escaping (LC, DC, RC) -> [Module]) -> Self where LC: Module, DC: Module, RC: Module
where evalClosure: @Sendable @escaping (LC, DC, RC) -> Bool,
produces produceClosure: @Sendable @escaping (LC, DC, RC) -> [Module]) -> Self where LC: Module, DC: Module, RC: Module
{
let newRule = RewriteRuleLeftDirectRight(leftType: leftContext, directType: directContext, rightType: rightContext, where: evalClosure, produces: produceClosure)
var newRuleSet: [Rule] = rules
Expand All @@ -237,7 +237,7 @@ public extension ContextualLSystem {
/// - produce: A closure that you provide that returns a list of modules to replace the matching module.
/// - Returns: A new L-System with the additional rule added.
func rewrite<LC, DC, RC>(leftContext: LC.Type, directContext: DC.Type, rightContext: RC.Type,
produces produceClosure: @escaping (LC, DC, RC) -> [Module]) -> Self where LC: Module, DC: Module, RC: Module
produces produceClosure: @Sendable @escaping (LC, DC, RC) -> [Module]) -> Self where LC: Module, DC: Module, RC: Module
{
let newRule = RewriteRuleLeftDirectRight(leftType: leftContext, directType: directContext, rightType: rightContext, where: nil, produces: produceClosure)
var newRuleSet: [Rule] = rules
Expand Down
Loading