Created
July 26, 2017 23:20
-
-
Save justinmeiners/ac1617b58d1853286c12641f5ca2d8df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template <typename T> | |
struct vector | |
{ | |
constexpr static int DEFAULT_SIZE = 16; | |
vector() | |
{ | |
size = 0; | |
capacity = 0; | |
} | |
void append(const T& x) | |
{ | |
++size; | |
if (!buffer) | |
{ | |
capacity = DEFAULT_SIZE; | |
buffer = (T*)malloc(sizeof(T) * capacity); | |
} | |
if (size > capacity || !buffer) | |
{ | |
capacity *= 2; | |
buffer = (T*)realloc(buffer, sizeof(T) * capacity); | |
} | |
buffer[size - 1] = x; | |
} | |
T* buffer; | |
size_t size; | |
size_t capacity; | |
}; | |
int main(int argc, const char* argv[]) | |
{ | |
vector<float> v; | |
for (int i = 0; i < 100000; ++i) | |
{ | |
v.append(i / 2.0f); | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment