Implement circular array

This commit is contained in:
Krzosa Karol
2024-07-30 07:27:05 +02:00
parent 7b13dff29c
commit 7df09a9ebd
2 changed files with 41 additions and 0 deletions

View File

@@ -694,6 +694,46 @@ ReverseIter<T> IterateInReverse(Slice<T> *slice) {
return {slice->end() - 1, slice};
}
template <class T>
struct CircularArray {
Allocator allocator;
T *data;
int16_t cap;
int16_t write;
int16_t buffer_is_full;
};
template <class T>
CircularArray<T> MakeCircularArray(Allocator allocator, int size) {
CircularArray<T> arr = {allocator};
arr.data = AllocArray(allocator, T, size);
arr.cap = size;
return arr;
}
template <class T>
void Add(CircularArray<T> *arr, T item) {
int idx = arr->write;
arr->write = (arr->write + 1) % arr->cap;
if (arr->write == 0) arr->buffer_is_full = 1;
arr->data[idx] = item;
}
template <class T>
T Get(CircularArray<T> *arr, int idx, T default_value) {
if (idx >= arr->cap) return default_value;
int i = arr->write - 1 - idx;
if (i < 0 && arr->buffer_is_full) {
i = arr->cap + i;
}
if (i >= 0 && i < arr->cap) {
return arr->data[i];
} else {
return default_value;
}
}
struct UTF32Result {
uint32_t out_str;
int64_t advance;

View File

@@ -260,6 +260,7 @@ void ReplaceDebugData() {
String view_list = DebugViewList(scratch);
Append(buffer, ToString16(scratch, view_list));
Append(buffer, L"\n");
String window_list = DebugWindowList(scratch);
Append(buffer, ToString16(scratch, window_list));