From c591db16c854780cae2f97ec096fed9835a8511b Mon Sep 17 00:00:00 2001 From: def Date: Wed, 9 Jul 2014 18:43:00 +0200 Subject: Add reversed proc --- lib/pure/algorithm.nim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index 86d329763..7af1b2ad8 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -34,6 +34,20 @@ proc reverse*[T](a: var openArray[T]) = ## reverses the array `a`. reverse(a, 0, a.high) +proc reversed*[T](a: openArray[T], first, last: int): seq[T] = + ## returns the reverse of the array `a[first..last]`. + result = newSeq[T](last - first) + var x = first + var y = last + while x < last: + result[x] = a[y] + dec(y) + inc(x) + +proc reversed*[T](a: openArray[T]): seq[T] = + ## returns the reverse of the array `a`. + reversed(a, 0, a.high) + proc binarySearch*[T](a: openArray[T], key: T): int = ## binary search for `key` in `a`. Returns -1 if not found. var b = len(a) -- cgit 1.4.1-2-gfad0 From 8b796763a37a41be107b1f4aae50b28a56a3cdb9 Mon Sep 17 00:00:00 2001 From: def Date: Wed, 9 Jul 2014 18:54:05 +0200 Subject: Fix to included last element in reversed --- lib/pure/algorithm.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index 7af1b2ad8..89c83a8a4 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -36,10 +36,10 @@ proc reverse*[T](a: var openArray[T]) = proc reversed*[T](a: openArray[T], first, last: int): seq[T] = ## returns the reverse of the array `a[first..last]`. - result = newSeq[T](last - first) + result = newSeq[T](last - first + 1) var x = first var y = last - while x < last: + while x <= last: result[x] = a[y] dec(y) inc(x) -- cgit 1.4.1-2-gfad0 From 66fad123916924b42228487067eb155af830bef1 Mon Sep 17 00:00:00 2001 From: def Date: Wed, 9 Jul 2014 20:40:01 +0200 Subject: Change wordwrap to remove separators at newlines --- lib/pure/strutils.nim | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index e642f6a99..51392ba50 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -535,7 +535,12 @@ proc wordWrap*(s: string, maxLineWidth = 80, ## word wraps `s`. result = newStringOfCap(s.len + s.len shr 6) var spaceLeft = maxLineWidth + var lastSep = "" for word, isSep in tokenize(s, seps): + if isSep: + lastSep = word + spaceLeft = spaceLeft - len(word) + continue if len(word) > spaceLeft: if splitLongWords and len(word) > maxLineWidth: result.add(substr(word, 0, spaceLeft-1)) @@ -554,7 +559,8 @@ proc wordWrap*(s: string, maxLineWidth = 80, result.add(word) else: spaceLeft = spaceLeft - len(word) - result.add(word) + result.add(lastSep & word) + lastSep.setLen(0) proc unindent*(s: string, eatAllIndent = false): string {. noSideEffect, rtl, extern: "nsuUnindent".} = -- cgit 1.4.1-2-gfad0 From ce3d1b302ebf30d93697be9c85112350bb9e4eb0 Mon Sep 17 00:00:00 2001 From: def Date: Wed, 9 Jul 2014 21:25:39 +0200 Subject: Update news.txt --- web/news.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/web/news.txt b/web/news.txt index eef71a2f7..d432a0e95 100644 --- a/web/news.txt +++ b/web/news.txt @@ -20,6 +20,7 @@ News - Added module ``cpuinfo``. - Added module ``threadpool``. - ``sequtils.distnct`` has been renamed to ``sequtils.deduplicate``. + - Added ``algorithm.reversed`` 2014-04-21 Version 0.9.4 released -- cgit 1.4.1-2-gfad0 From 7c51f67e859d364fcfed515351a3c7657386561b Mon Sep 17 00:00:00 2001 From: Grzegorz Adam Hankiewicz Date: Sat, 12 Jul 2014 10:45:32 +0200 Subject: Improves docstring contents and visibility of paramStr and friends. --- lib/pure/os.nim | 80 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 11 deletions(-) diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 0b4538abc..380c9e6e7 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -1560,7 +1560,52 @@ proc getTempDir*(): string {.rtl, extern: "nos$1", tags: [FReadEnv].} = when defined(windows): return string(getEnv("TEMP")) & "\\" else: return "/tmp/" -when defined(windows): +when defined(nimdoc): + # Common forward declaration docstring block for parameter retrieval procs. + proc paramCount*(): int {.tags: [FReadIO].} = + ## Returns the number of `command line arguments`:idx: given to the + ## application. + ## + ## If your binary was called without parameters this will return zero. You + ## can later query each individual paramater with `paramStr() <#paramStr>`_ + ## or retrieve all of them in one go with `commandLineParams() + ## <#commandLineParams>`_. + ## + ## **Availability**: On Posix there is no portable way to get the command + ## line from a DLL and thus the proc isn't defined in this environment. You + ## can test for its availability with `defined() `_. + ## Example: + ## + ## .. code-block:: nimrod + ## when defined(paramCount): + ## # Use paramCount() here + ## else: + ## # Do something else! + + proc paramStr*(i: int): TaintedString {.tags: [FReadIO].} = + ## Returns the `i`-th `command line argument`:idx: given to the application. + ## + ## `i` should be in the range `1..paramCount()`, the `EInvalidIndex` + ## exception will be raised for invalid values. Instead of iterating over + ## `paramCount() <#paramCount>`_ with this proc you can call the + ## convenience `commandLineParams() <#commandLineParams>`_. + ## + ## It is possible to call ``paramStr(0)`` but this will return OS specific + ## contents (usually the name of the invoked executable). You should avoid + ## this and call `getAppFilename() <#getAppFilename>`_ instead. + ## + ## **Availability**: On Posix there is no portable way to get the command + ## line from a DLL and thus the proc isn't defined in this environment. You + ## can test for its availability with `defined() `_. + ## Example: + ## + ## .. code-block:: nimrod + ## when defined(paramStr): + ## # Use paramStr() here + ## else: + ## # Do something else! + +elif defined(windows): # Since we support GUI applications with Nimrod, we sometimes generate # a WinMain entry proc. But a WinMain proc has no access to the parsed # command line arguments. The way to get them differs. Thus we parse them @@ -1570,18 +1615,13 @@ when defined(windows): ownArgv {.threadvar.}: seq[string] proc paramCount*(): int {.rtl, extern: "nos$1", tags: [FReadIO].} = - ## Returns the number of `command line arguments`:idx: given to the - ## application. + # Docstring in nimdoc block. if isNil(ownArgv): ownArgv = parseCmdLine($getCommandLine()) result = ownArgv.len-1 proc paramStr*(i: int): TaintedString {.rtl, extern: "nos$1", tags: [FReadIO].} = - ## Returns the `i`-th `command line argument`:idx: given to the - ## application. - ## - ## `i` should be in the range `1..paramCount()`, else - ## the `EOutOfIndex` exception is raised. + # Docstring in nimdoc block. if isNil(ownArgv): ownArgv = parseCmdLine($getCommandLine()) return TaintedString(ownArgv[i]) @@ -1592,13 +1632,31 @@ elif not defined(createNimRtl): cmdLine {.importc: "cmdLine".}: cstringArray proc paramStr*(i: int): TaintedString {.tags: [FReadIO].} = - if i < cmdCount and i >= 0: return TaintedString($cmdLine[i]) + # Docstring in nimdoc block. + if i < cmdCount and i >= 0: result = TaintedString($cmdLine[i]) raise newException(EInvalidIndex, "invalid index") - proc paramCount*(): int {.tags: [FReadIO].} = return cmdCount-1 + proc paramCount*(): int {.tags: [FReadIO].} = + # Docstring in nimdoc block. + result = cmdCount-1 -when defined(paramCount): +when defined(paramCount) or defined(nimdoc): proc commandLineParams*(): seq[TaintedString] = + ## Convenience proc which returns the command line parameters. + ## + ## This returns **only** the parameters. If you want to get the application + ## executable filename, call `getAppFilename() <#getAppFilename>`_. + ## + ## **Availability**: On Posix there is no portable way to get the command + ## line from a DLL and thus the proc isn't defined in this environment. You + ## can test for its availability with `defined() `_. + ## Example: + ## + ## .. code-block:: nimrod + ## when defined(commandLineParams): + ## # Use commandLineParams() here + ## else: + ## # Do something else! result = @[] for i in 1..paramCount(): result.add(paramStr(i)) -- cgit 1.4.1-2-gfad0 From b1a494e8b917750f92cb41692fa550e37243a22c Mon Sep 17 00:00:00 2001 From: def Date: Mon, 14 Jul 2014 14:44:58 +0200 Subject: Add missing complex arithmetic procs --- lib/pure/complex.nim | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lib/pure/complex.nim b/lib/pure/complex.nim index df08ace72..1392b73aa 100644 --- a/lib/pure/complex.nim +++ b/lib/pure/complex.nim @@ -113,6 +113,45 @@ proc `*` *(x: TComplex, y: float): TComplex = result.im = x.im * y +proc `+=` *(x: var TComplex, y: TComplex) = + ## Add `y` to `x`. + x.re += y.re + x.im += y.im + +proc `+=` *(x: var TComplex, y: float) = + ## Add `y` to the complex number `x`. + x.re += y + +proc `-=` *(x: var TComplex, y: TComplex) = + ## Subtract `y` from `x`. + x.re -= y.re + x.im -= y.im + +proc `-=` *(x: var TComplex, y: float) = + ## Subtract `y` from the complex number `x`. + x.re -= y + +proc `*=` *(x: var TComplex, y: TComplex) = + ## Multiply `y` to `x`. + let im = x.im * y.re + x.re * y.im + x.re = x.re * y.re - x.im * y.im + x.im = im + +proc `*=` *(x: var TComplex, y: float) = + ## Multiply `y` to the complex number `x`. + x.re *= y + x.im *= y + +proc `/=` *(x: var TComplex, y: TComplex) = + ## Divide `x` by `y` in place. + x = x / y + +proc `/=` *(x : var TComplex, y: float) = + ## Divide complex `x` by float `y` in place. + x.re /= y + x.im /= y + + proc abs*(z: TComplex): float = ## Return the distance from (0,0) to `z`. -- cgit 1.4.1-2-gfad0 From 5bfea58ffb9fd731131bbbd3f3d077cb69c5f419 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 14 Jul 2014 15:03:09 +0200 Subject: Add missing difference and to sets module --- lib/pure/collections/sets.nim | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index 4ba67cb2e..2f0c4d3d2 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -167,6 +167,12 @@ proc intersection*[A](s1, s2: TSet[A]): TSet[A] = for item in s1: if item in s2: incl(result, item) +proc difference*[A](s1, s2: TSet[A]): TSet[A] = + ## returns a new set of all items that are contained in `s1`, but not in `s2` + result = s1 + for item in s2: + if contains(result, item): excl(result, item) + proc symmetricDifference*[A](s1, s2: TSet[A]): TSet[A] = ## returns a new set of all items that are contained in either ## `s1` or `s2`, but not both @@ -182,6 +188,10 @@ proc `*`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = ## alias for `intersection` result = intersection(s1, s2) +proc `-`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = + ## alias for `difference` + result = difference(s1, s2) + proc `-+-`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = ## alias for `symmetricDifference` result = symmetricDifference(s1, s2) -- cgit 1.4.1-2-gfad0 From 68c3c8ebfafbcd169bceaf2bebed8f7af8dbf124 Mon Sep 17 00:00:00 2001 From: def Date: Mon, 14 Jul 2014 17:56:19 +0200 Subject: More effificent TSet difference --- lib/pure/collections/sets.nim | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index 2f0c4d3d2..f1eed0004 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -169,9 +169,10 @@ proc intersection*[A](s1, s2: TSet[A]): TSet[A] = proc difference*[A](s1, s2: TSet[A]): TSet[A] = ## returns a new set of all items that are contained in `s1`, but not in `s2` - result = s1 - for item in s2: - if contains(result, item): excl(result, item) + result = initSet[A]() + for item in s1: + if not contains(s2, item): + incl(result, item) proc symmetricDifference*[A](s1, s2: TSet[A]): TSet[A] = ## returns a new set of all items that are contained in either -- cgit 1.4.1-2-gfad0 From 54d61d2f665d47e361cad6c6c1075a9aca2de4d3 Mon Sep 17 00:00:00 2001 From: def Date: Tue, 15 Jul 2014 02:16:12 +0200 Subject: Add TSet difference tests --- tests/sets/tsets3.nim | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/sets/tsets3.nim b/tests/sets/tsets3.nim index d2b15d72d..f599f8e7d 100644 --- a/tests/sets/tsets3.nim +++ b/tests/sets/tsets3.nim @@ -74,8 +74,27 @@ block symmetricDifference: assert((s3 -+- s3) == initSet[int]()) assert((s3 -+- s1) == s1_s3) +block difference: + let + s1_s2 = difference(s1, s2) + s1_s3 = difference(s1, s3) + s2_s3 = s2 - s3 + + assert s1_s2.len == 2 + assert s1_s3.len == 5 + assert s2_s3.len == 3 + + for i in s1: + assert i in s1_s2 xor i in s2 + assert i in s1_s3 xor i in s3 + for i in s2: + assert i in s2_s3 xor i in s3 + + assert((s2 - s2) == initSet[int]()) + assert((s1 - s3 - s1) == s1 -+- s3) + block disjoint: assert(not disjoint(s1, s2)) assert disjoint(s1, s3) assert(not disjoint(s2, s3)) - assert(not disjoint(s2, s2)) \ No newline at end of file + assert(not disjoint(s2, s2)) -- cgit 1.4.1-2-gfad0