diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/packages/docutils/rstgen.nim | 2 | ||||
-rw-r--r-- | lib/pure/base64.nim | 2 | ||||
-rw-r--r-- | lib/pure/collections/sets.nim | 702 | ||||
-rw-r--r-- | lib/pure/collections/tables.nim | 7 | ||||
-rw-r--r-- | lib/pure/json.nim | 8 | ||||
-rw-r--r-- | lib/pure/os.nim | 65 | ||||
-rw-r--r-- | lib/pure/osproc.nim | 4 |
7 files changed, 703 insertions, 87 deletions
diff --git a/lib/packages/docutils/rstgen.nim b/lib/packages/docutils/rstgen.nim index fdbca4ca8..e9bae69b5 100644 --- a/lib/packages/docutils/rstgen.nim +++ b/lib/packages/docutils/rstgen.nim @@ -775,7 +775,7 @@ proc renderCodeBlock(d: PDoc, n: PRstNode, result: var string) = dispA(d.target, result, "<pre>", "\\begin{rstpre}\n", []) if lang == langNone: d.msgHandler(d.filename, 1, 0, mwUnsupportedLanguage, langstr) - result.add(m.text) + for letter in m.text: escChar(d.target, result, letter) else: var g: TGeneralTokenizer initGeneralTokenizer(g, m.text) diff --git a/lib/pure/base64.nim b/lib/pure/base64.nim index 4e59a6ca6..7b3b0e6f5 100644 --- a/lib/pure/base64.nim +++ b/lib/pure/base64.nim @@ -58,7 +58,7 @@ template encodeInternal(s: expr, lineLen: int, newLine: string): stmt {.immediat if r+4 != result.len: setLen(result, r+4) else: - assert(r == result.len) + #assert(r == result.len) proc encode*[T:TInteger|char](s: openarray[T], lineLen = 75, newLine="\13\10"): string = ## encodes `s` into base64 representation. After `lineLen` characters, a diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index f1eed0004..42cdc682f 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -9,6 +9,10 @@ ## The ``sets`` module implements an efficient hash set and ordered hash set. ## +## Hash sets are different from the `built in set type +## <manual.html#set-type>`_. Sets allow you to store any value that can be +## `hashed <hashes.html>`_ and they don't contain duplicate entries. +## ## **Note**: The data types declared here have *value semantics*: This means ## that ``=`` performs a copy of the set. @@ -23,20 +27,69 @@ type TSlotEnum = enum seEmpty, seFilled, seDeleted TKeyValuePair[A] = tuple[slot: TSlotEnum, key: A] TKeyValuePairSeq[A] = seq[TKeyValuePair[A]] - TSet* {.final, myShallow.}[A] = object ## a generic hash set + TSet* {.final, myShallow.}[A] = object ## \ + ## A generic hash set. + ## + ## Use `init() <#init,TSet[A],int>`_ or `initSet[type]() <#initSet>`_ + ## before calling other procs on it. data: TKeyValuePairSeq[A] counter: int +proc isValid*[A](s: TSet[A]): bool = + ## Returns `true` if the set has been initialized with `initSet <#initSet>`_. + ## + ## Most operations over an uninitialized set will crash at runtime and + ## `assert <system.html#assert>`_ in debug builds. You can use this proc in + ## your own procs to verify that sets passed to your procs are correctly + ## initialized. Example: + ## + ## .. code-block :: nimrod + ## proc savePreferences(options: TSet[string]) = + ## assert options.isValid, "Pass an initialized set!" + ## # Do stuff here, may crash in release builds! + result = not s.data.isNil + proc len*[A](s: TSet[A]): int = - ## returns the number of keys in `s`. + ## Returns the number of keys in `s`. + ## + ## Due to an implementation detail you can call this proc on variables which + ## have not been initialized yet. The proc will return zero as the length + ## then. Example: + ## + ## .. code-block:: + ## + ## var values: TSet[int] + ## assert(not values.isValid) + ## assert values.len == 0 result = s.counter proc card*[A](s: TSet[A]): int = - ## alias for `len`. + ## Alias for `len() <#len,TSet[A]>`_. + ## + ## Card stands for the `cardinality + ## <http://en.wikipedia.org/wiki/Cardinality>`_ of a set. result = s.counter iterator items*[A](s: TSet[A]): A = - ## iterates over any key in the table `t`. + ## Iterates over keys in the set `s`. + ## + ## If you need a sequence with the keys you can use `sequtils.toSeq() + ## <sequtils.html#toSeq>`_ on the iterator. Usage example: + ## + ## .. code-block:: + ## type + ## pair = tuple[a, b: int] + ## var + ## a, b = initSet[pair]() + ## a.incl((2, 3)) + ## a.incl((3, 2)) + ## a.incl((2, 3)) + ## for x, y in a.items: + ## b.incl((x - 2, y + 1)) + ## assert a.len == 2 + ## echo b + ## # --> {(a: 1, b: 3), (a: 0, b: 4)} + assert s.isValid, "The set needs to be initialized." for h in 0..high(s.data): if s.data[h].slot == seFilled: yield s.data[h].key @@ -73,12 +126,24 @@ proc mget*[A](s: var TSet[A], key: A): var A = ## value as 'key' or raises the ``EInvalidKey`` exception. This is useful ## when one overloaded 'hash' and '==' but still needs reference semantics ## for sharing. + assert s.isValid, "The set needs to be initialized." var index = rawGet(s, key) if index >= 0: result = t.data[index].key else: raise newException(EInvalidKey, "key not found: " & $key) proc contains*[A](s: TSet[A], key: A): bool = - ## returns true iff `key` is in `s`. + ## Returns true iff `key` is in `s`. + ## + ## Example: + ## + ## .. code-block:: + ## var values = initSet[int]() + ## assert(not values.contains(2)) + ## values.incl(2) + ## assert values.contains(2) + ## values.excl(2) + ## assert(not values.contains(2)) + assert s.isValid, "The set needs to be initialized." var index = rawGet(s, key) result = index >= 0 @@ -109,38 +174,124 @@ template containsOrInclImpl() {.dirty.} = inc(s.counter) proc incl*[A](s: var TSet[A], key: A) = - ## includes an element `key` in `s`. + ## Includes an element `key` in `s`. + ## + ## This doesn't do anything if `key` is already in `s`. Example: + ## + ## .. code-block:: + ## var values = initSet[int]() + ## values.incl(2) + ## values.incl(2) + ## assert values.len == 1 + assert s.isValid, "The set needs to be initialized." inclImpl() proc incl*[A](s: var TSet[A], other: TSet[A]) = - ## includes everything in `other` in `s` + ## Includes all elements from `other` into `s`. + ## + ## Example: + ## + ## .. code-block:: + ## var values = initSet[int]() + ## values.incl(2) + ## var others = toSet([6, 7]) + ## values.incl(others) + ## assert values.len == 3 + assert s.isValid, "The set `s` needs to be initialized." + assert other.isValid, "The set `other` needs to be initialized." for item in other: incl(s, item) proc excl*[A](s: var TSet[A], key: A) = - ## excludes `key` from the set `s`. + ## Excludes `key` from the set `s`. + ## + ## This doesn't do anything if `key` is not found in `s`. Example: + ## + ## .. code-block:: + ## var s = toSet([2, 3, 6, 7]) + ## s.excl(2) + ## s.excl(2) + ## assert s.len == 3 + assert s.isValid, "The set needs to be initialized." var index = rawGet(s, key) if index >= 0: s.data[index].slot = seDeleted dec(s.counter) proc excl*[A](s: var TSet[A], other: TSet[A]) = - ## excludes everything in `other` from `s`. + ## Excludes everything in `other` from `s`. + ## + ## Example: + ## + ## .. code-block:: + ## var + ## numbers = toSet([1, 2, 3, 4, 5]) + ## even = toSet([2, 4, 6, 8]) + ## numbers.excl(even) + ## echo numbers + ## # --> {1, 3, 5} + assert s.isValid, "The set `s` needs to be initialized." + assert other.isValid, "The set `other` needs to be initialized." for item in other: excl(s, item) proc containsOrIncl*[A](s: var TSet[A], key: A): bool = - ## returns true if `s` contains `key`, otherwise `key` is included in `s` - ## and false is returned. + ## Includes `key` in the set `s` and tells if `key` was added to `s`. + ## + ## The difference with regards to the `incl() <#incl,TSet[A],A>`_ proc is + ## that this proc returns `true` if `key` was already present in `s`. The + ## proc will return false if `key` was added as a new value to `s` during + ## this call. Example: + ## + ## .. code-block:: + ## var values = initSet[int]() + ## assert values.containsOrIncl(2) == false + ## assert values.containsOrIncl(2) == true + assert s.isValid, "The set needs to be initialized." containsOrInclImpl() -proc initSet*[A](initialSize=64): TSet[A] = - ## creates a new hash set that is empty. `initialSize` needs to be - ## a power of two. +proc init*[A](s: var TSet[A], initialSize=64) = + ## Initializes a hash set. + ## + ## The `initialSize` parameter needs to be a power of too. You can use + ## `math.nextPowerOfTwo() <math.html#nextPowerOfTwo>`_ to guarantee that at + ## runtime. All set variables have to be initialized before you can use them + ## with other procs from this module with the exception of `isValid() + ## <#isValid,TSet[A]>`_ and `len() <#len,TSet[A]>`_. + ## + ## You can call this proc on a previously initialized hash set, which will + ## discard all its values. This might be more convenient than iterating over + ## existing values and calling `excl() <#excl,TSet[A],A>`_ on them. Example: + ## + ## .. code-block :: + ## var a: TSet[int] + ## a.init(4) + ## a.incl(2) + ## a.init + ## assert a.len == 0 and a.isValid assert isPowerOfTwo(initialSize) - result.counter = 0 - newSeq(result.data, initialSize) + s.counter = 0 + newSeq(s.data, initialSize) + +proc initSet*[A](initialSize=64): TSet[A] = + ## Wrapper around `init() <#init,TSet[A],int>`_ for initialization of hash + ## sets. + ## + ## Returns an empty hash set you can assign directly in ``var`` blocks in a + ## single line. Example: + ## + ## .. code-block :: + ## var a = initSet[int](4) + ## a.incl(2) + result.init(initialSize) proc toSet*[A](keys: openArray[A]): TSet[A] = - ## creates a new hash set that contains the given `keys`. + ## Creates a new hash set that contains the given `keys`. + ## + ## Example: + ## + ## .. code-block:: + ## var numbers = toSet([1, 2, 3, 4, 5]) + ## assert numbers.contains(2) + ## assert numbers.contains(4) result = initSet[A](nextPowerOfTwo(keys.len+10)) for key in items(keys): result.incl(key) @@ -152,57 +303,190 @@ template dollarImpl(): stmt {.dirty.} = result.add("}") proc `$`*[A](s: TSet[A]): string = - ## The `$` operator for hash sets. + ## Converts the set `s` to a string, mostly for logging purposes. + ## + ## Don't use this proc for serialization, the representation may change at + ## any moment and values are not escaped. Example: + ## + ## Example: + ## + ## .. code-block:: + ## echo toSet([2, 4, 5]) + ## # --> {2, 4, 5} + ## echo toSet(["no", "esc'aping", "is \" provided"]) + ## # --> {no, esc'aping, is " provided} + assert s.isValid, "The set needs to be initialized." dollarImpl() proc union*[A](s1, s2: TSet[A]): TSet[A] = - ## returns a new set of all items that are contained in at - ## least one of `s1` and `s2` + ## Returns the union of the sets `s1` and `s2`. + ## + ## The union of two sets is represented mathematically as *A ∪ B* and is the + ## set of all objects that are members of `s1`, `s2` or both. Example: + ## + ## .. code-block:: + ## var + ## a = toSet(["a", "b"]) + ## b = toSet(["b", "c"]) + ## c = union(a, b) + ## assert c == toSet(["a", "b", "c"]) + assert s1.isValid, "The set `s1` needs to be initialized." + assert s2.isValid, "The set `s2` needs to be initialized." result = s1 incl(result, s2) proc intersection*[A](s1, s2: TSet[A]): TSet[A] = - ## returns a new set of all items that are contained in both `s1` and `s2` + ## Returns the intersection of the sets `s1` and `s2`. + ## + ## The intersection of two sets is represented mathematically as *A ∩ B* and + ## is the set of all objects that are members of `s1` and `s2` at the same + ## time. Example: + ## + ## .. code-block:: + ## var + ## a = toSet(["a", "b"]) + ## b = toSet(["b", "c"]) + ## c = intersection(a, b) + ## assert c == toSet(["b"]) + assert s1.isValid, "The set `s1` needs to be initialized." + assert s2.isValid, "The set `s2` needs to be initialized." result = initSet[A](min(s1.data.len, s2.data.len)) 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` + ## Returns the difference of the sets `s1` and `s2`. + ## + ## The difference of two sets is represented mathematically as *A \ B* and is + ## the set of all objects that are members of `s1` and not members of `s2`. + ## Example: + ## + ## .. code-block:: + ## var + ## a = toSet(["a", "b"]) + ## b = toSet(["b", "c"]) + ## c = difference(a, b) + ## assert c == toSet(["a"]) + assert s1.isValid, "The set `s1` needs to be initialized." + assert s2.isValid, "The set `s2` needs to be initialized." 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 - ## `s1` or `s2`, but not both + ## Returns the symmetric difference of the sets `s1` and `s2`. + ## + ## The symmetric difference of two sets is represented mathematically as *A △ + ## B* or *A ⊖ B* and is the set of all objects that are members of `s1` or + ## `s2` but not both at the same time. Example: + ## + ## .. code-block:: + ## var + ## a = toSet(["a", "b"]) + ## b = toSet(["b", "c"]) + ## c = symmetricDifference(a, b) + ## assert c == toSet(["a", "c"]) + assert s1.isValid, "The set `s1` needs to be initialized." + assert s2.isValid, "The set `s2` needs to be initialized." result = s1 for item in s2: if containsOrIncl(result, item): excl(result, item) proc `+`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = - ## alias for `union` + ## Alias for `union(s1, s2) <#union>`_. result = union(s1, s2) proc `*`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = - ## alias for `intersection` + ## Alias for `intersection(s1, s2) <#intersection>`_. result = intersection(s1, s2) proc `-`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = - ## alias for `difference` + ## Alias for `difference(s1, s2) <#difference>`_. result = difference(s1, s2) proc `-+-`*[A](s1, s2: TSet[A]): TSet[A] {.inline.} = - ## alias for `symmetricDifference` + ## Alias for `symmetricDifference(s1, s2) <#symmetricDifference>`_. result = symmetricDifference(s1, s2) proc disjoint*[A](s1, s2: TSet[A]): bool = - ## returns true iff `s1` and `s2` have no items in common + ## Returns true iff the sets `s1` and `s2` have no items in common. + ## + ## Example: + ## + ## .. code-block:: + ## var + ## a = toSet(["a", "b"]) + ## b = toSet(["b", "c"]) + ## assert disjoint(a, b) == false + ## assert disjoint(a, b - a) == true + assert s1.isValid, "The set `s1` needs to be initialized." + assert s2.isValid, "The set `s2` needs to be initialized." for item in s1: if item in s2: return false return true +proc `<`*[A](s, t: TSet[A]): bool = + ## Returns true if `s` is a strict or proper subset of `t`. + ## + ## A strict or proper subset `s` has all of its members in `t` but `t` has + ## more elements than `s`. Example: + ## + ## .. code-block:: + ## var + ## a = toSet(["a", "b"]) + ## b = toSet(["b", "c"]) + ## c = intersection(a, b) + ## assert c < a and c < b + ## assert((a < a) == false) + s.counter != t.counter and s <= t + +proc `<=`*[A](s, t: TSet[A]): bool = + ## Returns true if `s` is subset of `t`. + ## + ## A subset `s` has all of its members in `t` and `t` doesn't necessarily + ## have more members than `s`. That is, `s` can be equal to `t`. Example: + ## + ## .. code-block:: + ## var + ## a = toSet(["a", "b"]) + ## b = toSet(["b", "c"]) + ## c = intersection(a, b) + ## assert c <= a and c <= b + ## assert((a <= a)) + result = false + if s.counter > t.counter: return + result = true + for item in s: + if not(t.contains(item)): + result = false + return + +proc `==`*[A](s, t: TSet[A]): bool = + ## Returns true if both `s` and `t` have the same members and set size. + ## + ## Example: + ## + ## .. code-block:: + ## var + ## a = toSet([1, 2]) + ## b = toSet([1]) + ## b.incl(2) + ## assert a == b + s.counter == t.counter and s <= t + +proc map*[A, B](data: TSet[A], op: proc (x: A): B {.closure.}): TSet[B] = + ## Returns a new set after applying `op` on each of the elements of `data`. + ## + ## You can use this proc to transform the elements from a set. Example: + ## + ## .. code-block:: + ## var a = toSet([1, 2, 3]) + ## var b = a.map(proc (x: int): string = $x) + ## assert b == toSet(["1", "2", "3"]) + result = initSet[B]() + for item in data: result.incl(op(item)) + # ------------------------------ ordered set ------------------------------ type @@ -210,16 +494,48 @@ type slot: TSlotEnum, next: int, key: A] TOrderedKeyValuePairSeq[A] = seq[TOrderedKeyValuePair[A]] TOrderedSet* {. - final, myShallow.}[A] = object ## set that remembers insertion order + final, myShallow.}[A] = object ## \ + ## A generic hash set that remembers insertion order. + ## + ## Use `init() <#init,TOrderedSet[A],int>`_ or `initOrderedSet[type]() + ## <#initOrderedSet>`_ before calling other procs on it. data: TOrderedKeyValuePairSeq[A] counter, first, last: int +proc isValid*[A](s: TOrderedSet[A]): bool = + ## Returns `true` if the ordered set has been initialized with `initSet + ## <#initOrderedSet>`_. + ## + ## Most operations over an uninitialized ordered set will crash at runtime + ## and `assert <system.html#assert>`_ in debug builds. You can use this proc + ## in your own procs to verify that ordered sets passed to your procs are + ## correctly initialized. Example: + ## + ## .. code-block :: nimrod + ## proc saveTarotCards(cards: TOrderedSet[int]) = + ## assert cards.isValid, "Pass an initialized set!" + ## # Do stuff here, may crash in release builds! + result = not s.data.isNil + proc len*[A](s: TOrderedSet[A]): int {.inline.} = - ## returns the number of keys in `s`. + ## Returns the number of keys in `s`. + ## + ## Due to an implementation detail you can call this proc on variables which + ## have not been initialized yet. The proc will return zero as the length + ## then. Example: + ## + ## .. code-block:: + ## + ## var values: TOrderedSet[int] + ## assert(not values.isValid) + ## assert values.len == 0 result = s.counter proc card*[A](s: TOrderedSet[A]): int {.inline.} = - ## alias for `len`. + ## Alias for `len() <#len,TOrderedSet[A]>`_. + ## + ## Card stands for the `cardinality + ## <http://en.wikipedia.org/wiki/Cardinality>`_ of a set. result = s.counter template forAllOrderedPairs(yieldStmt: stmt) {.dirty, immediate.} = @@ -230,7 +546,24 @@ template forAllOrderedPairs(yieldStmt: stmt) {.dirty, immediate.} = h = nxt iterator items*[A](s: TOrderedSet[A]): A = - ## iterates over any key in the set `s` in insertion order. + ## Iterates over keys in the ordered set `s` in insertion order. + ## + ## If you need a sequence with the keys you can use `sequtils.toSeq() + ## <sequtils.html#toSeq>`_ on the iterator. Usage example: + ## + ## .. code-block:: + ## var a = initOrderedSet[int]() + ## for value in [9, 2, 1, 5, 1, 8, 4, 2]: + ## a.incl(value) + ## for value in a.items: + ## echo "Got ", value + ## # --> Got 9 + ## # --> Got 2 + ## # --> Got 1 + ## # --> Got 5 + ## # --> Got 8 + ## # --> Got 4 + assert s.isValid, "The set needs to be initialized." forAllOrderedPairs: yield s.data[h].key @@ -238,7 +571,16 @@ proc rawGet[A](s: TOrderedSet[A], key: A): int = rawGetImpl() proc contains*[A](s: TOrderedSet[A], key: A): bool = - ## returns true iff `key` is in `s`. + ## Returns true iff `key` is in `s`. + ## + ## Example: + ## + ## .. code-block:: + ## var values = initOrderedSet[int]() + ## assert(not values.contains(2)) + ## values.incl(2) + ## assert values.contains(2) + assert s.isValid, "The set needs to be initialized." var index = rawGet(s, key) result = index >= 0 @@ -264,53 +606,279 @@ proc enlarge[A](s: var TOrderedSet[A]) = swap(s.data, n) proc incl*[A](s: var TOrderedSet[A], key: A) = - ## includes an element `key` in `s`. + ## Includes an element `key` in `s`. + ## + ## This doesn't do anything if `key` is already in `s`. Example: + ## + ## .. code-block:: + ## var values = initOrderedSet[int]() + ## values.incl(2) + ## values.incl(2) + ## assert values.len == 1 + assert s.isValid, "The set needs to be initialized." inclImpl() proc incl*[A](s: var TSet[A], other: TOrderedSet[A]) = - ## includes everything in `other` in `s` + ## Includes all elements from `other` into `s`. + ## + ## Example: + ## + ## .. code-block:: + ## var values = initOrderedSet[int]() + ## values.incl(2) + ## var others = toOrderedSet([6, 7]) + ## values.incl(others) + ## assert values.len == 3 + assert s.isValid, "The set `s` needs to be initialized." + assert other.isValid, "The set `other` needs to be initialized." for item in other: incl(s, item) proc containsOrIncl*[A](s: var TOrderedSet[A], key: A): bool = - ## returns true if `s` contains `key`, otherwise `key` is included in `s` - ## and false is returned. + ## Includes `key` in the set `s` and tells if `key` was added to `s`. + ## + ## The difference with regards to the `incl() <#incl,TOrderedSet[A],A>`_ proc + ## is that this proc returns `true` if `key` was already present in `s`. The + ## proc will return false if `key` was added as a new value to `s` during + ## this call. Example: + ## + ## .. code-block:: + ## var values = initOrderedSet[int]() + ## assert values.containsOrIncl(2) == false + ## assert values.containsOrIncl(2) == true + assert s.isValid, "The set needs to be initialized." containsOrInclImpl() -proc initOrderedSet*[A](initialSize=64): TOrderedSet[A] = - ## creates a new ordered hash set that is empty. `initialSize` needs to be - ## a power of two. +proc init*[A](s: var TOrderedSet[A], initialSize=64) = + ## Initializes an ordered hash set. + ## + ## The `initialSize` parameter needs to be a power of too. You can use + ## `math.nextPowerOfTwo() <math.html#nextPowerOfTwo>`_ to guarantee that at + ## runtime. All set variables have to be initialized before you can use them + ## with other procs from this module with the exception of `isValid() + ## <#isValid,TOrderedSet[A]>`_ and `len() <#len,TOrderedSet[A]>`_. + ## + ## You can call this proc on a previously initialized ordered hash set to + ## discard its values. At the moment this is the only proc to remove elements + ## from an ordered hash set. Example: + ## + ## .. code-block :: + ## var a: TOrderedSet[int] + ## a.init(4) + ## a.incl(2) + ## a.init + ## assert a.len == 0 and a.isValid assert isPowerOfTwo(initialSize) - result.counter = 0 - result.first = -1 - result.last = -1 - newSeq(result.data, initialSize) + s.counter = 0 + s.first = -1 + s.last = -1 + newSeq(s.data, initialSize) + +proc initOrderedSet*[A](initialSize=64): TOrderedSet[A] = + ## Wrapper around `init() <#init,TOrderedSet[A],int>`_ for initialization of + ## ordered hash sets. + ## + ## Returns an empty ordered hash set you can assign directly in ``var`` + ## blocks in a single line. Example: + ## + ## .. code-block :: + ## var a = initOrderedSet[int](4) + ## a.incl(2) + result.init(initialSize) proc toOrderedSet*[A](keys: openArray[A]): TOrderedSet[A] = - ## creates a new ordered hash set that contains the given `keys`. + ## Creates a new ordered hash set that contains the given `keys`. + ## + ## Example: + ## + ## .. code-block:: + ## var numbers = toOrderedSet([1, 2, 3, 4, 5]) + ## assert numbers.contains(2) + ## assert numbers.contains(4) result = initOrderedSet[A](nextPowerOfTwo(keys.len+10)) for key in items(keys): result.incl(key) proc `$`*[A](s: TOrderedSet[A]): string = - ## The `$` operator for ordered hash sets. + ## Converts the ordered hash set `s` to a string, mostly for logging purposes. + ## + ## Don't use this proc for serialization, the representation may change at + ## any moment and values are not escaped. Example: + ## + ## Example: + ## + ## .. code-block:: + ## echo toOrderedSet([2, 4, 5]) + ## # --> {2, 4, 5} + ## echo toOrderedSet(["no", "esc'aping", "is \" provided"]) + ## # --> {no, esc'aping, is " provided} + assert s.isValid, "The set needs to be initialized." dollarImpl() -proc `<`*[A](s, t: TSet[A]): bool = - ## Is s a strict subset of t? - s.counter != t.counter and s <= t - -proc `<=`*[A](s, t: TSet[A]): bool = - ## Is s a subset of t? - result = false - if s.counter > t.counter: return - result = true - for item in s: - if not(t.contains(item)): - result = false - return - -proc `==`*[A](s, t: TSet[A]): bool = - s.counter == t.counter and s <= t - -proc map*[A, B](data: TSet[A], op: proc (x: A): B {.closure.}): TSet[B] = - result = initSet[B]() - for item in data: result.incl(op(item)) +proc testModule() = + ## Internal micro test to validate docstrings and such. + block isValidTest: + var options: TSet[string] + proc savePreferences(options: TSet[string]) = + assert options.isValid, "Pass an initialized set!" + options = initSet[string]() + options.savePreferences + + block lenTest: + var values: TSet[int] + assert(not values.isValid) + assert values.len == 0 + assert values.card == 0 + + block setIterator: + type pair = tuple[a, b: int] + var a, b = initSet[pair]() + a.incl((2, 3)) + a.incl((3, 2)) + a.incl((2, 3)) + for x, y in a.items: + b.incl((x - 2, y + 1)) + assert a.len == b.card + assert a.len == 2 + #echo b + + block setContains: + var values = initSet[int]() + assert(not values.contains(2)) + values.incl(2) + assert values.contains(2) + values.excl(2) + assert(not values.contains(2)) + + values.incl(4) + var others = toSet([6, 7]) + values.incl(others) + assert values.len == 3 + + values.init + assert values.containsOrIncl(2) == false + assert values.containsOrIncl(2) == true + var + a = toSet([1, 2]) + b = toSet([1]) + b.incl(2) + assert a == b + + block exclusions: + var s = toSet([2, 3, 6, 7]) + s.excl(2) + s.excl(2) + assert s.len == 3 + + var + numbers = toSet([1, 2, 3, 4, 5]) + even = toSet([2, 4, 6, 8]) + numbers.excl(even) + #echo numbers + # --> {1, 3, 5} + + block toSeqAndString: + var a = toSet([2, 4, 5]) + var b = initSet[int]() + for x in [2, 4, 5]: b.incl(x) + assert($a == $b) + #echo a + #echo toSet(["no", "esc'aping", "is \" provided"]) + + #block orderedToSeqAndString: + # echo toOrderedSet([2, 4, 5]) + # echo toOrderedSet(["no", "esc'aping", "is \" provided"]) + + block setOperations: + var + a = toSet(["a", "b"]) + b = toSet(["b", "c"]) + c = union(a, b) + assert c == toSet(["a", "b", "c"]) + var d = intersection(a, b) + assert d == toSet(["b"]) + var e = difference(a, b) + assert e == toSet(["a"]) + var f = symmetricDifference(a, b) + assert f == toSet(["a", "c"]) + assert d < a and d < b + assert((a < a) == false) + assert d <= a and d <= b + assert((a <= a)) + # Alias test. + assert a + b == toSet(["a", "b", "c"]) + assert a * b == toSet(["b"]) + assert a - b == toSet(["a"]) + assert a -+- b == toSet(["a", "c"]) + assert disjoint(a, b) == false + assert disjoint(a, b - a) == true + + block mapSet: + var a = toSet([1, 2, 3]) + var b = a.map(proc (x: int): string = $x) + assert b == toSet(["1", "2", "3"]) + + block isValidTest: + var cards: TOrderedSet[string] + proc saveTarotCards(cards: TOrderedSet[string]) = + assert cards.isValid, "Pass an initialized set!" + cards = initOrderedSet[string]() + cards.saveTarotCards + + block lenTest: + var values: TOrderedSet[int] + assert(not values.isValid) + assert values.len == 0 + assert values.card == 0 + + block setIterator: + type pair = tuple[a, b: int] + var a, b = initOrderedSet[pair]() + a.incl((2, 3)) + a.incl((3, 2)) + a.incl((2, 3)) + for x, y in a.items: + b.incl((x - 2, y + 1)) + assert a.len == b.card + assert a.len == 2 + + #block orderedSetIterator: + # var a = initOrderedSet[int]() + # for value in [9, 2, 1, 5, 1, 8, 4, 2]: + # a.incl(value) + # for value in a.items: + # echo "Got ", value + + block setContains: + var values = initOrderedSet[int]() + assert(not values.contains(2)) + values.incl(2) + assert values.contains(2) + + block toSeqAndString: + var a = toOrderedSet([2, 4, 5]) + var b = initOrderedSet[int]() + for x in [2, 4, 5]: b.incl(x) + assert($a == $b) + # assert(a == b) # https://github.com/Araq/Nimrod/issues/1413 + + block initBlocks: + var a: TOrderedSet[int] + a.init(4) + a.incl(2) + a.init + assert a.len == 0 and a.isValid + a = initOrderedSet[int](4) + a.incl(2) + assert a.len == 1 + + var b: TSet[int] + b.init(4) + b.incl(2) + b.init + assert b.len == 0 and b.isValid + b = initSet[int](4) + b.incl(2) + assert b.len == 1 + + echo "Micro tests run successfully." + +when isMainModule and not defined(release): testModule() diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index ce9df09e1..dcf2ab481 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -246,7 +246,8 @@ template equalsImpl() = # different insertion orders mean different 'data' seqs, so we have # to use the slow route here: for key, val in s: - if not hasKey(t, key): return false + # prefix notation leads to automatic dereference in case of PTable + if not t.hasKey(key): return false if t[key] != val: return false return true @@ -332,7 +333,9 @@ proc `$`*[A, B](t: PTable[A, B]): string = dollarImpl() proc `==`*[A, B](s, t: PTable[A, B]): bool = - equalsImpl() + if isNil(s): result = isNil(t) + elif isNil(t): result = false + else: result = equalsImpl() proc newTableFrom*[A, B, C](collection: A, index: proc(x: B): C): PTable[C, B] = ## Index the collection with the proc provided. diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 508e564c5..a45900f29 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -621,9 +621,13 @@ proc `%`*(elements: openArray[PJsonNode]): PJsonNode = proc `==`* (a,b: PJsonNode): bool = ## Check two nodes for equality - if a.kind != b.kind: false + if a.isNil: + if b.isNil: return true + return false + elif b.isNil or a.kind != b.kind: + return false else: - case a.kind + return case a.kind of JString: a.str == b.str of JInt: diff --git a/lib/pure/os.nim b/lib/pure/os.nim index a7f4f7d91..a70bfa7f1 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -955,11 +955,12 @@ proc copyFile*(source, dest: string) {.rtl, extern: "nos$1", ## ## If this fails, `EOS` is raised. On the Windows platform this proc will ## copy the source file's attributes into dest. On other platforms you need - ## to use getFilePermissions and setFilePermissions to copy them by hand (or - ## use the convenience copyFileWithPermissions() proc), otherwise `dest` will - ## inherit the default permissions of a newly created file for the user. If - ## `dest` already exists, the file attributes will be preserved and the - ## content overwritten. + ## to use `getFilePermissions() <#getFilePermissions>`_ and + ## `setFilePermissions() <#setFilePermissions>`_ to copy them by hand (or use + ## the convenience `copyFileWithPermissions() <#copyFileWithPermissions>`_ + ## proc), otherwise `dest` will inherit the default permissions of a newly + ## created file for the user. If `dest` already exists, the file attributes + ## will be preserved and the content overwritten. when defined(Windows): when useWinUnicode: let s = newWideCString(source) @@ -1363,7 +1364,13 @@ proc createDir*(dir: string) {.rtl, extern: "nos$1", tags: [FWriteDir].} = proc copyDir*(source, dest: string) {.rtl, extern: "nos$1", tags: [FWriteIO, FReadIO].} = - ## Copies a directory from `source` to `dest`. If this fails, `EOS` is raised. + ## Copies a directory from `source` to `dest`. + ## + ## If this fails, `EOS` is raised. On the Windows platform this proc will + ## copy the attributes from `source` into `dest`. On other platforms created + ## files and directories will inherit the default permissions of a newly + ## created file/directory for the user. To preserve attributes recursively on + ## these platforms use `copyDirWithPermissions() <#copyDirWithPermissions>`_. createDir(dest) for kind, path in walkDir(source): var noSource = path.substr(source.len()+1) @@ -1507,14 +1514,17 @@ proc copyFileWithPermissions*(source, dest: string, ignorePermissionErrors = true) = ## Copies a file from `source` to `dest` preserving file permissions. ## - ## This is a wrapper proc around copyFile, getFilePermissions and - ## setFilePermissions on non Windows platform. On windows this proc is just a - ## wrapper for copyFile since that proc already copies attributes. + ## This is a wrapper proc around `copyFile() <#copyFile>`_, + ## `getFilePermissions() <#getFilePermissions>`_ and `setFilePermissions() + ## <#setFilePermissions>`_ on non Windows platform. On Windows this proc is + ## just a wrapper for `copyFile() <#copyFile>`_ since that proc already + ## copies attributes. ## - ## On non windows systems permissions are copied after the file itself has + ## On non Windows systems permissions are copied after the file itself has ## been copied, which won't happen atomically and could lead to a race - ## condition. If ignorePermissionErrors is true, errors while reading/setting - ## file attributes will be ignored, otherwise will raise `OSError`. + ## condition. If `ignorePermissionErrors` is true, errors while + ## reading/setting file attributes will be ignored, otherwise will raise + ## `OSError`. copyFile(source, dest) when not defined(Windows): try: @@ -1523,6 +1533,37 @@ proc copyFileWithPermissions*(source, dest: string, if not ignorePermissionErrors: raise +proc copyDirWithPermissions*(source, dest: string, + ignorePermissionErrors = true) {.rtl, extern: "nos$1", + tags: [FWriteIO, FReadIO].} = + ## Copies a directory from `source` to `dest` preserving file permissions. + ## + ## If this fails, `EOS` is raised. This is a wrapper proc around `copyDir() + ## <#copyDir>`_ and `copyFileWithPermissions() <#copyFileWithPermissions>`_ + ## on non Windows platforms. On Windows this proc is just a wrapper for + ## `copyDir() <#copyDir>`_ since that proc already copies attributes. + ## + ## On non Windows systems permissions are copied after the file or directory + ## itself has been copied, which won't happen atomically and could lead to a + ## race condition. If `ignorePermissionErrors` is true, errors while + ## reading/setting file attributes will be ignored, otherwise will raise + ## `OSError`. + createDir(dest) + when not defined(Windows): + try: + setFilePermissions(dest, getFilePermissions(source)) + except: + if not ignorePermissionErrors: + raise + for kind, path in walkDir(source): + var noSource = path.substr(source.len()+1) + case kind + of pcFile: + copyFileWithPermissions(path, dest / noSource, ignorePermissionErrors) + of pcDir: + copyDirWithPermissions(path, dest / noSource, ignorePermissionErrors) + else: discard + proc inclFilePermissions*(filename: string, permissions: set[TFilePermission]) {. rtl, extern: "nos$1", tags: [FReadDir, FWriteDir].} = diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index 04a0c2403..c74fa1ceb 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -763,7 +763,7 @@ elif not defined(useNimRtl): discard write(data.pErrorPipe[writeIdx], addr error, sizeof(error)) exitnow(1) - when defined(macosx): + when defined(macosx) or defined(freebsd): var environ {.importc.}: cstringArray proc startProcessAfterFork(data: ptr TStartProcessData) = @@ -793,7 +793,7 @@ elif not defined(useNimRtl): discard fcntl(data.pErrorPipe[writeIdx], F_SETFD, FD_CLOEXEC) if data.optionPoUsePath: - when defined(macosx): + when defined(macosx) or defined(freebsd): # MacOSX doesn't have execvpe, so we need workaround. # On MacOSX we can arrive here only from fork, so this is safe: environ = data.sysEnv |