# How to make the Ring Buffer circular?

**URL:** <https://forums.kodeco.com/t/how-to-make-the-ring-buffer-circular/67034>\
**Category:** Data Structures & Algorithms in Swift\
**Created:** [April 8, 2019, 1:32pm UTC](https://forums.kodeco.com/t/how-to-make-the-ring-buffer-circular/67034 "2019-04-08T13:32:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/tomn/32/81494_2.png) [@tomn](https://forums.kodeco.com/u/tomn)\
**Post date:** [April 8, 2019, 1:32pm UTC](https://forums.kodeco.com/t/how-to-make-the-ring-buffer-circular/67034/1 "2019-04-08T13:32:27Z")

</div>

The chapter 8: Queues talks about Ring buffer implementation. Yet this buffer is not circular. It does not wrap around and it does not start from the beginning when full. How to make it circular?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [April 30, 2019, 3:26am UTC](https://forums.kodeco.com/t/how-to-make-the-ring-buffer-circular/67034/2 "2019-04-30T03:26:09Z")

</div>

@jomoka Can you please help with this when you get a chance? Thank you - much appreciated! :]

---

<div class="post-metadata">

**Author:** ![jomoka](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jomoka/32/109828_2.png) [@jomoka](https://forums.kodeco.com/u/jomoka)\
**Post date:** [April 30, 2019, 11:40pm UTC](https://forums.kodeco.com/t/how-to-make-the-ring-buffer-circular/67034/3 "2019-04-30T23:40:37Z")

</div>

Hi @tomn , could you please give some more context on your use case? Maybe an example test case? Ring Buffer implementation is circular in the sense that it has a fixed size, and reuses other slots in an array.

So for example if you had the following data set of size 4:

```auto
[1, 2, 8, 3]
 w            

```

If you appended another element **(10)** it would override the next write pointer in the ring buffer:

```auto
[10, 2, 8, 3]
      w       

```

Hope this makes sense. thanks!

---

<div class="post-metadata">

**Author:** ![ys\_xs](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ys_xs/32/105896_2.png) [@ys\_xs](https://forums.kodeco.com/u/ys_xs)\
**Post date:** [September 2, 2022, 10:05am UTC](https://forums.kodeco.com/t/how-to-make-the-ring-buffer-circular/67034/4 "2022-09-02T10:05:22Z")

</div>

I also encountered this problem. This is the code in Supporting Materials. When the array is full, it does not continue to write, but returns flase. In addition, the “first” property may be out of index.

```auto
public mutating func write(_ element: T) -> Bool {
    if !isFull {
      array[writeIndex % array.count] = element
      writeIndex += 1
      return true
    } else {
      return false
    }
  }

```

[https://github.com/raywenderlich/alg-materials/blob/editions/4.0/08-queues/projects/starter/Queue.playground/Pages/QueueRingBuffer.xcplaygroundpage/Sources/RingBuffer.swift](https://github.com/raywenderlich/alg-materials/blob/editions/4.0/08-queues/projects/starter/Queue.playground/Pages/QueueRingBuffer.xcplaygroundpage/Sources/RingBuffer.swift)
