This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func computeHashes(_ value: T) -> [Int] { | |
var results: [Int] = [] | |
for hashFunction in hashFunctions { | |
let value = abs(hashFunction(value)) % array.count | |
results.append(value) | |
} | |
return results | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func computeHashes(_ value: T) -> [Int] { | |
return hashFunctions.map({ | |
(hashFunction: (T) -> Int) -> Int in | |
return abs(hashFunction(value)) % array.count | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func computeHashes(_ value: T) -> [Int] { | |
return hashFunctions.map() { hashFunction in abs(hashFunction(value) % array.count) } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BloomFilter<T> { | |
private var array: [Bool] | |
private var hashFunctions: [(T) -> Int] | |
public init(size: Int = 1024, hashFunctions: [(T) -> Int]) { | |
self.array = [Bool](repeating: false, count: size) | |
self.hashFunctions = hashFunctions | |
} | |
private func computeHashes(_ value: T) -> [Int] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(MemoryLayout<Bool>.size) | |
// A Bool is represented with 1 byte | |
print(MemoryLayout<Int>.size) | |
// An Int is represented with 8 bytes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var badDomains: Set<String> = ["badguys.com", "drevil.me", "virus.io"] | |
let websiteToLoad = NSURL(string: "http://virus.io") | |
if let validDomain = websiteToLoad?.host { | |
if badDomains.contains(validDomain) { | |
print("This website is known to be malicious!") | |
} else { | |
print("This website doesn't exist in our dossier. Let's proceed.") | |
} | |
} |
NewerOlder