Skip to content

Commit

Permalink
Add mitems and mpairs where it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
def- committed Jan 28, 2015
1 parent 6f4cd97 commit 46cf40c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/pure/collections/lists.nim
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ iterator items*[T](L: DoublyLinkedRing[T]): T =
## yields every value of `L`.
itemsRingImpl()

iterator mitems*[T](L: var DoublyLinkedList[T]): var T =
## yields every value of `L` so that you can modify it.
itemsListImpl()

iterator mitems*[T](L: var SinglyLinkedList[T]): var T =
## yields every value of `L` so that you can modify it.
itemsListImpl()

iterator mitems*[T](L: var SinglyLinkedRing[T]): var T =
## yields every value of `L` so that you can modify it.
itemsRingImpl()

iterator mitems*[T](L: var DoublyLinkedRing[T]): var T =
## yields every value of `L` so that you can modify it.
itemsRingImpl()

iterator nodes*[T](L: SinglyLinkedList[T]): SinglyLinkedNode[T] =
## iterates over every node of `x`. Removing the current node from the
## list during traversal is supported.
Expand Down
9 changes: 9 additions & 0 deletions lib/pure/collections/queues.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ iterator items*[T](q: Queue[T]): T =
yield q.data[i]
i = (i + 1) and q.mask

iterator mitems*[T](q: var Queue[T]): var T =
## yields every element of `q`.
var i = q.rd
var c = q.count
while c > 0:
dec c
yield q.data[i]
i = (i + 1) and q.mask

proc add*[T](q: var Queue[T], item: T) =
## adds an `item` to the end of the queue `q`.
var cap = q.mask+1
Expand Down
14 changes: 14 additions & 0 deletions lib/pure/json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -872,12 +872,26 @@ iterator items*(node: JsonNode): JsonNode =
for i in items(node.elems):
yield i

iterator mitems*(node: var JsonNode): var JsonNode =
## Iterator for the items of `node`. `node` has to be a JArray. Items can be
## modified.
assert node.kind == JArray
for i in mitems(node.elems):
yield i

iterator pairs*(node: JsonNode): tuple[key: string, val: JsonNode] =
## Iterator for the child elements of `node`. `node` has to be a JObject.
assert node.kind == JObject
for key, val in items(node.fields):
yield (key, val)

iterator mpairs*(node: var JsonNode): var tuple[key: string, val: JsonNode] =
## Iterator for the child elements of `node`. `node` has to be a JObject.
## Items can be modified
assert node.kind == JObject
for keyVal in mitems(node.fields):
yield keyVal

proc eat(p: var JsonParser, tok: TTokKind) =
if p.tok == tok: discard getTok(p)
else: raiseParseErr(p, tokToStr[tok])
Expand Down
10 changes: 10 additions & 0 deletions lib/pure/xmltree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,21 @@ proc `[]`* (n: XmlNode, i: int): XmlNode {.inline.} =
assert n.k == xnElement
result = n.s[i]

proc mget* (n: var XmlNode, i: int): var XmlNode {.inline.} =
## returns the `i`'th child of `n` so that it can be modified
assert n.k == xnElement
result = n.s[i]

iterator items*(n: XmlNode): XmlNode {.inline.} =
## iterates over any child of `n`.
assert n.k == xnElement
for i in 0 .. n.len-1: yield n[i]

iterator mitems*(n: var XmlNode): var XmlNode {.inline.} =
## iterates over any child of `n`.
assert n.k == xnElement
for i in 0 .. n.len-1: yield mget(n, i)

proc attrs*(n: XmlNode): XmlAttributes {.inline.} =
## gets the attributes belonging to `n`.
## Returns `nil` if attributes have not been initialised for this node.
Expand Down
68 changes: 68 additions & 0 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,13 @@ iterator items*[T](a: openArray[T]): T {.inline.} =
yield a[i]
inc(i)

iterator mitems*[T](a: var openArray[T]): var T {.inline.} =
## iterates over each item of `a` so that you can modify the yielded value.
var i = 0
while i < len(a):
yield a[i]
inc(i)

iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
## iterates over each item of `a`.
var i = low(IX)
Expand All @@ -1662,6 +1669,15 @@ iterator items*[IX, T](a: array[IX, T]): T {.inline.} =
if i >= high(IX): break
inc(i)

iterator mitems*[IX, T](a: var array[IX, T]): var T {.inline.} =
## iterates over each item of `a` so that you can modify the yielded value.
var i = low(IX)
if i <= high(IX):
while true:
yield a[i]
if i >= high(IX): break
inc(i)

iterator items*[T](a: set[T]): T {.inline.} =
## iterates over each element of `a`. `items` iterates only over the
## elements that are really in the set (and not over the ones the set is
Expand Down Expand Up @@ -1692,6 +1708,14 @@ iterator pairs*[T](a: openArray[T]): tuple[key: int, val: T] {.inline.} =
yield (i, a[i])
inc(i)

iterator mpairs*[T](a: var openArray[T]): tuple[key: int, val: var T] {.inline.} =
## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
## ``a[index]`` can be modified.
var i = 0
while i < len(a):
yield (i, a[i])
inc(i)

iterator pairs*[IX, T](a: array[IX, T]): tuple[key: IX, val: T] {.inline.} =
## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
var i = low(IX)
Expand All @@ -1701,20 +1725,46 @@ iterator pairs*[IX, T](a: array[IX, T]): tuple[key: IX, val: T] {.inline.} =
if i >= high(IX): break
inc(i)

iterator mpairs*[IX, T](a: var array[IX, T]): tuple[key: IX, val: var T] {.inline.} =
## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
## ``a[index]`` can be modified.
var i = low(IX)
if i <= high(IX):
while true:
yield (i, a[i])
if i >= high(IX): break
inc(i)

iterator pairs*[T](a: seq[T]): tuple[key: int, val: T] {.inline.} =
## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
var i = 0
while i < len(a):
yield (i, a[i])
inc(i)

iterator mpairs*[T](a: var seq[T]): tuple[key: int, val: var T] {.inline.} =
## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
## ``a[index]`` can be modified.
var i = 0
while i < len(a):
yield (i, a[i])
inc(i)

iterator pairs*(a: string): tuple[key: int, val: char] {.inline.} =
## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
var i = 0
while i < len(a):
yield (i, a[i])
inc(i)

iterator mpairs*(a: var string): tuple[key: int, val: var char] {.inline.} =
## iterates over each item of `a`. Yields ``(index, a[index])`` pairs.
## ``a[index]`` can be modified.
var i = 0
while i < len(a):
yield (i, a[i])
inc(i)


proc isNil*[T](x: seq[T]): bool {.noSideEffect, magic: "IsNil".}
proc isNil*[T](x: ref T): bool {.noSideEffect, magic: "IsNil".}
Expand Down Expand Up @@ -2992,6 +3042,15 @@ iterator items*[T](a: seq[T]): T {.inline.} =
inc(i)
assert(len(a) == L, "seq modified while iterating over it")

iterator mitems*[T](a: var seq[T]): var T {.inline.} =
## iterates over each item of `a` so that you can modify the yielded value.
var i = 0
let L = len(a)
while i < L:
yield a[i]
inc(i)
assert(len(a) == L, "seq modified while iterating over it")

iterator items*(a: string): char {.inline.} =
## iterates over each item of `a`.
var i = 0
Expand All @@ -3001,6 +3060,15 @@ iterator items*(a: string): char {.inline.} =
inc(i)
assert(len(a) == L, "string modified while iterating over it")

iterator mitems*(a: var string): var char {.inline.} =
## iterates over each item of `a` so that you can modify the yielded value.
var i = 0
let L = len(a)
while i < L:
yield a[i]
inc(i)
assert(len(a) == L, "string modified while iterating over it")

when not defined(nimhygiene):
{.pragma: inject.}

Expand Down

0 comments on commit 46cf40c

Please sign in to comment.