IntExtensions.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // IntExtensions.swift
  3. // EZSwiftExtensions
  4. //
  5. // Created by Goktug Yilmaz on 16/07/15.
  6. // Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
  7. //
  8. import Foundation
  9. extension Int {
  10. /// EZSE: Checks if the integer is even.
  11. public var isEven: Bool { return (self % 2 == 0) }
  12. /// EZSE: Checks if the integer is odd.
  13. public var isOdd: Bool { return (self % 2 != 0) }
  14. /// EZSE: Checks if the integer is positive.
  15. public var isPositive: Bool { return (self > 0) }
  16. /// EZSE: Checks if the integer is negative.
  17. public var isNegative: Bool { return (self < 0) }
  18. /// EZSE: Converts integer value to Double.
  19. public var toDouble: Double { return Double(self) }
  20. /// EZSE: Converts integer value to Float.
  21. public var toFloat: Float { return Float(self) }
  22. /// EZSE: Converts integer value to CGFloat.
  23. public var toCGFloat: CGFloat { return CGFloat(self) }
  24. /// EZSE: Converts integer value to String.
  25. public var toString: String { return String(self) }
  26. /// EZSE: Converts integer value to UInt.
  27. public var toUInt: UInt { return UInt(self) }
  28. /// EZSE: Converts integer value to Int32.
  29. public var toInt32: Int32 { return Int32(self) }
  30. /// EZSE: Converts integer value to a 0..<Int range. Useful in for loops.
  31. public var range: CountableRange<Int> { return 0..<self }
  32. /// EZSE: Returns number of digits in the integer.
  33. public var digits: Int {
  34. if self == 0 {
  35. return 1
  36. } else if Int(fabs(Double(self))) <= LONG_MAX {
  37. return Int(log10(fabs(Double(self)))) + 1
  38. }
  39. }
  40. /// EZSE: The digits of an integer represented in an array(from most significant to least).
  41. /// This method ignores leading zeros and sign
  42. public var digitArray: [Int] {
  43. var digits = [Int]()
  44. for char in self.toString {
  45. if let digit = Int(String(char)) {
  46. digits.append(digit)
  47. }
  48. }
  49. return digits
  50. }
  51. /// EZSE: Returns a random integer number in the range min...max, inclusive.
  52. public static func random(within: Range<Int>) -> Int {
  53. let delta = within.upperBound - within.lowerBound
  54. return within.lowerBound + Int(arc4random_uniform(UInt32(delta)))
  55. }
  56. }
  57. extension UInt {
  58. /// EZSE: Convert UInt to Int
  59. public var toInt: Int { return Int(self) }
  60. /// EZSE: Greatest common divisor of two integers using the Euclid's algorithm.
  61. /// Time complexity of this in O(log(n))
  62. public static func gcd(_ firstNum: UInt, _ secondNum: UInt) -> UInt {
  63. let remainder = firstNum % secondNum
  64. if remainder != 0 {
  65. return gcd(secondNum, remainder)
  66. } else {
  67. return secondNum
  68. }
  69. }
  70. /// EZSE: Least common multiple of two numbers. LCM = n * m / gcd(n, m)
  71. public static func lcm(_ firstNum: UInt, _ secondNum: UInt) -> UInt {
  72. return firstNum * secondNum / UInt.gcd(firstNum, secondNum)
  73. }
  74. }