Skip to content

Commit

Permalink
Clip to range for Comparables.
Browse files Browse the repository at this point in the history
  • Loading branch information
benfed committed Nov 21, 2018
1 parent 9a60274 commit a6c2638
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions BXSwiftUtils.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
48DF72F520697E1E00D224AA /* TypedKVOTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48DF72F420697E1E00D224AA /* TypedKVOTests.swift */; };
48FF9BC820E116B100024C10 /* Weak.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48FF9BC620E116B100024C10 /* Weak.swift */; };
48FF9BCA20E1174C00024C10 /* Weak.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48FF9BC920E1174C00024C10 /* Weak.swift */; };
95CBED9321A5B4D100344220 /* Comparable+Clip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95CBED9221A5B4D100344220 /* Comparable+Clip.swift */; };
D0149008213D566500A38870 /* KVO+propagateChanges.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0149006213D566500A38870 /* KVO+propagateChanges.swift */; };
D03234EB205BF939003D6CCB /* URL+Info.swift in Sources */ = {isa = PBXBuildFile; fileRef = D03234E9205BF938003D6CCB /* URL+Info.swift */; };
D03234EF205BFC9F003D6CCB /* String+Localized.swift in Sources */ = {isa = PBXBuildFile; fileRef = D03234ED205BFC9F003D6CCB /* String+Localized.swift */; };
Expand Down Expand Up @@ -152,6 +153,7 @@
48DF72F420697E1E00D224AA /* TypedKVOTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypedKVOTests.swift; sourceTree = "<group>"; };
48FF9BC620E116B100024C10 /* Weak.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Weak.swift; sourceTree = "<group>"; };
48FF9BC920E1174C00024C10 /* Weak.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Weak.swift; sourceTree = "<group>"; };
95CBED9221A5B4D100344220 /* Comparable+Clip.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Comparable+Clip.swift"; sourceTree = "<group>"; };
D0149006213D566500A38870 /* KVO+propagateChanges.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "KVO+propagateChanges.swift"; sourceTree = "<group>"; };
D03234E9205BF938003D6CCB /* URL+Info.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "URL+Info.swift"; sourceTree = "<group>"; };
D03234ED205BFC9F003D6CCB /* String+Localized.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Localized.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -461,6 +463,7 @@
D089124A215B723C00403100 /* CGRect+String.swift */,
481A25BC2175D36000FB8E9A /* CGPath+RoundedRect.swift */,
481A25BF2175D38700FB8E9A /* CACornerMask+Convenience.swift */,
95CBED9221A5B4D100344220 /* Comparable+Clip.swift */,
);
path = "Math & Geometry";
sourceTree = "<group>";
Expand Down Expand Up @@ -738,6 +741,7 @@
48A971E22159024100215F9F /* Enum+Comparable.swift in Sources */,
D06F462E209073D0000986B8 /* Collection+Codable.swift in Sources */,
D03234EF205BFC9F003D6CCB /* String+Localized.swift in Sources */,
95CBED9321A5B4D100344220 /* Comparable+Clip.swift in Sources */,
D046DF85216B7ED0004331C3 /* Optional+If.swift in Sources */,
D03234FE205C2919003D6CCB /* CGRect+Accessors.swift in Sources */,
D032350E205C362A003D6CCB /* Date+Components.swift in Sources */,
Expand Down Expand Up @@ -931,6 +935,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_DYLIB_INSTALL_NAME = "$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.14;
PRODUCT_BUNDLE_IDENTIFIER = com.boinx.BXSwiftUtils;
PRODUCT_NAME = BXSwiftUtils;
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -960,6 +965,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_DYLIB_INSTALL_NAME = "$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.14;
PRODUCT_BUNDLE_IDENTIFIER = com.boinx.BXSwiftUtils;
PRODUCT_NAME = BXSwiftUtils;
SKIP_INSTALL = YES;
Expand Down
33 changes: 33 additions & 0 deletions BXSwiftUtils/Math & Geometry/Comparable+Clip.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Comparable+Clip.swift
// BXSwiftUtils
//
// Created by Benjamin Federer on 21.11.18.
// Copyright © 2018 Boinx Software Ltd. & Imagine GbR. All rights reserved.
//

import Foundation

extension Comparable
{
/// Returns `self` clipped to the closed interval `[minValue, maxValue]`.
///
/// - Parameters:
/// - minValue: The minimum allowed value. Will clip `self` if smaller.
/// - maxValue: The maximum allowed value. Will clip `self` if greater.
/// - Returns: `self` if `self` is within the interval `[minValue, maxValue]`, `minValue` or `maxValue` otherwise.
public func clipped<T>(minValue: T, maxValue: T) -> T where T : Comparable
{
return min(maxValue, max(minValue, self as! T))
}

/// Clips to the closed interval `[minValue, maxValue]`.
///
/// - Parameters:
/// - minValue: The minimum allowed value. Will clip `self` if smaller.
/// - maxValue: The maximum allowed value. Will clip `self` if greater.
public mutating func clip<T>(minValue: T, maxValue: T) where T : Comparable
{
self = min(maxValue, max(minValue, self as! T)) as! Self
}
}

0 comments on commit a6c2638

Please sign in to comment.