diff --git a/TurboList.hpp b/TurboList.hpp index a11e4f8..37430bb 100644 --- a/TurboList.hpp +++ b/TurboList.hpp @@ -17,22 +17,23 @@ #define TL_UNLIKELY(x) __builtin_expect(!!(x), 0) #endif /* TL_UNLIKELY */ +template class TurboList { - int *old; - int *nex; + T *old; + T *nex; uint32_t mid; // non-inclusive . . . m uint32_t end; // non-inclusive e . . . . uint32_t capacity; - TL_NOINLINE void grow_and_insert(int elem) noexcept { + TL_NOINLINE void grow_and_insert(T elem) noexcept { // assert(mid == 0); if(old) free(old); old = nex; mid = end; capacity *= 2; - nex = (int *) malloc(this->capacity * sizeof(int)); + nex = (int *) malloc(this->capacity * sizeof(T)); // Will go into the INSERT code path here insert(elem); @@ -45,7 +46,7 @@ public: end(initial_size), capacity(initial_cap) { - nex = (int *) malloc(this->capacity * sizeof(int)); + nex = (int *) malloc(this->capacity * sizeof(T)); } inline ~TurboList() noexcept { @@ -53,11 +54,11 @@ public: if(old) free(old); } - inline int& operator[](uint32_t i) noexcept { + inline T& operator[](uint32_t i) noexcept { return (i < mid) ? old[i] : nex[i]; } - inline void insert(int elem) noexcept { + inline void insert(T elem) noexcept { if(TL_LIKELY(end < capacity)) { // INSERT diff --git a/main.cpp b/main.cpp index 2268d3f..291c26a 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,9 @@ static inline void printt(const char *prefix, size_t before, size_t after) noexc } int main() { + + // std::vector // + std::vector vec; auto before_vector = ms_now(); for(int i = 0; i < N; ++i) { @@ -32,7 +35,9 @@ int main() { auto after_vector = ms_now(); printt("vector", before_vector, after_vector); - TurboList list; + // TurboList // + + TurboList list; auto before_TurboList = ms_now(); for(int i = 0; i < N; ++i) { @@ -47,6 +52,8 @@ int main() { } #endif /* PRINT_DBG */ + // C turbolist // + turbolist clist = turbolist_create(); auto before_cturbolist = ms_now();