# Module /sorted-map

B-Tree backed ordered map (Sorted-Map) maintaining keys in ascending sorted order.

```tangu
(use /sorted-map)
```

## Functions & symbols

### Sorted-Map

`(Sorted-Map [initial-entries]) -> Sorted-Map`

Constructs a mutable `Sorted-Map` instance.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `initial` | `Map | Array` | yes | Optional initial Map or [k v] pairs |

```tangu
(let m (Sorted-Map {:b 2 :a 1}))
```

### contains-key?

`(contains-key? map key) -> Bool`

Returns `true` if `key` exists in the map.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Map |
| `key` | `Any` | no | Key to test |

```tangu
(m/contains-key? m :b)
```

### count

`(count map) -> Int`

Alias of `count-of`: returns the entry count.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Sorted-Map instance |

```tangu
(m/count m)
```

### count-of

`(count-of map) -> Int`

Returns the number of entries currently stored.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Sorted-Map instance |

```tangu
(count-of m)
```

### entries-of

`(entries-of map) -> (Array Array)`

Returns all `[key value]` pairs as an Array.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Sorted-Map instance |

```tangu
(entries-of m)
```

### first-entry

`(first-entry map) -> (Or Array None)`

Returns the `[key value]` pair with the smallest key, or `()`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Sorted-Map instance |

```tangu
(sorted-map/first-entry m)
```

### get

`(get map key [default]) -> Any`

Looks up `key` in `Sorted-Map`, returning `default` or `()` if missing.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Map |
| `key` | `Any` | no | Key to look up |
| `default` | `Any` | yes | Optional fallback when key absent |

```tangu
(m/get m :a)
```

### into-iter

`(into-iter map) -> Iter`

Returns an iterator yielding `[key value]` pairs.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Sorted-Map instance |

```tangu
(into-iter m)
```

### is-empty?

`(is-empty? map) -> Bool`

Returns `true` when the map has zero entries.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Sorted-Map instance |

```tangu
(is-empty? m)
```

### keys-of

`(keys-of map) -> Array`

Returns all keys as an Array (sorted for `Sorted-Map`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Sorted-Map instance |

```tangu
(keys-of m)
```

### last-entry

`(last-entry map) -> (Or Array None)`

Returns the `[key value]` pair with the largest key, or `()`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Sorted-Map instance |

```tangu
(sorted-map/last-entry m)
```

### put

`(put map key value) -> Sorted-Map`

Inserts or updates `key -> value` inside `Sorted-Map`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Mutable map |
| `key` | `Any` | no | Entry key |
| `value` | `Any` | no | Entry value |

```tangu
(m/put m :c 3)
```

### remove

`(remove map key) -> Any`

Removes `key` and returns its previous value (or `()`).

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Mutable map |
| `key` | `Any` | no | Key to delete |

```tangu
(m/remove m :a)
```

### values-of

`(values-of map) -> Array`

Returns all values matching key order as an Array.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `map` | `Sorted-Map` | no | Sorted-Map instance |

```tangu
(values-of m)
```
