shadow_common.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. package mat
  5. const (
  6. // regionOverlap is the panic string used for the general case
  7. // of a matrix region overlap between a source and destination.
  8. regionOverlap = "mat: bad region: overlap"
  9. // regionIdentity is the panic string used for the specific
  10. // case of complete agreement between a source and a destination.
  11. regionIdentity = "mat: bad region: identical"
  12. // mismatchedStrides is the panic string used for overlapping
  13. // data slices with differing strides.
  14. mismatchedStrides = "mat: bad region: different strides"
  15. )
  16. // rectanglesOverlap returns whether the strided rectangles a and b overlap
  17. // when b is offset by off elements after a but has at least one element before
  18. // the end of a. off must be positive. a and b have aCols and bCols respectively.
  19. //
  20. // rectanglesOverlap works by shifting both matrices left such that the left
  21. // column of a is at 0. The column indexes are flattened by obtaining the shifted
  22. // relative left and right column positions modulo the common stride. This allows
  23. // direct comparison of the column offsets when the matrix backing data slices
  24. // are known to overlap.
  25. func rectanglesOverlap(off, aCols, bCols, stride int) bool {
  26. if stride == 1 {
  27. // Unit stride means overlapping data
  28. // slices must overlap as matrices.
  29. return true
  30. }
  31. // Flatten the shifted matrix column positions
  32. // so a starts at 0, modulo the common stride.
  33. aTo := aCols
  34. // The mod stride operations here make the from
  35. // and to indexes comparable between a and b when
  36. // the data slices of a and b overlap.
  37. bFrom := off % stride
  38. bTo := (bFrom + bCols) % stride
  39. if bTo == 0 || bFrom < bTo {
  40. // b matrix is not wrapped: compare for
  41. // simple overlap.
  42. return bFrom < aTo
  43. }
  44. // b strictly wraps and so must overlap with a.
  45. return true
  46. }