Skip to content

Commit 744f91f

Browse files
committed
Add extensions for StringConvertible protocols
1 parent a7a463d commit 744f91f

File tree

8 files changed

+148
-138
lines changed

8 files changed

+148
-138
lines changed

Sources/Numerix/Core/Matrix.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,6 @@ extension Matrix: ExpressibleByArrayLiteral {
139139
}
140140
}
141141

142-
extension Matrix: CustomStringConvertible {
143-
144-
public var description: String {
145-
self.formatted()
146-
}
147-
}
148-
149-
extension Matrix: CustomDebugStringConvertible {
150-
151-
public var debugDescription: String {
152-
"""
153-
\(self.rows)x\(self.columns) \(type(of: self))
154-
\(self.description)
155-
"""
156-
}
157-
}
158-
159142
extension Matrix: Equatable {
160143

161144
/// Compare two matrices for equality.

Sources/Numerix/Core/ShapedArray.swift

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -86,107 +86,3 @@ extension ShapedArray: ExpressibleByArrayLiteral {
8686
self.init(elements)
8787
}
8888
}
89-
90-
extension ShapedArray: CustomStringConvertible where Scalar: NumberStyle {
91-
92-
public var description: String {
93-
94-
// Handle one-dimension array
95-
if shape.count == 1 || shape.count == 2 && shape[0] == 1 {
96-
var descr = "( "
97-
descr += self.buffer.map { "\($0)" }.joined(separator: " ")
98-
descr += " )"
99-
return descr
100-
}
101-
102-
// Chunk size is number of columns
103-
let chunkSize = shape.last ?? 1
104-
105-
let chunks = stride(from: 0, to: self.buffer.count, by: chunkSize).map {
106-
Array(self.buffer[$0..<min($0 + chunkSize, self.buffer.count)])
107-
}
108-
109-
// Width used for the printed elements is based on max character length
110-
let width = self.buffer.map { "\($0)".count }.max() ?? 1
111-
112-
// Get each line of the printed array, each line represents a row
113-
let lines = chunks.map { row in
114-
row.map { element in
115-
(String(repeating: " ", count: width) + "\(element)").suffix(width)
116-
}.joined(separator: " ")
117-
}
118-
119-
// Last rows for each sub-matrix
120-
var r = shape
121-
r.removeLast()
122-
r.reverse()
123-
let rows = getLastRows(r)
124-
125-
// Counter and variable to store description
126-
var n = 0
127-
var descr = ""
128-
129-
// Add a line to the description.
130-
func addToDescription(line: String) {
131-
132-
// Prepend symbol or space to the row
133-
for row in rows.reversed() {
134-
switch n % (row + 1) {
135-
case 0:
136-
if row == 1 { descr += "( " } else { descr += "" }
137-
case row - 1:
138-
descr += ""
139-
case row:
140-
descr += " "
141-
default:
142-
descr += ""
143-
}
144-
}
145-
146-
// Add line after prepended symbol
147-
descr += line
148-
149-
// Append symbol or space to the row
150-
for row in rows {
151-
switch n % (row + 1) {
152-
case 0:
153-
if row == 1 { descr += " )" } else { descr += "" }
154-
case row - 1:
155-
descr += ""
156-
case row:
157-
descr += " "
158-
default:
159-
descr += ""
160-
}
161-
}
162-
163-
// Line return except at end of description
164-
if n != rows.last! - 1 { descr += "\n" }
165-
166-
n += 1
167-
}
168-
169-
// Last row of the first sub-matrix
170-
let last = rows.first ?? 1
171-
172-
// Create each line for the description
173-
for line in lines {
174-
if n % (last + 1) == last {
175-
addToDescription(line: String(repeating: " ", count: (width + 2) * chunkSize - 2))
176-
}
177-
addToDescription(line: line)
178-
}
179-
180-
return descr
181-
}
182-
}
183-
184-
extension ShapedArray: CustomDebugStringConvertible where Scalar: NumberStyle {
185-
186-
public var debugDescription: String {
187-
"""
188-
\(self.shape.map { "\($0)" }.joined(separator: "x")) \(type(of: self))
189-
\(self.description)
190-
"""
191-
}
192-
}

Sources/Numerix/Core/Vector.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,6 @@ extension Vector: ExpressibleByArrayLiteral {
8686
}
8787
}
8888

