Skip to content

Commit 091f17d

Browse files
Merge branch 'master' into Fix_Boyer-Moore-Horspool
2 parents 17990d8 + f144470 commit 091f17d

File tree

37 files changed

+201
-80
lines changed

37 files changed

+201
-80
lines changed

All-Pairs Shortest Paths/APSP/APSP.xcodeproj/project.pbxproj

+5-3
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
};
197197
493D8DF01CDD5B960089795A = {
198198
CreatedOnToolsVersion = 7.3;
199-
LastSwiftMigration = 0820;
199+
LastSwiftMigration = 1010;
200200
};
201201
};
202202
};
@@ -420,7 +420,8 @@
420420
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSP";
421421
PRODUCT_NAME = "$(TARGET_NAME)";
422422
SKIP_INSTALL = YES;
423-
SWIFT_VERSION = 3.0;
423+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
424+
SWIFT_VERSION = 4.2;
424425
VERSIONING_SYSTEM = "apple-generic";
425426
VERSION_INFO_PREFIX = "";
426427
};
@@ -442,7 +443,8 @@
442443
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSP";
443444
PRODUCT_NAME = "$(TARGET_NAME)";
444445
SKIP_INSTALL = YES;
445-
SWIFT_VERSION = 3.0;
446+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
447+
SWIFT_VERSION = 4.2;
446448
VERSIONING_SYSTEM = "apple-generic";
447449
VERSION_INFO_PREFIX = "";
448450
};

B-Tree/BTree.playground/Contents.swift

+1-4
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

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

+20-4
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
};
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

+1-1
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

+3-6
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)
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

-6
This file was deleted.

Bloom Filter/README.markdown

+28-1
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

+2-2
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

Bloom Filter/Tests/Tests.xcodeproj/project.pbxproj

+20-4
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@
8383
isa = PBXProject;
8484
attributes = {
8585
LastSwiftUpdateCheck = 0720;
86-
LastUpgradeCheck = 0800;
86+
LastUpgradeCheck = 1000;
8787
ORGANIZATIONNAME = "Swift Algorithm Club";
8888
TargetAttributes = {
8989
7B2BBC7F1C779D720067B71D = {
9090
CreatedOnToolsVersion = 7.2;
91-
LastSwiftMigration = 0800;
91+
LastSwiftMigration = 1000;
9292
};
9393
};
9494
};
@@ -141,14 +141,22 @@
141141
CLANG_CXX_LIBRARY = "libc++";
142142
CLANG_ENABLE_MODULES = YES;
143143
CLANG_ENABLE_OBJC_ARC = YES;
144+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
144145
CLANG_WARN_BOOL_CONVERSION = YES;
146+
CLANG_WARN_COMMA = YES;
145147
CLANG_WARN_CONSTANT_CONVERSION = YES;
148+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
146149
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
147150
CLANG_WARN_EMPTY_BODY = YES;
148151
CLANG_WARN_ENUM_CONVERSION = YES;
149152
CLANG_WARN_INFINITE_RECURSION = YES;
150153
CLANG_WARN_INT_CONVERSION = YES;
154+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
155+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
156+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
151157
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
158+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
159+
CLANG_WARN_STRICT_PROTOTYPES = YES;
152160
CLANG_WARN_SUSPICIOUS_MOVE = YES;
153161
CLANG_WARN_UNREACHABLE_CODE = YES;
154162
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -188,14 +196,22 @@
188196
CLANG_CXX_LIBRARY = "libc++";
189197
CLANG_ENABLE_MODULES = YES;
190198
CLANG_ENABLE_OBJC_ARC = YES;
199+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
191200
CLANG_WARN_BOOL_CONVERSION = YES;
201+
CLANG_WARN_COMMA = YES;
192202
CLANG_WARN_CONSTANT_CONVERSION = YES;
203+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
193204
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
194205
CLANG_WARN_EMPTY_BODY = YES;
195206
CLANG_WARN_ENUM_CONVERSION = YES;
196207
CLANG_WARN_INFINITE_RECURSION = YES;
197208
CLANG_WARN_INT_CONVERSION = YES;
209+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
210+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
211+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
198212
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
213+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
214+
CLANG_WARN_STRICT_PROTOTYPES = YES;
199215
CLANG_WARN_SUSPICIOUS_MOVE = YES;
200216
CLANG_WARN_UNREACHABLE_CODE = YES;
201217
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -228,7 +244,7 @@
228244
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
229245
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
230246
PRODUCT_NAME = "$(TARGET_NAME)";
231-
SWIFT_VERSION = 4.0;
247+
SWIFT_VERSION = 4.2;
232248
};
233249
name = Debug;
234250
};
@@ -240,7 +256,7 @@
240256
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
241257
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
242258
PRODUCT_NAME = "$(TARGET_NAME)";
243-
SWIFT_VERSION = 4.0;
259+
SWIFT_VERSION = 4.2;
244260
};
245261
name = Release;
246262
};
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/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme

