-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f66ac1
commit e6574e8
Showing
21 changed files
with
3,843 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+18.4 KB
(200%)
...project.xcworkspace/xcuserdata/albertoirurueta.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
2 changes: 1 addition & 1 deletion
2
...tProtocolsAndGenerics.xcodeproj/xcshareddata/xcschemes/swiftProtocolsAndGenerics.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
swiftProtocolsAndGenerics/swiftProtocolsAndGenerics/Comparator.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Comparator.swift | ||
// swiftProtocolsAndGenerics | ||
// | ||
// Created by Alberto Irurueta Carro on 14/12/16. | ||
// Copyright © 2016 Alberto Irurueta Carro. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Protocolo para permitir comparar objetos de tipo T. | ||
*/ | ||
public protocol Comparator { | ||
|
||
/** | ||
Compara dos objetos de tipo T. | ||
- parameter o1: primer objeto a comparar. | ||
- parameter o2: segundo objeto a comparar. | ||
- returns: -1 si o1 es menor que o2, 0 si o1 y o2 son iguales o 1 si o1 | ||
es mayor que o2. | ||
*/ | ||
func compare<T>(_ o1: T, _ o2: T) -> Int | ||
} |
23 changes: 23 additions & 0 deletions
23
swiftProtocolsAndGenerics/swiftProtocolsAndGenerics/ComparatorAndAverager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// ComparatorAndAverager.swift | ||
// swiftProtocolsAndGenerics | ||
// | ||
// Created by Alberto Irurueta Carro on 15/12/16. | ||
// Copyright © 2016 Alberto Irurueta Carro. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Permite comparar objetos y obtener el promedio entre dos objetos de tipo T. | ||
Este protocolo se utiliza para obtener la mediana en array de longitud par. | ||
*/ | ||
public protocol ComparatorAndAverager : Comparator { | ||
/** | ||
Calcula el promedio de los dos valores proporcionados. | ||
- parameter o1: primer valor. | ||
- parameter o2: segundo valor. | ||
- returns: promedio de los dos valores proporcionados. | ||
*/ | ||
func average<T>(_ o1: T, _ o2: T) -> T | ||
} |
39 changes: 39 additions & 0 deletions
39
swiftProtocolsAndGenerics/swiftProtocolsAndGenerics/Container.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// Container.swift | ||
// swiftProtocolsAndGenerics | ||
// | ||
// Created by Alberto Irurueta Carro on 15/12/16. | ||
// Copyright © 2016 Alberto Irurueta Carro. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Contenedor de un objeto cualquiera de tipo T. | ||
*/ | ||
public class Container<T> { | ||
/** | ||
Valor interno almacenado por esta clase. | ||
*/ | ||
private var _value: T | ||
|
||
/** | ||
Constructor. | ||
- parameter value: valor inicial proporcionado. | ||
*/ | ||
public init(value: T) { | ||
_value = value | ||
} | ||
|
||
/** | ||
Obtiene o establece el valor interno de esta clase. | ||
*/ | ||
public var value: T { | ||
get{ | ||
return _value | ||
} | ||
set { | ||
_value = newValue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
swiftProtocolsAndGenerics/swiftProtocolsAndGenerics/FoundationSorter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// | ||
// FoundationSorter.swift | ||
// swiftProtocolsAndGenerics | ||
// | ||
// Created by Alberto Irurueta Carro on 15/12/16. | ||
// Copyright © 2016 Alberto Irurueta Carro. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/** | ||
Ordena array utilizando el algoritmo proporcionado por la Foundation de Swift. | ||
*/ | ||
public class FoundationSorter : Sorter { | ||
/** | ||
Constructor. | ||
*/ | ||
public override init() { | ||
super.init() | ||
} | ||
|
||
/** | ||
Devuelve el método utilizado para ordenar. | ||
Esta propiedad debe sobreescribirse en las correspondientes subclases. | ||
*/ | ||
public override var method: SortingMethod { | ||
get{ | ||
return SortingMethod.foundation | ||
} | ||
} | ||
|
||
/** | ||
Ordena el array proporcionado de forma ascendente de modo que: | ||
array[i - 1] < array[i] para cualquier i válido. | ||
Este método modifica el array proporcionado de modo que tras la ejecución | ||
los elementos del array quedan ordenados. | ||
Esta implementación ordena todo el array de forma completa, por lo | ||
que los parámetros fromIndex y toIndex se ignoran. | ||
- parameter array: array a ordenar. Tras la ejecución de este método los | ||
elementos entre la sposiciones fomIndex (incluído) y toIndex (excluído) | ||
se modifican de modo que quedan en orden ascendente. | ||
- parameter fromIndex: posición donde se inicia la ordenación (se ignora). | ||
- parameter toIndex: posición donde finaliza la ordenación (se ignora). | ||
- parameter comparator: determina si un elemento es mayor o menor que otro. | ||
Devuelve -1 si o1 es menor que o2, 0 si o1 es igual a o2 y 1 si o1 es mayor | ||
que o2. | ||
- parameter o1: primer elemento a comparar | ||
- parameter o2: segundo elemento a comparar | ||
*/ | ||
public override func sort<T>(_ array: inout [T], fromIndex: Int, | ||
toIndex: Int, | ||
comparator: (_ o1: T, _ o2: T) -> Int) { | ||
|
||
array.sort { (o1, o2) -> Bool in | ||
FoundationSorter.compare(o1, o2, Int.self) | ||
} | ||
} | ||
|
||
/** | ||
Hack para comparar dos objetos cualesquiera sólo si son comparables. | ||
- returns: true si o1 es mayor que o2, false en caso contrario o si los | ||
objetos no son Comparables. | ||
*/ | ||
private static func compare<T: Comparable>(_ o1: Any, _ o2: Any, | ||
_ type: T.Type) -> Bool{ | ||
if let c1 = o1 as? T, let c2 = o2 as? T { | ||
return c1 < c2 | ||
} else { | ||
return false | ||
} | ||
} | ||
} |
Oops, something went wrong.