# Module /circular-buffer

Fixed-capacity ring buffer (Circular-Buffer) supporting O(1) push/pop and wraparound iteration.

```tangu
(use /circular-buffer)
```

## Functions & symbols

### Circular-Buffer

`(Circular-Buffer capacity [fill-value]) -> Circular-Buffer`

Creates a fixed-capacity ring buffer.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `capacity` | `Int` | no | Maximum capacity > 0 |
| `fill` | `Any` | yes | Optional pre-fill value |

```tangu
(let ring (cb/Circular-Buffer 4))
```

### Iter

`(Iter buf) -> Iter`

Returns a forward iterator over elements from oldest to newest.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `buf` | `Circular-Buffer` | no | Ring buffer instance |

```tangu
(for [x (cb/Iter ring)] (say x))
```

### as-array

`(as-array buf) -> Array`

Materializes the buffered elements in chronological order as an Array.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `buf` | `Circular-Buffer` | no | Ring buffer |

```tangu
(cb/as-array ring)
```

### count-of

`(count-of buf) -> Int`

Returns the current number of stored elements.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `buf` | `Circular-Buffer` | no | Ring buffer |

```tangu
(count-of ring)
```

### into-iter

`(into-iter buf) -> Iter`

Returns an iterator implementing `Into-Iter`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `buf` | `Circular-Buffer` | no | Ring buffer |

```tangu
(into-iter ring)
```

### push

`(push buf value) -> Circular-Buffer`

Appends `value` at the tail, evicting the oldest element when full.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `buf` | `Circular-Buffer` | no | Target ring buffer |
| `value` | `Any` | no | Item to append |

```tangu
(cb/push ring 42)
```

### put

`(put buf value) -> Circular-Buffer`

Alias of `push`: writes `value` into the ring buffer.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `buf` | `Circular-Buffer` | no | Target ring buffer |
| `value` | `Any` | no | Item to insert |

```tangu
(cb/put ring 42)
```
