-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsets_basics.nim
More file actions
40 lines (30 loc) · 1.04 KB
/
sets_basics.nim
File metadata and controls
40 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Sets and hash sets
# ==================
#
# Nim has two kinds of sets:
# - `set[T]` — built-in bitsets for small ordinal types (char, enum, int8..int16)
# - `HashSet[T]` — hash-based sets for any hashable type
import std/[syncio, assertions, strutils, hashes, sets]
# --- Built-in bitsets ---
# `set[char]` is common for character classification:
const vowels = {'a', 'e', 'i', 'o', 'u'}
assert 'a' in vowels
assert 'b' notin vowels
# Set operations:
const consonants = Letters - vowels - UppercaseLetters
assert 'b' in consonants
assert 'a' notin consonants
# `contains` is the proc behind `in`:
assert vowels.contains('e')
# --- HashSet for larger types ---
var seen = initHashSet[string]()
seen.incl "apple"
seen.incl "banana"
seen.incl "apple" # duplicate, no effect
assert "apple" in seen
assert "cherry" notin seen
# `containsOrIncl` is useful for deduplication loops:
assert seen.containsOrIncl("banana") == true # was already there
assert seen.containsOrIncl("cherry") == false # newly added
assert "cherry" in seen
echo "sets: OK"