mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-25 08:43:58 +00:00
fixes #9263
This commit is contained in:
117
tests/destructor/tmatrix.nim
Normal file
117
tests/destructor/tmatrix.nim
Normal file
@@ -0,0 +1,117 @@
|
||||
discard """
|
||||
output: '''after 3 3
|
||||
after 3 3
|
||||
after 2 2'''
|
||||
"""
|
||||
# bug #9263
|
||||
type
|
||||
Matrix* = object
|
||||
# Array for internal storage of elements.
|
||||
data: ptr UncheckedArray[float]
|
||||
# Row and column dimensions.
|
||||
m*, n*: int
|
||||
|
||||
var
|
||||
allocCount, deallocCount: int
|
||||
|
||||
proc `=destroy`*(m: var Matrix) =
|
||||
if m.data != nil:
|
||||
dealloc(m.data)
|
||||
deallocCount.inc
|
||||
m.data = nil
|
||||
m.m = 0
|
||||
m.n = 0
|
||||
|
||||
proc `=sink`*(a: var Matrix; b: Matrix) =
|
||||
if a.data != nil and a.data != b.data:
|
||||
dealloc(a.data)
|
||||
deallocCount.inc
|
||||
a.data = b.data
|
||||
a.m = b.m
|
||||
a.n = b.n
|
||||
|
||||
proc `=`*(a: var Matrix; b: Matrix) =
|
||||
if a.data != nil and a.data != b.data:
|
||||
dealloc(a.data)
|
||||
deallocCount.inc
|
||||
a.data = nil
|
||||
a.m = b.m
|
||||
a.n = b.n
|
||||
if b.data != nil:
|
||||
a.data = cast[type(a.data)](alloc(a.m * a.n * sizeof(float)))
|
||||
allocCount.inc
|
||||
copyMem(a.data, b.data, b.m * b.n * sizeof(float))
|
||||
|
||||
proc matrix*(m, n: int, s: float): Matrix =
|
||||
## Construct an m-by-n constant matrix.
|
||||
result.m = m
|
||||
result.n = n
|
||||
result.data = cast[type(result.data)](alloc(m * n * sizeof(float)))
|
||||
allocCount.inc
|
||||
for i in 0 ..< m * n:
|
||||
result.data[i] = s
|
||||
|
||||
proc `[]`*(m: Matrix, i, j: int): float {.inline.} =
|
||||
## Get a single element.
|
||||
m.data[i * m.n + j]
|
||||
|
||||
proc `[]`*(m: var Matrix, i, j: int): var float {.inline.} =
|
||||
## Get a single element.
|
||||
m.data[i * m.n + j]
|
||||
|
||||
proc `[]=`*(m: var Matrix, i, j: int, s: float) =
|
||||
## Set a single element.
|
||||
m.data[i * m.n + j] = s
|
||||
|
||||
proc `-`*(m: sink Matrix): Matrix =
|
||||
## Unary minus
|
||||
result = m
|
||||
for i in 0 ..< m.m:
|
||||
for j in 0 ..< m.n:
|
||||
result[i, j] = -m[i, j]
|
||||
|
||||
proc `+`*(a: sink Matrix; b: Matrix): Matrix =
|
||||
## ``C = A + B``
|
||||
assert(b.m == a.m and b.n == a.n, "Matrix dimensions must agree.")
|
||||
result = a
|
||||
for i in 0 ..< a.m:
|
||||
for j in 0 ..< a.n:
|
||||
result[i, j] = a[i, j] + b[i, j]
|
||||
|
||||
proc `-`*(a: sink Matrix; b: Matrix): Matrix =
|
||||
## ``C = A - B``
|
||||
assert(b.m == a.m and b.n == a.n, "Matrix dimensions must agree.")
|
||||
result = a
|
||||
for i in 0 ..< a.m:
|
||||
for j in 0 ..< a.n:
|
||||
result[i, j] = a[i, j] - b[i, j]
|
||||
|
||||
proc info =
|
||||
echo "after ", allocCount, " ", deallocCount
|
||||
allocCount = 0
|
||||
deallocCount = 0
|
||||
|
||||
proc test1 =
|
||||
var a = matrix(5, 5, 1.0)
|
||||
var b = a
|
||||
var c = a + b
|
||||
|
||||
proc test2 =
|
||||
var a = matrix(5, 5, 1.0)
|
||||
var b = a
|
||||
var c = -a
|
||||
|
||||
proc test3 =
|
||||
var a = matrix(5, 5, 1.0)
|
||||
var b = matrix(5, 5, 2.0)
|
||||
# a = a - b
|
||||
b = -b + a
|
||||
|
||||
test1()
|
||||
info()
|
||||
|
||||
test2()
|
||||
info()
|
||||
|
||||
test3()
|
||||
info()
|
||||
Reference in New Issue
Block a user