Protocol-Oriented Integers #cswift

59

Transcript of Protocol-Oriented Integers #cswift

Page 1: Protocol-Oriented Integers #cswift
Page 2: Protocol-Oriented Integers #cswift
Page 3: Protocol-Oriented Integers #cswift
Page 4: Protocol-Oriented Integers #cswift
Page 5: Protocol-Oriented Integers #cswift
Page 6: Protocol-Oriented Integers #cswift
Page 7: Protocol-Oriented Integers #cswift
Page 8: Protocol-Oriented Integers #cswift
Page 9: Protocol-Oriented Integers #cswift
Page 10: Protocol-Oriented Integers #cswift
Page 11: Protocol-Oriented Integers #cswift

Swift 3.0 and Later

Page 12: Protocol-Oriented Integers #cswift

http://ieeexplore.ieee.org/servlet/opac?punumber=4610933

Page 13: Protocol-Oriented Integers #cswift
Page 14: Protocol-Oriented Integers #cswift
Page 15: Protocol-Oriented Integers #cswift
Page 16: Protocol-Oriented Integers #cswift
Page 17: Protocol-Oriented Integers #cswift
Page 18: Protocol-Oriented Integers #cswift
Page 19: Protocol-Oriented Integers #cswift

Swift 4.0 and Later

Page 20: Protocol-Oriented Integers #cswift
Page 21: Protocol-Oriented Integers #cswift
Page 22: Protocol-Oriented Integers #cswift
Page 23: Protocol-Oriented Integers #cswift
Page 24: Protocol-Oriented Integers #cswift

init?<T>(exactly source: T) where T : BinaryInteger

Page 25: Protocol-Oriented Integers #cswift

associatedtype Magnitude : Comparable, ExpressibleByIntegerLiteral

var magnitude: Magnitude { get }

“-5.magnitude”

Page 26: Protocol-Oriented Integers #cswift
Page 27: Protocol-Oriented Integers #cswift

prefix static func +(x: Self) -> Self {

return x }

Page 28: Protocol-Oriented Integers #cswift
Page 29: Protocol-Oriented Integers #cswift
Page 30: Protocol-Oriented Integers #cswift
Page 31: Protocol-Oriented Integers #cswift

// 対象の型で、自身の型とその内部の絶対値表現の型とが同じだった場合 func abs<T>(_ x: T) -> T where T : SignedNumeric, T == T.Magnitude

// 対象の型が、比較可能な符号付き整数だった場合 func abs<T>(_ x: T) -> T where T : Comparable, T : SignedNumeric

Page 32: Protocol-Oriented Integers #cswift
Page 33: Protocol-Oriented Integers #cswift
Page 34: Protocol-Oriented Integers #cswift
Page 35: Protocol-Oriented Integers #cswift

// 任意の整数型からの全幅変換 init<T>(_ source: T) where T : BinaryInteger

// ビットパターンを使った内部表現そのままの変換 init<T>(extendingOrTruncating source: T) where T : BinaryInteger

// 強制的に範囲内に収める変換 init<T>(clamping source: T) where T : BinaryInteger

Page 36: Protocol-Oriented Integers #cswift

// 浮動小数点数型からの丸め変換(0 方向へ丸める) init<T>(_ source: T) where T : FloatingPoint

// 浮動小数点数型からの Exact 変換(値を丸めない) init?<T>(exactly source: T) where T : FloatingPoint

Page 37: Protocol-Oriented Integers #cswift

// 0 をインスタンス化するイニシャライザー init() {

self = 0 }

Page 38: Protocol-Oriented Integers #cswift

associatedtype Words

var words: Words { get }

// WORD 表現したときの WORD 数を取得 var countRepresentedWords: Int { get }

Page 39: Protocol-Oriented Integers #cswift
Page 40: Protocol-Oriented Integers #cswift
Page 41: Protocol-Oriented Integers #cswift
Page 42: Protocol-Oriented Integers #cswift
Page 43: Protocol-Oriented Integers #cswift
Page 44: Protocol-Oriented Integers #cswift

static func &>> <Other>(lhs: Self, rhs: Other) -> Self where Other : BinaryInteger

static func &>>= <Other>(lhs: inout Self, rhs: Other) where Other : BinaryInteger

static func &<< <Other>(lhs: Self, rhs: Other) -> Self where Other : BinaryInteger

static func &<<= <Other>(lhs: inout Self, rhs: Other) where Other : BinaryInteger

Page 45: Protocol-Oriented Integers #cswift
Page 46: Protocol-Oriented Integers #cswift

var words: [UInt] {

var result = [UInt]()

result.reserveCapacity(countRepresentedWords)

for i in 0 ..< countRepresentedWords {

result.append(_word(at: i)) }

return result }

Page 47: Protocol-Oriented Integers #cswift
Page 48: Protocol-Oriented Integers #cswift
Page 49: Protocol-Oriented Integers #cswift
Page 50: Protocol-Oriented Integers #cswift
Page 51: Protocol-Oriented Integers #cswift
Page 52: Protocol-Oriented Integers #cswift
Page 53: Protocol-Oriented Integers #cswift
Page 54: Protocol-Oriented Integers #cswift
Page 55: Protocol-Oriented Integers #cswift

init(bigEndian value: Int) init(littleEndian value: Int)

// 内部データを序列を加味して解釈する var bigEndian: Int { get } var littleEndian: Int { get }

// 内部データの序列を交換して解釈する var byteSwapped: Int { get }

Page 56: Protocol-Oriented Integers #cswift

// ビットパターンからの変換(nil の場合は 0 で初期化される様子) init(bitPattern pointer: OpaquePointer?)

// デコーダーからの変換(使用方法は未確認) init(from decoder: Decoder) throws

// エンコード機能もある様子(使用方法は未確認) func encode(to encoder: Encoder) throws

Page 57: Protocol-Oriented Integers #cswift
Page 58: Protocol-Oriented Integers #cswift

func multipliedFullWidth(by other: Int) -> (high: Int, low: Int.Magnitude)

func dividingFullWidth(_ dividend: (high: Int, low: Int.Magnitude)) -> (quotient: Int, remainder: Int)

Page 59: Protocol-Oriented Integers #cswift