Add support for asynchronous modifications

This commit is contained in:
david-swift 2024-09-29 11:51:30 +02:00
parent f6b1282e3f
commit 4e54213113
6 changed files with 99 additions and 2 deletions

View File

@ -35,7 +35,7 @@ jobs:
- name: Modify Docs - name: Modify Docs
run: | run: |
echo "<script>window.location.href += \"/documentation/levenshteintransformations\"</script>" > docs/index.html; echo "<script>window.location.href += \"/documentation/levenshteintransformations\"</script>" > docs/index.html;
sed -i '' 's/,2px/,10px/g' docs/css/index.038e887c.css sed -i '' 's/,2px/,10px/g' docs/css/index.*.css
- name: Upload Artifact - name: Upload Artifact
uses: actions/upload-pages-artifact@v3 uses: actions/upload-pages-artifact@v3
with: with:

View File

@ -141,7 +141,7 @@ file_header:
missing_docs: missing_docs:
warning: [internal, private] warning: [internal, private]
error: [open, public] error: [open, public]
excludes_extensions: false excludes_extensions: true
excludes_inherited_types: false excludes_inherited_types: false
type_contents_order: type_contents_order:
order: order:

View File

@ -0,0 +1,33 @@
//
// AsyncFunctions.swift
// LevenshteinTransformations
//
// Created by david-swift on 29.09.24.
//
/// The replace, delete and insert functions type.
public struct AsyncFunctions<Element> {
/// The replace function.
var replace: (Int, Element) async -> Void
/// The delete function.
var delete: (Int) async -> Void
/// The insert function.
var insert: (Int, Element) async -> Void
/// Initialize a functions value.
/// - Parameters:
/// - replace: Replace the element at a certain index with a certain element.
/// - delete: Delete the element at a certain index.
/// - insert: Insert a certain element at a certain index.
public init(
replace: @escaping (Int, Element) async -> Void,
delete: @escaping (Int) async -> Void,
insert: @escaping (Int, Element) async -> Void
) {
self.replace = replace
self.delete = delete
self.insert = insert
}
}

View File

@ -34,6 +34,19 @@ extension Array where Element: Equatable {
} }
} }
/// Call every transformation step needed to transform the array into the target array.
/// - Parameters:
/// - target: The target array.
/// - functions: The transformation functions.
public func transform(to target: [Element], functions: AsyncFunctions<Element>) async {
var transformations = getTransformations(to: target)
while !transformations.isEmpty {
let transformation = transformations.removeFirst()
await transformation.transform(functions: functions, nextTransformations: &transformations)
}
}
} }
extension Array where Element: Identifiable { extension Array where Element: Identifiable {
@ -65,4 +78,17 @@ extension Array where Element: Identifiable {
} }
} }
/// Call every transformation step needed to transform the array into the target array.
/// - Parameters:
/// - target: The target array.
/// - functions: The transformation functions.
public func identifiableTransform(to target: [Element], functions: AsyncFunctions<Element>) async {
var transformations = identifiableGetTransformations(to: target)
while !transformations.isEmpty {
let transformation = transformations.removeFirst()
await transformation.transform(functions: functions, nextTransformations: &transformations)
}
}
} }

View File

@ -34,4 +34,17 @@ extension String {
} }
} }
/// Call every transformation step needed to transform the string into the target string.
/// - Parameters:
/// - target: The target string.
/// - functions: The transformation functions.
public func transform(to target: String, functions: AsyncFunctions<Character>) async {
var transformations = getTransformations(to: target)
while !transformations.isEmpty {
let transformation = transformations.removeFirst()
await transformation.transform(functions: functions, nextTransformations: &transformations)
}
}
} }

View File

@ -101,6 +101,31 @@ public enum Transformation<Element> {
} }
} }
} }
/// Apply the transformation using a functions value.
/// - Parameters:
/// - functions: The functions value.
/// - nextTransformations: All the following transformations for modifying the indices.
public func transform(
functions: AsyncFunctions<Element>,
nextTransformations: inout [Transformation<Element>]
) async {
switch self {
case let .replace(index, element):
await functions.replace(index, element)
case let .delete(index):
await functions.delete(index)
for index in nextTransformations.indices {
nextTransformations[index].index -= 1
}
case let .insert(index, element):
await functions.insert(index, element)
for index in nextTransformations.indices {
nextTransformations[index].index += 1
}
}
}
} }
// swiftlint:enable identifier_name // swiftlint:enable identifier_name