[48in24 Exercise] [03-05] Circular Buffer

I have found the following approaches for Circular Buffer:

  1. Derive from a Queue
    Treat a circular buffer as a queue. Not the idea of a circular buffer though, should use fixed memory

  2. Dynamic backing array
    Dynamically append elements to an array. Not the idea of a circular buffer

  3. Use static backing array with read and write index fields
    Use static array with two indexes for read and write

  4. Use static backing array with read and size field
    Last element’s index is computed from read and size

  5. Immutable data structure
    Implemented as an immutable data structure

  6. genserver (agents)
    Use agent-based implementation

Do any of you know more?

I’ll look at porting for vlang.

1 Like

For V Lang:

2 Likes

I’ve just opened this one:

2 Likes

Raku implementation:

1 Like

Case 3 has several subcases based on how you distinguish an empty buffer from a full one. For brevity, I will use N to refer to the capacity.

3a) Already mentioned: allocate N + 1 slots and report full if you are about to write into the last one.

3b) Keep the read and write indices mod N and use a boolean to distinguish full from empty.

3c) Increment both indices forever, but reduce them mod N when you use them. Eventually, they will overflow, but this may not be practical concern.

3d) Reduce the indices mod 2N when you increment them, and mod N when you use them. This eliminates the possibility of overflow but leaves enough index space so that full and empty look different.

Case 4 is closely related.

I’ll add to Euphoria.