-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathsubmatrix.go
79 lines (66 loc) · 1.62 KB
/
submatrix.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
package matrix
import (
"context"
"errors"
"sync"
)
// SubMatrix extracts a submatrix from the current matrix.
func (m Matrix[T]) SubMatrix(rowStart, colStart, numRows, numCols int) (Matrix[T], error) {
if rowStart < 0 || colStart < 0 || numRows < 0 || numCols < 0 {
return Matrix[T]{}, errors.New("negative dimensions are not allowed")
}
if rowStart+numRows > m.rows || colStart+numCols > m.columns {
return Matrix[T]{}, errors.New("submatrix dimensions exceed matrix bounds")
}
var zeroVal T
if numRows == 0 || numCols == 0 {
return New(numRows, numCols, zeroVal), nil // Return an empty matrix
}
subMatrix := New(numRows, numCols, zeroVal)
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // Make sure it's called to release resources even if no errors
var wg sync.WaitGroup
errCh := make(chan error, 1)
for i := 0; i < numRows; i++ {
i := i // Capture the loop variable for the goroutine
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < numCols; j++ {
select {
case <-ctx.Done():
return // Context canceled; return without an error
default:
}
val, err := m.Get(rowStart+i, colStart+j)
if err != nil {
cancel()
select {
case errCh <- err:
default:
}
return
}
err = subMatrix.Set(i, j, val)
if err != nil {
cancel()
select {
case errCh <- err:
default:
}
return
}
}
}()
}
// Wait for all goroutines to finish
go func() {
wg.Wait()
close(errCh)
}()
// Check for any errors
if err := <-errCh; err != nil {
return Matrix[T]{}, err
}
return subMatrix, nil
}