-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
std/pointers; std/chunks; fixes sequtils.applyIt
- Loading branch information
1 parent
1440e70
commit f810db8
Showing
4 changed files
with
140 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#[ | ||
`Slice` would be a better name but unfortunately, `system.Slice` exists. | ||
]# | ||
|
||
import std/pointers | ||
|
||
type Chunk*[T] = object | ||
data*: ptr T | ||
len*: int # TODO: or lenImpl + template accessor? | ||
|
||
type MChunk*[T] = object | ||
data*: ptr T | ||
len*: int | ||
|
||
proc toChunk*[T](a: openArray[T]): Chunk[T] = | ||
result = Chunk[T](data: a[0].addr, len: a.len) | ||
|
||
# proc toChunk*[T](a: var openArray[T]): var Chunk[T] = | ||
# let x = a[0].unsafeAddr | ||
# # result = Chunk[T](data: a[0].unsafeAddr, len: a.len) | ||
# result = Chunk[T](data: x, len: a.len) | ||
|
||
proc toMChunk*[T](a: var openArray[T]): MChunk[T] = | ||
result = MChunk[T](data: a[0].addr, len: a.len) | ||
|
||
iterator mitems*[T](a: MChunk[T]): var T = | ||
for i in 0..<a.len: | ||
yield a.data[i] | ||
|
||
iterator items*[T](a: MChunk[T] | Chunk[T]): lent T = | ||
for i in 0..<a.len: | ||
yield a.data[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
template `+`*[T](p: ptr T, off: int): ptr T = | ||
type T = typeof(p[]) # pending https://github.com/nim-lang/Nim/issues/13527 | ||
cast[ptr T](cast[ByteAddress](p) +% off * sizeof(T)) | ||
|
||
template `-`*[T](p: ptr T, off: int): ptr T = | ||
type T = typeof(p[]) | ||
cast[ptr T](cast[ByteAddress](p) -% off * sizeof(T)) | ||
|
||
template `[]`*[T](p: ptr T, off: int): T = | ||
(p + off)[] | ||
|
||
template `[]=`*[T](p: ptr T, off: int, val: T) = | ||
(p + off)[] = val | ||
|
||
proc `+=`*[T](p: ptr T, off: int) {.inline.} = | ||
# not a template to avoid double evaluation issues | ||
p = p + off | ||
|
||
proc `-=`*[T](p: ptr T, off: int) {.inline.} = | ||
p = p - off |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters