lean4-htt/Lake/Util/OrdHashSet.lean
2022-12-02 14:17:56 -05:00

56 lines
1.6 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2022 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone
-/
import Lean.Data.HashSet
open Lean
namespace Lake
/-- A `HashSet` that preserves insertion order. -/
structure OrdHashSet (α) [Hashable α] [BEq α] where
toHashSet : HashSet α
toArray : Array α
namespace OrdHashSet
variable [Hashable α] [BEq α]
def empty : OrdHashSet α :=
⟨.empty, .empty⟩
def mkEmpty (size : Nat) : OrdHashSet α :=
⟨.empty, .mkEmpty size⟩
def insert (self : OrdHashSet α) (a : α) : OrdHashSet α :=
if self.toHashSet.contains a then
self
else
⟨self.toHashSet.insert a, self.toArray.push a⟩
def appendArray (self : OrdHashSet α) (arr : Array α) :=
arr.foldl (·.insert ·) self
instance : HAppend (OrdHashSet α) (Array α) (OrdHashSet α) := ⟨OrdHashSet.appendArray⟩
protected def append (self other : OrdHashSet α) :=
self.appendArray other.toArray
instance : Append (OrdHashSet α) := ⟨OrdHashSet.append⟩
def ofArray (arr : Array α) : OrdHashSet α :=
mkEmpty arr.size |>.appendArray arr
@[inline] def foldl (f : β → α → β) (init : β) (self : OrdHashSet α) : β :=
self.toArray.foldl f init
@[inline] def foldlM [Monad m] (f : β → α → m β) (init : β) (self : OrdHashSet α) : m β :=
self.toArray.foldlM f init
@[inline] def foldr (f : α → β → β) (init : β) (self : OrdHashSet α) : β :=
self.toArray.foldr f init
@[inline] def foldrM [Monad m] (f : α → β → m β) (init : β) (self : OrdHashSet α) : m β :=
self.toArray.foldrM f init