on d5bbe91 help sign in
2 files changed+6−70
since splitsnapshots
unifiedsplit
Galmin/Printer.swift+6−49 README.md−21
Galmin/Printer.swift+6−49
@@ -8,53 +8,6 @@
88
99import Foundation
1010
11−#if os(Linux)
12− import Glibc
13−
14− // Use fopen/fwrite to output string
15− func writeStringToFile(string: String, path: String) -> Bool {
16− let fp = fopen(path, "w"); defer { fclose(fp) }
17− let byteArray = Array(string.utf8)
18− let count = fwrite(byteArray, 1, byteArray.count, fp)
19− return count == string.utf8.count
20− }
21−
22− // Use fread to input string
23− func readStringFromFile(path: String) -> String {
24− let fp = fopen(path, "r"); defer { fclose(fp) }
25− var outputString = ""
26− let chunkSize = 1024
27− let buffer: UnsafeMutablePointer<CChar> = UnsafeMutablePointer.allocate(capacity: chunkSize)
28− defer { buffer.deallocate(capacity: chunkSize) }
29− repeat {
30− let count: Int = fread(buffer, 1, chunkSize, fp)
31− guard ferror(fp) == 0 else { break }
32− if count > 0 {
33− let ptr = unsafeBitCast(buffer, to: UnsafePointer<CChar>.self)
34− if let newString = String(validatingUTF8: ptr) {
35− outputString += newString
36− }
37− }
38− } while feof(fp) == 0
39−
40− return outputString
41− }
42−
43− extension String {
44− func write(with name: String) throws {
45− if !writeStringToFile(string: self, path: "diskraDz3/\(name)") {
46− print("Couldn't write files ¯\\_(ツ)_/¯")
47− }
48− }
49− }
50−#else
51− private extension String {
52− func write(with name: String) throws {
53− try self.write(toFile: "diskraDz3/\(name)", atomically: true, encoding: .utf8)
54− }
55− }
56−#endif
57−
5811private extension String {
5912 var sub: String {
6013 return self.map { c in
@@ -159,7 +112,7 @@ class Printer<Vertex> where Vertex: Hashable & Comparable {
159112
160113 func processInitialMinimization(minimization: Graph<Vertex>.Minimization, counter: Int) -> () {
161114 do {
162− try FileManager.default.createDirectory(atPath: "diskraDz3", withIntermediateDirectories: false)
115+ try FileManager.default.createDirectory(atPath: "diskraDz3", withIntermediateDirectories: true)
163116 FileManager.default.createFile(atPath: "diskraDz3/solutions.txt", contents: nil)
164117 solutions = FileHandle(forWritingAtPath: "diskraDz3/solutions.txt")
165118 try table(for: minimization.before, with: minimization.reductions, prefix: "", coef: false).write(with: "\(counter)_1.csv")
@@ -253,4 +206,8 @@ extension Tree {
253206 }
254207}
255208
256−
209+private extension String {
210+ func write(with name: String) throws {
211+ try self.write(toFile: "diskraDz3/\(name)", atomically: true, encoding: .utf8)
212+ }
213+}
README.mddeleted−21
@@ -1,21 +0,0 @@
1−# Galmin
2−
3−Galmin is a command line application for generating the full solution to the problem of the traveling salesman between six give fully connected points.
4−
5−## Input
6−
7−On launch you will be asked to enter the coordinates of six points for which the application should generate the solution.
8−
9−The weight of the edge between two given points is then calculated using the following formula: `|AB| = |Xa - Xb| + |Ya - Yb|`.
10−
11−## Output
12−
13−Out files are created in the directory `diskraDz3`. Output consists of six `.csv` files and one `.gv` file for every step of the solution and one `solution.txt` containing the actual solution to the problem.
14−
15−The `.gv` file is is a [GraphViz](http://www.graphviz.org/) file. Displays correctly only when exporting as `.svg`.
16−
17−## Support
18−
19−Swift 4.
20−
21−Is intended to work as a mac command line application. Linux support is rudimentary. Doesn't work with embedded standard library (requires Swift standard library to be installed).