plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
fd0d134
— Peter John
2026-11-10T23:17:04+05:30
std: membership is contains on every collection (Map, Set and the std/collections trees drop has)
- docs/std.md +3 -3
- examples/games/game_loop.plum +1 -1
- std/collections/BPlusTree.plum +12 -12
- std/collections/BTree.plum +9 -9
- std/collections/Bvh.plum +2 -2
- std/collections/Graph.plum +1 -1
- std/collections/Hamt.plum +11 -11
- std/collections/IntervalTree.plum +12 -12
- std/collections/Map.plum +16 -16
- std/collections/Quadtree.plum +17 -17
- std/collections/RadixTree.plum +8 -8
- std/collections/Set.plum +8 -8
- std/collections/Trie.plum +11 -11
- std/game/SceneTree.plum +1 -1
- tests/std/map_model.plum +6 -6
docs/std.md
CHANGED
|
@@ -96,7 +96,7 @@ fun main() =
|
|
|
96
96
|
- **Change:** `push`, `pop`, `set`, `add`, `remove`, `removeAt`, `clear`, `reverse`, `shuffle`
|
|
97
97
|
- **Other:** `sort(before)` (a new slice; a stable merge sort, O(n log n)), `slice` (a view that shares storage), `sublist` (a copy), `chunk`, `join`, `drop`, `==` (the same elements in order; a view equals a copy), and with `std/collections/Map` imported `groupBy(key)` (a `Map` from each key to its elements, in order; the groups come in key order)
|
|
98
98
|
|
|
99
|
-
Renamed or removed: `Slice.head` (use `first`), `Set.toList` is now `toSlice`.
|
|
99
|
+
Renamed or removed: `Slice.head` (use `first`), `Set.toList` is now `toSlice`, and membership is `contains` on every collection (`Map.has`, `Set.has` and the `has` of the trees in `std/collections` are gone).
|
|
100
100
|
|
|
101
101
|
**`Array`** has a fixed size: `Array.filled(n, value)`, `get`, `set`, `length`. **`Grid`** is a 2D array: `Grid.filled(w, h, value)`, `get(x, y)`, `set(x, y, value)` (both crash with the cell and the size off the grid; `inBounds(x, y)` tells first).
|
|
102
102
|
|
|
@@ -121,7 +121,7 @@ fun main() =
|
|
|
121
121
|
- **Ordered keys.** Keys are compared with their `compare(self, o) -> Int` method (negative before, 0 the same key, positive after). `Int`, `Float`, `Str`, `Bool` and `Byte` have one (see [Numbers](#numbers) for `Float`'s total order); a struct becomes a key by defining one. A key type without it is a compile error that names the type and the call that needed it: `Map.with<Pt, Int> (called at main.plum:8:8): Pt has no method 'compare'`. Keys need no `hash` any more.
|
|
122
122
|
- **Key order everywhere.** `for e := range m`, `keys`, `values`, `entries`, `fold`, `each`, `toStr` and `==` go in ascending key order, which depends only on the keys, never on the order they were added.
|
|
123
123
|
- **Build:** `Map.empty()`, `Map<Str, Int>()`, `Map.single(k, v)`, a literal `[k => v, ..]` (it is `Map.empty().with(k, v)...`, so O(n log n), and a later duplicate key wins), `Map.fromSlice(entries)` and `Map.fromList(entries)` (O(n) when the keys already strictly ascend, else O(n log n); a later duplicate wins).
|
|
124
|
-
- **Read, O(log n):** `get(k)` (an `Option`), `getOr(k, fallback)` (no `Option` made), `
|
|
124
|
+
- **Read, O(log n):** `get(k)` (an `Option`), `getOr(k, fallback)` (no `Option` made), `contains(k)`; O(1): `length`, `isEmpty`.
|
|
125
125
|
- **Update, O(log n), each returning the new map:** `with(k, v)` (also called `set`), `remove(k)`, `putIfAbsent(k, v)`, `upsert(k, v, f)` (`f` of the old value, or `v` when absent: Data.Map's `insertWith`), `update(k, f)` (only when present: `adjust`), `alter(k, f)` (`f` takes the value or `None` and returns the new one or `None` to leave `k` out).
|
|
126
126
|
- **Order, O(log n):** `min`, `max` (the entry with the smallest or largest key, an `Option<Pair<K, V>>`), `removeMin`, `removeMax`, the neighbours of a key `below(k)` (largest key `< k`), `atMost(k)` (`<=`), `above(k)` (`>`), `atLeast(k)` (`>=`), `entryAt(i)` (the `i`-th entry from 0), `indexOf(k)` (an `Option<Int>`), `rank(k)` (how many keys come before `k`, present or not), `height`.
|
|
127
127
|
- **Ranges, O(log n), sharing nodes with the map:** `from(lo)` (keys `>= lo`), `until(hi)` (keys `< hi`), `range(lo, hi)` (`lo <= key < hi`), `split(k)` (the maps of keys before and after `k`, as a `Pair`), and its inverse `a.join(b)` (every key of `a` must be before every key of `b`, or it crashes). `foldRange(lo, hi, acc, f)` folds over `lo <= key < hi` without building a map.
|
|
@@ -130,7 +130,7 @@ fun main() =
|
|
|
130
130
|
|
|
131
131
|
Gone or changed from the old hash table: `set`, `remove` and `putIfAbsent` return the new map instead of changing the receiver (`with` no longer changes it either); `clear` is gone (use `Map.empty()`); keys need `compare` instead of `hash`; iteration is key order, not insertion order; `Map.toJson` writes object fields in key order. Against the hash table, `Map<Int, Int>` gets and sets are about 3x slower (`benchmarks/master_vs_simpler/branch/map`): every insert allocates the O(log n) nodes on its path, and the compiler's cell reuse does not yet reach a generic method, so those nodes are not updated in place even when the map is unique.
|
|
132
132
|
|
|
133
|
-
**`Set<T>`** is a persistent ordered set, the keys of a `Map<T, Bool>`, with the same costs and the same `compare` on its members. `Set<Int>()` or `Set.empty()`, `Set.fromSlice`, `Set.fromList`; `add`, `remove` (each returns the new set), `
|
|
133
|
+
**`Set<T>`** is a persistent ordered set, the keys of a `Map<T, Bool>`, with the same costs and the same `compare` on its members. `Set<Int>()` or `Set.empty()`, `Set.fromSlice`, `Set.fromList`; `add`, `remove` (each returns the new set), `contains`, `length`, `isEmpty`, `min`, `max`, `removeMin`, `removeMax`, `below`, `atMost`, `above`, `atLeast`, `at(i)`, `indexOf`, `rank`, `from`, `until`, `range`, `foldRange`, `split`, `join`, `union`, `intersection`, `difference`, `isSubsetOf`, `fold`, `each`, `filter`, `any`, `all`, `toSlice` (ascending), `for x := range s`, `==`, `toStr` (`[1, 2, 3]`). `clear` is gone.
|
|
134
134
|
|
|
135
135
|
- **`Deque`:** a queue you can use from both ends: `pushBack`, `pushFront`, `popBack`, `popFront`, `get`, `at` (crashes out of range)
|
|
136
136
|
- **`Heap`:** gives back first the item nothing comes `before`: `h: Heap<Int> := Heap.by(|a, b| a < b)`, or `Heap.byKey(|n| n.cost)` by a key of any type with `<`; `push`, `pop`, `peek`
|
examples/games/game_loop.plum
CHANGED
|
@@ -90,5 +90,5 @@ test "events change the entity map"
|
|
|
90
90
|
assert world.get(2)!.name == "elf"
|
|
91
91
|
after := handle(world, Despawn(id: 2))
|
|
92
92
|
assert after.length() == 1
|
|
93
|
-
assert !after.
|
|
93
|
+
assert !after.contains(2)
|
|
94
94
|
assert world.length() == 2
|
std/collections/BPlusTree.plum
CHANGED
|
@@ -35,7 +35,7 @@ import std/Slice
|
|
|
35
35
|
# level above it, the children still to visit) whose `next()` gives the entry and the
|
|
36
36
|
# cursor after it. `fromSorted` bulk-loads a sorted `List` in O(n).
|
|
37
37
|
#
|
|
38
|
-
# `get`/`
|
|
38
|
+
# `get`/`contains`/`insert`/`remove` are O(order * log_order n); `range` is
|
|
39
39
|
# O(order * log_order n + m) for m results; `length` is O(1). Stack: the tree recursion
|
|
40
40
|
# is O(log_order n) deep; every list function, `fromSorted` and the cursor run by tail
|
|
41
41
|
# calls and `List` loops, in constant stack.
|
|
@@ -79,7 +79,7 @@ struct BPlusTree<K, V>(root: BpNode<K, V>, size: Int, order: Int, before: fn(K,
|
|
|
79
79
|
fun get(self, k: K) -> Option<V> =
|
|
80
80
|
self.root.get(k, self.before)
|
|
81
81
|
|
|
82
|
-
fun
|
|
82
|
+
fun contains(self, k: K) -> Bool =
|
|
83
83
|
self.get(k).isSome()
|
|
84
84
|
|
|
85
85
|
# A tree with `k` mapped to `v`, replacing the value of an equal key (which keeps
|
|
@@ -624,7 +624,7 @@ test "an empty tree has nothing in it"
|
|
|
624
624
|
assert t.isEmpty()
|
|
625
625
|
assert t.length() == 0
|
|
626
626
|
assert t.get(1).isNone()
|
|
627
|
-
assert !t.
|
|
627
|
+
assert !t.contains(1)
|
|
628
628
|
assert t.min().isNone()
|
|
629
629
|
assert t.max().isNone()
|
|
630
630
|
assert t.keys().length() == 0
|
|
@@ -641,7 +641,7 @@ test "insert, get and has; inserting an existing key replaces its value"
|
|
|
641
641
|
t := empty.insert(5, "five").insert(1, "one").insert(3, "three")
|
|
642
642
|
assert t.length() == 3
|
|
643
643
|
assert t.get(3)! == "three"
|
|
644
|
-
assert t.
|
|
644
|
+
assert t.contains(1) && t.contains(5) && !t.contains(2)
|
|
645
645
|
assert t.get(4).isNone()
|
|
646
646
|
u := t.insert(3, "THREE")
|
|
647
647
|
assert u.length() == 3
|
|
@@ -691,8 +691,8 @@ test "remove drops a key; removing a missing key changes nothing"
|
|
|
691
691
|
assert u.length() == 40
|
|
692
692
|
v := t.remove(17)
|
|
693
693
|
assert v.length() == 39
|
|
694
|
-
assert !v.
|
|
694
|
+
assert !v.contains(17)
|
|
695
|
-
assert v.
|
|
695
|
+
assert v.contains(16) && v.contains(18)
|
|
696
696
|
assert bpValid(v)
|
|
697
697
|
w := 0.until(40).fold(v, |acc, i|
|
|
698
698
|
next := acc.remove(i)
|
|
@@ -713,9 +713,9 @@ test "old versions are unchanged after insert and remove"
|
|
|
713
713
|
t := bpUpTo(3, 30)
|
|
714
714
|
t2 := t.insert(100, 100).insert(5, -5)
|
|
715
715
|
t3 := t2.remove(0).remove(1).remove(2).remove(3)
|
|
716
|
-
assert t.length() == 30 && !t.
|
|
716
|
+
assert t.length() == 30 && !t.contains(100) && t.get(5)! == 5 && t.contains(0)
|
|
717
|
-
assert t2.length() == 31 && t2.get(5)! == -5 && t2.
|
|
717
|
+
assert t2.length() == 31 && t2.get(5)! == -5 && t2.contains(0)
|
|
718
|
-
assert t3.length() == 27 && !t3.
|
|
718
|
+
assert t3.length() == 27 && !t3.contains(0) && t3.contains(100)
|
|
719
719
|
assert bpValid(t) && bpValid(t2) && bpValid(t3)
|
|
720
720
|
assert t.keys().length() == 30
|
|
721
721
|
|
|
@@ -732,7 +732,7 @@ test "range is half-open and matches a filtered sorted list"
|
|
|
732
732
|
all := Slice<Int>()
|
|
733
733
|
for i := range 300
|
|
734
734
|
k := run.roll() % 1000
|
|
735
|
-
if !run.tree.
|
|
735
|
+
if !run.tree.contains(k)
|
|
736
736
|
all.push(k)
|
|
737
737
|
run.tree = run.tree.insert(k, k + 1)
|
|
738
738
|
t := run.tree
|
|
@@ -787,7 +787,7 @@ test "fromSorted builds a valid tree for every size and several orders"
|
|
|
787
787
|
assert t.min()!.first == 0
|
|
788
788
|
assert t.max()!.first == 2 * {n - 1}
|
|
789
789
|
assert t.get(2 * {n / 2})! == n / 2
|
|
790
|
-
assert !t.
|
|
790
|
+
assert !t.contains(1)
|
|
791
791
|
full: BPlusTree<Int, Int> := BPlusTree.fromSorted(bpSorted(200), |a, b| a < b, 4)
|
|
792
792
|
drained := 0.until(200).fold(full, |acc, i|
|
|
793
793
|
next := acc.remove(i * 2)
|
|
@@ -821,7 +821,7 @@ test "20,000 entries at the default order"
|
|
|
821
821
|
half := run.tree
|
|
822
822
|
assert half.length() == n / 2
|
|
823
823
|
assert bpValid(half)
|
|
824
|
-
assert !half.
|
|
824
|
+
assert !half.contains(ks.get(0)) && half.contains(ks.get(1))
|
|
825
825
|
b: BPlusTree<Int, Int> := BPlusTree.fromSorted(bpSorted(50000), |a, b| a < b)
|
|
826
826
|
assert b.length() == 50000 && bpValid(b)
|
|
827
827
|
assert b.range(1000, 1100).length() == 50
|
std/collections/BTree.plum
CHANGED
|
@@ -29,7 +29,7 @@ import std/collections/List
|
|
|
29
29
|
# the way down and shrinks an empty root, so both make one root-to-leaf pass; only
|
|
30
30
|
# the nodes on that path are rebuilt and every other node is shared with the old tree.
|
|
31
31
|
#
|
|
32
|
-
# `get`/`
|
|
32
|
+
# `get`/`contains`/`insert`/`remove` are O(t log_t n) (a linear scan per node where the twin
|
|
33
33
|
# did a binary search; the twin copied O(t) per node anyway). `length` is O(1),
|
|
34
34
|
# `entries` O(n). Stack: the tree recursion is O(log_t n) deep (a node's descent, the
|
|
35
35
|
# invariant checker, `entries`, `shape`); every list function walks a node's lists by
|
|
@@ -59,14 +59,14 @@ struct BTree<K, V>(root: BtNode<K, V>, size: Int, t: Int, before: fn(K, K) -> Bo
|
|
|
59
59
|
fun get(self, k: K) -> Option<V> =
|
|
60
60
|
self.root.get(k, self.before)
|
|
61
61
|
|
|
62
|
-
fun
|
|
62
|
+
fun contains(self, k: K) -> Bool =
|
|
63
63
|
self.get(k).isSome()
|
|
64
64
|
|
|
65
65
|
# A tree with `k` set to `v`, replacing the value of an existing `k` (which keeps
|
|
66
66
|
# its original key).
|
|
67
67
|
fun insert(self, k: K, v: V) -> BTree<K, V> =
|
|
68
68
|
root := self.insertInto(self.splitRoot(self.root), k, v)
|
|
69
|
-
size := if self.
|
|
69
|
+
size := if self.contains(k) then self.size else self.size + 1
|
|
70
70
|
BTree(root: root, size: size, t: self.t, before: self.before)
|
|
71
71
|
|
|
72
72
|
# `n`, or when it is full, a new root above it with `n` split in two.
|
|
@@ -108,7 +108,7 @@ struct BTree<K, V>(root: BtNode<K, V>, size: Int, t: Int, before: fn(K, K) -> Bo
|
|
|
108
108
|
|
|
109
109
|
# A tree without `k`; the same tree when `k` is absent.
|
|
110
110
|
fun remove(self, k: K) -> BTree<K, V> =
|
|
111
|
-
if !self.
|
|
111
|
+
if !self.contains(k)
|
|
112
112
|
self
|
|
113
113
|
else
|
|
114
114
|
BTree(root: self.removeFrom(self.root, k).shrink(), size: self.size - 1, t: self.t, before: self.before)
|
|
@@ -508,7 +508,7 @@ test "empty tree"
|
|
|
508
508
|
assert tree.isEmpty()
|
|
509
509
|
assert tree.height() == 0
|
|
510
510
|
assert tree.get(1).isNone()
|
|
511
|
-
assert !tree.
|
|
511
|
+
assert !tree.contains(1)
|
|
512
512
|
assert tree.min().isNone()
|
|
513
513
|
assert tree.max().isNone()
|
|
514
514
|
assert tree.keys().isEmpty()
|
|
@@ -532,7 +532,7 @@ test "insert, get and has; insert replaces an existing value"
|
|
|
532
532
|
tree := empty.insert(2, "b").insert(1, "a").insert(3, "c")
|
|
533
533
|
assert tree.length() == 3
|
|
534
534
|
assert tree.get(2).unwrapOr("") == "b"
|
|
535
|
-
assert tree.
|
|
535
|
+
assert tree.contains(1) && tree.contains(3) && !tree.contains(4)
|
|
536
536
|
t2 := tree.insert(2, "B")
|
|
537
537
|
assert t2.length() == 3
|
|
538
538
|
assert t2.get(2).unwrapOr("") == "B"
|
|
@@ -546,7 +546,7 @@ test "a custom comparator orders Float keys descending; min and max"
|
|
|
546
546
|
assert tree.min()!.first == 9.0
|
|
547
547
|
assert tree.max()!.first == -1.0
|
|
548
548
|
assert tree.get(0.5).unwrapOr("") == "0.5"
|
|
549
|
-
assert !tree.
|
|
549
|
+
assert !tree.contains(1.0)
|
|
550
550
|
assert tree.isValid()
|
|
551
551
|
|
|
552
552
|
test "a 2-3-4 tree splits full nodes on the way down"
|
|
@@ -604,7 +604,7 @@ test "old versions are unchanged after insert and remove"
|
|
|
604
604
|
assert a.length() == 200 && b.length() == 201 && c.length() == 198
|
|
605
605
|
assert a.get(50).unwrapOr(0) == 500
|
|
606
606
|
assert b.get(50).unwrapOr(0) == -1
|
|
607
|
-
assert a.
|
|
607
|
+
assert a.contains(100) && !c.contains(100) && !a.contains(1000) && b.contains(1000)
|
|
608
608
|
assert a.isValid() && b.isValid() && c.isValid()
|
|
609
609
|
|
|
610
610
|
test "pseudo-random inserts and removes keep every invariant (t = 2 and t = 3)"
|
|
@@ -634,7 +634,7 @@ test "pseudo-random inserts and removes keep every invariant (t = 2 and t = 3)"
|
|
|
634
634
|
assert btValid(tree)
|
|
635
635
|
assert tree.length() == run.count
|
|
636
636
|
for k := range 500
|
|
637
|
-
assert tree.
|
|
637
|
+
assert tree.contains(k) == present.get(k)
|
|
638
638
|
if present.get(k)
|
|
639
639
|
assert tree.get(k)! == value.get(k)
|
|
640
640
|
assert btAscending(tree.keys())
|
std/collections/Bvh.plum
CHANGED
|
@@ -343,7 +343,7 @@ fun bvhCheckPairs(items: Slice<Pair<Aabb, Int>>, tree: Bvh<Int>) -> Unit =
|
|
|
343
343
|
a := p.first.min(p.second)
|
|
344
344
|
b := p.first.max(p.second)
|
|
345
345
|
key := a * n + b
|
|
346
|
-
assert !seen.s.
|
|
346
|
+
assert !seen.s.contains(key)
|
|
347
347
|
seen.s = seen.s.add(key)
|
|
348
348
|
got := seen.s
|
|
349
349
|
count := BvhTally(n: 0)
|
|
@@ -351,7 +351,7 @@ fun bvhCheckPairs(items: Slice<Pair<Aabb, Int>>, tree: Bvh<Int>) -> Unit =
|
|
|
351
351
|
for j := range {i + 1}.until(n)
|
|
352
352
|
if items.at(i).first.overlaps(items.at(j).first)
|
|
353
353
|
count.n = count.n + 1
|
|
354
|
-
assert got.
|
|
354
|
+
assert got.contains(i * n + j)
|
|
355
355
|
assert got.length() == count.n
|
|
356
356
|
|
|
357
357
|
test "an empty Bvh finds nothing"
|
std/collections/Graph.plum
CHANGED
|
@@ -85,7 +85,7 @@ enum Graph =
|
|
|
85
85
|
fun walk(self, stack: List<Int>, seen: Set<Int>, order: List<Int>) -> List<Int> = match stack
|
|
86
86
|
Nil => order.reverse()
|
|
87
87
|
Cons(v, rest) =>
|
|
88
|
-
if seen.
|
|
88
|
+
if seen.contains(v)
|
|
89
89
|
self.walk(rest, seen, order)
|
|
90
90
|
else
|
|
91
91
|
self.walk(self.neighbours(v).append(rest), seen.add(v), Cons(head: v, tail: order))
|
std/collections/Hamt.plum
CHANGED
|
@@ -18,10 +18,10 @@ import std/collections/Map
|
|
|
18
18
|
# walking the list. Collision buckets (keys with equal full hashes) are cons lists of
|
|
19
19
|
# `Pair(key, value)` in insertion order instead of two parallel arrays. The twin's `while`
|
|
20
20
|
# loops (lookup, collecting entries, the popcount) are recursion or single bindings here.
|
|
21
|
-
# The API is `Hamt`'s: `empty`/`init`, `get`, `
|
|
21
|
+
# The API is `Hamt`'s: `empty`/`init`, `get`, `contains`, `set`, `remove`, `length`, `isEmpty`,
|
|
22
22
|
# `entries`, `keys`, `values`, `toStr`, except that sequences come back as `List`.
|
|
23
23
|
#
|
|
24
|
-
# `get`/`
|
|
24
|
+
# `get`/`contains`/`set`/`remove` are O(log32 n) levels, each O(32) to walk a child list (the
|
|
25
25
|
# twin's is O(1) per level to index, O(32) to copy). Every update rebuilds the branches on
|
|
26
26
|
# the path and the child-list cells in front of the changed slot, sharing everything else;
|
|
27
27
|
# the old map is unchanged. A remove that leaves a non-root branch with one leaf or
|
|
@@ -53,7 +53,7 @@ struct Hamt<K, V>(root: HamtNode<K, V>, size: Int) =
|
|
|
53
53
|
fun get(self, k: K) -> Option<V> =
|
|
54
54
|
self.root.find(0, self.hashOf(k), k)
|
|
55
55
|
|
|
56
|
-
fun
|
|
56
|
+
fun contains(self, k: K) -> Bool =
|
|
57
57
|
self.get(k).isSome()
|
|
58
58
|
|
|
59
59
|
# A map with `k` set to `v` (replacing `k`'s old value, if any).
|
|
@@ -404,7 +404,7 @@ test "empty map"
|
|
|
404
404
|
assert m.isEmpty()
|
|
405
405
|
assert m.length() == 0
|
|
406
406
|
assert m.get("a").isNone()
|
|
407
|
-
assert m.
|
|
407
|
+
assert m.contains("a") == false
|
|
408
408
|
assert m.toStr() == "[]"
|
|
409
409
|
assert m.keys().length() == 0
|
|
410
410
|
assert m.entries().isEmpty()
|
|
@@ -433,8 +433,8 @@ test "set, get, has, and set replaces a key's value"
|
|
|
433
433
|
assert m.length() == 2
|
|
434
434
|
assert m.get("a")! == 1
|
|
435
435
|
assert m.get("b")! == 2
|
|
436
|
-
assert m.
|
|
436
|
+
assert m.contains("b")
|
|
437
|
-
assert m.
|
|
437
|
+
assert m.contains("c") == false
|
|
438
438
|
m2 := m.set("a", 9)
|
|
439
439
|
assert m2.length() == 2
|
|
440
440
|
assert m2.get("a")! == 9
|
|
@@ -445,7 +445,7 @@ test "remove drops a key; a missing key leaves the map as it was"
|
|
|
445
445
|
m := m0.set("a", 1).set("b", 2).set("c", 3)
|
|
446
446
|
r := m.remove("b")
|
|
447
447
|
assert r.length() == 2
|
|
448
|
-
assert r.
|
|
448
|
+
assert r.contains("b") == false
|
|
449
449
|
assert r.get("a")! == 1
|
|
450
450
|
assert r.get("c")! == 3
|
|
451
451
|
assert r.remove("zzz").length() == 2
|
|
@@ -460,11 +460,11 @@ test "persistence: old versions are unchanged after set and remove"
|
|
|
460
460
|
c := a.remove(7)
|
|
461
461
|
assert a.length() == 100
|
|
462
462
|
assert a.get(5)! == 5
|
|
463
|
-
assert a.
|
|
463
|
+
assert a.contains(1000) == false
|
|
464
464
|
assert a.get(7)! == 7
|
|
465
465
|
assert b.get(5)! == 500
|
|
466
466
|
assert b.length() == 101
|
|
467
|
-
assert c.
|
|
467
|
+
assert c.contains(7) == false
|
|
468
468
|
assert c.get(5)! == 5
|
|
469
469
|
assert c.length() == 99
|
|
470
470
|
assert hamtValid(a) && hamtValid(b) && hamtValid(c)
|
|
@@ -491,7 +491,7 @@ test "forced collisions: keys with a bad hash"
|
|
|
491
491
|
assert hamtValid(m)
|
|
492
492
|
for i := range 40
|
|
493
493
|
assert m.get(HamtBadKey(id: i))! == i
|
|
494
|
-
assert m.
|
|
494
|
+
assert m.contains(HamtBadKey(id: 40)) == false
|
|
495
495
|
m2 := m.set(HamtBadKey(id: 3), 333)
|
|
496
496
|
assert m2.get(HamtBadKey(id: 3))! == 333
|
|
497
497
|
assert m2.length() == 40
|
|
@@ -605,7 +605,7 @@ test "20,000 Str keys, then remove them all"
|
|
|
605
605
|
assert h.entries().length() == 20000
|
|
606
606
|
bad := 0.until(20000).fold(0, |n, i| if h.get("key{i}")! != i then n + 1 else n)
|
|
607
607
|
assert bad == 0
|
|
608
|
-
assert h.
|
|
608
|
+
assert h.contains("key20000") == false
|
|
609
609
|
gone := 0.until(20000).fold(h, |acc, i| acc.remove("key{i}"))
|
|
610
610
|
assert gone.isEmpty()
|
|
611
611
|
assert hamtValid(gone)
|
std/collections/IntervalTree.plum
CHANGED
|
@@ -16,15 +16,15 @@ import std/Slice
|
|
|
16
16
|
# the rest with the old one, which stays as it was.
|
|
17
17
|
#
|
|
18
18
|
# Same API as the twin, except that `overlapping`, `containing` and `entries` return a
|
|
19
|
-
# `List<IvEntry<V>>` instead of a `Slice`. The twin's `while` loops (`
|
|
19
|
+
# `List<IvEntry<V>>` instead of a `Slice`. The twin's `while` loops (`contains`, `first`)
|
|
20
20
|
# are tail calls here, its reassigned locals in `join` are `Int.max`, and its queries,
|
|
21
21
|
# which pushed onto a `Slice`, cons each hit onto an accumulator argument: the right
|
|
22
22
|
# subtree is walked first, so the hits come out ordered by (lo, hi) with no reverse.
|
|
23
23
|
# `toStr` joins the entries with `List.join`.
|
|
24
24
|
#
|
|
25
|
-
# `insert`/`remove`/`
|
|
25
|
+
# `insert`/`remove`/`contains` are O(log n); `overlapping` and `containing` are
|
|
26
26
|
# O(log n + k log n) at worst for k results; `length` is O(1). Every recursion follows
|
|
27
|
-
# the balanced tree (`
|
|
27
|
+
# the balanced tree (`contains` and `first` as tail calls), so the stack depth is O(log n).
|
|
28
28
|
struct IntervalTree<V>(root: IvBranch<V>, size: Int) =
|
|
29
29
|
|
|
30
30
|
# The empty tree (`t: IntervalTree<Str> := IntervalTree.empty()`).
|
|
@@ -54,12 +54,12 @@ struct IntervalTree<V>(root: IvBranch<V>, size: Int) =
|
|
|
54
54
|
IntervalTree(root: self.root.insert(lo, hi, v), size: self.size + 1)
|
|
55
55
|
|
|
56
56
|
# Whether `[lo, hi]` itself is stored (not just an overlapping interval).
|
|
57
|
-
fun
|
|
57
|
+
fun contains(self, lo: Int, hi: Int) -> Bool =
|
|
58
|
-
self.root.
|
|
58
|
+
self.root.contains(lo, hi)
|
|
59
59
|
|
|
60
60
|
# A tree with one stored `[lo, hi]` taken out; the same tree when there is none.
|
|
61
61
|
fun remove(self, lo: Int, hi: Int) -> IntervalTree<V> =
|
|
62
|
-
if !self.
|
|
62
|
+
if !self.contains(lo, hi)
|
|
63
63
|
self
|
|
64
64
|
else
|
|
65
65
|
IntervalTree(root: self.root.remove(lo, hi), size: self.size - 1)
|
|
@@ -118,16 +118,16 @@ enum IvBranch<V> =
|
|
|
118
118
|
IvNode(_, _, _, _, _, l, r) => l.height() - r.height()
|
|
119
119
|
|
|
120
120
|
# Tail recursive: one comparison per level.
|
|
121
|
-
fun
|
|
121
|
+
fun contains(self, lo: Int, hi: Int) -> Bool = match self
|
|
122
122
|
IvLeaf => false
|
|
123
123
|
IvNode(nlo, nhi, _, _, _, l, r) =>
|
|
124
124
|
c := ivCompare(lo, hi, nlo, nhi)
|
|
125
125
|
if c == 0
|
|
126
126
|
true
|
|
127
127
|
else if c < 0
|
|
128
|
-
l.
|
|
128
|
+
l.contains(lo, hi)
|
|
129
129
|
else
|
|
130
|
-
r.
|
|
130
|
+
r.contains(lo, hi)
|
|
131
131
|
|
|
132
132
|
# A node over `l` and `r` with its height and `maxHi` computed (no rebalancing).
|
|
133
133
|
fun join(lo: Int, hi: Int, v: V, l: IvBranch<V>, r: IvBranch<V>) -> IvBranch<V> =
|
|
@@ -312,7 +312,7 @@ test "an empty tree"
|
|
|
312
312
|
assert t.height() == 0
|
|
313
313
|
assert t.overlapping(0, 10).length() == 0
|
|
314
314
|
assert t.containing(3).length() == 0
|
|
315
|
-
assert t.
|
|
315
|
+
assert t.contains(1, 2) == false
|
|
316
316
|
assert t.remove(1, 2).isEmpty()
|
|
317
317
|
assert t.toStr() == "[]"
|
|
318
318
|
e: IntervalTree<Str> := IntervalTree.empty()
|
|
@@ -325,8 +325,8 @@ test "insert keeps intervals ordered by lo then hi, and toStr shows them"
|
|
|
325
325
|
u := t.insert(5, 9, "c").insert(1, 4, "a").insert(1, 2, "b").insert(7, 7, "d")
|
|
326
326
|
assert u.length() == 4
|
|
327
327
|
assert u.toStr() == "[[1, 2]: b, [1, 4]: a, [5, 9]: c, [7, 7]: d]"
|
|
328
|
-
assert u.
|
|
328
|
+
assert u.contains(1, 4)
|
|
329
|
-
assert u.
|
|
329
|
+
assert u.contains(1, 3) == false
|
|
330
330
|
|
|
331
331
|
test "overlapping uses closed ends"
|
|
332
332
|
t := ivOf([1, 3, 5, 8, 10, 10, 2, 6])
|
std/collections/Map.plum
CHANGED
|
@@ -29,7 +29,7 @@ import std/collections/List
|
|
|
29
29
|
# the same key, and every NaN is one key, after `Infinity`. Iteration, `keys`, `values`,
|
|
30
30
|
# `entries`, `fold`, `each` and `toStr` all go in ascending key order.
|
|
31
31
|
#
|
|
32
|
-
# Costs: `get`, `
|
|
32
|
+
# Costs: `get`, `contains`, `with`, `remove`, `update`, `min`, `max`, the neighbour lookups,
|
|
33
33
|
# `entryAt`, `indexOf` and `rank` are O(log n); `length` and `isEmpty` O(1); `keys`, `values`,
|
|
34
34
|
# `entries`, `fold`, `each`, `map`, `filter`, `==` and `toStr` O(n); `split`, `join`, `from`,
|
|
35
35
|
# `until` and `range` O(log n); `foldRange` O(log n) plus the entries it visits;
|
|
@@ -115,11 +115,11 @@ enum Map<K, V> =
|
|
|
115
115
|
c := k.compare(nk)
|
|
116
116
|
if c < 0 then l.getOr(k, fallback) else if c > 0 then r.getOr(k, fallback) else nv
|
|
117
117
|
|
|
118
|
-
fun
|
|
118
|
+
fun contains(self, k: K) -> Bool = match self
|
|
119
119
|
MapTip => false
|
|
120
120
|
MapBin(_, nk, _, l, r) =>
|
|
121
121
|
c := k.compare(nk)
|
|
122
|
-
if c < 0 then l.
|
|
122
|
+
if c < 0 then l.contains(k) else if c > 0 then r.contains(k) else true
|
|
123
123
|
|
|
124
124
|
# The number of nodes on the longest path from the root (0 when empty).
|
|
125
125
|
fun height(self) -> Int = match self
|
|
@@ -157,7 +157,7 @@ enum Map<K, V> =
|
|
|
157
157
|
|
|
158
158
|
# The map with `k` mapped to `v` if `k` is absent; the same map otherwise.
|
|
159
159
|
fun putIfAbsent(self, k: K, v: V) -> Map<K, V> =
|
|
160
|
-
if self.
|
|
160
|
+
if self.contains(k) then self else self.with(k, v)
|
|
161
161
|
|
|
162
162
|
# The map with `k` mapped to `f` of its value, or to `v` when `k` is absent:
|
|
163
163
|
# `counts.upsert(word, 1, |n| n + 1)` (Data.Map's `insertWith`).
|
|
@@ -732,7 +732,7 @@ struct MapTestDesc(n: Int) =
|
|
|
732
732
|
test "an empty map"
|
|
733
733
|
m: Map<Int, Int> := Map.empty()
|
|
734
734
|
assert m.isEmpty() && m.length() == 0 && m.height() == 0
|
|
735
|
-
assert m.get(1).isNone() && !m.
|
|
735
|
+
assert m.get(1).isNone() && !m.contains(1) && m.getOr(1, 7) == 7
|
|
736
736
|
assert m.min().isNone() && m.max().isNone()
|
|
737
737
|
assert m.removeMin().isEmpty() && m.removeMax().isEmpty()
|
|
738
738
|
assert m.below(1).isNone() && m.atMost(1).isNone() && m.above(1).isNone() && m.atLeast(1).isNone()
|
|
@@ -767,7 +767,7 @@ test "with, get, has, getOr and set"
|
|
|
767
767
|
m := Map<Str, Int>().with("b", 2).with("a", 1).set("c", 3)
|
|
768
768
|
assert m.length() == 3
|
|
769
769
|
assert m.get("a")! == 1 && m.get("c")! == 3 && m.get("z").isNone()
|
|
770
|
-
assert m.
|
|
770
|
+
assert m.contains("b") && !m.contains("bb")
|
|
771
771
|
assert m.getOr("b", 0) == 2 && m.getOr("q", -1) == -1
|
|
772
772
|
r := m.with("a", 10)
|
|
773
773
|
assert r.length() == 3 && r.get("a")! == 10
|
|
@@ -817,10 +817,10 @@ test "putIfAbsent, upsert, update and alter"
|
|
|
817
817
|
test "remove a leaf, an inner node, the root, missing keys and everything"
|
|
818
818
|
t := Map.fromList([50, 30, 70, 20, 40, 60, 80, 10, 35, 45, 65].map(|k| Pair(first: k, second: k * 10)))
|
|
819
819
|
a := t.remove(10)
|
|
820
|
-
assert !a.
|
|
820
|
+
assert !a.contains(10) && a.length() == 10 && mapValid(a)
|
|
821
821
|
b := t.remove(30)
|
|
822
822
|
assert b.keys().toStr() == "[10,20,35,40,45,50,60,65,70,80]" && mapValid(b)
|
|
823
|
-
assert mapValid(t.remove(50)) && !t.remove(50).
|
|
823
|
+
assert mapValid(t.remove(50)) && !t.remove(50).contains(50)
|
|
824
824
|
assert t.remove(99).length() == 11 && t.remove(99) == t
|
|
825
825
|
e := t.keys().fold(t, |acc, k|
|
|
826
826
|
next := acc.remove(k)
|
|
@@ -893,9 +893,9 @@ test "range and foldRange agree with a filter over pseudo-random maps"
|
|
|
893
893
|
hi := {x / 450} % 450 - 25
|
|
894
894
|
r := m.range(lo, hi)
|
|
895
895
|
assert mapValid(r) && mapValid(m.from(lo)) && mapValid(m.until(hi))
|
|
896
|
-
assert r.keys().toStr() == mapKeysWhere(400, |k| m.
|
|
896
|
+
assert r.keys().toStr() == mapKeysWhere(400, |k| m.contains(k) && k >= lo && k < hi)
|
|
897
|
-
assert m.from(lo).keys().toStr() == mapKeysWhere(400, |k| m.
|
|
897
|
+
assert m.from(lo).keys().toStr() == mapKeysWhere(400, |k| m.contains(k) && k >= lo)
|
|
898
|
-
assert m.until(hi).keys().toStr() == mapKeysWhere(400, |k| m.
|
|
898
|
+
assert m.until(hi).keys().toStr() == mapKeysWhere(400, |k| m.contains(k) && k < hi)
|
|
899
899
|
assert r.all(|k, v| v == m.get(k)!)
|
|
900
900
|
assert m.foldRange(lo, hi, 0, |a, k, v| a + 1) == r.length()
|
|
901
901
|
assert m.foldRange(lo, hi, "", |a, k, v| "{a}{k}={v};") == r.fold("", |a, k, v| "{a}{k}={v};")
|
|
@@ -950,14 +950,14 @@ test "set operations agree with a filter over pseudo-random maps, and stay balan
|
|
|
950
950
|
d := a.difference(b)
|
|
951
951
|
w := a.unionWith(b, |x, y| x + y)
|
|
952
952
|
assert mapValid(u) && mapValid(i) && mapValid(d) && mapValid(w)
|
|
953
|
-
assert u.keys().toStr() == mapKeysWhere(400, |k| a.
|
|
953
|
+
assert u.keys().toStr() == mapKeysWhere(400, |k| a.contains(k) || b.contains(k))
|
|
954
|
-
assert i.keys().toStr() == mapKeysWhere(400, |k| a.
|
|
954
|
+
assert i.keys().toStr() == mapKeysWhere(400, |k| a.contains(k) && b.contains(k))
|
|
955
|
-
assert d.keys().toStr() == mapKeysWhere(400, |k| a.
|
|
955
|
+
assert d.keys().toStr() == mapKeysWhere(400, |k| a.contains(k) && !b.contains(k))
|
|
956
956
|
assert u.all(|k, v| v == a.getOr(k, b.getOr(k, -1)))
|
|
957
957
|
assert w.all(|k, v| v == a.getOr(k, 0) + b.getOr(k, 0))
|
|
958
958
|
p := a.split(200)
|
|
959
959
|
assert mapValid(p.first) && mapValid(p.second)
|
|
960
|
-
assert p.first.keys().toStr() == mapKeysWhere(200, |k| a.
|
|
960
|
+
assert p.first.keys().toStr() == mapKeysWhere(200, |k| a.contains(k))
|
|
961
961
|
big := mapRandom(7, 3000, 100000, 0)
|
|
962
962
|
small := [5 => 0, 500 => 0, 50000 => 0, 99999 => 0, 123456 => 0]
|
|
963
963
|
assert mapValid(big.union(small)) && mapValid(small.union(big)) && mapValid(big.intersection(small))
|
|
@@ -1053,7 +1053,7 @@ test "keys of every std kind: Str, Float, Bool, Byte and a struct with compare"
|
|
|
1053
1053
|
assert f.length() == 5
|
|
1054
1054
|
assert f.get(-0.0)! == 3 && f.get(0.0)! == 3
|
|
1055
1055
|
assert f.get(nan)! == 7 && f.max()!.second == 7 && f.min()!.second == 5
|
|
1056
|
-
assert f.remove(nan).length() == 4 && !f.remove(nan).
|
|
1056
|
+
assert f.remove(nan).length() == 4 && !f.remove(nan).contains(nan)
|
|
1057
1057
|
b := [true => "t", false => "f"]
|
|
1058
1058
|
assert b.keys().toStr() == "[false,true]"
|
|
1059
1059
|
y := Map<Byte, Int>().with(Byte(200), 1).with(Byte(3), 2)
|
std/collections/Quadtree.plum
CHANGED
|
@@ -23,7 +23,7 @@ import std/collections/List
|
|
|
23
23
|
# `List` (in the twin's order).
|
|
24
24
|
#
|
|
25
25
|
# What changed from the twin: the split's `for` over the leaf's entries is a tail-
|
|
26
|
-
# recursive `insertAll`; the leaf scans in `
|
|
26
|
+
# recursive `insertAll`; the leaf scans in `contains` and `remove` are tail calls (`remove`
|
|
27
27
|
# keeps the entries it passed reversed and puts them back with `revOnto`); `query`
|
|
28
28
|
# and `nearby` thread a `List` accumulator through the four quarters instead of
|
|
29
29
|
# pushing to a `Slice`, and reverse it once at the end; the `while` over the quarters
|
|
@@ -56,14 +56,14 @@ struct Quadtree<V>(root: QuadNode<V>, bounds: Aabb, capacity: Int, maxDepth: Int
|
|
|
56
56
|
Quadtree(root: root, bounds: self.bounds, capacity: self.capacity, maxDepth: self.maxDepth, size: self.size + 1)
|
|
57
57
|
|
|
58
58
|
# Whether some entry has point `p` and value `v` (`==` on both).
|
|
59
|
-
fun
|
|
59
|
+
fun contains(self, p: Vec2, v: V) -> Bool =
|
|
60
|
-
self.bounds.containsPoint(p) && self.root.
|
|
60
|
+
self.bounds.containsPoint(p) && self.root.contains(self.bounds, p, v)
|
|
61
61
|
|
|
62
62
|
# A tree with one entry at point `p` with value `v` removed (both compared with
|
|
63
63
|
# `==`); the same tree when there is none. Of duplicates, one goes. A split node
|
|
64
64
|
# left with `capacity` or fewer entries merges back into a leaf.
|
|
65
65
|
fun remove(self, p: Vec2, v: V) -> Quadtree<V> =
|
|
66
|
-
if !self.
|
|
66
|
+
if !self.contains(p, v)
|
|
67
67
|
self
|
|
68
68
|
else
|
|
69
69
|
root := self.root.remove(self.bounds, self.capacity, p, v)
|
|
@@ -152,12 +152,12 @@ enum QuadNode<V> =
|
|
|
152
152
|
Nil => self
|
|
153
153
|
Cons(it, rest) => self.insert(b, depth, cap, maxDepth, it.first, it.second).insertAll(rest, b, depth, cap, maxDepth)
|
|
154
154
|
|
|
155
|
-
fun
|
|
155
|
+
fun contains(self, b: Aabb, p: Vec2, v: V) -> Bool =
|
|
156
156
|
match self
|
|
157
157
|
QuadLeaf(items, _) => quadHasItem(items, p, v)
|
|
158
158
|
QuadSplit(_, _, _, _, _) =>
|
|
159
159
|
i := quadQuadrantOf(b, p)
|
|
160
|
-
self.child(i).
|
|
160
|
+
self.child(i).contains(quadQuadrant(b, i), p, v)
|
|
161
161
|
|
|
162
162
|
# Every entry below this node pushed onto `acc`, quarter 0 first.
|
|
163
163
|
fun gather(self, acc: List<Pair<Vec2, V>>) -> List<Pair<Vec2, V>> =
|
|
@@ -372,7 +372,7 @@ test "an empty quadtree"
|
|
|
372
372
|
assert t.isEmpty()
|
|
373
373
|
assert t.query(quadBox()).length() == 0
|
|
374
374
|
assert t.nearby(Vec2(x: 1.0, y: 1.0), 50.0).length() == 0
|
|
375
|
-
assert t.
|
|
375
|
+
assert t.contains(Vec2(x: 1.0, y: 1.0), 0) == false
|
|
376
376
|
assert t.remove(Vec2(x: 1.0, y: 1.0), 0).length() == 0
|
|
377
377
|
assert t.toStr() == "[]"
|
|
378
378
|
assert quadValid(t)
|
|
@@ -388,8 +388,8 @@ test "insert, has and toStr"
|
|
|
388
388
|
e: Quadtree<Str> := Quadtree.over(quadBox())
|
|
389
389
|
t := e.insert(Vec2(x: 1.0, y: 2.0), "a").insert(Vec2(x: 3.5, y: 4.0), "b")
|
|
390
390
|
assert t.length() == 2
|
|
391
|
-
assert t.
|
|
391
|
+
assert t.contains(Vec2(x: 1.0, y: 2.0), "a")
|
|
392
|
-
assert t.
|
|
392
|
+
assert t.contains(Vec2(x: 1.0, y: 2.0), "b") == false
|
|
393
393
|
assert t.toStr() == "[(3.5, 4.0): b, (1.0, 2.0): a]"
|
|
394
394
|
|
|
395
395
|
test "points outside the bounds are rejected"
|
|
@@ -397,7 +397,7 @@ test "points outside the bounds are rejected"
|
|
|
397
397
|
t := e.insert(Vec2(x: 100.0, y: 0.0), 1)
|
|
398
398
|
u := t.insert(Vec2(x: -0.5, y: 3.0), 2).insert(Vec2(x: 5.0, y: 100.5), 3)
|
|
399
399
|
assert u.length() == 1
|
|
400
|
-
assert u.
|
|
400
|
+
assert u.contains(Vec2(x: -0.5, y: 3.0), 2) == false
|
|
401
401
|
nan := 0.0 / 0.0
|
|
402
402
|
assert u.insert(Vec2(x: nan, y: 1.0), 4).length() == 1
|
|
403
403
|
assert u.query(Aabb.of(-10.0, -10.0, 200.0, 200.0)).length() == 1
|
|
@@ -411,7 +411,7 @@ test "duplicates are all kept and removed one at a time"
|
|
|
411
411
|
assert t.nearby(p, 0.0).length() == 10
|
|
412
412
|
u := t.remove(p, 1)
|
|
413
413
|
assert u.length() == 9
|
|
414
|
-
assert u.
|
|
414
|
+
assert u.contains(p, 1)
|
|
415
415
|
assert u.remove(p, 2).length() == 9
|
|
416
416
|
assert quadValid(u)
|
|
417
417
|
assert t.length() == 10
|
|
@@ -437,9 +437,9 @@ test "updates leave the old tree as it was"
|
|
|
437
437
|
u := t.insert(Vec2(x: 80.0, y: 80.0), 1)
|
|
438
438
|
w := u.remove(Vec2(x: 10.0, y: 10.0), 0)
|
|
439
439
|
assert t.length() == 1 && u.length() == 2 && w.length() == 1
|
|
440
|
-
assert t.
|
|
440
|
+
assert t.contains(Vec2(x: 80.0, y: 80.0), 1) == false
|
|
441
|
-
assert u.
|
|
441
|
+
assert u.contains(Vec2(x: 10.0, y: 10.0), 0)
|
|
442
|
-
assert w.
|
|
442
|
+
assert w.contains(Vec2(x: 10.0, y: 10.0), 0) == false
|
|
443
443
|
assert t.toStr() == "[(10.0, 10.0): 0]"
|
|
444
444
|
assert u.toStr() == "[(10.0, 10.0): 0, (80.0, 80.0): 1]"
|
|
445
445
|
assert quadValid(t) && quadValid(u) && quadValid(w)
|
|
@@ -480,8 +480,8 @@ test "pseudo-random inserts and removes match brute force"
|
|
|
480
480
|
assert full.length() == 600 && quadValid(full)
|
|
481
481
|
n := 0.until(600).fold(0, |c, i| if live.at(i) then c + 1 else c)
|
|
482
482
|
for i := range 600
|
|
483
|
-
assert t.
|
|
483
|
+
assert t.contains(pts.at(i), i) == live.at(i)
|
|
484
|
-
assert full.
|
|
484
|
+
assert full.contains(pts.at(i), i)
|
|
485
485
|
assert t.length() == n
|
|
486
486
|
for q := range 40
|
|
487
487
|
ax := Float(rng.roll() % 120) - 10.0
|
|
@@ -515,7 +515,7 @@ test "10,000 entries at one point stay in one leaf at maxDepth"
|
|
|
515
515
|
assert quadValid(t)
|
|
516
516
|
assert t.nearby(p, 0.0).length() == 10000
|
|
517
517
|
assert t.remove(p, 5).length() == 9999
|
|
518
|
-
assert t.
|
|
518
|
+
assert t.contains(p, 6) == false
|
|
519
519
|
|
|
520
520
|
test "queries with no hits on a non-empty tree"
|
|
521
521
|
e: Quadtree<Int> := Quadtree.over(quadBox(), 1)
|
std/collections/RadixTree.plum
CHANGED
|
@@ -29,7 +29,7 @@ import std/collections/Trie
|
|
|
29
29
|
# node back under them with a second tail call; `entries` is a tail-recursive
|
|
30
30
|
# depth-first walk over a `List` of (key, node) frames, each key built by one
|
|
31
31
|
# `concat` when its frame is pushed.
|
|
32
|
-
# `get`/`
|
|
32
|
+
# `get`/`contains`/`insert`/`remove` take O(L + d * s) for a key of L bytes on a path of d
|
|
33
33
|
# nodes with s edges each; `length` is O(1).
|
|
34
34
|
#
|
|
35
35
|
# Stack: every walk along a key is a tail call, so all of it runs in constant stack
|
|
@@ -52,7 +52,7 @@ struct RadixTree<V>(root: RadixNode<V>, size: Int) =
|
|
|
52
52
|
fun get(self, k: Str) -> Option<V> =
|
|
53
53
|
self.root.lookup(k, 0)
|
|
54
54
|
|
|
55
|
-
fun
|
|
55
|
+
fun contains(self, k: Str) -> Bool =
|
|
56
56
|
self.get(k).isSome()
|
|
57
57
|
|
|
58
58
|
# The node holding the keys that start with `p`, with the key that leads to it
|
|
@@ -63,7 +63,7 @@ struct RadixTree<V>(root: RadixNode<V>, size: Int) =
|
|
|
63
63
|
# A tree with `k` mapped to `v`, replacing the value of an existing `k`.
|
|
64
64
|
fun insert(self, k: Str, v: V) -> RadixTree<V> =
|
|
65
65
|
root := self.root.insertAt(k, 0, v, Nil)
|
|
66
|
-
if self.
|
|
66
|
+
if self.contains(k)
|
|
67
67
|
RadixTree(root: root, size: self.size)
|
|
68
68
|
else
|
|
69
69
|
RadixTree(root: root, size: self.size + 1)
|
|
@@ -71,7 +71,7 @@ struct RadixTree<V>(root: RadixNode<V>, size: Int) =
|
|
|
71
71
|
# A tree without `k`, dropping a node left empty and merging a node left with one
|
|
72
72
|
# edge and no value into its parent's edge; the same tree when `k` is absent.
|
|
73
73
|
fun remove(self, k: Str) -> RadixTree<V> =
|
|
74
|
-
if !self.
|
|
74
|
+
if !self.contains(k)
|
|
75
75
|
self
|
|
76
76
|
else
|
|
77
77
|
RadixTree(root: self.root.removeAt(k, 0, Nil), size: self.size - 1)
|
|
@@ -470,7 +470,7 @@ test "an empty tree"
|
|
|
470
470
|
assert t.isEmpty()
|
|
471
471
|
assert t.length() == 0
|
|
472
472
|
assert t.get("a").isNone()
|
|
473
|
-
assert t.
|
|
473
|
+
assert t.contains("") == false
|
|
474
474
|
assert t.hasPrefix("") == false
|
|
475
475
|
assert t.withPrefix("").isEmpty()
|
|
476
476
|
assert t.longestPrefixOf("abc").isNone()
|
|
@@ -487,8 +487,8 @@ test "insert, get and has"
|
|
|
487
487
|
assert t.get("r").isNone()
|
|
488
488
|
assert t.get("rubiconx").isNone()
|
|
489
489
|
assert t.get("rx").isNone()
|
|
490
|
-
assert t.
|
|
490
|
+
assert t.contains("ruber")
|
|
491
|
-
assert t.
|
|
491
|
+
assert t.contains("rube") == false
|
|
492
492
|
assert radixValid(t)
|
|
493
493
|
|
|
494
494
|
test "edges are compressed: one edge per shared run"
|
|
@@ -531,7 +531,7 @@ test "the empty key is a key like any other"
|
|
|
531
531
|
assert t.keys().toStr() == "[,a]"
|
|
532
532
|
assert t.longestPrefixOf("xyz")! == ""
|
|
533
533
|
u := t.remove("")
|
|
534
|
-
assert u.
|
|
534
|
+
assert u.contains("") == false
|
|
535
535
|
assert u.length() == 1
|
|
536
536
|
assert radixValid(u)
|
|
537
537
|
|
std/collections/Set.plum
CHANGED
|
@@ -37,8 +37,8 @@ struct Set<T>(map: Map<T, Bool>) =
|
|
|
37
37
|
fun remove(self, v: T) -> Set<T> =
|
|
38
38
|
Set(map: self.map.remove(v))
|
|
39
39
|
|
|
40
|
-
fun
|
|
40
|
+
fun contains(self, v: T) -> Bool =
|
|
41
|
-
self.map.
|
|
41
|
+
self.map.contains(v)
|
|
42
42
|
|
|
43
43
|
fun length(self) -> Int =
|
|
44
44
|
self.map.length()
|
|
@@ -103,7 +103,7 @@ struct Set<T>(map: Map<T, Bool>) =
|
|
|
103
103
|
|
|
104
104
|
# True when every member of `self` is in `o`.
|
|
105
105
|
fun isSubsetOf(self, o: Set<T>) -> Bool =
|
|
106
|
-
self.length() <= o.length() && self.map.all(|k, v| o.
|
|
106
|
+
self.length() <= o.length() && self.map.all(|k, v| o.contains(k))
|
|
107
107
|
|
|
108
108
|
# `f(..f(f(acc, x0), x1).., xn)`, ascending.
|
|
109
109
|
fun fold<A>(self, acc: A, f: fn(A, T) -> A) -> A =
|
|
@@ -167,7 +167,7 @@ struct SetTestRun(mut s: Set<Int>)
|
|
|
167
167
|
|
|
168
168
|
test "add keeps one copy, remove and has; the old set stays as it was"
|
|
169
169
|
s := Set<Int>().add(3).add(1).add(3).add(2)
|
|
170
|
-
assert s.length() == 3 && s.
|
|
170
|
+
assert s.length() == 3 && s.contains(3) && !s.contains(10)
|
|
171
171
|
t := s.remove(1).remove(99)
|
|
172
172
|
assert t.toStr() == "[2, 3]" && s.toStr() == "[1, 2, 3]"
|
|
173
173
|
assert Set<Int>().isEmpty() && !s.isEmpty() && Set<Str>().toStr() == "[]"
|
|
@@ -199,7 +199,7 @@ test "rank, indexOf, from, until, range, foldRange and join"
|
|
|
199
199
|
a := setRandom(seed, 150, 300, Set.empty())
|
|
200
200
|
lo := seed * 37
|
|
201
201
|
hi := seed * 41 + 20
|
|
202
|
-
assert a.range(lo, hi).toSlice().toStr() == setWhere(300, |x| a.
|
|
202
|
+
assert a.range(lo, hi).toSlice().toStr() == setWhere(300, |x| a.contains(x) && x >= lo && x < hi)
|
|
203
203
|
assert a.foldRange(lo, hi, 0, |n, x| n + 1) == a.range(lo, hi).length()
|
|
204
204
|
assert a.rank(lo) == a.until(lo).length()
|
|
205
205
|
assert s.length() == 5
|
|
@@ -208,9 +208,9 @@ test "union, intersection, difference and isSubsetOf agree with a filter"
|
|
|
208
208
|
for seed := range 1.to(10)
|
|
209
209
|
a := setRandom(seed, 200, 300, Set.empty())
|
|
210
210
|
b := setRandom(seed * 7919, 150, 300, Set.empty())
|
|
211
|
-
assert a.union(b).toSlice().toStr() == setWhere(300, |x| a.
|
|
211
|
+
assert a.union(b).toSlice().toStr() == setWhere(300, |x| a.contains(x) || b.contains(x))
|
|
212
|
-
assert a.intersection(b).toSlice().toStr() == setWhere(300, |x| a.
|
|
212
|
+
assert a.intersection(b).toSlice().toStr() == setWhere(300, |x| a.contains(x) && b.contains(x))
|
|
213
|
-
assert a.difference(b).toSlice().toStr() == setWhere(300, |x| a.
|
|
213
|
+
assert a.difference(b).toSlice().toStr() == setWhere(300, |x| a.contains(x) && !b.contains(x))
|
|
214
214
|
assert a.intersection(b).isSubsetOf(a) && a.isSubsetOf(a.union(b))
|
|
215
215
|
assert a.difference(b).isSubsetOf(b) == a.difference(b).isEmpty()
|
|
216
216
|
|
std/collections/Trie.plum
CHANGED
|
@@ -25,7 +25,7 @@ import std/collections/Map
|
|
|
25
25
|
# `entries` is a tail-recursive depth-first walk over a `List` of frames, a
|
|
26
26
|
# frame below a chain of valueless single-child nodes keeping the chain's bytes as a
|
|
27
27
|
# list and turning them into text only where a key ends or the path branches.
|
|
28
|
-
# `get`/`
|
|
28
|
+
# `get`/`contains`/`insert`/`remove` take O(L * s) for a key of L bytes, where s is the
|
|
29
29
|
# number of children per node (at most 256, usually a few); `length` is O(1).
|
|
30
30
|
#
|
|
31
31
|
# Stack: every walk along a key is a tail call, so all of it runs in constant stack
|
|
@@ -53,13 +53,13 @@ struct Trie<V>(root: TrieNode<V>, size: Int) =
|
|
|
53
53
|
None => None
|
|
54
54
|
Some(n) => n.value
|
|
55
55
|
|
|
56
|
-
fun
|
|
56
|
+
fun contains(self, k: Str) -> Bool =
|
|
57
57
|
self.get(k).isSome()
|
|
58
58
|
|
|
59
59
|
# A trie with `k` mapped to `v`, replacing the value of an existing `k`.
|
|
60
60
|
fun insert(self, k: Str, v: V) -> Trie<V> =
|
|
61
61
|
root := self.root.setAt(k, 0, Some(v), Nil)
|
|
62
|
-
if self.
|
|
62
|
+
if self.contains(k)
|
|
63
63
|
Trie(root: root, size: self.size)
|
|
64
64
|
else
|
|
65
65
|
Trie(root: root, size: self.size + 1)
|
|
@@ -67,7 +67,7 @@ struct Trie<V>(root: TrieNode<V>, size: Int) =
|
|
|
67
67
|
# A trie without `k`, pruning the branches left without keys; the same trie
|
|
68
68
|
# when `k` is absent.
|
|
69
69
|
fun remove(self, k: Str) -> Trie<V> =
|
|
70
|
-
if !self.
|
|
70
|
+
if !self.contains(k)
|
|
71
71
|
self
|
|
72
72
|
else
|
|
73
73
|
none: Option<V> := None
|
|
@@ -395,7 +395,7 @@ test "an empty trie"
|
|
|
395
395
|
assert t.isEmpty()
|
|
396
396
|
assert t.length() == 0
|
|
397
397
|
assert t.get("a").isNone()
|
|
398
|
-
assert t.
|
|
398
|
+
assert t.contains("") == false
|
|
399
399
|
assert t.hasPrefix("") == false
|
|
400
400
|
assert t.withPrefix("").isEmpty()
|
|
401
401
|
assert t.longestPrefixOf("abc").isNone()
|
|
@@ -413,8 +413,8 @@ test "insert, get and has"
|
|
|
413
413
|
assert t.get("te").isNone()
|
|
414
414
|
assert t.get("t").isNone()
|
|
415
415
|
assert t.get("teas").isNone()
|
|
416
|
-
assert t.
|
|
416
|
+
assert t.contains("to")
|
|
417
|
-
assert t.
|
|
417
|
+
assert t.contains("in") == false
|
|
418
418
|
assert trieValid(t)
|
|
419
419
|
|
|
420
420
|
test "insert replaces the value of an existing key"
|
|
@@ -431,7 +431,7 @@ test "the empty key is a key like any other"
|
|
|
431
431
|
assert t.keys().toStr() == "[,a]"
|
|
432
432
|
assert t.longestPrefixOf("xyz")! == ""
|
|
433
433
|
u := t.remove("")
|
|
434
|
-
assert u.
|
|
434
|
+
assert u.contains("") == false
|
|
435
435
|
assert u.length() == 1
|
|
436
436
|
assert trieValid(u)
|
|
437
437
|
|
|
@@ -453,8 +453,8 @@ test "remove prunes the branch it empties"
|
|
|
453
453
|
t := trieOf(["tea", "ten"])
|
|
454
454
|
u := t.remove("tea")
|
|
455
455
|
assert u.length() == 1
|
|
456
|
-
assert u.
|
|
456
|
+
assert u.contains("tea") == false
|
|
457
|
-
assert u.
|
|
457
|
+
assert u.contains("ten")
|
|
458
458
|
assert u.hasPrefix("tea") == false
|
|
459
459
|
assert u.hasPrefix("te")
|
|
460
460
|
assert trieValid(u)
|
|
@@ -561,7 +561,7 @@ test "invariants_after_random_ops: pseudo-random inserts and removes match a Map
|
|
|
561
561
|
sorted := List.fromSlice(m.keys().sort(|a, b| trieBytesBefore(a, b)))
|
|
562
562
|
assert t.keys().toStr() == sorted.toStr()
|
|
563
563
|
for k := range t.keys()
|
|
564
|
-
assert m.
|
|
564
|
+
assert m.contains(k)
|
|
565
565
|
for p := range ["", "a", "ab", "abc", "e", "eee", "x"]
|
|
566
566
|
expected := sorted.filter(|k| k.startsWith(p))
|
|
567
567
|
assert t.withPrefix(p).map(|e| e.first).toStr() == expected.toStr()
|
std/game/SceneTree.plum
CHANGED
|
@@ -314,7 +314,7 @@ struct TreeWalk<M>(
|
|
|
314
314
|
bitHas(self.killBits, id)
|
|
315
315
|
else
|
|
316
316
|
match self.kills
|
|
317
|
-
Some(k) => k.
|
|
317
|
+
Some(k) => k.contains(id)
|
|
318
318
|
None => false
|
|
319
319
|
|
|
320
320
|
# The nodes to add under the actor `id`, as a chain in the order pushed (empty if none).
|
tests/std/map_model.plum
CHANGED
|
@@ -22,7 +22,7 @@ fun modelStr(xs: Slice<Pair<Int, Int>>) -> Str =
|
|
|
22
22
|
fun sortedModel(m: Map<Int, Int>, b: Int, keep: fn(Int) -> Bool) -> Str =
|
|
23
23
|
out := Slice<Pair<Int, Int>>()
|
|
24
24
|
for k := range {0 - 20}.until(b + 20)
|
|
25
|
-
if keep(k) && m.
|
|
25
|
+
if keep(k) && m.contains(k)
|
|
26
26
|
out.push(Pair(first: k, second: m.get(k)!))
|
|
27
27
|
modelStr(out)
|
|
28
28
|
|
|
@@ -74,19 +74,19 @@ test "every operation keeps the tree valid and agrees with the model; inputs sta
|
|
|
74
74
|
k2 := draw(st, bnd + 20) - 10
|
|
75
75
|
check(st, "with", a.with(k, -1), sortedModel(a.with(k, -1), bnd, |q| true))
|
|
76
76
|
w := a.with(k, -1)
|
|
77
|
-
if w.getOr(k, 0) != -1 || w.length() != a.length() + {if a.
|
|
77
|
+
if w.getOr(k, 0) != -1 || w.length() != a.length() + {if a.contains(k) then 0 else 1}
|
|
78
78
|
st.bad = st.bad + 1
|
|
79
79
|
check(st, "remove", a.remove(k), sortedModel(a, bnd, |q| q != k))
|
|
80
|
-
check(st, "union", a.union(b), sortedModel(a.union(b), bnd, |q| a.
|
|
80
|
+
check(st, "union", a.union(b), sortedModel(a.union(b), bnd, |q| a.contains(q) || b.contains(q)))
|
|
81
81
|
if !a.union(b).all(|q, v| v == a.getOr(q, b.getOr(q, -99)))
|
|
82
82
|
st.bad = st.bad + 1
|
|
83
83
|
st.log.push("union values")
|
|
84
84
|
uw := a.unionWith(b, |x, y| x * 1000 + y)
|
|
85
|
-
if !map.mapValid(uw) || uw.length() != a.union(b).length() || !uw.all(|q, v| v == {if a.
|
|
85
|
+
if !map.mapValid(uw) || uw.length() != a.union(b).length() || !uw.all(|q, v| v == {if a.contains(q) && b.contains(q) then a.get(q)! * 1000 + b.get(q)! else a.getOr(q, b.getOr(q, -99))})
|
|
86
86
|
st.bad = st.bad + 1
|
|
87
87
|
st.log.push("unionWith")
|
|
88
|
-
check(st, "intersection", a.intersection(b), sortedModel(a, bnd, |q| b.
|
|
88
|
+
check(st, "intersection", a.intersection(b), sortedModel(a, bnd, |q| b.contains(q)))
|
|
89
|
-
check(st, "difference", a.difference(b), sortedModel(a, bnd, |q| !b.
|
|
89
|
+
check(st, "difference", a.difference(b), sortedModel(a, bnd, |q| !b.contains(q)))
|
|
90
90
|
p := a.split(k)
|
|
91
91
|
check(st, "split.first", p.first, sortedModel(a, bnd, |q| q < k))
|
|
92
92
|
check(st, "split.second", p.second, sortedModel(a, bnd, |q| q > k))
|