# CircularBuffer//insert value

**URL:** https://discourse.julialang.org/t/circularbuffer-insert-value/115893
**Category:** General Usage
**Tags:** array
**Created:** [June 19, 2024, 10:49pm UTC](https://discourse.julialang.org/t/circularbuffer-insert-value/115893 "2024-06-19T22:49:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Sihyun\_Kim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sihyun_kim/32/207216_2.png) [@Sihyun\_Kim](https://discourse.julialang.org/u/Sihyun_Kim)
#### Post date: [June 19, 2024, 10:49pm UTC](https://discourse.julialang.org/t/circularbuffer-insert-value/115893/1 "2024-06-19T22:49:40Z")

</div>

I have CircularBuffer (sorted) array, and want to insert the value into it. A simple example is

```julia
cb = CircularBuffer{Float64}(5)
#when full capacity
append!(cb, [1.0, 2.0, 3.0, 4.0, 5.0])
ii = searchsortedlast(cb, 3.5)
copyto!(cb, [cb[2:ii]; 3.5])
#when not full capacity
empty!(cb)
append!(cb, [2.0, 3.0, 4.0, 5.0])
ii = searchsortedlast(cb, 3.5)
#want to have cb = [2.0, 3.0, 3.5, 4.0, 5.0]

```

With full capacity, I use copyto! to get the result (although not sure if the most efficient way), but I can’t figure it out when the array is not full capacity. Also, this is inside of several for loops, so the speed is essential. I would appreciate if you give some help.

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [June 20, 2024, 1:12am UTC](https://discourse.julialang.org/t/circularbuffer-insert-value/115893/2 "2024-06-20T01:12:05Z")

</div>

From the details here this seems like a heap would be a better choice. That is, if you just want to maintain the n-largest elements from some process and get them sorted at the end.

```julia
using DataStructures
h = BinaryHeap{Float64, DataStructures.FasterForward}(1.0:5.0) # improved performance for floats

function push_with_cap!(heap, item, cap)
  push!(heap, item)
  while length(heap) > cap
    pop!(heap)
  end
end

push_with_cap!(h, 3.5, 5)

...

# get elements in sorted order
extract_all!(h)

```

---

<div class="post-metadata">

### Author: ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)
#### Post date: [June 20, 2024, 1:27am UTC](https://discourse.julialang.org/t/circularbuffer-insert-value/115893/3 "2024-06-20T01:27:38Z")

</div>

If you actually need to maintain a sorted list between insertions I’m not sure that a CircularBuffer is going to help and you probably won’t do much better than just maintaining a sorted vector

```julia
function push_with_cap!(v, item, cap)
  ii = searchsortedlast(v, item; rev=true)
  insert!(v, ii+1, item)
  while length(v) > cap
    pop!(v)
  end
end

buf = Float64[]
sizehint!(buf, capacity+1)
append!(buf, 1.0:5.0)
sort!(buf; rev=true)

push_with_cap!(buf, 3.5, 5)

```

Since it is more efficient to add and remove from the end of a vector we maintain it in reverse order.

HTH
