Skip to content

Commit

Permalink
Added generic sorters and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albertoirurueta committed Dec 17, 2016
1 parent 2f66ac1 commit e6574e8
Show file tree
Hide file tree
Showing 21 changed files with 3,843 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@
objects = {

/* Begin PBXBuildFile section */
28000BE71E028F1F002D97E0 /* Container.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BE61E028F1F002D97E0 /* Container.swift */; };
28000BE91E02B9F6002D97E0 /* StraightInsertionSorter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BE81E02B9F6002D97E0 /* StraightInsertionSorter.swift */; };
28000BEB1E02BC2C002D97E0 /* ShellSorter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BEA1E02BC2C002D97E0 /* ShellSorter.swift */; };
28000BED1E02BFE8002D97E0 /* QuicksortSorter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BEC1E02BFE8002D97E0 /* QuicksortSorter.swift */; };
28000BEF1E02C85F002D97E0 /* HeapsortSorter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BEE1E02C85F002D97E0 /* HeapsortSorter.swift */; };
28000BF11E02CC4B002D97E0 /* FoundationSorter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BF01E02CC4B002D97E0 /* FoundationSorter.swift */; };
28000BF31E03162B002D97E0 /* ContainerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BF21E03162B002D97E0 /* ContainerTests.swift */; };
28000BF51E034521002D97E0 /* SorterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BF41E034521002D97E0 /* SorterTests.swift */; };
28000BF71E0468F4002D97E0 /* StraightInsertionSorterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BF61E0468F4002D97E0 /* StraightInsertionSorterTests.swift */; };
28000BF91E048E83002D97E0 /* ShellSorterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BF81E048E83002D97E0 /* ShellSorterTests.swift */; };
28000BFB1E0490DA002D97E0 /* QuicksortSorterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BFA1E0490DA002D97E0 /* QuicksortSorterTests.swift */; };
28000BFD1E0498FC002D97E0 /* HeapsortSorterTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BFC1E0498FC002D97E0 /* HeapsortSorterTest.swift */; };
28000BFF1E057D45002D97E0 /* FoundationSorterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28000BFE1E057D45002D97E0 /* FoundationSorterTests.swift */; };
283291951E01FC8A0015A5CB /* Sorter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 283291941E01FC8A0015A5CB /* Sorter.swift */; };
283291971E020FA70015A5CB /* ComparatorAndAverager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 283291961E020FA70015A5CB /* ComparatorAndAverager.swift */; };
283291991E02109F0015A5CB /* SortingMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 283291981E02109F0015A5CB /* SortingMethod.swift */; };
2832919B1E02114E0015A5CB /* Comparator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2832919A1E02114E0015A5CB /* Comparator.swift */; };
286E79251DE6E6420045F165 /* swiftProtocolsAndGenerics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 286E791B1DE6E6420045F165 /* swiftProtocolsAndGenerics.framework */; };
286E792C1DE6E6420045F165 /* swiftProtocolsAndGenerics.h in Headers */ = {isa = PBXBuildFile; fileRef = 286E791E1DE6E6420045F165 /* swiftProtocolsAndGenerics.h */; settings = {ATTRIBUTES = (Public, ); }; };
286E79361DE6EAA40045F165 /* EditDistance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 286E79351DE6EAA40045F165 /* EditDistance.swift */; };
Expand All @@ -24,6 +41,23 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
28000BE61E028F1F002D97E0 /* Container.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Container.swift; sourceTree = "<group>"; };
28000BE81E02B9F6002D97E0 /* StraightInsertionSorter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StraightInsertionSorter.swift; sourceTree = "<group>"; };
28000BEA1E02BC2C002D97E0 /* ShellSorter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShellSorter.swift; sourceTree = "<group>"; };
28000BEC1E02BFE8002D97E0 /* QuicksortSorter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QuicksortSorter.swift; sourceTree = "<group>"; };
28000BEE1E02C85F002D97E0 /* HeapsortSorter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HeapsortSorter.swift; sourceTree = "<group>"; };
28000BF01E02CC4B002D97E0 /* FoundationSorter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationSorter.swift; sourceTree = "<group>"; };
28000BF21E03162B002D97E0 /* ContainerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContainerTests.swift; sourceTree = "<group>"; };
28000BF41E034521002D97E0 /* SorterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SorterTests.swift; sourceTree = "<group>"; };
28000BF61E0468F4002D97E0 /* StraightInsertionSorterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StraightInsertionSorterTests.swift; sourceTree = "<group>"; };
28000BF81E048E83002D97E0 /* ShellSorterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShellSorterTests.swift; sourceTree = "<group>"; };
28000BFA1E0490DA002D97E0 /* QuicksortSorterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QuicksortSorterTests.swift; sourceTree = "<group>"; };
28000BFC1E0498FC002D97E0 /* HeapsortSorterTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HeapsortSorterTest.swift; sourceTree = "<group>"; };
28000BFE1E057D45002D97E0 /* FoundationSorterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationSorterTests.swift; sourceTree = "<group>"; };
283291941E01FC8A0015A5CB /* Sorter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Sorter.swift; sourceTree = "<group>"; };
283291961E020FA70015A5CB /* ComparatorAndAverager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ComparatorAndAverager.swift; sourceTree = "<group>"; };
283291981E02109F0015A5CB /* SortingMethod.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SortingMethod.swift; sourceTree = "<group>"; };
2832919A1E02114E0015A5CB /* Comparator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Comparator.swift; sourceTree = "<group>"; };
286E791B1DE6E6420045F165 /* swiftProtocolsAndGenerics.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = swiftProtocolsAndGenerics.framework; sourceTree = BUILT_PRODUCTS_DIR; };
286E791E1DE6E6420045F165 /* swiftProtocolsAndGenerics.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = swiftProtocolsAndGenerics.h; sourceTree = "<group>"; };
286E791F1DE6E6420045F165 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -76,6 +110,16 @@
286E791E1DE6E6420045F165 /* swiftProtocolsAndGenerics.h */,
286E791F1DE6E6420045F165 /* Info.plist */,
286E79351DE6EAA40045F165 /* EditDistance.swift */,
28000BE61E028F1F002D97E0 /* Container.swift */,
2832919A1E02114E0015A5CB /* Comparator.swift */,
283291961E020FA70015A5CB /* ComparatorAndAverager.swift */,
283291981E02109F0015A5CB /* SortingMethod.swift */,
283291941E01FC8A0015A5CB /* Sorter.swift */,
28000BE81E02B9F6002D97E0 /* StraightInsertionSorter.swift */,
28000BEA1E02BC2C002D97E0 /* ShellSorter.swift */,
28000BEC1E02BFE8002D97E0 /* QuicksortSorter.swift */,
28000BEE1E02C85F002D97E0 /* HeapsortSorter.swift */,
28000BF01E02CC4B002D97E0 /* FoundationSorter.swift */,
);
path = swiftProtocolsAndGenerics;
sourceTree = "<group>";
Expand All @@ -85,6 +129,13 @@
children = (
286E792B1DE6E6420045F165 /* Info.plist */,
286E79371DE705220045F165 /* EditDistanceTests.swift */,
28000BF21E03162B002D97E0 /* ContainerTests.swift */,
28000BF41E034521002D97E0 /* SorterTests.swift */,
28000BF61E0468F4002D97E0 /* StraightInsertionSorterTests.swift */,
28000BF81E048E83002D97E0 /* ShellSorterTests.swift */,
28000BFA1E0490DA002D97E0 /* QuicksortSorterTests.swift */,
28000BFC1E0498FC002D97E0 /* HeapsortSorterTest.swift */,
28000BFE1E057D45002D97E0 /* FoundationSorterTests.swift */,
);
path = swiftProtocolsAndGenericsTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -146,7 +197,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0810;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = "Alberto Irurueta Carro";
TargetAttributes = {
286E791A1DE6E6420045F165 = {
Expand Down Expand Up @@ -202,15 +253,32 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
283291971E020FA70015A5CB /* ComparatorAndAverager.swift in Sources */,
28000BE71E028F1F002D97E0 /* Container.swift in Sources */,
283291951E01FC8A0015A5CB /* Sorter.swift in Sources */,
28000BED1E02BFE8002D97E0 /* QuicksortSorter.swift in Sources */,
28000BF11E02CC4B002D97E0 /* FoundationSorter.swift in Sources */,
28000BEF1E02C85F002D97E0 /* HeapsortSorter.swift in Sources */,
28000BEB1E02BC2C002D97E0 /* ShellSorter.swift in Sources */,
286E79361DE6EAA40045F165 /* EditDistance.swift in Sources */,
28000BE91E02B9F6002D97E0 /* StraightInsertionSorter.swift in Sources */,
283291991E02109F0015A5CB /* SortingMethod.swift in Sources */,
2832919B1E02114E0015A5CB /* Comparator.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
286E79201DE6E6420045F165 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
28000BFB1E0490DA002D97E0 /* QuicksortSorterTests.swift in Sources */,
28000BF71E0468F4002D97E0 /* StraightInsertionSorterTests.swift in Sources */,
28000BF51E034521002D97E0 /* SorterTests.swift in Sources */,
28000BFD1E0498FC002D97E0 /* HeapsortSorterTest.swift in Sources */,
28000BF91E048E83002D97E0 /* ShellSorterTests.swift in Sources */,
286E79381DE705220045F165 /* EditDistanceTests.swift in Sources */,
28000BF31E03162B002D97E0 /* ContainerTests.swift in Sources */,
28000BFF1E057D45002D97E0 /* FoundationSorterTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -243,6 +311,7 @@
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_SUSPICIOUS_MOVES = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -296,6 +365,7 @@
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_SUSPICIOUS_MOVES = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0810"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
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
}
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
}
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class EditDistance {

/**
Calcula la distance de Levenshtein entre dos Strings.
- param x: el primer String.
- param y: el segundo String.
- parameter x: el primer String.
- parameter y: el segundo String.
- returns: la distancia de Levenshtein.
*/
public static func distance(x: String, y: String) -> Int {
Expand All @@ -35,8 +35,8 @@ public class EditDistance {
EditDistanceMeasurable.
Se puede utilizar este método con cualquier tipo de datos que implemente
el protocolo EditDistanceMeasurable.
- param x: el primer EditDistanceMeasurable.
- param y: el segundo EditDistanceMeasurable.
- parameter x: el primer EditDistanceMeasurable.
- parameter y: el segundo EditDistanceMeasurable.
- returns: la distancia de Levenshtein.
*/
internal static func internalDistance(x: EditDistanceMeasurable,
Expand Down Expand Up @@ -110,9 +110,9 @@ internal protocol EditDistanceMeasurable {
/**
Compara este EditDistanceMeasurable con otro proporcionado en las
posiciones indicadas.
- param other: otro string a comparar con este.
- param posX: posición en este EditDistanceMeasurable que debe compararse.
- param posY: posición en el otro EditDistanceMeasurable que debe
- parameter other: otro string a comparar con este.
- parameter posX: posición en este EditDistanceMeasurable que debe compararse.
- parameter posY: posición en el otro EditDistanceMeasurable que debe
compararse.
- returns: true si ambos EditDistanceMeasurable son iguales en las
posiciones indicadas.
Expand All @@ -136,9 +136,9 @@ extension String : EditDistanceMeasurable {
/**
Compara este MeasurableString con otro proporcionado en las posiciones
indicadas.
- param other: otro string a comparar con este.
- param posX: posición en este MeasurableString que debe compararse.
- param posY: posición en el otro MeasurableString que debe compararse.
- parameter other: otro string a comparar con este.
- parameter posX: posición en este MeasurableString que debe compararse.
- parameter posY: posición en el otro MeasurableString que debe compararse.
- returns: true si ambos MeasurableStrings son iguales en las posiciones
indicadas.
*/
Expand Down
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
}
}
}

0 comments on commit e6574e8

Please sign in to comment.