Created
June 17, 2016 06:44
-
-
Save sodastsai/a25a30c02516c4b89874585309f4e0b7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
struct VirtualPrinter: CustomStringConvertible { | |
var sortingIndex: Int | |
let name: String | |
init(name: String, sortingIndex: Int) { | |
self.name = name | |
self.sortingIndex = sortingIndex | |
} | |
var description: String { | |
return "\(self.name): \(self.sortingIndex)" | |
} | |
} | |
var virtualPrinters = [ | |
VirtualPrinter(name: "A", sortingIndex: 0), | |
VirtualPrinter(name: "B", sortingIndex: 1), | |
VirtualPrinter(name: "C", sortingIndex: 2), | |
VirtualPrinter(name: "D", sortingIndex: 3), | |
VirtualPrinter(name: "E", sortingIndex: 4), | |
] | |
func moveVirtualPrinter(from srcIndex: Int, | |
to destIndex: Int, | |
inVirtualPrinters printers: [VirtualPrinter]) -> [String: Int] { | |
if srcIndex == destIndex { | |
return [:] | |
} | |
var result = [printers[srcIndex].name: destIndex] | |
let indexesOfPrintersBetweenMovement: Range<Int>! | |
let deltaOfSortingIndex: Int! | |
if destIndex > srcIndex { // Down | |
indexesOfPrintersBetweenMovement = (srcIndex+1)..<(destIndex+1) | |
deltaOfSortingIndex = -1 | |
} else { // Up | |
indexesOfPrintersBetweenMovement = destIndex..<srcIndex | |
deltaOfSortingIndex = 1 | |
} | |
for printer in printers[indexesOfPrintersBetweenMovement] { | |
result[printer.name] = printer.sortingIndex + deltaOfSortingIndex | |
} | |
return result | |
} | |
moveVirtualPrinter(from: 2, to: 0, inVirtualPrinters: virtualPrinters) | |
moveVirtualPrinter(from: 1, to: 3, inVirtualPrinters: virtualPrinters) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment