summary refs log tree commit diff stats
path: root/lib/deprecated/pure
diff options
context:
space:
mode:
Diffstat (limited to 'lib/deprecated/pure')
-rw-r--r--lib/deprecated/pure/future.nim6
-rw-r--r--lib/deprecated/pure/mersenne.nim51
-rw-r--r--lib/deprecated/pure/ospaths.nim22
-rw-r--r--lib/deprecated/pure/oswalkdir.nim13
-rw-r--r--lib/deprecated/pure/sums.nim80
5 files changed, 172 insertions, 0 deletions
diff --git a/lib/deprecated/pure/future.nim b/lib/deprecated/pure/future.nim
new file mode 100644
index 000000000..0e06161f2
--- /dev/null
+++ b/lib/deprecated/pure/future.nim
@@ -0,0 +1,6 @@
+## This module is a deprecated alias for the `sugar` module. Deprecated since 0.19.0.
+
+{.deprecated: "Use the new 'sugar' module instead".}
+
+import std/sugar
+export sugar
diff --git a/lib/deprecated/pure/mersenne.nim b/lib/deprecated/pure/mersenne.nim
new file mode 100644
index 000000000..37c5085b1
--- /dev/null
+++ b/lib/deprecated/pure/mersenne.nim
@@ -0,0 +1,51 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2015 Nim Contributors
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+## The [Mersenne Twister](https://en.wikipedia.org/wiki/Mersenne_Twister)
+## random number generator.
+## .. note:: The procs in this module work at compile-time.
+
+{.deprecated: "use `std/random` instead".}
+
+runnableExamples:
+  var rand = newMersenneTwister(uint32.high)  ## must be "var"
+  doAssert rand.getNum() != rand.getNum()  ## pseudorandom number
+## See also
+## ========
+## * `random module<random.html>`_ for Nim's standard random number generator
+type
+  MersenneTwister* = object
+    ## The Mersenne Twister.
+    mt: array[0..623, uint32]
+    index: int
+
+proc newMersenneTwister*(seed: uint32): MersenneTwister =
+  ## Creates a new `MersenneTwister` with seed `seed`.
+  result.index = 0
+  result.mt[0] = seed
+  for i in 1'u32 .. 623'u32:
+    result.mt[i] = (0x6c078965'u32 * (result.mt[i-1] xor
+                                      (result.mt[i-1] shr 30'u32)) + i)
+
+proc generateNumbers(m: var MersenneTwister) =
+
+  for i in 0..623:
+    var y = (m.mt[i] and 0x80000000'u32) +
+            (m.mt[(i+1) mod 624] and 0x7fffffff'u32)
+    m.mt[i] = m.mt[(i+397) mod 624] xor uint32(y shr 1'u32)
+    if (y mod 2'u32) != 0:
+      m.mt[i] = m.mt[i] xor 0x9908b0df'u32
+
+proc getNum*(m: var MersenneTwister): uint32 =
+  ## Returns the next pseudorandom `uint32`.
+  if m.index == 0:
+    generateNumbers(m)
+  result = m.mt[m.index]
+  m.index = (m.index + 1) mod m.mt.len
+  result = result xor (result shr 11'u32)
+  result = result xor ((result shl 7'u32) and 0x9d2c5680'u32)
+  result = result xor ((result shl 15'u32) and 0xefc60000'u32)
+  result = result xor (result shr 18'u32)
diff --git a/lib/deprecated/pure/ospaths.nim b/lib/deprecated/pure/ospaths.nim
new file mode 100644
index 000000000..43fcb17cc
--- /dev/null
+++ b/lib/deprecated/pure/ospaths.nim
@@ -0,0 +1,22 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2015 Andreas Rumpf
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+## This module is deprecated since 0.20.0, `import std/os` instead.
+
+{.deprecated: "use `std/os` instead".}
+
+import std/os
+export ReadEnvEffect, WriteEnvEffect, ReadDirEffect, WriteDirEffect, OSErrorCode,
+  doslikeFileSystem, CurDir, ParDir, DirSep, AltSep, PathSep, FileSystemCaseSensitive,
+  ExeExt, ScriptExt, DynlibFormat, ExtSep, joinPath, `/`, splitPath, parentDir,
+  tailDir, isRootDir, parentDirs, `/../`, searchExtPos, splitFile, extractFilename,
+  lastPathPart, changeFileExt, addFileExt, cmpPaths, isAbsolute, unixToNativePath,
+  `==`, `$`, osErrorMsg, raiseOSError, osLastError, getEnv, existsEnv, putEnv,
+  getHomeDir, getConfigDir, getTempDir, expandTilde, quoteShellWindows,
+  quoteShellPosix, quoteShell, quoteShellCommand
diff --git a/lib/deprecated/pure/oswalkdir.nim b/lib/deprecated/pure/oswalkdir.nim
new file mode 100644
index 000000000..57a2cb81d
--- /dev/null
+++ b/lib/deprecated/pure/oswalkdir.nim
@@ -0,0 +1,13 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2015 Andreas Rumpf
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+## This module is deprecated, `import std/os` instead.
+{.deprecated: "import 'std/os' instead".}
+import std/os
+export PathComponent, walkDir, walkDirRec
diff --git a/lib/deprecated/pure/sums.nim b/lib/deprecated/pure/sums.nim
new file mode 100644
index 000000000..a6ce1b85d
--- /dev/null
+++ b/lib/deprecated/pure/sums.nim
@@ -0,0 +1,80 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2019 b3liever
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+
+## Accurate summation functions.
+
+{.deprecated: "use the nimble package `sums` instead.".}
+
+runnableExamples:
+  import std/math
+
+  template `~=`(x, y: float): bool = abs(x - y) < 1e-4
+
+  let
+    n = 1_000_000
+    first = 1e10
+    small = 0.1
+  var data = @[first]
+  for _ in 1 .. n:
+    data.add(small)
+
+  let result = first + small * n.float
+
+  doAssert abs(sum(data) - result) > 0.3
+  doAssert sumKbn(data) ~= result
+  doAssert sumPairs(data) ~= result
+
+## See also
+## ========
+## * `math module <math.html>`_ for a standard `sum proc <math.html#sum,openArray[T]>`_
+
+func sumKbn*[T](x: openArray[T]): T =
+  ## Kahan-Babuška-Neumaier summation: O(1) error growth, at the expense
+  ## of a considerable increase in computational cost.
+  ##
+  ## See:
+  ## * https://en.wikipedia.org/wiki/Kahan_summation_algorithm#Further_enhancements
+  if len(x) == 0: return
+  var sum = x[0]
+  var c = T(0)
+  for i in 1 ..< len(x):
+    let xi = x[i]
+    let t = sum + xi
+    if abs(sum) >= abs(xi):
+      c += (sum - t) + xi
+    else:
+      c += (xi - t) + sum
+    sum = t
+  result = sum + c
+
+func sumPairwise[T](x: openArray[T], i0, n: int): T =
+  if n < 128:
+    result = x[i0]
+    for i in i0 + 1 ..< i0 + n:
+      result += x[i]
+  else:
+    let n2 = n div 2
+    result = sumPairwise(x, i0, n2) + sumPairwise(x, i0 + n2, n - n2)
+
+func sumPairs*[T](x: openArray[T]): T =
+  ## Pairwise (cascade) summation of `x[i0:i0+n-1]`, with O(log n) error growth
+  ## (vs O(n) for a simple loop) with negligible performance cost if
+  ## the base case is large enough.
+  ##
+  ## See, e.g.:
+  ## * https://en.wikipedia.org/wiki/Pairwise_summation
+  ## * Higham, Nicholas J. (1993), "The accuracy of floating point
+  ##   summation", SIAM Journal on Scientific Computing 14 (4): 783–799.
+  ##
+  ## In fact, the root-mean-square error growth, assuming random roundoff
+  ## errors, is only O(sqrt(log n)), which is nearly indistinguishable from O(1)
+  ## in practice. See:
+  ## * Manfred Tasche and Hansmartin Zeuner, Handbook of
+  ##   Analytic-Computational Methods in Applied Mathematics (2000).
+  let n = len(x)
+  if n == 0: T(0) else: sumPairwise(x, 0, n)