generated from dankinsoid/iOSLibraryTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c5cf01
commit 7f7d20a
Showing
35 changed files
with
2,342 additions
and
879 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import Foundation | ||
|
||
/// Range expression that supports all range operators | ||
public struct AnyRange<Bound: Comparable>: RangeExpression { | ||
|
||
public let lowerBound: Bound? | ||
public let upperBound: Bound? | ||
public let include: Set<RangeEdge> | ||
|
||
public init(lowerBound: Bound?, upperBound: Bound?, include: Set<RangeEdge>) { | ||
self.lowerBound = lowerBound.map { lowerBound in | ||
upperBound.map { min(lowerBound, $0) } ?? lowerBound | ||
} | ||
self.upperBound = upperBound.map { upperBound in | ||
lowerBound.map { min(upperBound, $0) } ?? upperBound | ||
} | ||
self.include = include | ||
} | ||
|
||
public subscript(_ edge: RangeEdge) -> Bound? { | ||
switch edge { | ||
case .lower: return lowerBound | ||
case .upper: return upperBound | ||
} | ||
} | ||
|
||
public func relative<C: Collection>(to collection: C) -> Range<Bound> where Bound == C.Index { | ||
switch (lowerBound, upperBound, include.contains(.lower), include.contains(.upper)) { | ||
case (.some(let lower), let .some(upper), true, true): | ||
return (lower ... upper).relative(to: collection) | ||
case (.some(let lower), let .some(upper), false, true): | ||
return upLowerBound((lower ... upper).relative(to: collection), in: collection) | ||
case (.some(let lower), let .some(upper), true, false): | ||
return (lower ..< upper).relative(to: collection) | ||
case (.some(let lower), let .some(upper), false, false): | ||
return upLowerBound((lower ..< upper).relative(to: collection), in: collection) | ||
case (.some(let lower), .none, true, _): | ||
return (lower...).relative(to: collection) | ||
case (.some(let lower), .none, false, _): | ||
return upLowerBound((lower...).relative(to: collection), in: collection) | ||
case (.none, let .some(upper), _, true): | ||
return (...upper).relative(to: collection) | ||
case (.none, let .some(upper), _, false): | ||
return (..<upper).relative(to: collection) | ||
case (.none, .none, _, _): | ||
return collection.startIndex ..< collection.endIndex | ||
} | ||
} | ||
|
||
public func contains(_ element: Bound) -> Bool { | ||
let isUpper: Bool | ||
if let lowerBound { | ||
isUpper = include.contains(.lower) ? element >= lowerBound : element > lowerBound | ||
} else { | ||
isUpper = true | ||
} | ||
let isLower: Bool | ||
if let upperBound { | ||
isLower = include.contains(.upper) ? element <= upperBound : element < upperBound | ||
} else { | ||
isLower = true | ||
} | ||
return isLower && isUpper | ||
} | ||
|
||
public static var any: AnyRange { | ||
AnyRange(lowerBound: nil, upperBound: nil, include: []) | ||
} | ||
|
||
private func upLowerBound<C: Collection>(_ range: Range<Bound>, in collection: C) -> Range<Bound> where Bound == C.Index { | ||
guard range.upperBound > range.lowerBound else { | ||
return range | ||
} | ||
return collection.index(after: range.lowerBound) ..< range.upperBound | ||
} | ||
} | ||
|
||
@_disfavoredOverload | ||
public func ... <Bound: Comparable>(_ lhs: Bound, _ rhs: Bound) -> AnyRange<Bound> { | ||
AnyRange(lowerBound: lhs, upperBound: rhs, include: [.lower, .upper]) | ||
} | ||
|
||
@_disfavoredOverload | ||
public func ..< <Bound: Comparable>(_ lhs: Bound, _ rhs: Bound) -> AnyRange<Bound> { | ||
AnyRange(lowerBound: lhs, upperBound: rhs, include: [.lower]) | ||
} | ||
|
||
@_disfavoredOverload | ||
public postfix func ... <Bound: Comparable>(_ lhs: Bound) -> AnyRange<Bound> { | ||
AnyRange(lowerBound: lhs, upperBound: nil, include: [.lower]) | ||
} | ||
|
||
@_disfavoredOverload | ||
public prefix func ..< <Bound: Comparable>(_ rhs: Bound) -> AnyRange<Bound> { | ||
AnyRange(lowerBound: nil, upperBound: rhs, include: []) | ||
} | ||
|
||
@_disfavoredOverload | ||
public prefix func ... <Bound: Comparable>(_ rhs: Bound) -> AnyRange<Bound> { | ||
AnyRange(lowerBound: nil, upperBound: rhs, include: [.upper]) | ||
} | ||
|
||
public enum RangeEdge: Hashable { | ||
|
||
case lower, upper | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
|
||
extension Bool { | ||
|
||
var nilIfFalse: Bool? { | ||
self ? self : nil | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
|
||
extension Decimal { | ||
|
||
var double: Double { | ||
(self as NSDecimalNumber).doubleValue | ||
} | ||
} |
Oops, something went wrong.