Skip to content

Commit

Permalink
Merge pull request #86 from dn-m/swift-4.1
Browse files Browse the repository at this point in the history
Update for Swift 4.1
  • Loading branch information
jsbean authored May 17, 2018
2 parents 54a91b8 + a109a37 commit d281c82
Show file tree
Hide file tree
Showing 19 changed files with 169 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ os:
language: generic
sudo: required
dist: trusty
osx_image: xcode9
osx_image: xcode9.3
script:
- swift build
- swift test
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

[![Build Status](https://travis-ci.org/dn-m/Structure.svg?branch=master)](https://travis-ci.org/dn-m/Structure)

Algabraic and data structures in Swift 4.0.
Algebraic and data structures in Swift 4.1.

## Algebra
Protocols for representing Algabraic structures (`Semigroup`, `Monoid`), and their operations.
Protocols for representing Algebraic structures (`Semigroup`, `Monoid`), and their operations.

## Destructure
Break a `Collection` into a head and tail for functional-style recursive implementations of algorithms.
Expand Down
2 changes: 1 addition & 1 deletion Sources/DataStructures/CircularArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ extension CircularArray: RangeReplaceableCollection {
}
}

extension CircularArray where Element: Equatable {
extension CircularArray: Equatable where Element: Equatable {

/// - Returns: `true` if the elements contained both `CircularArray` values are equivalent.
/// Otherwise, `false`.
Expand Down
30 changes: 23 additions & 7 deletions Sources/DataStructures/LinkedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@
//

/// The `LinkedList`.
public enum LinkedList <T> {
public enum LinkedList <Element> {

/// Last node in list.
case end

/// Node with pointer to next node.
indirect case node(T, next: LinkedList<T>)
indirect case node(Element, next: LinkedList<Element>)
}

extension LinkedList {

/// Construct a new list with the given `value` held by a node at the front.
func cons(value: T) -> LinkedList {
func cons(value: Element) -> LinkedList {
return .node(value, next: self)
}
}

extension LinkedList {

/// Push a node holding the given `value` onto the front of the list.
public mutating func push(x: T) {
public mutating func push(x: Element) {
self = self.cons(value: x)
}

/// - returns: The element contained by the node at the front of the list, if the
/// list is not empty. Otherwise, `nil`.
public mutating func pop() -> T? {
public mutating func pop() -> Element? {

switch self {
case .end:
Expand Down Expand Up @@ -72,7 +72,7 @@ extension LinkedList: Collection {
}

/// - returns: Element at the given `index`.
public subscript(position: Int) -> T {
public subscript(position: Int) -> Element {

switch (self, position) {
case (.end, _):
Expand All @@ -85,12 +85,28 @@ extension LinkedList: Collection {
}
}

extension LinkedList: Equatable where Element: Equatable {

/// - returns: `true` if two `LinkedList` values are equivalent. Otherwise `false`.
public static func == (lhs: LinkedList, rhs: LinkedList) -> Bool {
switch (lhs, rhs) {
case (.end, .end):
return true
case let (.node(elementA, nextA), .node(elementB, nextB)):
return elementA == elementB && nextA == nextB
default:
return false
}
}

}

extension LinkedList: ExpressibleByArrayLiteral {

// - MARK: `ExpressibleByArrayLiteral`

/// Create a `LinkedList` with an array literal.
public init(arrayLiteral elements: T...) {
public init(arrayLiteral elements: Element...) {
self = elements
.lazy
.reversed()
Expand Down
34 changes: 17 additions & 17 deletions Sources/DataStructures/Matrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@
//
//

/// 2-dimensional matrix with user-definable dimensions, parameterized over any type `T`.
public struct Matrix <T> {
/// 2-dimensional matrix with user-definable dimensions, parameterized over any type `Element`.
public struct Matrix <Element> {

/// Amount of rows.
fileprivate let rowCount: Int
private let rowCount: Int

/// Amount of columns.
fileprivate let columnCount: Int
private let columnCount: Int

/// Items of type `T` stored as `[row, row, row, ...]`
fileprivate var grid: [T] = []
/// Items of type `Element` stored as `[row, row, row, ...]`
private var grid: [Element] = []

// MARK: - Initializers

/// - returns: Array of rows.
public var rows: [[T]] {
public var rows: [[Element]] {
return (0 ..< rowCount).map { self[row: $0] }
}

/// - returns: Array of columns.
public var columns: [[T]] {
public var columns: [[Element]] {
return (0 ..< columnCount).map { self[column: $0] }
}

/// Create a `Matrix` with the given dimensions and given `defaultValue`.
public init(height rowCount: Int, width columnCount: Int, initial: T) {
public init(height rowCount: Int, width columnCount: Int, initial: Element) {
self.rowCount = rowCount
self.columnCount = columnCount
self.grid = Array(repeating: initial, count: Int(rowCount * columnCount))
Expand All @@ -41,7 +41,7 @@ public struct Matrix <T> {

/// Get and set the value for the given `row` and `column`, if these are valid indices.
/// Otherwise, `nil` is returned or nothing is set.
public subscript (row: Int, column: Int) -> T {
public subscript (row: Int, column: Int) -> Element {

get {

Expand All @@ -63,7 +63,7 @@ public struct Matrix <T> {
}

/// Get and set an row of values.
public subscript (row row: Int) -> [T] {
public subscript (row row: Int) -> [Element] {

get {
let startIndex = row * columnCount
Expand All @@ -79,7 +79,7 @@ public struct Matrix <T> {
}

/// Get and set a column of values.
public subscript (column column: Int) -> [T] {
public subscript (column column: Int) -> [Element] {

get {
return (0 ..< rowCount).map { index in grid[index * columnCount + column] }
Expand Down Expand Up @@ -129,12 +129,12 @@ extension Matrix: Collection {
}

/// - returns: Element at the given `index`.
public subscript (index: Int) -> T {
public subscript (index: Int) -> Element {
return grid[index]
}
}

extension Matrix where T: Equatable {
extension Matrix: Equatable where Element: Equatable {

/// - returns: `true` if all values of both matrices are equivalent. Otherwise, `false`.
public static func == (lhs: Matrix, rhs: Matrix) -> Bool {
Expand All @@ -156,16 +156,16 @@ extension Matrix: CustomStringConvertible {
/// - warning: Assumes primitive type with no fancier `CustomStringConvertible`
/// implementation.
func width(_ value: Any) -> Int {
return "\(value)".characters.count
return "\(value)".count
}

/// - warning: Don't use `\t`, though. Doesn't register correctly.
func format <T> (_ row: [T]) -> String {
func format <Element> (_ row: [Element]) -> String {

let separator = " "

let columnWidth = columns
.flatMap { $0.flatMap(width) }
.flatMap { $0.compactMap(width) }
.max() ?? 0

return row
Expand Down
59 changes: 34 additions & 25 deletions Sources/DataStructures/OrderedDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ extension OrderedDictionary: Collection {
}
}

extension OrderedDictionary: Equatable where Value: Equatable {

/// - returns: `true` if all values contained in both `OrderedDictionary` values are
/// equivalent. Otherwise, `false`.
public static func == (lhs: OrderedDictionary, rhs: OrderedDictionary) -> Bool {

guard lhs.keys == rhs.keys else {
return false
}

for key in lhs.keys {

if rhs.values[key] == nil || rhs.values[key]! != lhs.values[key]! {
return false
}
}

for key in rhs.keys {

if lhs.values[key] == nil || lhs.values[key]! != rhs.values[key]! {
return false
}
}

return true
}
}

extension OrderedDictionary: ExpressibleByDictionaryLiteral {

// MARK: - `ExpressibleByDictionaryLiteral`
Expand All @@ -126,29 +154,10 @@ extension OrderedDictionary: ExpressibleByDictionaryLiteral {
}
}

/// - returns: `true` if all values contained in both `OrderedDictionary` values are
/// equivalent. Otherwise, `false`.
public func == <K, V: Equatable> (lhs: OrderedDictionary<K,V>, rhs: OrderedDictionary<K,V>)
-> Bool
{

guard lhs.keys == rhs.keys else {
return false
}

for key in lhs.keys {

if rhs.values[key] == nil || rhs.values[key]! != lhs.values[key]! {
return false
}
}

for key in rhs.keys {

if lhs.values[key] == nil || lhs.values[key]! != rhs.values[key]! {
return false
}
}

return true
}
//public func == <K, V: Equatable> (lhs: OrderedDictionary<K,V>, rhs: OrderedDictionary<K,V>)
// -> Bool
//{
//
//
//}
9 changes: 9 additions & 0 deletions Sources/DataStructures/SortedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public struct SortedArray <Element: Comparable>:
SortedCollectionWrapping
{

/// Underlying storage of elements contained herein.
public var base: [Element] = []

// MARK: - Initializers
Expand Down Expand Up @@ -78,6 +79,14 @@ public struct SortedArray <Element: Comparable>:
}
}

extension SortedArray {

/// - Returns: The slice of the `SortedArray` for the given `bounds`.
public subscript(bounds: Range<Base.Index>) -> Slice<Base> {
return Slice(base: base, bounds: bounds)
}
}

extension SortedArray: Equatable {

// MARK: - Equatable
Expand Down
2 changes: 1 addition & 1 deletion Sources/DataStructures/SortedDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct SortedDictionary<Key, Value>: DictionaryProtocol where Key: Hashab
// MARK: - Instance Properties

/// Values contained herein, in order sorted by their associated keys.
public var values: LazyMapRandomAccessCollection<SortedArray<Key>,Value> {
public var values: LazyMapCollection<SortedArray<Key>,Value> {
return keys.lazy.map { self.unsorted[$0]! }
}

Expand Down
13 changes: 8 additions & 5 deletions Sources/DataStructures/Stack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ extension Stack: BidirectionalCollection {
}
}

extension Stack: Equatable where Element: Equatable {

/// - returns: `true` if two `Stack` values are equivalent. Otherwise `false`.
public static func == (lhs: Stack, rhs: Stack) -> Bool {
return lhs.elements == rhs.elements
}
}

extension Stack: ExpressibleByArrayLiteral {

// MARK: - `ExpressibleByArrayLiteral`.
Expand Down Expand Up @@ -159,8 +167,3 @@ extension Stack: Monoid {
return lhs + rhs
}
}

/// - returns: `true` if all items in both `Stack` structs are equivalent. Otherwise `false`.
public func == <T: Equatable> (lhs: Stack<T>, rhs: Stack<T>) -> Bool {
return lhs.elements == rhs.elements
}
4 changes: 2 additions & 2 deletions Sources/Restructure/Rotate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ extension MutableCollection where Self: BidirectionalCollection {

/// - Returns: A mutable and bidirectional collection with its elements rotated by the given
/// `amount`.
public func rotated(by amount: IndexDistance) -> Self {
public func rotated(by amount: Int) -> Self {
var copy = self
copy.rotate(by: amount)
return copy
}

/// Rotates the elements contained herein by the given `amount`.
public mutating func rotate(by amount: IndexDistance) {
public mutating func rotate(by amount: Int) {
guard amount != 0 else { return }
let amount = (amount < 0 ? count + amount : amount) % count
let amountIndex = index(startIndex, offsetBy: amount)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Restructure/StableSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//

extension RangeReplaceableCollection where Index == Int, IndexDistance == Int {
extension RangeReplaceableCollection where Index == Int {

public func stableSort(_ isOrderedBefore: @escaping (Element, Element) -> Bool) -> [Element] {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension RandomAccessCollectionWrapping {
///
/// - Complexity: O(1)
///
public var count: Base.IndexDistance {
public var count: Int {
return base.count
}

Expand Down
Loading

0 comments on commit d281c82

Please sign in to comment.