Skip to content

Commit

Permalink
fix issues andreaferretti#43, add backwardindex for vector
Browse files Browse the repository at this point in the history
  • Loading branch information
ippaveln committed Apr 3, 2022
1 parent 39cde72 commit 8033f79
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions neo/dense.nim
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ proc `[]=`*[A](m: var Matrix[A], i, j: int, val: A) {. inline .} =
else:
elRowMajor(mp, m, i, j) = val

proc `[]`*[A](v: Vector[A], i: BackwardsIndex): A {. inline .} =
return v[v.len - i.int]

proc `[]`*[A](v: var Vector[A], i: BackwardsIndex): var A {. inline .} =
return v[v.len - i.int]

proc `[]=`*[A](v: Vector[A], i: BackwardsIndex, val: A) {. inline .} =
v[v.len - i.int] = val

proc column*[A](m: Matrix[A], j: int): Vector[A] {. inline .} =
checkBounds(j >= 0 and j < m.N)
let mp = cast[CPointer[A]](m.fp)
Expand Down
7 changes: 7 additions & 0 deletions tests/dense/daccess.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ proc run() =
check v[2] == 4.0
check v[3] == 7.0
check v[4] == 10.0
test "reading vector elements backwise index":
let v = makeVector(5, proc(i: int): float64 = (3 * i - 2).float64)
check v[^5] == -2.0
check v[^4] == 1.0
check v[^3] == 4.0
check v[^2] == 7.0
check v[^1] == 10.0
test "mutating vector elements":
var v = zeros(3)
v[0] += 2.1
Expand Down

0 comments on commit 8033f79

Please sign in to comment.