Guest User

Untitled

a guest
Jan 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. ///
  2. public protocol ClampSupport {
  3. func clamped(from lowerBound: Self, to upperBound: Self) -> Self
  4.  
  5. func clamped(from lowerBound: Self) -> Self
  6.  
  7. func clamped(to lowerBound: Self) -> Self
  8. }
  9.  
  10. public extension ClampSupport where Self: Comparable {
  11. @_transparent
  12. func clamped(from lowerBound: Self, to upperBound: Self) -> Self {
  13. return min(max(self, lowerBound), upperBound)
  14. }
  15.  
  16. @_transparent
  17. func clamped(from lowerBound: Self) -> Self {
  18. return max(self, lowerBound)
  19. }
  20.  
  21. @_transparent
  22. func clamped(to lowerBound: Self) -> Self {
  23. return min(self, lowerBound)
  24. }
  25.  
  26. @_transparent
  27. func clamped(to range: ClosedRange<Self>) -> Self {
  28. return self.clamped(from: range.lowerBound, to: range.upperBound)
  29. }
  30.  
  31. @_transparent
  32. func clamped(from range: PartialRangeFrom<Self>) -> Self {
  33. return self.clamped(from: range.lowerBound)
  34. }
  35.  
  36. @_transparent
  37. func clamped(to range: PartialRangeThrough<Self>) -> Self {
  38. return self.clamped(to: range.upperBound)
  39. }
  40. }
Add Comment
Please, Sign In to add comment