Skip to content

Commit d01fb8d

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 9ba8b54 + 98c0d55 commit d01fb8d

File tree

31 files changed

+184
-70
lines changed

31 files changed

+184
-70
lines changed

B-Tree/BTree.playground/Contents.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import Foundation
44

5-
// last checked with Xcode 9.0b4
6-
#if swift(>=4.0)
7-
print("Hello, Swift 4!")
8-
#endif
5+
// last checked with Xcode 10.0
96

107
let bTree = BTree<Int, Int>(order: 1)!
118

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

B-Tree/Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@
8585
isa = PBXProject;
8686
attributes = {
8787
LastSwiftUpdateCheck = 0730;
88-
LastUpgradeCheck = 0800;
88+
LastUpgradeCheck = 1000;
8989
ORGANIZATIONNAME = "Viktor Szilárd Simkó";
9090
TargetAttributes = {
9191
C66702771D0EEE25008CD769 = {
9292
CreatedOnToolsVersion = 7.3.1;
93-
LastSwiftMigration = 0800;
93+
LastSwiftMigration = 1000;
9494
};
9595
};
9696
};
@@ -144,14 +144,22 @@
144144
CLANG_CXX_LIBRARY = "libc++";
145145
CLANG_ENABLE_MODULES = YES;
146146
CLANG_ENABLE_OBJC_ARC = YES;
147+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
147148
CLANG_WARN_BOOL_CONVERSION = YES;
149+
CLANG_WARN_COMMA = YES;
148150
CLANG_WARN_CONSTANT_CONVERSION = YES;
151+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
149152
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
150153
CLANG_WARN_EMPTY_BODY = YES;
151154
CLANG_WARN_ENUM_CONVERSION = YES;
152155
CLANG_WARN_INFINITE_RECURSION = YES;
153156
CLANG_WARN_INT_CONVERSION = YES;
157+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
158+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
159+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
154160
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
161+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
162+
CLANG_WARN_STRICT_PROTOTYPES = YES;
155163
CLANG_WARN_SUSPICIOUS_MOVE = YES;
156164
CLANG_WARN_UNREACHABLE_CODE = YES;
157165
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -192,14 +200,22 @@
192200
CLANG_CXX_LIBRARY = "libc++";
193201
CLANG_ENABLE_MODULES = YES;
194202
CLANG_ENABLE_OBJC_ARC = YES;
203+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
195204
CLANG_WARN_BOOL_CONVERSION = YES;
205+
CLANG_WARN_COMMA = YES;
196206
CLANG_WARN_CONSTANT_CONVERSION = YES;
207+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
197208
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
198209
CLANG_WARN_EMPTY_BODY = YES;
199210
CLANG_WARN_ENUM_CONVERSION = YES;
200211
CLANG_WARN_INFINITE_RECURSION = YES;
201212
CLANG_WARN_INT_CONVERSION = YES;
213+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
214+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
215+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
202216
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
217+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
218+
CLANG_WARN_STRICT_PROTOTYPES = YES;
203219
CLANG_WARN_SUSPICIOUS_MOVE = YES;
204220
CLANG_WARN_UNREACHABLE_CODE = YES;
205221
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -234,7 +250,7 @@
234250
PRODUCT_BUNDLE_IDENTIFIER = viktorsimko.Tests;
235251
PRODUCT_NAME = "$(TARGET_NAME)";
236252
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
237-
SWIFT_VERSION = 4.0;
253+
SWIFT_VERSION = 4.2;
238254
};
239255
name = Debug;
240256
};
@@ -247,7 +263,7 @@
247263
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
248264
PRODUCT_BUNDLE_IDENTIFIER = viktorsimko.Tests;
249265
PRODUCT_NAME = "$(TARGET_NAME)";
250-
SWIFT_VERSION = 4.0;
266+
SWIFT_VERSION = 4.2;
251267
};
252268
name = Release;
253269
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

