-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathintmat.go
107 lines (89 loc) · 2.03 KB
/
intmat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package villa
import (
"fmt"
"github.com/golangplus/bytes"
)
/*
IntMatrix is 2D array of integers. Elements are store in a single int slice and slices of each row are created.
NOTE the matrix can be sized of 0x0, but never 0x10 or 10x0.
*/
type IntMatrix [][]int
// NewIntMatrix creates a new IntMatrix instance with specified number of rows and columns
func NewIntMatrix(nRow, nCol int) IntMatrix {
s := make([]int, nCol*nRow)
mat := make(IntMatrix, nRow)
for i, p := 0, 0; i < nRow; i++ {
mat[i] = s[p : p+nCol]
p += nCol
}
return mat
}
// Clone clones an IntMatrix
func (m IntMatrix) Clone() IntMatrix {
mat := NewIntMatrix(m.Rows(), m.Cols())
n := m.Rows() * m.Cols()
if n > 0 {
copy(mat[0][:n], m[0][:n])
}
return mat
}
// Cols returns the number of columns
func (m IntMatrix) Cols() int {
if len(m) == 0 {
return 0
}
return len(m[0])
}
// Rows returns the number of rows
func (m IntMatrix) Rows() int {
return len(m)
}
// PrettyString returns a pretty text form of the matrix.
// This function is mainly for debugging.
func (m IntMatrix) PrettyString() string {
sa := make([][]string, 0, m.Rows())
for _, row := range m {
sr := make([]string, 0, len(row))
for _, cell := range row {
sr = append(sr, fmt.Sprint(cell))
}
sa = append(sa, sr)
}
wds := make([]int, m.Cols())
for i := 0; i < m.Rows(); i++ {
for j := 0; j < m.Cols(); j++ {
if len(sa[i][j]) > wds[j] {
wds[j] = len(sa[i][j])
}
}
}
var res bytesp.Slice
for i, row := range sa {
if i == 0 {
res.WriteString("[")
} else {
res.WriteString(" ")
}
res.WriteString("[")
for j, cell := range row {
if j > 0 {
res.WriteString(" ")
}
fmt.Fprintf(&res, "%*s", wds[j], cell)
}
res.WriteString("]")
if i == len(sa)-1 {
fmt.Fprintf(&res, "](%dx%d)", m.Rows(), m.Cols())
}
res.WriteString("\n")
}
return string(res)
}
// Fill sets all elements of the matrix to a specified value
func (m IntMatrix) Fill(vl int) {
if len(m) == 0 {
return
}
n := m.Rows() * m.Cols()
IntSlice(m[0][:n]).Fill(0, n, vl)
}