| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Maps from keys to values, based on AVL trees |
| 5 |
-- This modules provides a simpler map interface, without a dependency |
| 6 |
-- between the key and value types. |
| 7 |
------------------------------------------------------------------------ |
| 8 |
|
| 9 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 10 |
|
| 11 |
open import Relation.Binary.Bundles using (StrictTotalOrder) |
| 12 |
|
| 13 |
module Data.Tree.AVL.Map |
| 14 |
{a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂) |
| 15 |
where |
| 16 |
|
| 17 |
open import Data.Bool.Base using (Bool) |
| 18 |
open import Data.List.Base as List using (List) |
| 19 |
open import Data.Maybe.Base as Maybe using (Maybe) |
| 20 |
open import Data.Nat.Base using (ℕ) |
| 21 |
open import Data.Product.Base as Prod using (_×_) |
| 22 |
open import Function.Base using (_∘′_) |
| 23 |
open import Level using (Level; _⊔_) |
| 24 |
|
| 25 |
private |
| 26 |
variable |
| 27 |
l v w x : Level |
| 28 |
A : Set l |
| 29 |
V : Set v |
| 30 |
W : Set w |
| 31 |
X : Set x |
| 32 |
|
| 33 |
import Data.Tree.AVL strictTotalOrder as AVL |
| 34 |
open StrictTotalOrder strictTotalOrder renaming (Carrier to Key) |
| 35 |
|
| 36 |
------------------------------------------------------------------------ |
| 37 |
-- The map type |
| 38 |
|
| 39 |
Map : (V : Set v) → Set (a ⊔ v ⊔ ℓ₂) |
| 40 |
Map V = AVL.Tree (AVL.const V) |
| 41 |
|
| 42 |
------------------------------------------------------------------------ |
| 43 |
-- Repackaged functions |
| 44 |
|
| 45 |
empty : Map V |
| 46 |
empty = AVL.empty |
| 47 |
|
| 48 |
singleton : Key → V → Map V |
| 49 |
singleton = AVL.singleton |
| 50 |
|
| 51 |
insert : Key → V → Map V → Map V |
| 52 |
insert = AVL.insert |
| 53 |
|
| 54 |
insertWith : Key → (Maybe V → V) → Map V → Map V |
| 55 |
insertWith = AVL.insertWith |
| 56 |
|
| 57 |
delete : Key → Map V → Map V |
| 58 |
delete = AVL.delete |
| 59 |
|
| 60 |
lookup : Map V → Key → Maybe V |
| 61 |
lookup = AVL.lookup |
| 62 |
|
| 63 |
map : (V → W) → Map V → Map W |
| 64 |
map f = AVL.map f |
| 65 |
|
| 66 |
member : Key → Map V → Bool |
| 67 |
member = AVL.member |
| 68 |
|
| 69 |
headTail : Map V → Maybe ((Key × V) × Map V) |
| 70 |
headTail = Maybe.map (Prod.map₁ AVL.toPair) ∘′ AVL.headTail |
| 71 |
|
| 72 |
initLast : Map V → Maybe (Map V × (Key × V)) |
| 73 |
initLast = Maybe.map (Prod.map₂ AVL.toPair) ∘′ AVL.initLast |
| 74 |
|
| 75 |
foldr : (Key → V → A → A) → A → Map V → A |
| 76 |
foldr cons = AVL.foldr (λ {k} → cons k) |
| 77 |
|
| 78 |
fromList : List (Key × V) → Map V |
| 79 |
fromList = AVL.fromList ∘′ List.map AVL.fromPair |
| 80 |
|
| 81 |
toList : Map V → List (Key × V) |
| 82 |
toList = List.map AVL.toPair ∘′ AVL.toList |
| 83 |
|
| 84 |
size : Map V → ℕ |
| 85 |
size = AVL.size |
| 86 |
|
| 87 |
------------------------------------------------------------------------ |
| 88 |
-- Naïve implementations of union |
| 89 |
|
| 90 |
unionWith : (V → Maybe W → W) → |
| 91 |
Map V → Map W → Map W |
| 92 |
unionWith f = AVL.unionWith f |
| 93 |
|
| 94 |
union : Map V → Map V → Map V |
| 95 |
union = AVL.union |
| 96 |
|
| 97 |
unionsWith : (V → Maybe V → V) → List (Map V) → Map V |
| 98 |
unionsWith f = AVL.unionsWith f |
| 99 |
|
| 100 |
unions : List (Map V) → Map V |
| 101 |
unions = AVL.unions |
| 102 |
|
| 103 |
------------------------------------------------------------------------ |
| 104 |
-- Naïve implementations of intersection |
| 105 |
|
| 106 |
intersectionWith : (V → W → X) → Map V → Map W → Map X |
| 107 |
intersectionWith f = AVL.intersectionWith f |
| 108 |
|
| 109 |
intersection : Map V → Map V → Map V |
| 110 |
intersection = AVL.intersection |
| 111 |
|
| 112 |
intersectionsWith : (V → V → V) → List (Map V) → Map V |
| 113 |
intersectionsWith f = AVL.intersectionsWith f |
| 114 |
|
| 115 |
intersections : List (Map V) → Map V |
| 116 |
intersections = AVL.intersections |
| 117 |
|
| 118 |
|
| 119 |
------------------------------------------------------------------------ |
| 120 |
-- DEPRECATED |
| 121 |
------------------------------------------------------------------------ |
| 122 |
-- Please use the new names as continuing support for the old names is |
| 123 |
-- not guaranteed. |
| 124 |
|
| 125 |
-- Version 2.0 |
| 126 |
|
| 127 |
infixl 4 _∈?_ |
| 128 |
_∈?_ : Key → Map V → Bool |
| 129 |
_∈?_ = member |
| 130 |
{-# WARNING_ON_USAGE _∈?_ |
| 131 |
"Warning: _∈?_ was deprecated in v2.0. |
| 132 |
Please use member instead." |
| 133 |
#-} |