Skip to content

Commit

Permalink
Use bsearch from UtiliKit
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenbensley committed Sep 5, 2024
1 parent 6e3037c commit 3f6a9dc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
24 changes: 2 additions & 22 deletions Core/PositionEvaluator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions Queah.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -78,13 +80,15 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
DC77B7B82C8A608C00D9DF28 /* UtiliKit in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
DCA32CC62C596FB8009DF4A5 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
DC77B7B62C8A5F8600D9DF28 /* UtiliKit in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -192,6 +196,9 @@
dependencies = (
);
name = Queah;
packageProductDependencies = (
DC77B7B72C8A608C00D9DF28 /* UtiliKit */,
);
productName = Queah;
productReference = DC84DF5A2A3F5669009368A3 /* Queah.app */;
productType = "com.apple.product-type.application";
Expand All @@ -209,6 +216,9 @@
dependencies = (
);
name = QueahSolver;
packageProductDependencies = (
DC77B7B52C8A5F8600D9DF28 /* UtiliKit */,
);
productName = Solver;
productReference = DCA32CC92C596FB8009DF4A5 /* Queah Solver.app */;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -240,6 +250,9 @@
Base,
);
mainGroup = DC84DF512A3F5669009368A3;
packageReferences = (
DC77B7B42C8A5F8600D9DF28 /* XCRemoteSwiftPackageReference "UtiliKit" */,
);
productRefGroup = DC84DF5B2A3F5669009368A3 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -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 */;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"originHash" : "9904166925030d438115cf3427cde4d59a2e94263ad3f7cb6cc541da2d8f7430",
"pins" : [
{
"identity" : "utilikit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/stephenbensley/UtiliKit",
"state" : {
"branch" : "main",
"revision" : "c1bdbc229b4b774a6331e733f64172aa9e6386f7"
}
}
],
"version" : 3
}

0 comments on commit 3f6a9dc

Please sign in to comment.