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

+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/Contents.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ extension String {
1616
func index(of pattern: String, usingHorspoolImprovement: Bool = false) -> Index? {
1717
// Cache the length of the search pattern because we're going to
1818
// use it a few times and it's expensive to calculate.
19-
let patternLength = pattern.characters.count
20-
guard patternLength > 0, patternLength <= characters.count else { return nil }
19+
let patternLength = pattern.count
20+
guard patternLength > 0, patternLength <= count else { return nil }
2121

2222
// Make the skip table. This table determines how far we skip ahead
2323
// when a character from the pattern is found.
2424
var skipTable = [Character: Int]()
25-
for (i, c) in pattern.characters.enumerated() {
25+
for (i, c) in pattern.enumerated() {
2626
skipTable[c] = patternLength - i - 1
2727
}
2828

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>

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 = "#CharacterRangeLen=0&amp;CharacterRangeLoc=75&amp;EndingColumnNumber=37&amp;EndingLineNumber=3&amp;StartingColumnNumber=9&amp;StartingLineNumber=3&amp;Timestamp=503226447.616217"
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 = "#CharacterRangeLen=0&amp;CharacterRangeLoc=75&amp;EndingColumnNumber=26&amp;EndingLineNumber=3&amp;StartingColumnNumber=9&amp;StartingLineNumber=3&amp;Timestamp=503226447.61657"
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 = "#CharacterRangeLen=0&amp;CharacterRangeLoc=75&amp;EndingColumnNumber=25&amp;EndingLineNumber=3&amp;StartingColumnNumber=9&amp;StartingLineNumber=3&amp;Timestamp=503226447.616725"
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ extension String {
99
func index(of pattern: String, usingHorspoolImprovement: Bool = false) -> Index? {
1010
// Cache the length of the search pattern because we're going to
1111
// use it a few times and it's expensive to calculate.
12-
let patternLength = pattern.characters.count
13-
guard patternLength > 0, patternLength <= characters.count else { return nil }
12+
let patternLength = pattern.count
13+
guard patternLength > 0, patternLength <= count else { return nil }
1414

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]()
18-
for (i, c) in pattern.characters.enumerated() {
18+
for (i, c) in pattern.enumerated() {
1919
skipTable[c] = patternLength - i - 1
2020
}
2121

Bubble Sort/README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ There is no Fifth pass
117117

118118
#### Conclusion
119119

120-
Even with the proposed optimizations, this is still a terribly inefficient sorting algorithm. A good alternative is [Merge Sort](), that not only is better performing, has a similar degree of dificulty to implement.
120+
Even with the proposed optimizations, this is still a terribly inefficient sorting algorithm. A good alternative is [Merge Sort](https://github.com/raywenderlich/swift-algorithm-club/tree/master/Merge%20Sort), that not only is better performing, has a similar degree of dificulty to implement.
121121

122122
*Updated for the Swift Algorithm Club by Julio Brazil*
123123

HaversineDistance/HaversineDistance.playground/Contents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func haversineDinstance(la1: Double, lo1: Double, la2: Double, lo2: Double, radi
1616

1717
// Converts from degrees to radians
1818
let dToR = { (angle: Double) -> Double in
19-
return (angle / 360) * 2 * M_PI
19+
return (angle / 360) * 2 * .pi
2020
}
2121

2222
let lat1 = dToR(la1)

0 commit comments

Comments
 (0)