diff options
author | reactormonk <hafnersimon@gmail.com> | 2015-02-01 05:19:47 +0500 |
---|---|---|
committer | reactormonk <hafnersimon@gmail.com> | 2015-02-01 05:19:47 +0500 |
commit | 014be3a29c7c4691cde5b446a04fe522e008ea34 (patch) | |
tree | 7a69e58657270b029982c4e2796fb6c3219e120b /lib/pure | |
parent | ab5b8f53914c92875d72b278f966092426d57323 (diff) | |
parent | 69d96b604bd953011749fa01af4363130c0ecd0f (diff) | |
download | Nim-014be3a29c7c4691cde5b446a04fe522e008ea34.tar.gz |
Merge pull request #2020 from def-/mitems
mitems and mpairs
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/collections/lists.nim | 16 | ||||
-rw-r--r-- | lib/pure/collections/queues.nim | 9 | ||||
-rw-r--r-- | lib/pure/json.nim | 14 | ||||
-rw-r--r-- | lib/pure/poly.nim | 2 | ||||
-rw-r--r-- | lib/pure/xmltree.nim | 10 |
5 files changed, 50 insertions, 1 deletions
diff --git a/lib/pure/collections/lists.nim b/lib/pure/collections/lists.nim index 2770a3950..095775cbb 100644 --- a/lib/pure/collections/lists.nim +++ b/lib/pure/collections/lists.nim @@ -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. diff --git a/lib/pure/collections/queues.nim b/lib/pure/collections/queues.nim index fb4d35310..af5e7b6cd 100644 --- a/lib/pure/collections/queues.nim +++ b/lib/pure/collections/queues.nim @@ -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 diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 733516bea..873e4b78e 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -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]) diff --git a/lib/pure/poly.nim b/lib/pure/poly.nim index 286e5a8fd..58dcdc1ad 100644 --- a/lib/pure/poly.nim +++ b/lib/pure/poly.nim @@ -58,7 +58,7 @@ proc `[]=` *(p:var Poly;idx:int,v:float)= iterator items*(p:Poly):float= - ## Iterates through the corfficients of the polynomial. + ## Iterates through the coefficients of the polynomial. var i=p.degree while i>=0: yield p[i] diff --git a/lib/pure/xmltree.nim b/lib/pure/xmltree.nim index 3c789d841..c783158ea 100644 --- a/lib/pure/xmltree.nim +++ b/lib/pure/xmltree.nim @@ -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. |