B-Tree/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0800"
3+
LastUpgradeVersion = "1000"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Bloom Filter/BloomFilter.playground/Contents.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
//: Playground - noun: a place where people can play
2-
// last checked with Xcode 9.0b4
3-
#if swift(>=4.0)
4-
print("Hello, Swift 4!")
5-
#endif
2+
63
public class BloomFilter<T> {
74
fileprivate var array: [Bool]
85
private var hashFunctions: [(T) -> Int]
@@ -54,15 +51,15 @@ public class BloomFilter<T> {
5451

5552
func djb2(x: String) -> Int {
5653
var hash = 5381
57-
for char in x.characters {
54+
for char in x {
5855
hash = ((hash << 5) &+ hash) &+ char.hashValue
5956
}
6057
return Int(hash)
6158
}
6259

6360
func sdbm(x: String) -> Int {
6461
var hash = 0
65-
for char in x.characters {
62+
for char in x {
6663
hash = char.hashValue &+ (hash << 6) &+ (hash << 16) &- hash
6764
}
6865
return Int(hash)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Bloom Filter/BloomFilter.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

Bloom Filter/README.markdown

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,31 @@ public func query(_ value: T) -> Bool {
100100

101101
If you're coming from another imperative language, you might notice the unusual syntax in the `exists` assignment. Swift makes use of functional paradigms when it makes code more consise and readable, and in this case `reduce` is a much more consise way to check if all the required bits are `true` than a `for` loop.
102102

103-
*Written for Swift Algorithm Club by Jamil Dhanani. Edited by Matthijs Hollemans.*
103+
## Another approach
104+
105+
In the previous section, you learnt about how using multiple different hash functions can help reduce the chance of collisions in the bloom filter. However, good hash functions are difficult to design. A simple alternative to multiple hash functions is to use a set of random numbers.
106+
107+
As an example, let's say a bloom filter wants to hash each element 15 times during insertion. Instead of using 15 different hash functions, you can rely on just one hash function. The hash value can then be combined with 15 different values to form the indices for flipping. This bloom filter would initialize a set of 15 random numbers ahead of time and use these values during each insertion.
108+
109+
```
110+
hash("Hello world!") >> hash(987654321) // would flip bit 8
111+
hash("Hello world!") >> hash(123456789) // would flip bit 2
112+
```
113+
114+
Since Swift 4.2, `Hasher` is now included in the Standard library, which is designed to reduce multiple hashes to a single hash in an efficient manner. This makes combining the hashes trivial.
115+
116+
```
117+
private func computeHashes(_ value: T) -> [Int] {
118+
return randomSeeds.map() { seed in
119+
let hasher = Hasher()
120+
hasher.combine(seed)
121+
hasher.combine(value)
122+
let hashValue = hasher.finalize()
123+
return abs(hashValue % array.count)
124+
}
125+
}
126+
```
127+
128+
If you want to learn more about this approach, you can read about the [Hasher documentation](https://developer.apple.com/documentation/swift/hasher) or Soroush Khanlou's [Swift 4.2 Bloom filter](http://khanlou.com/2018/09/bloom-filters/) implementation.
129+
130+
*Written for Swift Algorithm Club by Jamil Dhanani. Edited by Matthijs Hollemans. Updated by Bruno Scheele.*

Bloom Filter/Tests/BloomFilterTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import XCTest
66
func djb2(_ x: String) -> Int {
77
var hash = 5381
88

9-
for char in x.characters {
9+
for char in x {
1010
hash = ((hash << 5) &+ hash) &+ char.hashValue
1111
}
1212

@@ -16,7 +16,7 @@ func djb2(_ x: String) -> Int {
1616
func sdbm(_ x: String) -> Int {
1717
var hash = 0
1818

19-
for char in x.characters {
19+
for char in x {
2020
hash = char.hashValue &+ (hash << 6) &+ (hash << 16) &- hash
2121
}
2222

0 commit comments

Comments
 (0)