<commit_before># Ring Buffer

Static size Ring Buffer implementation in C with minimal dependencies and tailored to be fast and have small footprint.

This library privileges performance over safety, therefore we do not make safety checks before manipulating the buffer, nor we raise exceptions in the C++ wrappers. This library is useful for applications such as packet queue, I/O queue, embedded applications.

# Exmaples

You can include the files **ringbuffer.{c,h}** into your project and you should be ready to go.

## Using RingBuffer in C


```C
   #include "ringbuffer.h"

   #define STATIC_BUFFER_SIZE 512

   static uint8_t data[STATIC_BUFFER_SIZE];

   int main(){

      RingBuffer buffer;
      ringBufferInit(&buffer, data, BUFFER_SIZE);

      # Append multiples bytes at one time
      uint8_t new_elem[3] = {1, 10, 20};
      ringBufferAppendMultiple(&buffer, new_elem, 3);

      # You can inspect multiples bytes without removing 
      uint8_t inspect[2];
      ringBufferPeakMultiple(&buffer, inspect, 2);

      #You can discard multiples
      ringBufferDiscardMultiple(&buffer, 2);
      
      uint32_t freeSpace = ringBufferLenAvailable(&buffer)l
      uint32_t size = ringBufferLen(&buffer);
      uint32_t capacity = ringBufferLen(&buffer); 

      return 0;
   }

```

If you are working in a memory restrained environment, you may want to 
use statically allocated memory as internal data buffer, as we show in the example above.

However, you are not doomed to this approach. The library is flexible to allow
you to use dynamically allocated memory.

# Using Ring Buffer in C++

You can find the **RingBufferWrapper** implementation in the folder **cppwrappers**. It provides a C++ class wrappers for the C implementation. For now, the wrapper
does not allow custom allocators, or statically allocated memories. However, we intend to enhance the wrapper in the future. Moreover, the class also provides some boundary checkes before the insertion/remove/access operation. 

## Example

```C++
#include "ringbufferwrapper.h"

#include <iostream>

#define ADD_SIZE 10

int main(int argc, char *argv[]) {

   std::cout << "Runnign Ring-Buffer wrapper main()\n";

   RingBufferWrapper ringBuffer(2048);

   if(!ringBuffer.isValid()) {
      std::cout << "Couldn`t allocate the Ring Buffer\n";
      return -1; 
   }

   uint8_t values[ADD_SIZE];

   for(int i=0; i<ADD_SIZE; i++) { 
      values[i] = i*10;
   }

   uint32_t len = ringBuffer.appendMultiple(values, ADD_SIZE);
   std::cout << "Inserted: " << len << ", Tryied: " 
             << ADD_SIZE << ", Len Read: " << ringBuffer.length() <commit_msg>resolve conflict<commit_after># Ring Buffer

Static size Ring Buffer implementation in C with minimal dependencies and tailored to be fast and have small footprint.

This library privileges performance over safety, therefore we do not make safety checks before manipulating the buffer, nor we raise exceptions in the C++ wrappers. This library is useful for applications such as packet queue, I/O queue, embedded applications.

# Exampels

You can include the files **ringbuffer.{c,h}** into your project and you should be ready to go.

## Using RingBuffer in C


```C
   #include "ringbuffer.h"

   #define STATIC_BUFFER_SIZE 512

   static uint8_t data[STATIC_BUFFER_SIZE];

   int main(){

      RingBuffer buffer;
      ringBufferInit(&buffer, data, BUFFER_SIZE);

      # Append multiples bytes at one time
      uint8_t new_elem[3] = {1, 10, 20};
      ringBufferAppendMultiple(&buffer, new_elem, 3);

      # You can inspect multiples bytes without removing 
      uint8_t inspect[2];
      ringBufferPeakMultiple(&buffer, inspect, 2);

      #You can discard multiples
      ringBufferDiscardMultiple(&buffer, 2);
      
      uint32_t freeSpace = ringBufferLenAvailable(&buffer)l
      uint32_t size = ringBufferLen(&buffer);
      uint32_t capacity = ringBufferLen(&buffer); 

      return 0;
   }

```

If you are working in a memory restrained environment, you may want to 
use statically allocated memory as internal data buffer, as we show in the example above.

However, you are not doomed to this approach. The library is flexible to allow
you to use dynamically allocated memory.

## Using Ring Buffer in C++

You can find the **RingBufferWrapper** implementation in the folder **cppwrappers**. It provides a C++ class wrappers for the C implementation. For now, the wrapper
does not allow custom allocators, or statically allocated memories. However, we intend to enhance the wrapper in the future. Moreover, the class also provides some boundary checkes before the insertion/remove/access operation. 


```C++
#include "ringbufferwrapper.h"

#include <iostream>

#define ADD_SIZE 10

int main(int argc, char *argv[]) {

   std::cout << "Runnign Ring-Buffer wrapper main()\n";

   RingBufferWrapper ringBuffer(2048);

   if(!ringBuffer.isValid()) {
      std::cout << "Couldn`t allocate the Ring Buffer\n";
      return -1; 
   }

   uint8_t values[ADD_SIZE];

   for(int i=0; i<ADD_SIZE; i++) { 
      values[i] = i*10;
   }

   uint32_t len = ringBuffer.appendMultiple(values, ADD_SIZE);
   std::cout << "Inserted: " << len << ", Tryied: " 
             << ADD_SIZE << ", Len Read: " << ringBuffer.length() 
{'fraction_non_alphanumeric': 0.08876639422163086, 'fraction_numerical': 0.013685611100551226, 'mean_word_length': 3.71505376344086, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4770294', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1444, 'n_words': 598}