Skip to content

Commit

Permalink
Merge pull request #148 from dn-m/newtype
Browse files Browse the repository at this point in the history
Implement NewType
  • Loading branch information
jsbean committed Aug 20, 2018
2 parents ec0c578 + 81ab886 commit ce548fb
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Sources/DataStructures/CircularArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/// loop[circular: 6] // => 0
/// loop[from: 2, through: 7] // => [2,3,4,5,0,1]
///
public struct CircularArray<Element> {
public struct CircularArray <Element> {

private var storage: Array<Element>

Expand Down
113 changes: 113 additions & 0 deletions Sources/DataStructures/NewType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// NewType.swift
// DataStructures
//
// Created by James Bean on 8/20/18.
//

public protocol NewType {
associatedtype Value
var value: Value { get }
init(value: Value)
}

extension NewType {
public init(_ value: Value) {
self.init(value: value)
}
}

extension NewType where Value: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Value.IntegerLiteralType) {
self.init(value: Value(integerLiteral: value))
}
}

extension NewType where Value: ExpressibleByFloatLiteral {
public init(floatLiteral value: Value.FloatLiteralType) {
self.init(value: Value(floatLiteral: value))
}
}

extension NewType where
Value: ExpressibleByArrayLiteral,
Value: RangeReplaceableCollection,
Value.ArrayLiteralElement == Value.Element
{
public init(arrayLiteral values: Value.ArrayLiteralElement...) {
self.init(value: Value(values))
}
}

extension NewType where Value: Numeric {

public var magnitude: Value.Magnitude {
return value.magnitude
}

public static func + (lhs: Self, rhs: Self) -> Self {
return Self(value: lhs.value + rhs.value)
}

public static func += (lhs: inout Self, rhs: Self) {
lhs = lhs + rhs
}

public static func - (lhs: Self, rhs: Self) -> Self {
return Self(value: lhs.value - rhs.value)
}

public static func -= (lhs: inout Self, rhs: Self) {
lhs = lhs - rhs
}

public static func * (lhs: Self, rhs: Self) -> Self {
return Self(value: lhs.value * rhs.value)
}

public static func *= (lhs: inout Self, rhs: Self) {
lhs = lhs * rhs
}

public init?<T>(exactly source: T) where T: BinaryInteger {
guard let value = Value(exactly: source) else { return nil }
self.init(value: value)
}
}

extension NewType where Value: SignedNumeric { }

extension NewType where Value: Sequence {
public func makeIterator() -> Value.Iterator {
return value.makeIterator()
}
}

extension NewType where Value: Collection {

public typealias Iterator = Value.Iterator

/// Start index.
public var startIndex: Value.Index {
return value.startIndex
}

/// End index.
public var endIndex: Value.Index {
return value.endIndex
}

/// Index after given index `i`.
public func index(after i: Value.Index) -> Value.Index {
return value.index(after: i)
}

public var indices: Value.Indices {
return value.indices
}

/// - returns: Element at the given `index`.
public subscript (index: Value.Index) -> Value.Element {
return value[index]
}
}
22 changes: 9 additions & 13 deletions Sources/DataStructures/Stack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ public struct Stack <Element> {

/// - returns: The `top` and the remaining items, if possible. Otherwise, `nil`.
public var destructured: (Element, Stack<Element>)? {

guard self.count > 0 else {
return nil
}

guard self.count > 0 else { return nil }
var copy = self
let top = copy.pop()!
return (top, copy)
Expand Down Expand Up @@ -101,6 +97,14 @@ extension Stack: Collection {
public subscript (index: Int) -> Element {
return elements[index]
}

/// Count of elements contained herein.
///
/// - Complexity: O(_1_)
///
public var count: Int {
return elements.count
}
}

extension Stack: BidirectionalCollection {
Expand All @@ -110,14 +114,6 @@ extension Stack: BidirectionalCollection {
assert(index > 0, "Cannot decrement index to \(index - 1)")
return index - 1
}

/// Count of elements contained herein.
///
/// - Complexity: O(_1_)
///
public var count: Int {
return elements.count
}
}

extension Stack: Equatable where Element: Equatable {
Expand Down
49 changes: 49 additions & 0 deletions Tests/DataStructuresTests/NewTypeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// NewTypeTests.swift
// DataStructuresTests
//
// Created by James Bean on 8/20/18.
//

import XCTest
import DataStructures

class NewTypeTests: XCTestCase {

func testExpressibleByIntegerLiteral() {
struct I: NewType, ExpressibleByIntegerLiteral { let value: Int }
let _: I = 42
}

func testExpressibleByFloatLiteral() {
struct F: NewType, ExpressibleByFloatLiteral { let value: Double }
let _: F = 42.0
}

func testNumeric() {
struct N: NewType, Numeric { let value: Double }
let a: N = 42
let b: N = 9000
let _ = a + b
let _ = a - b
let _ = a * b
}

func testSignedNumeric() {
struct SN: NewType, SignedNumeric { let value: Int }
let n: SN = 1
let _ = -n
}

func testSequence() {
struct S: NewType, Sequence { let value: [Int] }
let s = S([1,2,3])
let _ = s.map { $0 }
}

func testCollection() {
struct C: NewType, Collection { let value: [Int] }
let c = C([1,2,3])
let _ = c.count
}
}

0 comments on commit ce548fb

Please sign in to comment.