From e731bdb1e3d6a31e05683a04b0c4743cf40b5597 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sat, 5 Sep 2020 17:32:43 -0700 Subject: [PATCH] Renaming --- .../CodableBloomFilter/BloomFilter.swift | 26 +++++++++---------- .../CodableBloomFilterTests.swift | 4 +-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/CodableBloomFilter/Sources/CodableBloomFilter/BloomFilter.swift b/CodableBloomFilter/Sources/CodableBloomFilter/BloomFilter.swift index c811b0d..3e1abaf 100644 --- a/CodableBloomFilter/Sources/CodableBloomFilter/BloomFilter.swift +++ b/CodableBloomFilter/Sources/CodableBloomFilter/BloomFilter.swift @@ -8,26 +8,26 @@ import Foundation public struct BloomFilter { public let hashers: [DeterministicHasher] - public let bits: Int + public let bitCount: Int - private var data: BitArray + private var bitArray: BitArray - public init(hashes: [DeterministicHasher], bits: Int) { - self.hashers = hashes - self.bits = bits - data = BitArray(count: bits) + public init(hashers: [DeterministicHasher], bits: Int) { + self.hashers = hashers + bitCount = bits + bitArray = BitArray(count: bits) } } public extension BloomFilter { mutating func insert(_ newMember: T) { for index in indices(newMember) { - data[index] = true + bitArray[index] = true } } func contains(_ member: T) -> Bool { - indices(member).map { data[$0] }.allSatisfy { $0 } + indices(member).map { bitArray[$0] }.allSatisfy { $0 } } } @@ -42,21 +42,21 @@ extension BloomFilter: Codable { let container = try decoder.container(keyedBy: CodingKeys.self) hashers = try container.decode([DeterministicHasher].self, forKey: .hashers) - bits = try container.decode(Int.self, forKey: .bits) - data = BitArray(data: try container.decode(Data.self, forKey: .data), count: bits) + bitCount = try container.decode(Int.self, forKey: .bits) + bitArray = BitArray(data: try container.decode(Data.self, forKey: .data), count: bitCount) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(hashers, forKey: .hashers) - try container.encode(bits, forKey: .bits) - try container.encode(data.data, forKey: .data) + try container.encode(bitCount, forKey: .bits) + try container.encode(bitArray.data, forKey: .data) } } private extension BloomFilter { func indices(_ member: T) -> [Int] { - hashers.map { abs($0.apply(member)) % bits } + hashers.map { abs($0.apply(member)) % bitCount } } } diff --git a/CodableBloomFilter/Tests/CodableBloomFilterTests/CodableBloomFilterTests.swift b/CodableBloomFilter/Tests/CodableBloomFilterTests/CodableBloomFilterTests.swift index 5bf90bd..9d8105f 100644 --- a/CodableBloomFilter/Tests/CodableBloomFilterTests/CodableBloomFilterTests.swift +++ b/CodableBloomFilter/Tests/CodableBloomFilterTests/CodableBloomFilterTests.swift @@ -3,7 +3,7 @@ import XCTest final class CodableBloomFilterTests: XCTestCase { func testContains() { - var sut = BloomFilter(hashes: [.djb2, .sdbm], bits: 1024) + var sut = BloomFilter(hashers: [.djb2, .sdbm], bits: 1024) sut.insert("lol") sut.insert("ok") @@ -15,7 +15,7 @@ final class CodableBloomFilterTests: XCTestCase { } func testCoding() throws { - var sut = BloomFilter(hashes: [.djb2, .sdbm], bits: 64) + var sut = BloomFilter(hashers: [.djb2, .sdbm], bits: 64) let expectedSerialization = Data(#"{"bits":64,"data":"ABAAAAACAJA=","hashers":["djb2","sdbm"]}"#.utf8) sut.insert("lol")