Add support for asynchronous modifications
This commit is contained in:
parent
f6b1282e3f
commit
4e54213113
2
.github/workflows/docs.yml
vendored
2
.github/workflows/docs.yml
vendored
@ -35,7 +35,7 @@ jobs:
|
||||
- name: Modify Docs
|
||||
run: |
|
||||
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
|
||||
uses: actions/upload-pages-artifact@v3
|
||||
with:
|
||||
|
||||
@ -141,7 +141,7 @@ file_header:
|
||||
missing_docs:
|
||||
warning: [internal, private]
|
||||
error: [open, public]
|
||||
excludes_extensions: false
|
||||
excludes_extensions: true
|
||||
excludes_inherited_types: false
|
||||
type_contents_order:
|
||||
order:
|
||||
|
||||
33
Sources/LevenshteinTransformations/AsyncFunctions.swift
Normal file
33
Sources/LevenshteinTransformations/AsyncFunctions.swift
Normal 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
|
||||
}
|
||||
|
||||
}
|
||||
@ -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 {
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user