From 3f6a9dc76de9642d2bf36264bfb260663909e86a Mon Sep 17 00:00:00 2001 From: Stephen Bensley <76857027+stephenbensley@users.noreply.github.com> Date: Thu, 5 Sep 2024 14:57:18 -0700 Subject: [PATCH] Use bsearch from UtiliKit --- Core/PositionEvaluator.swift | 24 +----------- Queah.xcodeproj/project.pbxproj | 37 +++++++++++++++++++ .../xcshareddata/swiftpm/Package.resolved | 15 ++++++++ 3 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 Queah.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved diff --git a/Core/PositionEvaluator.swift b/Core/PositionEvaluator.swift index 5fa85e4..99f6b75 100644 --- a/Core/PositionEvaluator.swift +++ b/Core/PositionEvaluator.swift @@ -6,6 +6,7 @@ // import Foundation +import UtiliKit // Type used for storing game values. A game never takes more than 91 half-moves, so 8 bits is // plenty. Using an Int8 reduces both disk and memory footprint. @@ -19,27 +20,6 @@ extension Array where Element: Hashable { } } -extension Array where Element: Comparable { - // Binary search a sorted array. Returns the index of the matching element or nil if no - // match is found. - func bsearch(_ key: Element) -> Self.Index? { - var lo: Self.Index = 0 - var hi: Self.Index = self.count - 1 - - while lo <= hi { - let mid: Self.Index = (lo + hi) / 2 - if self[mid] == key { - return mid - } else if self[mid] < key { - lo = mid + 1 - } else { - hi = mid - 1 - } - } - return nil - } -} - // Stores the cached position values to allow rapid evaluation of game states. final class PositionEvaluator { // All reachable values of GamePosition.boardId in sorted order @@ -109,7 +89,7 @@ final class PositionEvaluator { private func index(_ position: GamePosition) -> Int { // Compute the indices into the 3D array - let idx1 = ids.bsearch(position.boardId)! + let idx1 = ids.bsearch(for: position.boardId)! let idx2 = position.attacker.reserveCount let idx3 = position.defender.reserveCount // Now compute the final offset diff --git a/Queah.xcodeproj/project.pbxproj b/Queah.xcodeproj/project.pbxproj index 1c71275..e8911d0 100644 --- a/Queah.xcodeproj/project.pbxproj +++ b/Queah.xcodeproj/project.pbxproj @@ -20,6 +20,8 @@ DC4A8AE62A3F59F70068924C /* MenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC4A8AE12A3F59F70068924C /* MenuView.swift */; }; DC4A8AE72A3F59F70068924C /* QueahApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC4A8AE22A3F59F70068924C /* QueahApp.swift */; }; DC73A5742C5AFD39008BBD55 /* queahSolution.data in Resources */ = {isa = PBXBuildFile; fileRef = DC73A5732C5AFD1C008BBD55 /* queahSolution.data */; }; + DC77B7B62C8A5F8600D9DF28 /* UtiliKit in Frameworks */ = {isa = PBXBuildFile; productRef = DC77B7B52C8A5F8600D9DF28 /* UtiliKit */; }; + DC77B7B82C8A608C00D9DF28 /* UtiliKit in Frameworks */ = {isa = PBXBuildFile; productRef = DC77B7B72C8A608C00D9DF28 /* UtiliKit */; }; DC84DF622A3F5669009368A3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DC84DF612A3F5669009368A3 /* Assets.xcassets */; }; DCA32CCC2C596FB8009DF4A5 /* SolverApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCA32CCB2C596FB8009DF4A5 /* SolverApp.swift */; }; DCA32CCE2C596FB8009DF4A5 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCA32CCD2C596FB8009DF4A5 /* ContentView.swift */; }; @@ -78,6 +80,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + DC77B7B82C8A608C00D9DF28 /* UtiliKit in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -85,6 +88,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + DC77B7B62C8A5F8600D9DF28 /* UtiliKit in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -192,6 +196,9 @@ dependencies = ( ); name = Queah; + packageProductDependencies = ( + DC77B7B72C8A608C00D9DF28 /* UtiliKit */, + ); productName = Queah; productReference = DC84DF5A2A3F5669009368A3 /* Queah.app */; productType = "com.apple.product-type.application"; @@ -209,6 +216,9 @@ dependencies = ( ); name = QueahSolver; + packageProductDependencies = ( + DC77B7B52C8A5F8600D9DF28 /* UtiliKit */, + ); productName = Solver; productReference = DCA32CC92C596FB8009DF4A5 /* Queah Solver.app */; productType = "com.apple.product-type.application"; @@ -240,6 +250,9 @@ Base, ); mainGroup = DC84DF512A3F5669009368A3; + packageReferences = ( + DC77B7B42C8A5F8600D9DF28 /* XCRemoteSwiftPackageReference "UtiliKit" */, + ); productRefGroup = DC84DF5B2A3F5669009368A3 /* Products */; projectDirPath = ""; projectRoot = ""; @@ -595,6 +608,30 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCRemoteSwiftPackageReference section */ + DC77B7B42C8A5F8600D9DF28 /* XCRemoteSwiftPackageReference "UtiliKit" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/stephenbensley/UtiliKit"; + requirement = { + branch = main; + kind = branch; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + DC77B7B52C8A5F8600D9DF28 /* UtiliKit */ = { + isa = XCSwiftPackageProductDependency; + package = DC77B7B42C8A5F8600D9DF28 /* XCRemoteSwiftPackageReference "UtiliKit" */; + productName = UtiliKit; + }; + DC77B7B72C8A608C00D9DF28 /* UtiliKit */ = { + isa = XCSwiftPackageProductDependency; + package = DC77B7B42C8A5F8600D9DF28 /* XCRemoteSwiftPackageReference "UtiliKit" */; + productName = UtiliKit; + }; +/* End XCSwiftPackageProductDependency section */ }; rootObject = DC84DF522A3F5669009368A3 /* Project object */; } diff --git a/Queah.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Queah.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 0000000..ae1f8df --- /dev/null +++ b/Queah.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,15 @@ +{ + "originHash" : "9904166925030d438115cf3427cde4d59a2e94263ad3f7cb6cc541da2d8f7430", + "pins" : [ + { + "identity" : "utilikit", + "kind" : "remoteSourceControl", + "location" : "https://github.com/stephenbensley/UtiliKit", + "state" : { + "branch" : "main", + "revision" : "c1bdbc229b4b774a6331e733f64172aa9e6386f7" + } + } + ], + "version" : 3 +}