Skip to content

Commit 3848ed5

Browse files
committed
Add Initial Extensions
1 parent 00661f3 commit 3848ed5

7 files changed

Lines changed: 661 additions & 0 deletions

Pod/Classes/Array2D.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// Array2D.swift
3+
//
4+
// Created by Mark Hamilton on 3/31/16.
5+
// Copyright © 2016 dryverless. (http://www.dryverless.com)
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
//
25+
26+
import Foundation
27+
28+
public struct Array2D {
29+
30+
var columns: Int
31+
var rows: Int
32+
var matrix: [Int]
33+
34+
35+
init(columns: Int, rows: Int) {
36+
37+
self.columns = columns
38+
39+
self.rows = rows
40+
41+
matrix = Array(count:columns*rows, repeatedValue:0)
42+
43+
}
44+
45+
subscript(column: Int, row: Int) -> Int {
46+
47+
get {
48+
49+
return matrix[columns * row + column]
50+
51+
}
52+
53+
set {
54+
55+
matrix[columns * row + column] = newValue
56+
57+
}
58+
59+
}
60+
61+
func columnCount() -> Int {
62+
63+
return self.columns
64+
65+
}
66+
67+
func rowCount() -> Int {
68+
69+
return self.rows
70+
71+
}
72+
}
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
//
2+
// BinarySearchExtensions.swift
3+
//
4+
// Created by Mark Hamilton on 3/26/16.
5+
// Copyright © 2016 dryverless. (http://www.dryverless.com)
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
//
25+
26+
import Foundation
27+
28+
public enum CalcError: ErrorType {
29+
30+
case InvalidAverage
31+
32+
case InvalidMiddleValue
33+
}
34+
35+
public func binarySearch< T : Comparable >(collection: [T], query: T) throws -> Bool {
36+
37+
var leftCount: Int = 0
38+
var rightCount: Int = collection.count - 1
39+
40+
while( leftCount <= rightCount ) {
41+
42+
guard let middleValue: Int = (( leftCount + rightCount ) / 2) else {
43+
44+
throw CalcError.InvalidAverage
45+
46+
}
47+
48+
guard let estimatedValue: T = collection[middleValue] else {
49+
50+
throw CalcError.InvalidMiddleValue
51+
52+
}
53+
54+
if (estimatedValue == query) {
55+
56+
return true
57+
58+
}
59+
60+
if (estimatedValue < query) {
61+
62+
leftCount = middleValue + 1
63+
64+
}
65+
66+
if (estimatedValue > query) {
67+
68+
rightCount = middleValue - 1
69+
70+
}
71+
72+
}
73+
74+
return false
75+
76+
}
77+
78+
public func binarySearchPrefix(collection: [String], query: String) throws -> Bool {
79+
80+
var leftCount: Int = 0
81+
var rightCount: Int = collection.count - 1
82+
83+
while( leftCount <= rightCount ) {
84+
85+
guard let middleValue: Int = (( leftCount + rightCount ) / 2) else {
86+
87+
throw CalcError.InvalidAverage
88+
89+
}
90+
91+
guard let estimatedValue: String = collection[middleValue] else {
92+
93+
throw CalcError.InvalidMiddleValue
94+
95+
}
96+
97+
if (estimatedValue.hasPrefix(query)) {
98+
99+
return true
100+
101+
}
102+
103+
if (estimatedValue < query) {
104+
105+
leftCount = middleValue + 1
106+
107+
}
108+
109+
if (estimatedValue > query) {
110+
111+
rightCount = middleValue - 1
112+
113+
}
114+
115+
}
116+
117+
return false
118+
119+
}
120+
121+
public func binarySearchFirst(collection: [String], query: String) throws -> Int {
122+
123+
var leftCount: Int = 0
124+
var rightCount: Int = collection.count - 1
125+
126+
while( leftCount <= rightCount ) {
127+
128+
guard let middleValue: Int = (( leftCount + rightCount ) / 2) else {
129+
130+
throw CalcError.InvalidAverage
131+
132+
}
133+
134+
guard let estimatedValue: String = collection[middleValue] else {
135+
136+
throw CalcError.InvalidMiddleValue
137+
138+
}
139+
140+
if (estimatedValue.hasPrefix(query) && leftCount == rightCount) {
141+
142+
return leftCount
143+
144+
}
145+
146+
if estimatedValue.hasPrefix(query) {
147+
148+
if middleValue > 0 && !collection[middleValue - 1].hasPrefix(query) {
149+
150+
return middleValue
151+
152+
}
153+
154+
rightCount = middleValue - 1
155+
156+
} else if (estimatedValue < query) {
157+
158+
leftCount = middleValue + 1
159+
160+
} else if (estimatedValue > query) {
161+
162+
rightCount = middleValue - 1
163+
164+
}
165+
166+
}
167+
168+
return -1
169+
170+
}
171+
172+
public func binarySearchLast(collection: [String], query: String) throws -> Int {
173+
174+
var leftCount: Int = 0
175+
var rightCount: Int = collection.count - 1
176+
177+
while( leftCount <= rightCount ) {
178+
179+
guard let middleValue: Int = (( leftCount + rightCount ) / 2) else {
180+
181+
throw CalcError.InvalidAverage
182+
183+
}
184+
185+
guard let estimatedValue: String = collection[middleValue] else {
186+
187+
throw CalcError.InvalidMiddleValue
188+
189+
}
190+
191+
if (estimatedValue.hasPrefix(query) && leftCount == rightCount) {
192+
193+
return leftCount
194+
195+
}
196+
197+
if estimatedValue.hasPrefix(query) {
198+
199+
if middleValue < collection.count - 1 && !collection[middleValue + 1].hasPrefix(query) {
200+
201+
return middleValue
202+
203+
}
204+
205+
leftCount = middleValue + 1
206+
207+
} else if (estimatedValue < query) {
208+
209+
leftCount = middleValue + 1
210+
211+
} else if (estimatedValue > query) {
212+
213+
rightCount = middleValue - 1
214+
215+
}
216+
217+
}
218+
219+
return -1
220+
221+
}

