| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Finite sets, based on AVL trees |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Bundles using (StrictTotalOrder) |
| 10 |
|
| 11 |
module Data.Tree.AVL.Sets |
| 12 |
{a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂) |
| 13 |
where |
| 14 |
|
| 15 |
open import Data.Bool.Base using (Bool) |
| 16 |
open import Data.List.Base as List using (List) |
| 17 |
open import Data.Maybe.Base as Maybe |
| 18 |
open import Data.Nat.Base using (ℕ) |
| 19 |
open import Data.Product.Base as Product using (_×_; _,_; proj₁) |
| 20 |
open import Data.Unit.Base |
| 21 |
open import Function.Base |
| 22 |
open import Level using (Level; _⊔_) |
| 23 |
|
| 24 |
private |
| 25 |
variable |
| 26 |
b : Level |
| 27 |
B : Set b |
| 28 |
|
| 29 |
import Data.Tree.AVL.Map strictTotalOrder as AVL |
| 30 |
open StrictTotalOrder strictTotalOrder renaming (Carrier to A) |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- The set type (note that Set is a reserved word) |
| 34 |
|
| 35 |
⟨Set⟩ : Set (a ⊔ ℓ₂) |
| 36 |
⟨Set⟩ = AVL.Map ⊤ |
| 37 |
|
| 38 |
------------------------------------------------------------------------ |
| 39 |
-- Repackaged functions |
| 40 |
|
| 41 |
empty : ⟨Set⟩ |
| 42 |
empty = AVL.empty |
| 43 |
|
| 44 |
singleton : A → ⟨Set⟩ |
| 45 |
singleton k = AVL.singleton k _ |
| 46 |
|
| 47 |
insert : A → ⟨Set⟩ → ⟨Set⟩ |
| 48 |
insert k = AVL.insert k _ |
| 49 |
|
| 50 |
delete : A → ⟨Set⟩ → ⟨Set⟩ |
| 51 |
delete = AVL.delete |
| 52 |
|
| 53 |
member : A → ⟨Set⟩ → Bool |
| 54 |
member = AVL.member |
| 55 |
|
| 56 |
headTail : ⟨Set⟩ → Maybe (A × ⟨Set⟩) |
| 57 |
headTail s = Maybe.map (Product.map₁ proj₁) (AVL.headTail s) |
| 58 |
|
| 59 |
initLast : ⟨Set⟩ → Maybe (⟨Set⟩ × A) |
| 60 |
initLast s = Maybe.map (Product.map₂ proj₁) (AVL.initLast s) |
| 61 |
|
| 62 |
fromList : List A → ⟨Set⟩ |
| 63 |
fromList = AVL.fromList ∘ List.map (_, _) |
| 64 |
|
| 65 |
toList : ⟨Set⟩ → List A |
| 66 |
toList = List.map proj₁ ∘ AVL.toList |
| 67 |
|
| 68 |
foldr : (A → B → B) → B → ⟨Set⟩ → B |
| 69 |
foldr cons nil = AVL.foldr (const ∘′ cons) nil |
| 70 |
|
| 71 |
size : ⟨Set⟩ → ℕ |
| 72 |
size = AVL.size |
| 73 |
|
| 74 |
------------------------------------------------------------------------ |
| 75 |
-- Naïve implementations of union and intersection |
| 76 |
|
| 77 |
union : ⟨Set⟩ → ⟨Set⟩ → ⟨Set⟩ |
| 78 |
union = AVL.union |
| 79 |
|
| 80 |
unions : List ⟨Set⟩ → ⟨Set⟩ |
| 81 |
unions = AVL.unions |
| 82 |
|
| 83 |
intersection : ⟨Set⟩ → ⟨Set⟩ → ⟨Set⟩ |
| 84 |
intersection = AVL.intersection |
| 85 |
|
| 86 |
intersections : List ⟨Set⟩ → ⟨Set⟩ |
| 87 |
intersections = AVL.intersections |
| 88 |
|
| 89 |
|
| 90 |
------------------------------------------------------------------------ |
| 91 |
-- DEPRECATED |
| 92 |
------------------------------------------------------------------------ |
| 93 |
-- Please use the new names as continuing support for the old names is |
| 94 |
-- not guaranteed. |
| 95 |
|
| 96 |
-- Version 2.0 |
| 97 |
|
| 98 |
infixl 4 _∈?_ |
| 99 |
_∈?_ : A → ⟨Set⟩ → Bool |
| 100 |
_∈?_ = member |
| 101 |
{-# WARNING_ON_USAGE _∈?_ |
| 102 |
"Warning: _∈?_ was deprecated in v2.0. |
| 103 |
Please use member instead." |
| 104 |
#-} |