Skip to content

Commit

Permalink
Fix bits unit
Browse files Browse the repository at this point in the history
  • Loading branch information
fummicc1 committed Nov 16, 2024
1 parent 0d25c22 commit 08bc1f8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 52 deletions.
12 changes: 6 additions & 6 deletions Sources/GeoHashFramework/GeoHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Foundation

public struct GeoHash: Sendable, Hashable {
public private(set) var precision: GeoHashPrecision
public private(set) var precision: GeoHashBitsPrecision
public private(set) var binary: String

/// Base32 characters used to hash
Expand All @@ -19,7 +19,7 @@ public struct GeoHash: Sendable, Hashable {
/// - Parameter value: A string that contains only "0" or "1".
public init(
binary: String,
precision: GeoHashPrecision = .mid
precision: GeoHashBitsPrecision = .mid
) {
precondition(
binary.allSatisfy({
Expand All @@ -39,7 +39,7 @@ public struct GeoHash: Sendable, Hashable {
public init(
latitude: Double,
longitude: Double,
precision: GeoHashPrecision = .mid
precision: GeoHashBitsPrecision = .mid
) {
precondition(
latitude >= -90 && latitude <= 90,
Expand All @@ -62,7 +62,7 @@ public struct GeoHash: Sendable, Hashable {
)
}

public init(geoHash: String, precision: GeoHashPrecision = .mid) {
public init(geoHash: String, precision: GeoHashBitsPrecision = .mid) {
self.init(
binary: Self.makeBinary(
from: geoHash,
Expand All @@ -77,7 +77,7 @@ public struct GeoHash: Sendable, Hashable {
extension GeoHash {
static func makeBinary(
from geoHash: String,
precision: GeoHashPrecision
precision: GeoHashBitsPrecision
) -> String {
var binary = ""

Expand All @@ -98,7 +98,7 @@ extension GeoHash {
return binary
}

static func makeBinary(from coordinate: GeoHashCoordinate2D, precision: GeoHashPrecision)
static func makeBinary(from coordinate: GeoHashCoordinate2D, precision: GeoHashBitsPrecision)
-> String
{
let latitude = coordinate.latitude
Expand Down
33 changes: 33 additions & 0 deletions Sources/GeoHashFramework/GeoHashBitsPrecision.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/// A GeoHashPrecision enum represents the bits precision of the GeoHash in binary unit.
///
/// - low: 30 bits precision. This corresponds to 6 characters of GeoHash.
/// - mid: 40 bits precision. This corresponds to 8 characters of GeoHash.
/// - high: 50 bits precision. This corresponds to 10 characters of GeoHash.
public enum GeoHashBitsPrecision: Sendable, Hashable {
/// 6 digits GeoHash
case low
/// 8 digits GeoHash
case mid
/// 10 digits GeoHash
case high

/// `digits` **bits** precision.
/// - Note: `digits` must be a multiple of 4 because each GeoHash character is 5 bits.
case exact(digits: Int)

public var rawValue: Int {
switch self {
case .low: return 6 * 5
case .mid: return 8 * 5
case .high: return 10 * 5
case .exact(let digits):
precondition(digits % 5 == 0)
return digits
}
}

var format: String {
let rawValue = self.rawValue
return "%0\(rawValue / 5)d"
}
}
6 changes: 3 additions & 3 deletions Sources/GeoHashFramework/GeoHashCluster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
///
/// This data structure is used to store GeoHashes and their relations.
public struct GeoHashCluster: Sendable, Hashable {
private var clusters: [GeoHashPrecision: [GeoHash]] = [:]
private var clusters: [GeoHashBitsPrecision: [GeoHash]] = [:]

public init() {
clusters = [:]
}

public func isSame(geoHash1: GeoHash, geoHash2: GeoHash, precision: GeoHashPrecision) -> Bool {
public func isSame(geoHash1: GeoHash, geoHash2: GeoHash, precision: GeoHashBitsPrecision) -> Bool {
fatalError("Not implemented")
}

public func mergeIfPossible(geoHash1: GeoHash, geoHash2: GeoHash, precision: GeoHashPrecision) {
public func mergeIfPossible(geoHash1: GeoHash, geoHash2: GeoHash, precision: GeoHashBitsPrecision) {
fatalError("Not implemented")
}
}
33 changes: 0 additions & 33 deletions Sources/GeoHashFramework/GeoHashPrecision.swift

This file was deleted.

20 changes: 10 additions & 10 deletions Tests/GeoHashFrameworkTests/GeoHashTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@ import Testing
struct GeoHashTests {
@Test
func makeGeoHashFromBinary() async throws {
let input = "0110101001101010"
let input = "01101010011010100110"
let geoHash = GeoHash(
binary: input,
precision: .exact(digits: 16)
precision: .exact(digits: 20)
)
#expect(geoHash.geoHash == "e9p0")
#expect(geoHash.geoHash.count == input.count / 4)
#expect(geoHash.geoHash == "e9p6")
#expect(geoHash.geoHash.count == input.count / 5)
}

@Test
func makeFromBinary() async throws {
let input = "0110101001101010"
let input = "01101010011010100110"

let expectedLat = "10001000"
let expectedLng = "01110111"
let expectedLat = "1000100010"
let expectedLng = "0111011101"

let geoHash = GeoHash(
binary: input,
precision: .exact(
digits: 16
digits: 20
)
)
#expect(geoHash.latitudeBits == expectedLat)
Expand All @@ -45,10 +45,10 @@ struct GeoHashTests {

let geoHash = GeoHash(latitude: lat, longitude: lng)
#expect(
geoHash.binary == "11101101000011100110110101011111"
geoHash.binary == "1110110100001110011011010101111110001101"
)
#expect(
geoHash.geoHash == "xn76urs"
geoHash.geoHash == "xn76urwe"
)
}
}

0 comments on commit 08bc1f8

Please sign in to comment.