From 4e54213113bc43cb09c0032bb9c2101cfde320ab Mon Sep 17 00:00:00 2001 From: david-swift Date: Sun, 29 Sep 2024 11:51:30 +0200 Subject: [PATCH] Add support for asynchronous modifications --- .github/workflows/docs.yml | 2 +- .swiftlint.yml | 2 +- .../AsyncFunctions.swift | 33 +++++++++++++++++++ .../Extensions/Array.swift | 26 +++++++++++++++ .../Extensions/String.swift | 13 ++++++++ .../Transformation.swift | 25 ++++++++++++++ 6 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 Sources/LevenshteinTransformations/AsyncFunctions.swift diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 2e76983..71ad073 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -35,7 +35,7 @@ jobs: - name: Modify Docs run: | echo "" > 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: diff --git a/.swiftlint.yml b/.swiftlint.yml index 441da6e..54567f5 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -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: diff --git a/Sources/LevenshteinTransformations/AsyncFunctions.swift b/Sources/LevenshteinTransformations/AsyncFunctions.swift new file mode 100644 index 0000000..5823132 --- /dev/null +++ b/Sources/LevenshteinTransformations/AsyncFunctions.swift @@ -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 { + + /// 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 + } + +} diff --git a/Sources/LevenshteinTransformations/Extensions/Array.swift b/Sources/LevenshteinTransformations/Extensions/Array.swift index 99a56e7..ce79f29 100644 --- a/Sources/LevenshteinTransformations/Extensions/Array.swift +++ b/Sources/LevenshteinTransformations/Extensions/Array.swift @@ -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) 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) async { + var transformations = identifiableGetTransformations(to: target) + + while !transformations.isEmpty { + let transformation = transformations.removeFirst() + await transformation.transform(functions: functions, nextTransformations: &transformations) + } + } + } diff --git a/Sources/LevenshteinTransformations/Extensions/String.swift b/Sources/LevenshteinTransformations/Extensions/String.swift index da042e3..def2b2b 100644 --- a/Sources/LevenshteinTransformations/Extensions/String.swift +++ b/Sources/LevenshteinTransformations/Extensions/String.swift @@ -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) async { + var transformations = getTransformations(to: target) + + while !transformations.isEmpty { + let transformation = transformations.removeFirst() + await transformation.transform(functions: functions, nextTransformations: &transformations) + } + } + } diff --git a/Sources/LevenshteinTransformations/Transformation.swift b/Sources/LevenshteinTransformations/Transformation.swift index a9b17e9..1f9fda2 100644 --- a/Sources/LevenshteinTransformations/Transformation.swift +++ b/Sources/LevenshteinTransformations/Transformation.swift @@ -101,6 +101,31 @@ public enum Transformation { } } } + + /// 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, + nextTransformations: inout [Transformation] + ) 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