Skip to content

Commit 81ae981

Browse files
committed
feat: add Std.TreeSet.diff, Std.TreeSet.subset, SSet.subset, NameSet.diff, NameSet.subset, NameSSet.subset, SMap.any, SMap.all, SMap.subset
1 parent a4f6f39 commit 81ae981

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

src/Lean/Data/NameMap/Basic.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ instance : EmptyCollection NameSet := ⟨empty⟩
5252
instance : Inhabited NameSet := ⟨empty⟩
5353
def insert (s : NameSet) (n : Name) : NameSet := Std.TreeSet.insert s n
5454
def contains (s : NameSet) (n : Name) : Bool := Std.TreeSet.contains s n
55+
abbrev subset : NameSet -> NameSet -> Bool := Std.TreeSet.subset
56+
abbrev diff (t₁ t₂ : NameSet) : NameSet := Std.TreeSet.diff t₁ t₂
5557
instance : ForIn m NameSet Name :=
5658
inferInstanceAs (ForIn _ (Std.TreeSet _ _) ..)
5759

@@ -91,6 +93,7 @@ instance : EmptyCollection NameSSet := ⟨empty⟩
9193
instance : Inhabited NameSSet := ⟨empty⟩
9294
abbrev insert (s : NameSSet) (n : Name) : NameSSet := SSet.insert s n
9395
abbrev contains (s : NameSSet) (n : Name) : Bool := SSet.contains s n
96+
abbrev subset : NameSSet -> NameSSet -> Bool := SSet.subset
9497
end NameSSet
9598

9699
@[expose] def NameHashSet := Std.HashSet Name

src/Lean/Data/SMap.lean

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ def numBuckets (m : SMap α β) : Nat :=
105105
def toList (m : SMap α β) : List (α × β) :=
106106
m.fold (init := []) fun es a b => (a, b)::es
107107

108+
/-- Check if any element satisfies the predicate, short-circuiting if a predicate fails. -/
109+
@[inline]
110+
def any (s : SMap α β) (p : α → β → Bool) : Bool := Id.run do
111+
for ⟨a, b⟩ in s do
112+
if p a b then return true
113+
return false
114+
115+
/-- Check if all elements satisfy the predicate, short-circuiting if a predicate fails. -/
116+
@[inline]
117+
def all (s : SMap α β) (p : α → β → Bool) : Bool := Id.run do
118+
for ⟨a, b⟩ in s do
119+
if p a b = false then return false
120+
return true
121+
122+
/--
123+
`t₁.subset t₂` means that every element of `t₁` is also an element of `t₂`, ignoring multiplicity.
124+
-/
125+
@[inline]
126+
def subset [BEq β] (m₁ m₂ : SMap α β) : Bool :=
127+
m₁.all (fun a b => m₂.find? a == some b)
128+
108129
end SMap
109130

110131
def _root_.List.toSMap [BEq α] [Hashable α] (es : List (α × β)) : SMap α β :=

src/Lean/Data/SSet.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ abbrev fold (f : σ → α → σ) (init : σ) (s : SSet α) : σ :=
4141
def toList (m : SSet α) : List α :=
4242
m.fold (init := []) fun es a => a::es
4343

44+
abbrev subset (s₁ s₂ : SSet α) : Bool :=
45+
s₁.all (fun k .unit => s₂.contains k)
46+
4447
end SSet
4548

4649
end Lean

src/Std/Data/TreeSet/Basic.lean

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,22 @@ def any (t : TreeSet α cmp) (p : α → Bool) : Bool :=
449449
def all (t : TreeSet α cmp) (p : α → Bool) : Bool :=
450450
t.inner.all (fun a _ => p a)
451451

452+
/--
453+
`t₁.subset t₂` means that every element of `t₁` is also an element of `t₂`, ignoring multiplicity.
454+
-/
455+
@[inline]
456+
def subset (t₁ t₂ : TreeSet α cmp) : Bool :=
457+
t₁.all (t₂.contains ·)
458+
459+
/--
460+
`t₁.diff t₂` returns a new `TreeSet` containing all elements of `t₁`
461+
that are not present in `t₂`. Effectively, it removes from `t₁`
462+
every element found in `t₂`.
463+
-/
464+
@[inline]
465+
def diff (t₁ t₂ : TreeSet α cmp) : TreeSet α cmp :=
466+
t₂.foldl .erase t₁
467+
452468
/-- Transforms the tree set into a list of elements in ascending order. -/
453469
@[inline]
454470
def toList (t : TreeSet α cmp) : List α :=

0 commit comments

Comments
 (0)