gonum.org/v1/gonum@v0.14.0/mat/shadow_common.go (about) 1 // Copyright ©2015 The Gonum Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mat 6 7 const ( 8 // regionOverlap is the panic string used for the general case 9 // of a matrix region overlap between a source and destination. 10 regionOverlap = "mat: bad region: overlap" 11 12 // regionIdentity is the panic string used for the specific 13 // case of complete agreement between a source and a destination. 14 regionIdentity = "mat: bad region: identical" 15 16 // mismatchedStrides is the panic string used for overlapping 17 // data slices with differing strides. 18 mismatchedStrides = "mat: bad region: different strides" 19 ) 20 21 // rectanglesOverlap returns whether the strided rectangles a and b overlap 22 // when b is offset by off elements after a but has at least one element before 23 // the end of a. off must be positive. a and b have aCols and bCols respectively. 24 // 25 // rectanglesOverlap works by shifting both matrices left such that the left 26 // column of a is at 0. The column indexes are flattened by obtaining the shifted 27 // relative left and right column positions modulo the common stride. This allows 28 // direct comparison of the column offsets when the matrix backing data slices 29 // are known to overlap. 30 func rectanglesOverlap(off, aCols, bCols, stride int) bool { 31 if stride == 1 { 32 // Unit stride means overlapping data 33 // slices must overlap as matrices. 34 return true 35 } 36 37 // Flatten the shifted matrix column positions 38 // so a starts at 0, modulo the common stride. 39 aTo := aCols 40 // The mod stride operations here make the from 41 // and to indexes comparable between a and b when 42 // the data slices of a and b overlap. 43 bFrom := off % stride 44 bTo := (bFrom + bCols) % stride 45 46 if bTo == 0 || bFrom < bTo { 47 // b matrix is not wrapped: compare for 48 // simple overlap. 49 return bFrom < aTo 50 } 51 52 // b strictly wraps and so must overlap with a. 53 return true 54 }