89-
extension Vector: CustomStringConvertible {
90-
91-
public var description: String {
92-
self.formatted()
93-
}
94-
}
95-
96-
extension Vector: CustomDebugStringConvertible {
97-
98-
public var debugDescription: String {
99-
var descr = "\(self.size)-element \(type(of: self))\n"
100-
descr += self.formatted()
101-
return descr
102-
}
103-
}
104-
10589
extension Vector: Equatable {
10690

10791
/// Compare two vectors for equality.
File renamed without changes.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Matrix extensions to conform to CustomStringConvertible and CustomDebugStringConvertible protocols.
3+
*/
4+
5+
extension Matrix: CustomStringConvertible {
6+
7+
public var description: String {
8+
self.formatted()
9+
}
10+
}
11+
12+
extension Matrix: CustomDebugStringConvertible {
13+
14+
public var debugDescription: String {
15+
"""
16+
\(self.rows)x\(self.columns) \(type(of: self))
17+
\(self.description)
18+
"""
19+
}
20+
}

Sources/Numerix/Core/NumberStyle.swift renamed to Sources/Numerix/StringConvertible/NumberStyle.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
Number style protocol.
2+
NumberStyle protocol and extensions.
3+
This is used by ShapedArray StringConvertible extensions.
34
*/
45

56
@_documentation(visibility: private)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
ShapedArray extensions to conform to CustomStringConvertible and CustomDebugStringConvertible protocols.
3+
*/
4+
5+
extension ShapedArray: CustomStringConvertible where Scalar: NumberStyle {
6+
7+
public var description: String {
8+
9+
// Handle one-dimension array
10+
if shape.count == 1 || shape.count == 2 && shape[0] == 1 {
11+
var descr = "( "
12+
descr += self.buffer.map { "\($0)" }.joined(separator: " ")
13+
descr += " )"
14+
return descr
15+
}
16+
17+
// Chunk size is number of columns
18+
let chunkSize = shape.last ?? 1
19+
20+
let chunks = stride(from: 0, to: self.buffer.count, by: chunkSize).map {
21+
Array(self.buffer[$0..<min($0 + chunkSize, self.buffer.count)])
22+
}
23+
24+
// Width used for the printed elements is based on max character length
25+
let width = self.buffer.map { "\($0)".count }.max() ?? 1
26+
27+
// Get each line of the printed array, each line represents a row
28+
let lines = chunks.map { row in
29+
row.map { element in
30+
(String(repeating: " ", count: width) + "\(element)").suffix(width)
31+
}.joined(separator: " ")
32+
}
33+
34+
// Last rows for each sub-matrix
35+
var r = shape
36+
r.removeLast()
37+
r.reverse()
38+
let rows = getLastRows(r)
39+
40+
// Counter and variable to store description
41+
var n = 0
42+
var descr = ""
43+
44+
// Add a line to the description.
45+
func addToDescription(line: String) {
46+
47+
// Prepend symbol or space to the row
48+
for row in rows.reversed() {
49+
switch n % (row + 1) {
50+
case 0:
51+
if row == 1 { descr += "( " } else { descr += "" }
52+
case row - 1:
53+
descr += ""
54+
case row:
55+
descr += " "
56+
default:
57+
descr += ""
58+
}
59+
}
60+
61+
// Add line after prepended symbol
62+
descr += line
63+
64+
// Append symbol or space to the row
65+
for row in rows {
66+
switch n % (row + 1) {
67+
case 0:
68+
if row == 1 { descr += " )" } else { descr += "" }
69+
case row - 1:
70+
descr += ""
71+
case row:
72+
descr += " "
73+
default:
74+
descr += ""
75+
}
76+
}
77+
78+
// Line return except at end of description
79+
if n != rows.last! - 1 { descr += "\n" }
80+
81+
n += 1
82+
}
83+
84+
// Last row of the first sub-matrix
85+
let last = rows.first ?? 1
86+
87+
// Create each line for the description
88+
for line in lines {
89+
if n % (last + 1) == last {
90+
addToDescription(line: String(repeating: " ", count: (width + 2) * chunkSize - 2))
91+
}
92+
addToDescription(line: line)
93+
}
94+
95+
return descr
96+
}
97+
}
98+
99+
extension ShapedArray: CustomDebugStringConvertible where Scalar: NumberStyle {
100+
101+
public var debugDescription: String {
102+
"""
103+
\(self.shape.map { "\($0)" }.joined(separator: "x")) \(type(of: self))
104+
\(self.description)
105+
"""
106+
}
107+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Vector extensions to conform to CustomStringConvertible and CustomDebugStringConvertible protocols.
3+
*/
4+
5+
extension Vector: CustomStringConvertible {
6+
7+
public var description: String {
8+
self.formatted()
9+
}
10+
}
11+
12+
extension Vector: CustomDebugStringConvertible {
13+
14+
public var debugDescription: String {
15+
var descr = "\(self.size)-element \(type(of: self))\n"
16+
descr += self.formatted()
17+
return descr
18+
}
19+
}

0 commit comments

Comments
 (0)