intsets.nim: Add toIntSet proc (#15460)
Similar to: - `critbits.toCritBitTree` - `deques.toDeque` - `sets.toHashSet` - `tables.toTable`
This commit is contained in:
parent
7ef22bf912
commit
d48b356e49
2 changed files with 30 additions and 3 deletions
|
|
@ -201,6 +201,11 @@
|
||||||
|
|
||||||
- Add `readLines(p: Process)` to `osproc` module for `startProcess` convenience.
|
- Add `readLines(p: Process)` to `osproc` module for `startProcess` convenience.
|
||||||
|
|
||||||
|
- Added `intsets.toIntSet`, which creates an IntSet from an openArray. The usage
|
||||||
|
is similar to procs such as `sets.toHashSet` and `tables.toTable`. Previously,
|
||||||
|
it was necessary to create an empty IntSet and add items manually.
|
||||||
|
|
||||||
|
|
||||||
## Language changes
|
## Language changes
|
||||||
|
|
||||||
- The `=destroy` hook no longer has to reset its target, as the compiler now automatically inserts
|
- The `=destroy` hook no longer has to reset its target, as the compiler now automatically inserts
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,8 @@
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `sets module <sets.html>`_ for more general hash sets
|
## * `sets module <sets.html>`_ for more general hash sets
|
||||||
|
|
||||||
|
import std/private/since
|
||||||
import
|
import hashes
|
||||||
hashes
|
|
||||||
|
|
||||||
type
|
type
|
||||||
BitScalar = uint
|
BitScalar = uint
|
||||||
|
|
@ -161,6 +160,9 @@ iterator items*(s: IntSet): int {.inline.} =
|
||||||
|
|
||||||
proc initIntSet*: IntSet =
|
proc initIntSet*: IntSet =
|
||||||
## Returns an empty IntSet.
|
## Returns an empty IntSet.
|
||||||
|
##
|
||||||
|
## See also:
|
||||||
|
## * `toIntSet proc <#toIntSet,openArray[int]>`_
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
var a = initIntSet()
|
var a = initIntSet()
|
||||||
assert len(a) == 0
|
assert len(a) == 0
|
||||||
|
|
@ -251,6 +253,24 @@ proc incl*(s: var IntSet, other: IntSet) =
|
||||||
|
|
||||||
for item in other: incl(s, item)
|
for item in other: incl(s, item)
|
||||||
|
|
||||||
|
proc toIntSet*(x: openArray[int]): IntSet {.since: (1, 3).} =
|
||||||
|
## Creates a new IntSet that contains the elements of `x`.
|
||||||
|
##
|
||||||
|
## Duplicates are removed.
|
||||||
|
##
|
||||||
|
## See also:
|
||||||
|
## * `initIntSet proc <#initIntSet>`_
|
||||||
|
runnableExamples:
|
||||||
|
var
|
||||||
|
a = toIntSet([5, 6, 7])
|
||||||
|
b = toIntSet(@[1, 8, 8, 8])
|
||||||
|
assert len(a) == 3
|
||||||
|
assert len(b) == 2
|
||||||
|
|
||||||
|
result = initIntSet()
|
||||||
|
for item in items(x):
|
||||||
|
result.incl(item)
|
||||||
|
|
||||||
proc containsOrIncl*(s: var IntSet, key: int): bool =
|
proc containsOrIncl*(s: var IntSet, key: int): bool =
|
||||||
## Includes `key` in the set `s` and tells if `key` was already in `s`.
|
## Includes `key` in the set `s` and tells if `key` was already in `s`.
|
||||||
##
|
##
|
||||||
|
|
@ -585,6 +605,8 @@ when isMainModule:
|
||||||
x.incl(1044)
|
x.incl(1044)
|
||||||
x.excl(1044)
|
x.excl(1044)
|
||||||
|
|
||||||
|
assert x == [1, 2, 7, 1056].toIntSet
|
||||||
|
|
||||||
assert x.containsOrIncl(888) == false
|
assert x.containsOrIncl(888) == false
|
||||||
assert 888 in x
|
assert 888 in x
|
||||||
assert x.containsOrIncl(888) == true
|
assert x.containsOrIncl(888) == true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue