forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBit.swift
150 lines (123 loc) · 4.38 KB
/
Bit.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
//===--- Bit.swift - A 1-bit type that can be used as an Index ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Used to index CollectionOfOne<T>
//
//===----------------------------------------------------------------------===//
/// A `RandomAccessIndexType` that has two possible values. Used as
/// the `Index` type for `CollectionOfOne<T>`.
public enum Bit : Int, Comparable, RandomAccessIndexType, _Reflectable {
public typealias Distance = Int
case Zero = 0, One = 1
/// Returns the next consecutive value after `self`.
///
/// - Requires: `self == .Zero`.
public func successor() -> Bit {
_precondition(self == .Zero, "Can't increment past one")
return .One
}
/// Returns the previous consecutive value before `self`.
///
/// - Requires: `self != .Zero`.
public func predecessor() -> Bit {
_precondition(self == .One, "Can't decrement past zero")
return .Zero
}
public func distanceTo(other: Bit) -> Int {
return rawValue.distanceTo(other.rawValue)
}
public func advancedBy(n: Distance) -> Bit {
return rawValue.advancedBy(n) > 0 ? One : Zero
}
/// Returns a mirror that reflects `self`.
public func _getMirror() -> _MirrorType {
return _BitMirror(self)
}
}
internal struct _BitMirror : _MirrorType {
let _value: Bit
init(_ v: Bit) {
self._value = v
}
var value: Any { return _value }
var valueType: Any.Type { return (_value as Any).dynamicType }
var objectIdentifier: ObjectIdentifier? { return .None }
var count: Int { return 0 }
subscript(i: Int) -> (String, _MirrorType) {
_preconditionFailure("_MirrorType access out of bounds")
}
var summary: String {
switch _value {
case .Zero: return ".Zero"
case .One: return ".One"
}
}
var quickLookObject: PlaygroundQuickLook? { return .None }
var disposition: _MirrorDisposition { return .Enum }
}
@warn_unused_result
public func == (lhs: Bit, rhs: Bit) -> Bool {
return lhs.rawValue == rhs.rawValue
}
@warn_unused_result
public func < (lhs: Bit, rhs: Bit) -> Bool {
return lhs.rawValue < rhs.rawValue
}
extension Bit : IntegerArithmeticType {
static func _withOverflow(v: (Int, overflow: Bool)) -> (Bit, overflow: Bool) {
if let b = Bit(rawValue: v.0) {
return (b, v.overflow)
} else {
return (Bit(rawValue: v.0 % 2)!, true)
}
}
/// Add `lhs` and `rhs`, returning a result and a `Bool` that is
/// true iff the operation caused an arithmetic overflow.
public static func addWithOverflow(
lhs: Bit, _ rhs: Bit
) -> (Bit, overflow: Bool) {
return _withOverflow(Int.addWithOverflow(lhs.rawValue, rhs.rawValue))
}
/// Subtract `lhs` and `rhs`, returning a result and a `Bool` that is
/// true iff the operation caused an arithmetic overflow.
public static func subtractWithOverflow(
lhs: Bit, _ rhs: Bit
) -> (Bit, overflow: Bool) {
return _withOverflow(Int.subtractWithOverflow(lhs.rawValue, rhs.rawValue))
}
/// Multiply `lhs` and `rhs`, returning a result and a `Bool` that is
/// true iff the operation caused an arithmetic overflow.
public static func multiplyWithOverflow(
lhs: Bit, _ rhs: Bit
) -> (Bit, overflow: Bool) {
return _withOverflow(Int.multiplyWithOverflow(lhs.rawValue, rhs.rawValue))
}
/// Divide `lhs` and `rhs`, returning a result and a `Bool` that is
/// true iff the operation caused an arithmetic overflow.
public static func divideWithOverflow(
lhs: Bit, _ rhs: Bit
) -> (Bit, overflow: Bool) {
return _withOverflow(Int.divideWithOverflow(lhs.rawValue, rhs.rawValue))
}
/// Divide `lhs` and `rhs`, returning the remainder and a `Bool` that is
/// true iff the operation caused an arithmetic overflow.
public static func remainderWithOverflow(
lhs: Bit, _ rhs: Bit
) -> (Bit, overflow: Bool) {
return _withOverflow(Int.remainderWithOverflow(lhs.rawValue, rhs.rawValue))
}
/// Represent this number using Swift's widest native signed integer
/// type.
public func toIntMax() -> IntMax {
return IntMax(rawValue)
}
}