-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bounded.swift
178 lines (142 loc) · 3.14 KB
/
Bounded.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/// Example of a Swift program Syntax Highlighting.
//
// Bounded.swift
// Swiftz
//
// Created by Robert Widmann on 10/22/14.
// Copyright (c) 2014 Maxwell Swadling. All rights reserved.
//
import Darwin
/// Bounded types are types that have definable upper and lower limits. For types like Int and
/// Float, their limits are the minimum and maximum possible values representable in their bit-
/// width. While the definition of a "limit" is flexible, generally custom types that wish to
/// appear bounded must come with some kind of supremum or infimum.
public protocol Bounded {
static func minBound() -> Self
static func maxBound() -> Self
}
extension Bool : Bounded {
public static func minBound() -> Bool {
return false
}
public static func maxBound() -> Bool {
return true
}
}
extension Character : Bounded {
public static func minBound() -> Character {
return "\0"
}
public static func maxBound() -> Character {
return "\u{FFFF}"
}
}
extension UInt : Bounded {
public static func minBound() -> UInt {
return UInt.min
}
public static func maxBound() -> UInt {
return UInt.max
}
}
extension UInt8 : Bounded {
public static func minBound() -> UInt8 {
return UInt8.min
}
public static func maxBound() -> UInt8 {
return UInt8.max
}
}
extension UInt16 : Bounded {
public static func minBound() -> UInt16 {
return UInt16.min
}
public static func maxBound() -> UInt16 {
return UInt16.max
}
}
extension UInt32 : Bounded {
public static func minBound() -> UInt32 {
return UInt32.min
}
public static func maxBound() -> UInt32 {
return UInt32.max
}
}
extension UInt64 : Bounded {
public static func minBound() -> UInt64 {
return UInt64.min
}
public static func maxBound() -> UInt64 {
return UInt64.max
}
}
extension Int : Bounded {
public static func minBound() -> Int {
return Int.min
}
public static func maxBound() -> Int {
return Int.max
}
}
extension Int8 : Bounded {
public static func minBound() -> Int8 {
return Int8.min
}
public static func maxBound() -> Int8 {
return Int8.max
}
}
extension Int16 : Bounded {
public static func minBound() -> Int16 {
return Int16.min
}
public static func maxBound() -> Int16 {
return Int16.max
}
}
extension Int32 : Bounded {
public static func minBound() -> Int32 {
return Int32.min
}
public static func maxBound() -> Int32 {
return Int32.max
}
}
extension Int64 : Bounded {
public static func minBound() -> Int64 {
return Int64.min
}
public static func maxBound() -> Int64 {
return Int64.max
}
}
#if os(OSX)
extension Float : Bounded {
public static func minBound() -> Float {
return FLT_MIN
}
public static func maxBound() -> Float {
return FLT_MAX
}
}
extension Double : Bounded {
public static func minBound() -> Double {
return DBL_MIN
}
public static func maxBound() -> Double {
return DBL_MAX
}
}
#endif
/// float.h does not export Float80's limits, nor does the Swift STL.
/// rdar://18404510
//extension Swift.Float80 : Bounded {
// public static func minBound() -> Swift.Float80 {
// return LDBL_MIN
// }
//
// public static func maxBound() -> Swift.Float80 {
// return LDBL_MAX
// }
//}