Pod/Classes/GlobalExtensions.swift

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// GlobalExtensions.swift
3+
//
4+
// Created by Mark Hamilton on 3/23/16.
5+
// Copyright © 2016 dryverless. (http://www.dryverless.com)
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
//
25+
26+
import Foundation
27+
28+
29+
public extension Int {
30+
31+
public func add(num: Int) {
32+
33+
self.advancedBy(num)
34+
35+
}
36+
37+
}
38+
39+
public extension NSObject {
40+
41+
/*
42+
43+
example:
44+
45+
object.delay(2) {
46+
// do after 2 seconds
47+
}
48+
49+
*/
50+
51+
public func delay(delay:Double, closure:(() -> Void)) {
52+
53+
dispatch_after(
54+
55+
dispatch_time(
56+
57+
DISPATCH_TIME_NOW,
58+
59+
Int64(delay * Double(NSEC_PER_SEC))
60+
61+
), GlobalMainQueue, closure)
62+
63+
}
64+
65+
}
66+
67+
/*
68+
69+
example:
70+
71+
delay(2) {
72+
// do after 2 seconds
73+
}
74+
75+
*/
76+
77+
public func delay(delay:Double, closure:(() -> Void)) {
78+
79+
dispatch_after(
80+
81+
dispatch_time(
82+
83+
DISPATCH_TIME_NOW,
84+
85+
Int64(delay * Double(NSEC_PER_SEC))
86+
87+
), GlobalMainQueue, closure)
88+
89+
}
90+
91+
// Minimize 3
92+
public func min3(a: Int, b: Int, c: Int) -> Int {
93+
94+
return min( min(a, c), min(b, c))
95+
96+
}

0 commit comments

Comments
 (0)