+1-1
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"

Boyer-Moore-Horspool/BoyerMooreHorspool.playground/timeline.xctimeline

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
version = "3.0">
44
<TimelineItems>
55
<LoggerValueHistoryTimelineItem
6-
documentLocation = "file:///Users/francisco_saldana/Documents/Nearsoft/Open%20source/swift-algorithm-club/Boyer-Moore-Horspool/BoyerMooreHorspool.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=75&amp;EndingColumnNumber=21&amp;EndingLineNumber=2&amp;StartingColumnNumber=21&amp;StartingLineNumber=2&amp;Timestamp=563161016.665907"
6+
documentLocation = "file:///Users/neifmetus/Documents/Projects/swift-algorithm-club/Boyer-Moore-Horspool/BoyerMooreHorspool.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=75&amp;EndingColumnNumber=21&amp;EndingLineNumber=2&amp;StartingColumnNumber=21&amp;StartingLineNumber=2&amp;Timestamp=560891520.806637"
77
selectedRepresentationIndex = "0"
88
shouldTrackSuperviewWidth = "NO">
99
</LoggerValueHistoryTimelineItem>
1010
<LoggerValueHistoryTimelineItem
11-
documentLocation = "file:///Users/francisco_saldana/Documents/Nearsoft/Open%20source/swift-algorithm-club/Boyer-Moore-Horspool/BoyerMooreHorspool.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=75&amp;EndingColumnNumber=21&amp;EndingLineNumber=2&amp;StartingColumnNumber=21&amp;StartingLineNumber=2&amp;Timestamp=563161016.666045"
11+
documentLocation = "file:///Users/neifmetus/Documents/Projects/swift-algorithm-club/Boyer-Moore-Horspool/BoyerMooreHorspool.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=75&amp;EndingColumnNumber=21&amp;EndingLineNumber=2&amp;StartingColumnNumber=21&amp;StartingLineNumber=2&amp;Timestamp=560891520.806855"
1212
selectedRepresentationIndex = "0"
1313
shouldTrackSuperviewWidth = "NO">
1414
</LoggerValueHistoryTimelineItem>
1515
<LoggerValueHistoryTimelineItem
16-
documentLocation = "file:///Users/francisco_saldana/Documents/Nearsoft/Open%20source/swift-algorithm-club/Boyer-Moore-Horspool/BoyerMooreHorspool.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=75&amp;EndingColumnNumber=21&amp;EndingLineNumber=2&amp;StartingColumnNumber=21&amp;StartingLineNumber=2&amp;Timestamp=563161016.666169"
16+
documentLocation = "file:///Users/neifmetus/Documents/Projects/swift-algorithm-club/Boyer-Moore-Horspool/BoyerMooreHorspool.playground#CharacterRangeLen=0&amp;CharacterRangeLoc=75&amp;EndingColumnNumber=21&amp;EndingLineNumber=2&amp;StartingColumnNumber=21&amp;StartingLineNumber=2&amp;Timestamp=560891520.807023"
1717
selectedRepresentationIndex = "0"
1818
shouldTrackSuperviewWidth = "NO">
1919
</LoggerValueHistoryTimelineItem>

Boyer-Moore-Horspool/BoyerMooreHorspool.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ extension String {
1111
// use it a few times and it's expensive to calculate.
1212
let patternLength = pattern.count
1313
guard patternLength > 0, patternLength <= self.count else { return nil }
14-
14+
1515
// Make the skip table. This table determines how far we skip ahead
1616
// when a character from the pattern is found.
1717
var skipTable = [Character: Int]()

0 commit comments

Comments
 (0)