2024-08-27 10:13:29 +02:00
|
|
|
#ifndef TURBO_LIST_H
|
|
|
|
#define TURBO_LIST_H
|
|
|
|
|
2024-08-27 09:32:53 +02:00
|
|
|
#include<cstdint>
|
|
|
|
#include<cstdlib>
|
2024-08-27 10:13:29 +02:00
|
|
|
#include<cassert>
|
2024-08-27 09:32:53 +02:00
|
|
|
|
2024-08-27 11:08:58 +02:00
|
|
|
#ifndef TL_NOINLINE
|
|
|
|
#define TL_NOINLINE __attribute__((noinline))
|
|
|
|
#endif /* TL_NOINLINE */
|
|
|
|
|
|
|
|
#ifndef TL_LIKELY
|
|
|
|
#define TL_LIKELY(x) __builtin_expect(!!(x), 1)
|
|
|
|
#endif /* TL_LIKELY */
|
|
|
|
#ifndef TL_UNLIKELY
|
|
|
|
#define TL_UNLIKELY(x) __builtin_expect(!!(x), 0)
|
|
|
|
#endif /* TL_UNLIKELY */
|
|
|
|
|
2024-08-27 09:32:53 +02:00
|
|
|
class TurboList {
|
|
|
|
int *old;
|
|
|
|
int *nex;
|
|
|
|
|
|
|
|
uint32_t mid; // non-inclusive . . . m
|
|
|
|
uint32_t end; // non-inclusive e . . . .
|
|
|
|
|
|
|
|
uint32_t capacity;
|
2024-08-27 11:08:58 +02:00
|
|
|
|
|
|
|
TL_NOINLINE void grow_and_insert(int elem) noexcept {
|
|
|
|
// assert(mid == 0);
|
|
|
|
if(old) free(old);
|
|
|
|
old = nex;
|
|
|
|
mid = end;
|
|
|
|
capacity *= 2;
|
|
|
|
nex = (int *) malloc(this->capacity * sizeof(int));
|
|
|
|
|
|
|
|
// Will go into the INSERT code path here
|
|
|
|
insert(elem);
|
|
|
|
}
|
|
|
|
|
2024-08-27 09:32:53 +02:00
|
|
|
public:
|
2024-08-27 10:13:29 +02:00
|
|
|
inline TurboList(uint32_t initial_cap = 16) noexcept :
|
2024-08-27 09:32:53 +02:00
|
|
|
old(nullptr),
|
|
|
|
mid(0),
|
|
|
|
end(0),
|
2024-08-27 11:28:46 +02:00
|
|
|
capacity(initial_cap) {
|
2024-08-27 09:32:53 +02:00
|
|
|
|
2024-08-27 10:13:29 +02:00
|
|
|
nex = (int *) malloc(this->capacity * sizeof(int));
|
2024-08-27 09:32:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ~TurboList() noexcept {
|
|
|
|
if(nex) free(nex);
|
|
|
|
if(old) free(old);
|
|
|
|
}
|
2024-08-27 10:13:29 +02:00
|
|
|
|
|
|
|
inline int& operator[](uint32_t i) noexcept {
|
2024-08-27 10:53:44 +02:00
|
|
|
return (i < mid) ? old[i] : nex[i];
|
2024-08-27 10:13:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void insert(int elem) noexcept {
|
2024-08-27 11:28:46 +02:00
|
|
|
if(TL_LIKELY(size() < capacity)) {
|
2024-08-27 10:13:29 +02:00
|
|
|
// INSERT
|
2024-08-27 11:20:20 +02:00
|
|
|
|
|
|
|
/* Same as this:
|
2024-08-27 10:13:29 +02:00
|
|
|
if(mid > 0) {
|
|
|
|
nex[mid - 1] = old[mid - 1];
|
|
|
|
--mid;
|
|
|
|
}
|
2024-08-27 11:20:20 +02:00
|
|
|
*/
|
|
|
|
bool hasmid = (mid > 0);
|
|
|
|
mid -= hasmid;
|
|
|
|
nex[mid] = hasmid ? old[mid] : nex[mid];
|
|
|
|
|
2024-08-27 10:13:29 +02:00
|
|
|
nex[end++] = elem;
|
|
|
|
} else {
|
|
|
|
// GROW
|
2024-08-27 11:20:20 +02:00
|
|
|
|
2024-08-27 11:08:58 +02:00
|
|
|
grow_and_insert(elem);
|
2024-08-27 10:13:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-08-27 10:29:55 +02:00
|
|
|
|
|
|
|
inline uint32_t size() noexcept {
|
2024-08-27 11:28:46 +02:00
|
|
|
return mid + (capacity - end - 1);
|
2024-08-27 10:29:55 +02:00
|
|
|
}
|
2024-08-27 09:32:53 +02:00
|
|
|
};
|
2024-08-27 10:13:29 +02:00
|
|
|
|
|
|
|
#endif /* TURBO_LIST_H */
|