File tree Expand file tree Collapse file tree 4 files changed +43
-0
lines changed
Expand file tree Collapse file tree 4 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,8 @@ instance : EmptyCollection NameSet := ⟨empty⟩
5252instance : Inhabited NameSet := ⟨empty⟩
5353def insert (s : NameSet) (n : Name) : NameSet := Std.TreeSet.insert s n
5454def 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₂
5557instance : ForIn m NameSet Name :=
5658 inferInstanceAs (ForIn _ (Std.TreeSet _ _) ..)
5759
@@ -91,6 +93,7 @@ instance : EmptyCollection NameSSet := ⟨empty⟩
9193instance : Inhabited NameSSet := ⟨empty⟩
9294abbrev insert (s : NameSSet) (n : Name) : NameSSet := SSet.insert s n
9395abbrev contains (s : NameSSet) (n : Name) : Bool := SSet.contains s n
96+ abbrev subset : NameSSet -> NameSSet -> Bool := SSet.subset
9497end NameSSet
9598
9699@[expose] def NameHashSet := Std.HashSet Name
Original file line number Diff line number Diff line change @@ -105,6 +105,27 @@ def numBuckets (m : SMap α β) : Nat :=
105105def 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+
108129end SMap
109130
110131def _root_.List.toSMap [BEq α] [Hashable α] (es : List (α × β)) : SMap α β :=
Original file line number Diff line number Diff line change @@ -41,6 +41,9 @@ abbrev fold (f : σ → α → σ) (init : σ) (s : SSet α) : σ :=
4141def 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+
4447end SSet
4548
4649end Lean
Original file line number Diff line number Diff line change @@ -449,6 +449,22 @@ def any (t : TreeSet α cmp) (p : α → Bool) : Bool :=
449449def 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]
454470def toList (t : TreeSet α cmp) : List α :=
You can’t perform that action at this time.
0 commit comments